Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "refs.h" |
| 3 | #include "pkt-line.h" |
Junio C Hamano | 958c24b | 2006-09-10 03:20:24 -0700 | [diff] [blame] | 4 | #include "sideband.h" |
Junio C Hamano | f6b42a8 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 5 | #include "tag.h" |
| 6 | #include "object.h" |
Johannes Schindelin | f0243f2 | 2005-10-28 04:48:32 +0200 | [diff] [blame] | 7 | #include "commit.h" |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 8 | #include "exec_cmd.h" |
Johannes Schindelin | 9b8dc26 | 2006-10-30 20:08:43 +0100 | [diff] [blame] | 9 | #include "diff.h" |
| 10 | #include "revision.h" |
| 11 | #include "list-objects.h" |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 12 | #include "run-command.h" |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 13 | #include "sigchain.h" |
Jeff King | ff5effd | 2012-08-03 12:19:16 -0400 | [diff] [blame] | 14 | #include "version.h" |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 15 | #include "string-list.h" |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 16 | |
Štěpán Němec | 62b4698 | 2010-10-08 19:31:15 +0200 | [diff] [blame] | 17 | static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>"; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 18 | |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 19 | /* bits #0..7 in revision.h, #8..10 in commit.c */ |
| 20 | #define THEY_HAVE (1u << 11) |
| 21 | #define OUR_REF (1u << 12) |
| 22 | #define WANTED (1u << 13) |
| 23 | #define COMMON_KNOWN (1u << 14) |
| 24 | #define REACHABLE (1u << 15) |
| 25 | |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 26 | #define SHALLOW (1u << 16) |
| 27 | #define NOT_SHALLOW (1u << 17) |
| 28 | #define CLIENT_SHALLOW (1u << 18) |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 29 | #define HIDDEN_REF (1u << 19) |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 30 | |
Junio C Hamano | 3fbe2d5 | 2006-11-24 02:34:27 -0800 | [diff] [blame] | 31 | static unsigned long oldest_have; |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 32 | |
Junio C Hamano | 3f1da57 | 2013-01-28 20:45:43 -0800 | [diff] [blame] | 33 | static int multi_ack; |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 34 | static int no_done; |
Shawn O. Pearce | 348e390 | 2008-03-03 22:27:33 -0500 | [diff] [blame] | 35 | static int use_thin_pack, use_ofs_delta, use_include_tag; |
Johannes Sixt | 9462e3f | 2009-06-16 20:41:16 +0200 | [diff] [blame] | 36 | static int no_progress, daemon_mode; |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 37 | static int allow_tip_sha1_in_want; |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 38 | static int shallow_nr; |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 39 | static struct object_array have_obj; |
| 40 | static struct object_array want_obj; |
Nicolas Pitre | 6523078 | 2009-09-03 19:08:33 -0400 | [diff] [blame] | 41 | static struct object_array extra_edge_obj; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 42 | static unsigned int timeout; |
Junio C Hamano | d47f3db | 2006-09-10 16:27:08 -0700 | [diff] [blame] | 43 | /* 0 for no sideband, |
| 44 | * otherwise maximum packet size (up to 65520 bytes). |
| 45 | */ |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 46 | static int use_sideband; |
Shawn O. Pearce | 42526b4 | 2009-10-30 17:47:33 -0700 | [diff] [blame] | 47 | static int advertise_refs; |
| 48 | static int stateless_rpc; |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 49 | |
| 50 | static void reset_timeout(void) |
| 51 | { |
| 52 | alarm(timeout); |
| 53 | } |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 54 | |
Junio C Hamano | 583b7ea | 2006-06-21 00:30:21 -0700 | [diff] [blame] | 55 | static ssize_t send_client_data(int fd, const char *data, ssize_t sz) |
| 56 | { |
Junio C Hamano | 958c24b | 2006-09-10 03:20:24 -0700 | [diff] [blame] | 57 | if (use_sideband) |
Junio C Hamano | d47f3db | 2006-09-10 16:27:08 -0700 | [diff] [blame] | 58 | return send_sideband(1, fd, data, sz, use_sideband); |
Junio C Hamano | 958c24b | 2006-09-10 03:20:24 -0700 | [diff] [blame] | 59 | if (fd == 3) |
| 60 | /* emergency quit */ |
| 61 | fd = 2; |
| 62 | if (fd == 2) { |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 63 | /* XXX: are we happy to lose stuff here? */ |
Junio C Hamano | 958c24b | 2006-09-10 03:20:24 -0700 | [diff] [blame] | 64 | xwrite(fd, data, sz); |
| 65 | return sz; |
Junio C Hamano | 583b7ea | 2006-06-21 00:30:21 -0700 | [diff] [blame] | 66 | } |
Jeff King | cdf4fb8 | 2013-02-20 15:01:56 -0500 | [diff] [blame] | 67 | write_or_die(fd, data, sz); |
| 68 | return sz; |
Junio C Hamano | 583b7ea | 2006-06-21 00:30:21 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Junio C Hamano | 16befb8 | 2007-06-08 02:54:57 -0700 | [diff] [blame] | 71 | static FILE *pack_pipe = NULL; |
Christian Couder | 11c211f | 2009-04-06 21:28:36 +0200 | [diff] [blame] | 72 | static void show_commit(struct commit *commit, void *data) |
Johannes Schindelin | 9b8dc26 | 2006-10-30 20:08:43 +0100 | [diff] [blame] | 73 | { |
| 74 | if (commit->object.flags & BOUNDARY) |
| 75 | fputc('-', pack_pipe); |
| 76 | if (fputs(sha1_to_hex(commit->object.sha1), pack_pipe) < 0) |
| 77 | die("broken output pipe"); |
| 78 | fputc('\n', pack_pipe); |
| 79 | fflush(pack_pipe); |
| 80 | free(commit->buffer); |
| 81 | commit->buffer = NULL; |
| 82 | } |
| 83 | |
Junio C Hamano | 4947367 | 2011-09-01 15:43:33 -0700 | [diff] [blame] | 84 | static void show_object(struct object *obj, |
| 85 | const struct name_path *path, const char *component, |
| 86 | void *cb_data) |
Johannes Schindelin | 9b8dc26 | 2006-10-30 20:08:43 +0100 | [diff] [blame] | 87 | { |
Junio C Hamano | 91f1751 | 2011-08-17 14:30:34 -0700 | [diff] [blame] | 88 | show_object_with_name(pack_pipe, obj, path, component); |
Johannes Schindelin | 9b8dc26 | 2006-10-30 20:08:43 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void show_edge(struct commit *commit) |
| 92 | { |
| 93 | fprintf(pack_pipe, "-%s\n", sha1_to_hex(commit->object.sha1)); |
| 94 | } |
| 95 | |
Nguyễn Thái Ngọc Duy | 1e39d7d | 2010-07-28 16:39:10 +0700 | [diff] [blame] | 96 | static int do_rev_list(int in, int out, void *user_data) |
Johannes Sixt | 80ccaa7 | 2007-10-19 21:48:02 +0200 | [diff] [blame] | 97 | { |
| 98 | int i; |
| 99 | struct rev_info revs; |
| 100 | |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 101 | pack_pipe = xfdopen(out, "w"); |
Johannes Sixt | 80ccaa7 | 2007-10-19 21:48:02 +0200 | [diff] [blame] | 102 | init_revisions(&revs, NULL); |
| 103 | revs.tag_objects = 1; |
| 104 | revs.tree_objects = 1; |
| 105 | revs.blob_objects = 1; |
| 106 | if (use_thin_pack) |
| 107 | revs.edge_hint = 1; |
| 108 | |
Nguyễn Thái Ngọc Duy | 1e39d7d | 2010-07-28 16:39:10 +0700 | [diff] [blame] | 109 | for (i = 0; i < want_obj.nr; i++) { |
| 110 | struct object *o = want_obj.objects[i].item; |
| 111 | /* why??? */ |
| 112 | o->flags &= ~UNINTERESTING; |
| 113 | add_pending_object(&revs, o, NULL); |
Johannes Sixt | 80ccaa7 | 2007-10-19 21:48:02 +0200 | [diff] [blame] | 114 | } |
Nguyễn Thái Ngọc Duy | 1e39d7d | 2010-07-28 16:39:10 +0700 | [diff] [blame] | 115 | for (i = 0; i < have_obj.nr; i++) { |
| 116 | struct object *o = have_obj.objects[i].item; |
| 117 | o->flags |= UNINTERESTING; |
| 118 | add_pending_object(&revs, o, NULL); |
| 119 | } |
| 120 | setup_revisions(0, NULL, &revs, NULL); |
Martin Koegler | 3d51e1b | 2008-02-18 08:31:56 +0100 | [diff] [blame] | 121 | if (prepare_revision_walk(&revs)) |
| 122 | die("revision walk setup failed"); |
Johannes Sixt | 80ccaa7 | 2007-10-19 21:48:02 +0200 | [diff] [blame] | 123 | mark_edges_uninteresting(revs.commits, &revs, show_edge); |
Nicolas Pitre | 6523078 | 2009-09-03 19:08:33 -0400 | [diff] [blame] | 124 | if (use_thin_pack) |
| 125 | for (i = 0; i < extra_edge_obj.nr; i++) |
| 126 | fprintf(pack_pipe, "-%s\n", sha1_to_hex( |
| 127 | extra_edge_obj.objects[i].item->sha1)); |
Christian Couder | 11c211f | 2009-04-06 21:28:36 +0200 | [diff] [blame] | 128 | traverse_commit_list(&revs, show_commit, show_object, NULL); |
Johannes Sixt | 618ebe9 | 2007-12-08 22:19:14 +0100 | [diff] [blame] | 129 | fflush(pack_pipe); |
| 130 | fclose(pack_pipe); |
Johannes Sixt | 21edd3f | 2007-10-19 21:48:03 +0200 | [diff] [blame] | 131 | return 0; |
Johannes Sixt | 80ccaa7 | 2007-10-19 21:48:02 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 134 | static void create_pack_file(void) |
| 135 | { |
Johannes Sixt | 21edd3f | 2007-10-19 21:48:03 +0200 | [diff] [blame] | 136 | struct async rev_list; |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 137 | struct child_process pack_objects; |
Junio C Hamano | 363b781 | 2006-06-20 22:48:23 -0700 | [diff] [blame] | 138 | char data[8193], progress[128]; |
Junio C Hamano | 583b7ea | 2006-06-21 00:30:21 -0700 | [diff] [blame] | 139 | char abort_msg[] = "aborting due to possible repository " |
| 140 | "corruption on the remote side."; |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 141 | int buffered = -1; |
Junio C Hamano | 1456b04 | 2009-12-10 12:17:11 -0800 | [diff] [blame] | 142 | ssize_t sz; |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 143 | const char *argv[10]; |
| 144 | int arg = 0; |
Linus Torvalds | 75bfc6c | 2005-07-04 16:35:13 -0700 | [diff] [blame] | 145 | |
Jeff King | b961219 | 2011-04-06 17:33:33 -0400 | [diff] [blame] | 146 | argv[arg++] = "pack-objects"; |
| 147 | if (!shallow_nr) { |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 148 | argv[arg++] = "--revs"; |
Junio C Hamano | 3f1da57 | 2013-01-28 20:45:43 -0800 | [diff] [blame] | 149 | if (use_thin_pack) |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 150 | argv[arg++] = "--thin"; |
| 151 | } |
Linus Torvalds | 75bfc6c | 2005-07-04 16:35:13 -0700 | [diff] [blame] | 152 | |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 153 | argv[arg++] = "--stdout"; |
| 154 | if (!no_progress) |
| 155 | argv[arg++] = "--progress"; |
| 156 | if (use_ofs_delta) |
| 157 | argv[arg++] = "--delta-base-offset"; |
Shawn O. Pearce | 348e390 | 2008-03-03 22:27:33 -0500 | [diff] [blame] | 158 | if (use_include_tag) |
| 159 | argv[arg++] = "--include-tag"; |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 160 | argv[arg++] = NULL; |
| 161 | |
| 162 | memset(&pack_objects, 0, sizeof(pack_objects)); |
Jeff King | b961219 | 2011-04-06 17:33:33 -0400 | [diff] [blame] | 163 | pack_objects.in = -1; |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 164 | pack_objects.out = -1; |
| 165 | pack_objects.err = -1; |
| 166 | pack_objects.git_cmd = 1; |
| 167 | pack_objects.argv = argv; |
Johannes Sixt | 21edd3f | 2007-10-19 21:48:03 +0200 | [diff] [blame] | 168 | |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 169 | if (start_command(&pack_objects)) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 170 | die("git upload-pack: unable to fork git-pack-objects"); |
Johannes Schindelin | 83a5ad6 | 2007-02-20 03:01:44 +0100 | [diff] [blame] | 171 | |
Jeff King | b961219 | 2011-04-06 17:33:33 -0400 | [diff] [blame] | 172 | if (shallow_nr) { |
| 173 | memset(&rev_list, 0, sizeof(rev_list)); |
| 174 | rev_list.proc = do_rev_list; |
| 175 | rev_list.out = pack_objects.in; |
| 176 | if (start_async(&rev_list)) |
| 177 | die("git upload-pack: unable to fork git-rev-list"); |
| 178 | } |
| 179 | else { |
Jim Meyering | 4169837 | 2009-09-12 10:43:27 +0200 | [diff] [blame] | 180 | FILE *pipe_fd = xfdopen(pack_objects.in, "w"); |
Junio C Hamano | 3f1da57 | 2013-01-28 20:45:43 -0800 | [diff] [blame] | 181 | int i; |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 182 | |
Junio C Hamano | 3f1da57 | 2013-01-28 20:45:43 -0800 | [diff] [blame] | 183 | for (i = 0; i < want_obj.nr; i++) |
| 184 | fprintf(pipe_fd, "%s\n", |
| 185 | sha1_to_hex(want_obj.objects[i].item->sha1)); |
| 186 | fprintf(pipe_fd, "--not\n"); |
| 187 | for (i = 0; i < have_obj.nr; i++) |
| 188 | fprintf(pipe_fd, "%s\n", |
| 189 | sha1_to_hex(have_obj.objects[i].item->sha1)); |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 190 | fprintf(pipe_fd, "\n"); |
| 191 | fflush(pipe_fd); |
| 192 | fclose(pipe_fd); |
| 193 | } |
| 194 | |
| 195 | |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 196 | /* We read from pack_objects.err to capture stderr output for |
| 197 | * progress bar, and pack_objects.out to capture the pack data. |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 198 | */ |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 199 | |
| 200 | while (1) { |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 201 | struct pollfd pfd[2]; |
Junio C Hamano | 363b781 | 2006-06-20 22:48:23 -0700 | [diff] [blame] | 202 | int pe, pu, pollsize; |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 203 | |
Matthias Lederhofer | 0d516ad | 2006-07-18 19:14:51 +0200 | [diff] [blame] | 204 | reset_timeout(); |
| 205 | |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 206 | pollsize = 0; |
Junio C Hamano | 363b781 | 2006-06-20 22:48:23 -0700 | [diff] [blame] | 207 | pe = pu = -1; |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 208 | |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 209 | if (0 <= pack_objects.out) { |
| 210 | pfd[pollsize].fd = pack_objects.out; |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 211 | pfd[pollsize].events = POLLIN; |
| 212 | pu = pollsize; |
| 213 | pollsize++; |
| 214 | } |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 215 | if (0 <= pack_objects.err) { |
| 216 | pfd[pollsize].fd = pack_objects.err; |
Junio C Hamano | 363b781 | 2006-06-20 22:48:23 -0700 | [diff] [blame] | 217 | pfd[pollsize].events = POLLIN; |
| 218 | pe = pollsize; |
| 219 | pollsize++; |
| 220 | } |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 221 | |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 222 | if (!pollsize) |
| 223 | break; |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 224 | |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 225 | if (poll(pfd, pollsize, -1) < 0) { |
| 226 | if (errno != EINTR) { |
| 227 | error("poll failed, resuming: %s", |
| 228 | strerror(errno)); |
| 229 | sleep(1); |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 230 | } |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 231 | continue; |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 232 | } |
Nicolas Pitre | 6b59f51 | 2009-11-11 17:24:42 -0500 | [diff] [blame] | 233 | if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) { |
| 234 | /* Status ready; we ship that in the side-band |
| 235 | * or dump to the standard error. |
| 236 | */ |
| 237 | sz = xread(pack_objects.err, progress, |
| 238 | sizeof(progress)); |
| 239 | if (0 < sz) |
| 240 | send_client_data(2, progress, sz); |
| 241 | else if (sz == 0) { |
| 242 | close(pack_objects.err); |
| 243 | pack_objects.err = -1; |
| 244 | } |
| 245 | else |
| 246 | goto fail; |
| 247 | /* give priority to status messages */ |
| 248 | continue; |
| 249 | } |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 250 | if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) { |
| 251 | /* Data ready; we keep the last byte to ourselves |
| 252 | * in case we detect broken rev-list, so that we |
| 253 | * can leave the stream corrupted. This is |
| 254 | * unfortunate -- unpack-objects would happily |
| 255 | * accept a valid packdata with trailing garbage, |
| 256 | * so appending garbage after we pass all the |
| 257 | * pack data is not good enough to signal |
| 258 | * breakage to downstream. |
| 259 | */ |
| 260 | char *cp = data; |
| 261 | ssize_t outsz = 0; |
| 262 | if (0 <= buffered) { |
| 263 | *cp++ = buffered; |
| 264 | outsz++; |
| 265 | } |
| 266 | sz = xread(pack_objects.out, cp, |
| 267 | sizeof(data) - outsz); |
| 268 | if (0 < sz) |
Junio C Hamano | 1456b04 | 2009-12-10 12:17:11 -0800 | [diff] [blame] | 269 | ; |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 270 | else if (sz == 0) { |
| 271 | close(pack_objects.out); |
| 272 | pack_objects.out = -1; |
| 273 | } |
| 274 | else |
| 275 | goto fail; |
| 276 | sz += outsz; |
| 277 | if (1 < sz) { |
| 278 | buffered = data[sz-1] & 0xFF; |
| 279 | sz--; |
| 280 | } |
| 281 | else |
| 282 | buffered = -1; |
| 283 | sz = send_client_data(1, data, sz); |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 284 | if (sz < 0) |
| 285 | goto fail; |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 286 | } |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 287 | } |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 288 | |
| 289 | if (finish_command(&pack_objects)) { |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 290 | error("git upload-pack: git-pack-objects died with error."); |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 291 | goto fail; |
| 292 | } |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 293 | if (shallow_nr && finish_async(&rev_list)) |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 294 | goto fail; /* error was already reported */ |
| 295 | |
| 296 | /* flush the data */ |
| 297 | if (0 <= buffered) { |
| 298 | data[0] = buffered; |
| 299 | sz = send_client_data(1, data, 1); |
| 300 | if (sz < 0) |
| 301 | goto fail; |
| 302 | fprintf(stderr, "flushed.\n"); |
| 303 | } |
| 304 | if (use_sideband) |
| 305 | packet_flush(1); |
| 306 | return; |
| 307 | |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 308 | fail: |
Junio C Hamano | 583b7ea | 2006-06-21 00:30:21 -0700 | [diff] [blame] | 309 | send_client_data(3, abort_msg, sizeof(abort_msg)); |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 310 | die("git upload-pack: %s", abort_msg); |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 313 | static int got_sha1(char *hex, unsigned char *sha1) |
| 314 | { |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 315 | struct object *o; |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 316 | int we_knew_they_have = 0; |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 317 | |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 318 | if (get_sha1_hex(hex, sha1)) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 319 | die("git upload-pack: expected SHA1 object, got '%s'", hex); |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 320 | if (!has_sha1_file(sha1)) |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 321 | return -1; |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 322 | |
Jeff King | a6eec12 | 2013-03-16 06:25:25 -0400 | [diff] [blame] | 323 | o = parse_object(sha1); |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 324 | if (!o) |
| 325 | die("oops (%s)", sha1_to_hex(sha1)); |
Junio C Hamano | 182a8da | 2006-08-12 22:16:51 -0700 | [diff] [blame] | 326 | if (o->type == OBJ_COMMIT) { |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 327 | struct commit_list *parents; |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 328 | struct commit *commit = (struct commit *)o; |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 329 | if (o->flags & THEY_HAVE) |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 330 | we_knew_they_have = 1; |
| 331 | else |
| 332 | o->flags |= THEY_HAVE; |
| 333 | if (!oldest_have || (commit->date < oldest_have)) |
| 334 | oldest_have = commit->date; |
| 335 | for (parents = commit->parents; |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 336 | parents; |
| 337 | parents = parents->next) |
| 338 | parents->item->object.flags |= THEY_HAVE; |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 339 | } |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 340 | if (!we_knew_they_have) { |
| 341 | add_object_array(o, NULL, &have_obj); |
| 342 | return 1; |
| 343 | } |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | static int reachable(struct commit *want) |
| 348 | { |
| 349 | struct commit_list *work = NULL; |
| 350 | |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 351 | commit_list_insert_by_date(want, &work); |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 352 | while (work) { |
| 353 | struct commit_list *list = work->next; |
| 354 | struct commit *commit = work->item; |
| 355 | free(work); |
| 356 | work = list; |
| 357 | |
| 358 | if (commit->object.flags & THEY_HAVE) { |
| 359 | want->object.flags |= COMMON_KNOWN; |
| 360 | break; |
| 361 | } |
| 362 | if (!commit->object.parsed) |
| 363 | parse_object(commit->object.sha1); |
| 364 | if (commit->object.flags & REACHABLE) |
| 365 | continue; |
| 366 | commit->object.flags |= REACHABLE; |
| 367 | if (commit->date < oldest_have) |
| 368 | continue; |
| 369 | for (list = commit->parents; list; list = list->next) { |
| 370 | struct commit *parent = list->item; |
| 371 | if (!(parent->object.flags & REACHABLE)) |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 372 | commit_list_insert_by_date(parent, &work); |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | want->object.flags |= REACHABLE; |
| 376 | clear_commit_marks(want, REACHABLE); |
| 377 | free_commit_list(work); |
| 378 | return (want->object.flags & COMMON_KNOWN); |
| 379 | } |
| 380 | |
| 381 | static int ok_to_give_up(void) |
| 382 | { |
| 383 | int i; |
| 384 | |
| 385 | if (!have_obj.nr) |
| 386 | return 0; |
| 387 | |
| 388 | for (i = 0; i < want_obj.nr; i++) { |
| 389 | struct object *want = want_obj.objects[i].item; |
| 390 | |
| 391 | if (want->flags & COMMON_KNOWN) |
| 392 | continue; |
| 393 | want = deref_tag(want, "a want line", 0); |
| 394 | if (!want || want->type != OBJ_COMMIT) { |
| 395 | /* no way to tell if this is reachable by |
| 396 | * looking at the ancestry chain alone, so |
| 397 | * leave a note to ourselves not to worry about |
| 398 | * this object anymore. |
| 399 | */ |
| 400 | want_obj.objects[i].item->flags |= COMMON_KNOWN; |
| 401 | continue; |
| 402 | } |
| 403 | if (!reachable((struct commit *)want)) |
| 404 | return 0; |
| 405 | } |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 406 | return 1; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static int get_common_commits(void) |
| 410 | { |
Junio C Hamano | c04c4e5 | 2006-07-05 18:12:12 -0700 | [diff] [blame] | 411 | unsigned char sha1[20]; |
Shawn O. Pearce | 78affc4 | 2009-10-30 17:47:25 -0700 | [diff] [blame] | 412 | char last_hex[41]; |
Shawn O. Pearce | 49bee71 | 2011-03-14 16:48:39 -0700 | [diff] [blame] | 413 | int got_common = 0; |
| 414 | int got_other = 0; |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 415 | int sent_ready = 0; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 416 | |
Johannes Schindelin | f0243f2 | 2005-10-28 04:48:32 +0200 | [diff] [blame] | 417 | save_commit_buffer = 0; |
| 418 | |
Brian Gianforcaro | eeefa7c | 2009-09-01 01:35:10 -0400 | [diff] [blame] | 419 | for (;;) { |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 420 | char *line = packet_read_line(0, NULL); |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 421 | reset_timeout(); |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 422 | |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 423 | if (!line) { |
Shawn O. Pearce | 49bee71 | 2011-03-14 16:48:39 -0700 | [diff] [blame] | 424 | if (multi_ack == 2 && got_common |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 425 | && !got_other && ok_to_give_up()) { |
| 426 | sent_ready = 1; |
Shawn O. Pearce | 49bee71 | 2011-03-14 16:48:39 -0700 | [diff] [blame] | 427 | packet_write(1, "ACK %s ready\n", last_hex); |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 428 | } |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 429 | if (have_obj.nr == 0 || multi_ack) |
Johannes Schindelin | 1bd8c8f | 2005-10-28 04:49:16 +0200 | [diff] [blame] | 430 | packet_write(1, "NAK\n"); |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 431 | |
| 432 | if (no_done && sent_ready) { |
| 433 | packet_write(1, "ACK %s\n", last_hex); |
| 434 | return 0; |
| 435 | } |
Shawn O. Pearce | 42526b4 | 2009-10-30 17:47:33 -0700 | [diff] [blame] | 436 | if (stateless_rpc) |
| 437 | exit(0); |
Shawn O. Pearce | 49bee71 | 2011-03-14 16:48:39 -0700 | [diff] [blame] | 438 | got_common = 0; |
| 439 | got_other = 0; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 440 | continue; |
| 441 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 442 | if (!prefixcmp(line, "have ")) { |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 443 | switch (got_sha1(line+5, sha1)) { |
| 444 | case -1: /* they have what we do not */ |
Shawn O. Pearce | 49bee71 | 2011-03-14 16:48:39 -0700 | [diff] [blame] | 445 | got_other = 1; |
Shawn O. Pearce | 78affc4 | 2009-10-30 17:47:25 -0700 | [diff] [blame] | 446 | if (multi_ack && ok_to_give_up()) { |
| 447 | const char *hex = sha1_to_hex(sha1); |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 448 | if (multi_ack == 2) { |
| 449 | sent_ready = 1; |
Shawn O. Pearce | 78affc4 | 2009-10-30 17:47:25 -0700 | [diff] [blame] | 450 | packet_write(1, "ACK %s ready\n", hex); |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 451 | } else |
Shawn O. Pearce | 78affc4 | 2009-10-30 17:47:25 -0700 | [diff] [blame] | 452 | packet_write(1, "ACK %s continue\n", hex); |
| 453 | } |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 454 | break; |
| 455 | default: |
Shawn O. Pearce | 49bee71 | 2011-03-14 16:48:39 -0700 | [diff] [blame] | 456 | got_common = 1; |
Shawn O. Pearce | 78affc4 | 2009-10-30 17:47:25 -0700 | [diff] [blame] | 457 | memcpy(last_hex, sha1_to_hex(sha1), 41); |
| 458 | if (multi_ack == 2) |
| 459 | packet_write(1, "ACK %s common\n", last_hex); |
| 460 | else if (multi_ack) |
| 461 | packet_write(1, "ACK %s continue\n", last_hex); |
Junio C Hamano | c04c4e5 | 2006-07-05 18:12:12 -0700 | [diff] [blame] | 462 | else if (have_obj.nr == 1) |
Shawn O. Pearce | 78affc4 | 2009-10-30 17:47:25 -0700 | [diff] [blame] | 463 | packet_write(1, "ACK %s\n", last_hex); |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 464 | break; |
Junio C Hamano | af2d3aa | 2005-10-25 14:55:24 -0700 | [diff] [blame] | 465 | } |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 466 | continue; |
| 467 | } |
| 468 | if (!strcmp(line, "done")) { |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 469 | if (have_obj.nr > 0) { |
Johannes Schindelin | 1bd8c8f | 2005-10-28 04:49:16 +0200 | [diff] [blame] | 470 | if (multi_ack) |
Junio C Hamano | c04c4e5 | 2006-07-05 18:12:12 -0700 | [diff] [blame] | 471 | packet_write(1, "ACK %s\n", last_hex); |
Johannes Schindelin | 1bd8c8f | 2005-10-28 04:49:16 +0200 | [diff] [blame] | 472 | return 0; |
| 473 | } |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 474 | packet_write(1, "NAK\n"); |
| 475 | return -1; |
| 476 | } |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 477 | die("git upload-pack: expected SHA1 list, got '%s'", line); |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 478 | } |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 481 | static int is_our_ref(struct object *o) |
| 482 | { |
| 483 | return o->flags & |
| 484 | ((allow_tip_sha1_in_want ? HIDDEN_REF : 0) | OUR_REF); |
| 485 | } |
| 486 | |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 487 | static void check_non_tip(void) |
| 488 | { |
| 489 | static const char *argv[] = { |
| 490 | "rev-list", "--stdin", NULL, |
| 491 | }; |
| 492 | static struct child_process cmd; |
| 493 | struct object *o; |
| 494 | char namebuf[42]; /* ^ + SHA-1 + LF */ |
| 495 | int i; |
| 496 | |
| 497 | /* In the normal in-process case non-tip request can never happen */ |
| 498 | if (!stateless_rpc) |
| 499 | goto error; |
| 500 | |
| 501 | cmd.argv = argv; |
| 502 | cmd.git_cmd = 1; |
| 503 | cmd.no_stderr = 1; |
| 504 | cmd.in = -1; |
| 505 | cmd.out = -1; |
| 506 | |
| 507 | if (start_command(&cmd)) |
| 508 | goto error; |
| 509 | |
| 510 | /* |
| 511 | * If rev-list --stdin encounters an unknown commit, it |
| 512 | * terminates, which will cause SIGPIPE in the write loop |
| 513 | * below. |
| 514 | */ |
| 515 | sigchain_push(SIGPIPE, SIG_IGN); |
| 516 | |
| 517 | namebuf[0] = '^'; |
| 518 | namebuf[41] = '\n'; |
| 519 | for (i = get_max_object_index(); 0 < i; ) { |
| 520 | o = get_indexed_object(--i); |
Brian Harring | 2a74532 | 2011-08-23 22:47:17 -0700 | [diff] [blame] | 521 | if (!o) |
| 522 | continue; |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 523 | if (!is_our_ref(o)) |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 524 | continue; |
| 525 | memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40); |
| 526 | if (write_in_full(cmd.in, namebuf, 42) < 0) |
| 527 | goto error; |
| 528 | } |
| 529 | namebuf[40] = '\n'; |
| 530 | for (i = 0; i < want_obj.nr; i++) { |
| 531 | o = want_obj.objects[i].item; |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 532 | if (is_our_ref(o)) |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 533 | continue; |
| 534 | memcpy(namebuf, sha1_to_hex(o->sha1), 40); |
| 535 | if (write_in_full(cmd.in, namebuf, 41) < 0) |
| 536 | goto error; |
| 537 | } |
| 538 | close(cmd.in); |
| 539 | |
| 540 | sigchain_pop(SIGPIPE); |
| 541 | |
| 542 | /* |
| 543 | * The commits out of the rev-list are not ancestors of |
| 544 | * our ref. |
| 545 | */ |
| 546 | i = read_in_full(cmd.out, namebuf, 1); |
| 547 | if (i) |
| 548 | goto error; |
| 549 | close(cmd.out); |
| 550 | |
| 551 | /* |
| 552 | * rev-list may have died by encountering a bad commit |
| 553 | * in the history, in which case we do want to bail out |
| 554 | * even when it showed no commit. |
| 555 | */ |
| 556 | if (finish_command(&cmd)) |
| 557 | goto error; |
| 558 | |
| 559 | /* All the non-tip ones are ancestors of what we advertised */ |
| 560 | return; |
| 561 | |
| 562 | error: |
| 563 | /* Pick one of them (we know there at least is one) */ |
| 564 | for (i = 0; i < want_obj.nr; i++) { |
| 565 | o = want_obj.objects[i].item; |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 566 | if (!is_our_ref(o)) |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 567 | die("git upload-pack: not our ref %s", |
| 568 | sha1_to_hex(o->sha1)); |
| 569 | } |
| 570 | } |
| 571 | |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 572 | static void receive_needs(void) |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 573 | { |
Thiago Farina | 3cd4745 | 2010-08-28 23:04:17 -0300 | [diff] [blame] | 574 | struct object_array shallows = OBJECT_ARRAY_INIT; |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 575 | int depth = 0; |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 576 | int has_non_tip = 0; |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 577 | |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 578 | shallow_nr = 0; |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 579 | for (;;) { |
Junio C Hamano | 565ebbf | 2005-10-24 18:59:18 -0700 | [diff] [blame] | 580 | struct object *o; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 581 | const char *features; |
Junio C Hamano | 6ece0d3 | 2006-07-05 17:41:39 -0700 | [diff] [blame] | 582 | unsigned char sha1_buf[20]; |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 583 | char *line = packet_read_line(0, NULL); |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 584 | reset_timeout(); |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 585 | if (!line) |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 586 | break; |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 587 | |
Junio C Hamano | 599065a | 2007-02-20 01:54:00 -0800 | [diff] [blame] | 588 | if (!prefixcmp(line, "shallow ")) { |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 589 | unsigned char sha1[20]; |
| 590 | struct object *object; |
Jeff King | b7b0217 | 2013-02-20 14:53:33 -0500 | [diff] [blame] | 591 | if (get_sha1_hex(line + 8, sha1)) |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 592 | die("invalid shallow line: %s", line); |
| 593 | object = parse_object(sha1); |
| 594 | if (!object) |
Michael Heemskerk | af04fa2 | 2013-04-28 22:32:04 -0700 | [diff] [blame] | 595 | continue; |
Nguyễn Thái Ngọc Duy | 6293ded | 2013-01-08 18:32:36 +0700 | [diff] [blame] | 596 | if (object->type != OBJ_COMMIT) |
| 597 | die("invalid shallow object %s", sha1_to_hex(sha1)); |
Jeff King | e58e57e | 2013-02-20 14:54:57 -0500 | [diff] [blame] | 598 | if (!(object->flags & CLIENT_SHALLOW)) { |
| 599 | object->flags |= CLIENT_SHALLOW; |
| 600 | add_object_array(object, NULL, &shallows); |
| 601 | } |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 602 | continue; |
| 603 | } |
Junio C Hamano | 599065a | 2007-02-20 01:54:00 -0800 | [diff] [blame] | 604 | if (!prefixcmp(line, "deepen ")) { |
Johannes Schindelin | 016e6cc | 2006-10-30 20:09:29 +0100 | [diff] [blame] | 605 | char *end; |
| 606 | depth = strtol(line + 7, &end, 0); |
| 607 | if (end == line + 7 || depth <= 0) |
| 608 | die("Invalid deepen: %s", line); |
| 609 | continue; |
| 610 | } |
Junio C Hamano | 599065a | 2007-02-20 01:54:00 -0800 | [diff] [blame] | 611 | if (prefixcmp(line, "want ") || |
Junio C Hamano | 6ece0d3 | 2006-07-05 17:41:39 -0700 | [diff] [blame] | 612 | get_sha1_hex(line+5, sha1_buf)) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 613 | die("git upload-pack: protocol error, " |
Junio C Hamano | e091eb9 | 2005-10-05 14:49:54 -0700 | [diff] [blame] | 614 | "expected to get sha, not '%s'", line); |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 615 | |
| 616 | features = line + 45; |
| 617 | |
| 618 | if (parse_feature_request(features, "multi_ack_detailed")) |
Shawn O. Pearce | 78affc4 | 2009-10-30 17:47:25 -0700 | [diff] [blame] | 619 | multi_ack = 2; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 620 | else if (parse_feature_request(features, "multi_ack")) |
Johannes Schindelin | 1bd8c8f | 2005-10-28 04:49:16 +0200 | [diff] [blame] | 621 | multi_ack = 1; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 622 | if (parse_feature_request(features, "no-done")) |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 623 | no_done = 1; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 624 | if (parse_feature_request(features, "thin-pack")) |
Junio C Hamano | b19696c | 2006-02-20 00:38:39 -0800 | [diff] [blame] | 625 | use_thin_pack = 1; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 626 | if (parse_feature_request(features, "ofs-delta")) |
Nicolas Pitre | e4fe4b8 | 2006-09-26 11:27:39 -0400 | [diff] [blame] | 627 | use_ofs_delta = 1; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 628 | if (parse_feature_request(features, "side-band-64k")) |
Junio C Hamano | d47f3db | 2006-09-10 16:27:08 -0700 | [diff] [blame] | 629 | use_sideband = LARGE_PACKET_MAX; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 630 | else if (parse_feature_request(features, "side-band")) |
Junio C Hamano | d47f3db | 2006-09-10 16:27:08 -0700 | [diff] [blame] | 631 | use_sideband = DEFAULT_PACKET_MAX; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 632 | if (parse_feature_request(features, "no-progress")) |
Johannes Schindelin | b0e9089 | 2007-02-23 20:03:10 +0100 | [diff] [blame] | 633 | no_progress = 1; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 634 | if (parse_feature_request(features, "include-tag")) |
Shawn O. Pearce | 348e390 | 2008-03-03 22:27:33 -0500 | [diff] [blame] | 635 | use_include_tag = 1; |
Junio C Hamano | 565ebbf | 2005-10-24 18:59:18 -0700 | [diff] [blame] | 636 | |
Jeff King | f59de5d | 2013-03-16 06:28:30 -0400 | [diff] [blame] | 637 | o = parse_object(sha1_buf); |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 638 | if (!o) |
Elijah Newren | 9f9aa76 | 2010-07-31 14:11:46 -0600 | [diff] [blame] | 639 | die("git upload-pack: not our ref %s", |
| 640 | sha1_to_hex(sha1_buf)); |
Junio C Hamano | 565ebbf | 2005-10-24 18:59:18 -0700 | [diff] [blame] | 641 | if (!(o->flags & WANTED)) { |
| 642 | o->flags |= WANTED; |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 643 | if (!is_our_ref(o)) |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 644 | has_non_tip = 1; |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 645 | add_object_array(o, NULL, &want_obj); |
Junio C Hamano | 565ebbf | 2005-10-24 18:59:18 -0700 | [diff] [blame] | 646 | } |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 647 | } |
Johannes Sixt | 9462e3f | 2009-06-16 20:41:16 +0200 | [diff] [blame] | 648 | |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 649 | /* |
| 650 | * We have sent all our refs already, and the other end |
| 651 | * should have chosen out of them. When we are operating |
| 652 | * in the stateless RPC mode, however, their choice may |
| 653 | * have been based on the set of older refs advertised |
| 654 | * by another process that handled the initial request. |
| 655 | */ |
| 656 | if (has_non_tip) |
| 657 | check_non_tip(); |
| 658 | |
Johannes Sixt | 9462e3f | 2009-06-16 20:41:16 +0200 | [diff] [blame] | 659 | if (!use_sideband && daemon_mode) |
| 660 | no_progress = 1; |
| 661 | |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 662 | if (depth == 0 && shallows.nr == 0) |
| 663 | return; |
Johannes Schindelin | 016e6cc | 2006-10-30 20:09:29 +0100 | [diff] [blame] | 664 | if (depth > 0) { |
Nguyễn Thái Ngọc Duy | 4dcb167 | 2013-01-11 16:05:46 +0700 | [diff] [blame] | 665 | struct commit_list *result = NULL, *backup = NULL; |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 666 | int i; |
Nguyễn Thái Ngọc Duy | 4dcb167 | 2013-01-11 16:05:46 +0700 | [diff] [blame] | 667 | if (depth == INFINITE_DEPTH) |
| 668 | for (i = 0; i < shallows.nr; i++) { |
| 669 | struct object *object = shallows.objects[i].item; |
| 670 | object->flags |= NOT_SHALLOW; |
| 671 | } |
| 672 | else |
| 673 | backup = result = |
| 674 | get_shallow_commits(&want_obj, depth, |
| 675 | SHALLOW, NOT_SHALLOW); |
Johannes Schindelin | 016e6cc | 2006-10-30 20:09:29 +0100 | [diff] [blame] | 676 | while (result) { |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 677 | struct object *object = &result->item->object; |
Alexandre Julliard | 1f2de76 | 2006-11-24 15:58:25 +0100 | [diff] [blame] | 678 | if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) { |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 679 | packet_write(1, "shallow %s", |
| 680 | sha1_to_hex(object->sha1)); |
| 681 | register_shallow(object->sha1); |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 682 | shallow_nr++; |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 683 | } |
Johannes Schindelin | 016e6cc | 2006-10-30 20:09:29 +0100 | [diff] [blame] | 684 | result = result->next; |
| 685 | } |
| 686 | free_commit_list(backup); |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 687 | for (i = 0; i < shallows.nr; i++) { |
| 688 | struct object *object = shallows.objects[i].item; |
| 689 | if (object->flags & NOT_SHALLOW) { |
| 690 | struct commit_list *parents; |
| 691 | packet_write(1, "unshallow %s", |
| 692 | sha1_to_hex(object->sha1)); |
| 693 | object->flags &= ~CLIENT_SHALLOW; |
| 694 | /* make sure the real parents are parsed */ |
| 695 | unregister_shallow(object->sha1); |
Junio C Hamano | 176d45c | 2006-11-13 22:47:46 -0800 | [diff] [blame] | 696 | object->parsed = 0; |
Martin Koegler | dec38c8 | 2008-02-18 21:48:03 +0100 | [diff] [blame] | 697 | if (parse_commit((struct commit *)object)) |
| 698 | die("invalid commit"); |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 699 | parents = ((struct commit *)object)->parents; |
| 700 | while (parents) { |
| 701 | add_object_array(&parents->item->object, |
| 702 | NULL, &want_obj); |
| 703 | parents = parents->next; |
| 704 | } |
Nicolas Pitre | 6523078 | 2009-09-03 19:08:33 -0400 | [diff] [blame] | 705 | add_object_array(object, NULL, &extra_edge_obj); |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 706 | } |
| 707 | /* make sure commit traversal conforms to client */ |
| 708 | register_shallow(object->sha1); |
| 709 | } |
| 710 | packet_flush(1); |
| 711 | } else |
| 712 | if (shallows.nr > 0) { |
| 713 | int i; |
| 714 | for (i = 0; i < shallows.nr; i++) |
| 715 | register_shallow(shallows.objects[i].item->sha1); |
| 716 | } |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 717 | |
| 718 | shallow_nr += shallows.nr; |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 719 | free(shallows.objects); |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 722 | /* return non-zero if the ref is hidden, otherwise 0 */ |
Junio C Hamano | cbbe50d | 2013-01-18 15:48:49 -0800 | [diff] [blame] | 723 | static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
| 724 | { |
| 725 | struct object *o = lookup_unknown_object(sha1); |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 726 | |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 727 | if (ref_is_hidden(refname)) { |
| 728 | o->flags |= HIDDEN_REF; |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 729 | return 1; |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 730 | } |
Junio C Hamano | cbbe50d | 2013-01-18 15:48:49 -0800 | [diff] [blame] | 731 | if (!o) |
| 732 | die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1)); |
Junio C Hamano | 3f1da57 | 2013-01-28 20:45:43 -0800 | [diff] [blame] | 733 | o->flags |= OUR_REF; |
Junio C Hamano | cbbe50d | 2013-01-18 15:48:49 -0800 | [diff] [blame] | 734 | return 0; |
| 735 | } |
| 736 | |
Junio C Hamano | 8da1977 | 2006-09-20 22:02:01 -0700 | [diff] [blame] | 737 | static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 738 | { |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 739 | static const char *capabilities = "multi_ack thin-pack side-band" |
Shawn O. Pearce | 348e390 | 2008-03-03 22:27:33 -0500 | [diff] [blame] | 740 | " side-band-64k ofs-delta shallow no-progress" |
Shawn O. Pearce | 78affc4 | 2009-10-30 17:47:25 -0700 | [diff] [blame] | 741 | " include-tag multi_ack_detailed"; |
Josh Triplett | 6b01ecf | 2011-07-08 16:13:32 -0700 | [diff] [blame] | 742 | const char *refname_nons = strip_namespace(refname); |
Jeff King | 435c833 | 2012-10-04 04:03:33 -0400 | [diff] [blame] | 743 | unsigned char peeled[20]; |
Carl Worth | b5b1699 | 2006-02-17 16:14:52 -0800 | [diff] [blame] | 744 | |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 745 | if (mark_our_ref(refname, sha1, flag, cb_data)) |
| 746 | return 0; |
Junio C Hamano | cbbe50d | 2013-01-18 15:48:49 -0800 | [diff] [blame] | 747 | |
Johannes Schindelin | 1f5881b | 2005-10-28 05:56:41 +0200 | [diff] [blame] | 748 | if (capabilities) |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 749 | packet_write(1, "%s %s%c%s%s%s agent=%s\n", |
Jeff King | ff5effd | 2012-08-03 12:19:16 -0400 | [diff] [blame] | 750 | sha1_to_hex(sha1), refname_nons, |
Junio C Hamano | cf2ad8e | 2011-03-29 10:24:59 -0700 | [diff] [blame] | 751 | 0, capabilities, |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 752 | allow_tip_sha1_in_want ? " allow-tip-sha1-in-want" : "", |
Jeff King | ff5effd | 2012-08-03 12:19:16 -0400 | [diff] [blame] | 753 | stateless_rpc ? " no-done" : "", |
| 754 | git_user_agent_sanitized()); |
Johannes Schindelin | 1f5881b | 2005-10-28 05:56:41 +0200 | [diff] [blame] | 755 | else |
Josh Triplett | 6b01ecf | 2011-07-08 16:13:32 -0700 | [diff] [blame] | 756 | packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons); |
Johannes Schindelin | 1f5881b | 2005-10-28 05:56:41 +0200 | [diff] [blame] | 757 | capabilities = NULL; |
Jeff King | 435c833 | 2012-10-04 04:03:33 -0400 | [diff] [blame] | 758 | if (!peel_ref(refname, peeled)) |
| 759 | packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons); |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 760 | return 0; |
| 761 | } |
| 762 | |
David Rientjes | 59076eb | 2006-08-14 13:40:51 -0700 | [diff] [blame] | 763 | static void upload_pack(void) |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 764 | { |
Shawn O. Pearce | 42526b4 | 2009-10-30 17:47:33 -0700 | [diff] [blame] | 765 | if (advertise_refs || !stateless_rpc) { |
| 766 | reset_timeout(); |
Josh Triplett | 6b01ecf | 2011-07-08 16:13:32 -0700 | [diff] [blame] | 767 | head_ref_namespaced(send_ref, NULL); |
| 768 | for_each_namespaced_ref(send_ref, NULL); |
Shawn O. Pearce | 42526b4 | 2009-10-30 17:47:33 -0700 | [diff] [blame] | 769 | packet_flush(1); |
| 770 | } else { |
Josh Triplett | 6b01ecf | 2011-07-08 16:13:32 -0700 | [diff] [blame] | 771 | head_ref_namespaced(mark_our_ref, NULL); |
| 772 | for_each_namespaced_ref(mark_our_ref, NULL); |
Shawn O. Pearce | 42526b4 | 2009-10-30 17:47:33 -0700 | [diff] [blame] | 773 | } |
| 774 | if (advertise_refs) |
| 775 | return; |
| 776 | |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 777 | receive_needs(); |
David Rientjes | 59076eb | 2006-08-14 13:40:51 -0700 | [diff] [blame] | 778 | if (want_obj.nr) { |
| 779 | get_common_commits(); |
| 780 | create_pack_file(); |
| 781 | } |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 784 | static int upload_pack_config(const char *var, const char *value, void *unused) |
| 785 | { |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 786 | if (!strcmp("uploadpack.allowtipsha1inwant", var)) |
| 787 | allow_tip_sha1_in_want = git_config_bool(var, value); |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 788 | return parse_hide_refs_config(var, value, "uploadpack"); |
| 789 | } |
| 790 | |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 791 | int main(int argc, char **argv) |
| 792 | { |
Andreas Ericsson | 8d63013 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 793 | char *dir; |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 794 | int i; |
| 795 | int strict = 0; |
| 796 | |
Ævar Arnfjörð Bjarmason | 5e9637c | 2011-11-18 00:14:42 +0100 | [diff] [blame] | 797 | git_setup_gettext(); |
| 798 | |
Jeff King | bbc30f9 | 2011-02-24 09:30:19 -0500 | [diff] [blame] | 799 | packet_trace_identity("upload-pack"); |
Steffen Prohaska | 2fb3f6d | 2009-01-18 13:00:12 +0100 | [diff] [blame] | 800 | git_extract_argv0_path(argv[0]); |
Christian Couder | dae556b | 2009-01-23 10:07:46 +0100 | [diff] [blame] | 801 | read_replace_refs = 0; |
Steffen Prohaska | 2fb3f6d | 2009-01-18 13:00:12 +0100 | [diff] [blame] | 802 | |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 803 | for (i = 1; i < argc; i++) { |
| 804 | char *arg = argv[i]; |
| 805 | |
| 806 | if (arg[0] != '-') |
| 807 | break; |
Shawn O. Pearce | 42526b4 | 2009-10-30 17:47:33 -0700 | [diff] [blame] | 808 | if (!strcmp(arg, "--advertise-refs")) { |
| 809 | advertise_refs = 1; |
| 810 | continue; |
| 811 | } |
| 812 | if (!strcmp(arg, "--stateless-rpc")) { |
| 813 | stateless_rpc = 1; |
| 814 | continue; |
| 815 | } |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 816 | if (!strcmp(arg, "--strict")) { |
| 817 | strict = 1; |
| 818 | continue; |
| 819 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 820 | if (!prefixcmp(arg, "--timeout=")) { |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 821 | timeout = atoi(arg+10); |
Johannes Sixt | 9462e3f | 2009-06-16 20:41:16 +0200 | [diff] [blame] | 822 | daemon_mode = 1; |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 823 | continue; |
| 824 | } |
| 825 | if (!strcmp(arg, "--")) { |
| 826 | i++; |
| 827 | break; |
| 828 | } |
| 829 | } |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 830 | |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 831 | if (i != argc-1) |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 832 | usage(upload_pack_usage); |
Johannes Sixt | 04b3305 | 2008-02-12 12:28:01 +0100 | [diff] [blame] | 833 | |
Johannes Sixt | e1464ca | 2008-07-21 21:19:52 +0200 | [diff] [blame] | 834 | setup_path(); |
Johannes Sixt | 04b3305 | 2008-02-12 12:28:01 +0100 | [diff] [blame] | 835 | |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 836 | dir = argv[i]; |
Linus Torvalds | 113b947 | 2005-07-08 16:22:22 -0700 | [diff] [blame] | 837 | |
Andreas Ericsson | 8d63013 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 838 | if (!enter_repo(dir, strict)) |
Jeff King | 05ac6b3 | 2009-03-04 03:32:29 -0500 | [diff] [blame] | 839 | die("'%s' does not appear to be a git repository", dir); |
Junio C Hamano | a0022ee | 2007-01-21 22:23:58 -0800 | [diff] [blame] | 840 | if (is_repository_shallow()) |
| 841 | die("attempt to fetch/clone from a shallow repository"); |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 842 | git_config(upload_pack_config, NULL); |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 843 | upload_pack(); |
| 844 | return 0; |
| 845 | } |