]>
Commit | Line | Data |
---|---|---|
def88e9a LT |
1 | #include "cache.h" |
2 | #include "refs.h" | |
3 | #include "pkt-line.h" | |
958c24b1 | 4 | #include "sideband.h" |
f6b42a81 JH |
5 | #include "tag.h" |
6 | #include "object.h" | |
f0243f26 | 7 | #include "commit.h" |
77cb17e9 | 8 | #include "exec_cmd.h" |
9b8dc263 JS |
9 | #include "diff.h" |
10 | #include "revision.h" | |
11 | #include "list-objects.h" | |
cc41fa8d | 12 | #include "run-command.h" |
051e4005 | 13 | #include "sigchain.h" |
ff5effdf | 14 | #include "version.h" |
daebaa78 | 15 | #include "string-list.h" |
def88e9a | 16 | |
62b4698e | 17 | static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>"; |
def88e9a | 18 | |
937a515a JH |
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 | ||
f53514bc JS |
26 | #define SHALLOW (1u << 16) |
27 | #define NOT_SHALLOW (1u << 17) | |
28 | #define CLIENT_SHALLOW (1u << 18) | |
29 | ||
3fbe2d54 | 30 | static unsigned long oldest_have; |
937a515a | 31 | |
3f1da57f | 32 | static int multi_ack; |
4e10cf9a | 33 | static int no_done; |
348e390b | 34 | static int use_thin_pack, use_ofs_delta, use_include_tag; |
9462e3f5 | 35 | static int no_progress, daemon_mode; |
f0cea83f | 36 | static int shallow_nr; |
b1e9fff7 JH |
37 | static struct object_array have_obj; |
38 | static struct object_array want_obj; | |
6523078b | 39 | static struct object_array extra_edge_obj; |
96f1e58f | 40 | static unsigned int timeout; |
d47f3db7 JH |
41 | /* 0 for no sideband, |
42 | * otherwise maximum packet size (up to 65520 bytes). | |
43 | */ | |
96f1e58f | 44 | static int use_sideband; |
49aaddd1 | 45 | static int debug_fd; |
42526b47 SP |
46 | static int advertise_refs; |
47 | static int stateless_rpc; | |
960deccb PA |
48 | |
49 | static void reset_timeout(void) | |
50 | { | |
51 | alarm(timeout); | |
52 | } | |
fb9040cc | 53 | |
75bfc6c2 LT |
54 | static int strip(char *line, int len) |
55 | { | |
56 | if (len && line[len-1] == '\n') | |
57 | line[--len] = 0; | |
58 | return len; | |
59 | } | |
60 | ||
583b7ea3 JH |
61 | static ssize_t send_client_data(int fd, const char *data, ssize_t sz) |
62 | { | |
958c24b1 | 63 | if (use_sideband) |
d47f3db7 | 64 | return send_sideband(1, fd, data, sz, use_sideband); |
958c24b1 JH |
65 | if (fd == 3) |
66 | /* emergency quit */ | |
67 | fd = 2; | |
68 | if (fd == 2) { | |
93822c22 | 69 | /* XXX: are we happy to lose stuff here? */ |
958c24b1 JH |
70 | xwrite(fd, data, sz); |
71 | return sz; | |
583b7ea3 | 72 | } |
958c24b1 | 73 | return safe_write(fd, data, sz); |
583b7ea3 JH |
74 | } |
75 | ||
16befb8b | 76 | static FILE *pack_pipe = NULL; |
11c211fa | 77 | static void show_commit(struct commit *commit, void *data) |
9b8dc263 JS |
78 | { |
79 | if (commit->object.flags & BOUNDARY) | |
80 | fputc('-', pack_pipe); | |
81 | if (fputs(sha1_to_hex(commit->object.sha1), pack_pipe) < 0) | |
82 | die("broken output pipe"); | |
83 | fputc('\n', pack_pipe); | |
84 | fflush(pack_pipe); | |
85 | free(commit->buffer); | |
86 | commit->buffer = NULL; | |
87 | } | |
88 | ||
49473672 JH |
89 | static void show_object(struct object *obj, |
90 | const struct name_path *path, const char *component, | |
91 | void *cb_data) | |
9b8dc263 | 92 | { |
91f17516 | 93 | show_object_with_name(pack_pipe, obj, path, component); |
9b8dc263 JS |
94 | } |
95 | ||
96 | static void show_edge(struct commit *commit) | |
97 | { | |
98 | fprintf(pack_pipe, "-%s\n", sha1_to_hex(commit->object.sha1)); | |
99 | } | |
100 | ||
1e39d7de | 101 | static int do_rev_list(int in, int out, void *user_data) |
80ccaa78 JS |
102 | { |
103 | int i; | |
104 | struct rev_info revs; | |
105 | ||
ae6a5609 | 106 | pack_pipe = xfdopen(out, "w"); |
80ccaa78 JS |
107 | init_revisions(&revs, NULL); |
108 | revs.tag_objects = 1; | |
109 | revs.tree_objects = 1; | |
110 | revs.blob_objects = 1; | |
111 | if (use_thin_pack) | |
112 | revs.edge_hint = 1; | |
113 | ||
1e39d7de NTND |
114 | for (i = 0; i < want_obj.nr; i++) { |
115 | struct object *o = want_obj.objects[i].item; | |
116 | /* why??? */ | |
117 | o->flags &= ~UNINTERESTING; | |
118 | add_pending_object(&revs, o, NULL); | |
80ccaa78 | 119 | } |
1e39d7de NTND |
120 | for (i = 0; i < have_obj.nr; i++) { |
121 | struct object *o = have_obj.objects[i].item; | |
122 | o->flags |= UNINTERESTING; | |
123 | add_pending_object(&revs, o, NULL); | |
124 | } | |
125 | setup_revisions(0, NULL, &revs, NULL); | |
3d51e1b5 MK |
126 | if (prepare_revision_walk(&revs)) |
127 | die("revision walk setup failed"); | |
80ccaa78 | 128 | mark_edges_uninteresting(revs.commits, &revs, show_edge); |
6523078b NP |
129 | if (use_thin_pack) |
130 | for (i = 0; i < extra_edge_obj.nr; i++) | |
131 | fprintf(pack_pipe, "-%s\n", sha1_to_hex( | |
132 | extra_edge_obj.objects[i].item->sha1)); | |
11c211fa | 133 | traverse_commit_list(&revs, show_commit, show_object, NULL); |
618ebe9f JS |
134 | fflush(pack_pipe); |
135 | fclose(pack_pipe); | |
21edd3f1 | 136 | return 0; |
80ccaa78 JS |
137 | } |
138 | ||
fb9040cc LT |
139 | static void create_pack_file(void) |
140 | { | |
21edd3f1 | 141 | struct async rev_list; |
cc41fa8d | 142 | struct child_process pack_objects; |
363b7817 | 143 | char data[8193], progress[128]; |
583b7ea3 JH |
144 | char abort_msg[] = "aborting due to possible repository " |
145 | "corruption on the remote side."; | |
b1c71b72 | 146 | int buffered = -1; |
1456b043 | 147 | ssize_t sz; |
cc41fa8d JS |
148 | const char *argv[10]; |
149 | int arg = 0; | |
75bfc6c2 | 150 | |
b9612197 JK |
151 | argv[arg++] = "pack-objects"; |
152 | if (!shallow_nr) { | |
f0cea83f | 153 | argv[arg++] = "--revs"; |
3f1da57f | 154 | if (use_thin_pack) |
f0cea83f NE |
155 | argv[arg++] = "--thin"; |
156 | } | |
75bfc6c2 | 157 | |
cc41fa8d JS |
158 | argv[arg++] = "--stdout"; |
159 | if (!no_progress) | |
160 | argv[arg++] = "--progress"; | |
161 | if (use_ofs_delta) | |
162 | argv[arg++] = "--delta-base-offset"; | |
348e390b SP |
163 | if (use_include_tag) |
164 | argv[arg++] = "--include-tag"; | |
cc41fa8d JS |
165 | argv[arg++] = NULL; |
166 | ||
167 | memset(&pack_objects, 0, sizeof(pack_objects)); | |
b9612197 | 168 | pack_objects.in = -1; |
cc41fa8d JS |
169 | pack_objects.out = -1; |
170 | pack_objects.err = -1; | |
171 | pack_objects.git_cmd = 1; | |
172 | pack_objects.argv = argv; | |
21edd3f1 | 173 | |
4c324c00 | 174 | if (start_command(&pack_objects)) |
7e44c935 | 175 | die("git upload-pack: unable to fork git-pack-objects"); |
b1c71b72 | 176 | |
b9612197 JK |
177 | if (shallow_nr) { |
178 | memset(&rev_list, 0, sizeof(rev_list)); | |
179 | rev_list.proc = do_rev_list; | |
180 | rev_list.out = pack_objects.in; | |
181 | if (start_async(&rev_list)) | |
182 | die("git upload-pack: unable to fork git-rev-list"); | |
183 | } | |
184 | else { | |
41698375 | 185 | FILE *pipe_fd = xfdopen(pack_objects.in, "w"); |
3f1da57f | 186 | int i; |
f0cea83f | 187 | |
3f1da57f JH |
188 | for (i = 0; i < want_obj.nr; i++) |
189 | fprintf(pipe_fd, "%s\n", | |
190 | sha1_to_hex(want_obj.objects[i].item->sha1)); | |
191 | fprintf(pipe_fd, "--not\n"); | |
192 | for (i = 0; i < have_obj.nr; i++) | |
193 | fprintf(pipe_fd, "%s\n", | |
194 | sha1_to_hex(have_obj.objects[i].item->sha1)); | |
f0cea83f NE |
195 | fprintf(pipe_fd, "\n"); |
196 | fflush(pipe_fd); | |
197 | fclose(pipe_fd); | |
198 | } | |
199 | ||
200 | ||
cc41fa8d JS |
201 | /* We read from pack_objects.err to capture stderr output for |
202 | * progress bar, and pack_objects.out to capture the pack data. | |
b1c71b72 | 203 | */ |
b1c71b72 JH |
204 | |
205 | while (1) { | |
b1c71b72 | 206 | struct pollfd pfd[2]; |
363b7817 | 207 | int pe, pu, pollsize; |
b1c71b72 | 208 | |
0d516ada ML |
209 | reset_timeout(); |
210 | ||
b1c71b72 | 211 | pollsize = 0; |
363b7817 | 212 | pe = pu = -1; |
b1c71b72 | 213 | |
cc41fa8d JS |
214 | if (0 <= pack_objects.out) { |
215 | pfd[pollsize].fd = pack_objects.out; | |
b1c71b72 JH |
216 | pfd[pollsize].events = POLLIN; |
217 | pu = pollsize; | |
218 | pollsize++; | |
219 | } | |
cc41fa8d JS |
220 | if (0 <= pack_objects.err) { |
221 | pfd[pollsize].fd = pack_objects.err; | |
363b7817 JH |
222 | pfd[pollsize].events = POLLIN; |
223 | pe = pollsize; | |
224 | pollsize++; | |
225 | } | |
b1c71b72 | 226 | |
4c324c00 JS |
227 | if (!pollsize) |
228 | break; | |
229 | ||
230 | if (poll(pfd, pollsize, -1) < 0) { | |
231 | if (errno != EINTR) { | |
232 | error("poll failed, resuming: %s", | |
233 | strerror(errno)); | |
234 | sleep(1); | |
b1c71b72 | 235 | } |
4c324c00 JS |
236 | continue; |
237 | } | |
6b59f51b NP |
238 | if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) { |
239 | /* Status ready; we ship that in the side-band | |
240 | * or dump to the standard error. | |
241 | */ | |
242 | sz = xread(pack_objects.err, progress, | |
243 | sizeof(progress)); | |
244 | if (0 < sz) | |
245 | send_client_data(2, progress, sz); | |
246 | else if (sz == 0) { | |
247 | close(pack_objects.err); | |
248 | pack_objects.err = -1; | |
249 | } | |
250 | else | |
251 | goto fail; | |
252 | /* give priority to status messages */ | |
253 | continue; | |
254 | } | |
4c324c00 JS |
255 | if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) { |
256 | /* Data ready; we keep the last byte to ourselves | |
257 | * in case we detect broken rev-list, so that we | |
258 | * can leave the stream corrupted. This is | |
259 | * unfortunate -- unpack-objects would happily | |
260 | * accept a valid packdata with trailing garbage, | |
261 | * so appending garbage after we pass all the | |
262 | * pack data is not good enough to signal | |
263 | * breakage to downstream. | |
264 | */ | |
265 | char *cp = data; | |
266 | ssize_t outsz = 0; | |
267 | if (0 <= buffered) { | |
268 | *cp++ = buffered; | |
269 | outsz++; | |
b1c71b72 | 270 | } |
4c324c00 JS |
271 | sz = xread(pack_objects.out, cp, |
272 | sizeof(data) - outsz); | |
273 | if (0 < sz) | |
1456b043 | 274 | ; |
4c324c00 JS |
275 | else if (sz == 0) { |
276 | close(pack_objects.out); | |
277 | pack_objects.out = -1; | |
363b7817 | 278 | } |
4c324c00 JS |
279 | else |
280 | goto fail; | |
281 | sz += outsz; | |
282 | if (1 < sz) { | |
283 | buffered = data[sz-1] & 0xFF; | |
284 | sz--; | |
b1c71b72 | 285 | } |
4c324c00 JS |
286 | else |
287 | buffered = -1; | |
288 | sz = send_client_data(1, data, sz); | |
289 | if (sz < 0) | |
b1c71b72 | 290 | goto fail; |
4c324c00 | 291 | } |
4c324c00 | 292 | } |
b1c71b72 | 293 | |
4c324c00 | 294 | if (finish_command(&pack_objects)) { |
7e44c935 | 295 | error("git upload-pack: git-pack-objects died with error."); |
4c324c00 JS |
296 | goto fail; |
297 | } | |
f0cea83f | 298 | if (shallow_nr && finish_async(&rev_list)) |
4c324c00 | 299 | goto fail; /* error was already reported */ |
b1c71b72 | 300 | |
4c324c00 JS |
301 | /* flush the data */ |
302 | if (0 <= buffered) { | |
303 | data[0] = buffered; | |
304 | sz = send_client_data(1, data, 1); | |
305 | if (sz < 0) | |
306 | goto fail; | |
307 | fprintf(stderr, "flushed.\n"); | |
b1c71b72 | 308 | } |
4c324c00 JS |
309 | if (use_sideband) |
310 | packet_flush(1); | |
311 | return; | |
312 | ||
b1c71b72 | 313 | fail: |
583b7ea3 | 314 | send_client_data(3, abort_msg, sizeof(abort_msg)); |
7e44c935 | 315 | die("git upload-pack: %s", abort_msg); |
fb9040cc LT |
316 | } |
317 | ||
def88e9a LT |
318 | static int got_sha1(char *hex, unsigned char *sha1) |
319 | { | |
b1e9fff7 | 320 | struct object *o; |
937a515a | 321 | int we_knew_they_have = 0; |
b1e9fff7 | 322 | |
def88e9a | 323 | if (get_sha1_hex(hex, sha1)) |
7e44c935 | 324 | die("git upload-pack: expected SHA1 object, got '%s'", hex); |
fb9040cc | 325 | if (!has_sha1_file(sha1)) |
937a515a | 326 | return -1; |
b1e9fff7 | 327 | |
a6eec126 | 328 | o = parse_object(sha1); |
b1e9fff7 JH |
329 | if (!o) |
330 | die("oops (%s)", sha1_to_hex(sha1)); | |
182a8dab | 331 | if (o->type == OBJ_COMMIT) { |
b1e9fff7 | 332 | struct commit_list *parents; |
937a515a | 333 | struct commit *commit = (struct commit *)o; |
b1e9fff7 | 334 | if (o->flags & THEY_HAVE) |
937a515a JH |
335 | we_knew_they_have = 1; |
336 | else | |
337 | o->flags |= THEY_HAVE; | |
338 | if (!oldest_have || (commit->date < oldest_have)) | |
339 | oldest_have = commit->date; | |
340 | for (parents = commit->parents; | |
b1e9fff7 JH |
341 | parents; |
342 | parents = parents->next) | |
343 | parents->item->object.flags |= THEY_HAVE; | |
fb9040cc | 344 | } |
937a515a JH |
345 | if (!we_knew_they_have) { |
346 | add_object_array(o, NULL, &have_obj); | |
347 | return 1; | |
348 | } | |
349 | return 0; | |
350 | } | |
351 | ||
352 | static int reachable(struct commit *want) | |
353 | { | |
354 | struct commit_list *work = NULL; | |
355 | ||
47e44ed1 | 356 | commit_list_insert_by_date(want, &work); |
937a515a JH |
357 | while (work) { |
358 | struct commit_list *list = work->next; | |
359 | struct commit *commit = work->item; | |
360 | free(work); | |
361 | work = list; | |
362 | ||
363 | if (commit->object.flags & THEY_HAVE) { | |
364 | want->object.flags |= COMMON_KNOWN; | |
365 | break; | |
366 | } | |
367 | if (!commit->object.parsed) | |
368 | parse_object(commit->object.sha1); | |
369 | if (commit->object.flags & REACHABLE) | |
370 | continue; | |
371 | commit->object.flags |= REACHABLE; | |
372 | if (commit->date < oldest_have) | |
373 | continue; | |
374 | for (list = commit->parents; list; list = list->next) { | |
375 | struct commit *parent = list->item; | |
376 | if (!(parent->object.flags & REACHABLE)) | |
47e44ed1 | 377 | commit_list_insert_by_date(parent, &work); |
937a515a JH |
378 | } |
379 | } | |
380 | want->object.flags |= REACHABLE; | |
381 | clear_commit_marks(want, REACHABLE); | |
382 | free_commit_list(work); | |
383 | return (want->object.flags & COMMON_KNOWN); | |
384 | } | |
385 | ||
386 | static int ok_to_give_up(void) | |
387 | { | |
388 | int i; | |
389 | ||
390 | if (!have_obj.nr) | |
391 | return 0; | |
392 | ||
393 | for (i = 0; i < want_obj.nr; i++) { | |
394 | struct object *want = want_obj.objects[i].item; | |
395 | ||
396 | if (want->flags & COMMON_KNOWN) | |
397 | continue; | |
398 | want = deref_tag(want, "a want line", 0); | |
399 | if (!want || want->type != OBJ_COMMIT) { | |
400 | /* no way to tell if this is reachable by | |
401 | * looking at the ancestry chain alone, so | |
402 | * leave a note to ourselves not to worry about | |
403 | * this object anymore. | |
404 | */ | |
405 | want_obj.objects[i].item->flags |= COMMON_KNOWN; | |
406 | continue; | |
407 | } | |
408 | if (!reachable((struct commit *)want)) | |
409 | return 0; | |
410 | } | |
fb9040cc | 411 | return 1; |
def88e9a LT |
412 | } |
413 | ||
414 | static int get_common_commits(void) | |
415 | { | |
416 | static char line[1000]; | |
c04c4e57 | 417 | unsigned char sha1[20]; |
78affc49 | 418 | char last_hex[41]; |
49bee717 SP |
419 | int got_common = 0; |
420 | int got_other = 0; | |
4e10cf9a | 421 | int sent_ready = 0; |
def88e9a | 422 | |
f0243f26 JS |
423 | save_commit_buffer = 0; |
424 | ||
eeefa7c9 | 425 | for (;;) { |
fd13b21f | 426 | int len = packet_read_line(0, line, sizeof(line)); |
960deccb | 427 | reset_timeout(); |
def88e9a LT |
428 | |
429 | if (!len) { | |
49bee717 | 430 | if (multi_ack == 2 && got_common |
4e10cf9a JH |
431 | && !got_other && ok_to_give_up()) { |
432 | sent_ready = 1; | |
49bee717 | 433 | packet_write(1, "ACK %s ready\n", last_hex); |
4e10cf9a | 434 | } |
b1e9fff7 | 435 | if (have_obj.nr == 0 || multi_ack) |
1bd8c8f0 | 436 | packet_write(1, "NAK\n"); |
4e10cf9a JH |
437 | |
438 | if (no_done && sent_ready) { | |
439 | packet_write(1, "ACK %s\n", last_hex); | |
440 | return 0; | |
441 | } | |
42526b47 SP |
442 | if (stateless_rpc) |
443 | exit(0); | |
49bee717 SP |
444 | got_common = 0; |
445 | got_other = 0; | |
def88e9a LT |
446 | continue; |
447 | } | |
fd13b21f | 448 | strip(line, len); |
cc44c765 | 449 | if (!prefixcmp(line, "have ")) { |
937a515a JH |
450 | switch (got_sha1(line+5, sha1)) { |
451 | case -1: /* they have what we do not */ | |
49bee717 | 452 | got_other = 1; |
78affc49 SP |
453 | if (multi_ack && ok_to_give_up()) { |
454 | const char *hex = sha1_to_hex(sha1); | |
4e10cf9a JH |
455 | if (multi_ack == 2) { |
456 | sent_ready = 1; | |
78affc49 | 457 | packet_write(1, "ACK %s ready\n", hex); |
4e10cf9a | 458 | } else |
78affc49 SP |
459 | packet_write(1, "ACK %s continue\n", hex); |
460 | } | |
937a515a JH |
461 | break; |
462 | default: | |
49bee717 | 463 | got_common = 1; |
78affc49 SP |
464 | memcpy(last_hex, sha1_to_hex(sha1), 41); |
465 | if (multi_ack == 2) | |
466 | packet_write(1, "ACK %s common\n", last_hex); | |
467 | else if (multi_ack) | |
468 | packet_write(1, "ACK %s continue\n", last_hex); | |
c04c4e57 | 469 | else if (have_obj.nr == 1) |
78affc49 | 470 | packet_write(1, "ACK %s\n", last_hex); |
937a515a | 471 | break; |
af2d3aa4 | 472 | } |
def88e9a LT |
473 | continue; |
474 | } | |
475 | if (!strcmp(line, "done")) { | |
b1e9fff7 | 476 | if (have_obj.nr > 0) { |
1bd8c8f0 | 477 | if (multi_ack) |
c04c4e57 | 478 | packet_write(1, "ACK %s\n", last_hex); |
1bd8c8f0 JS |
479 | return 0; |
480 | } | |
def88e9a LT |
481 | packet_write(1, "NAK\n"); |
482 | return -1; | |
483 | } | |
7e44c935 | 484 | die("git upload-pack: expected SHA1 list, got '%s'", line); |
def88e9a | 485 | } |
def88e9a LT |
486 | } |
487 | ||
051e4005 JH |
488 | static void check_non_tip(void) |
489 | { | |
490 | static const char *argv[] = { | |
491 | "rev-list", "--stdin", NULL, | |
492 | }; | |
493 | static struct child_process cmd; | |
494 | struct object *o; | |
495 | char namebuf[42]; /* ^ + SHA-1 + LF */ | |
496 | int i; | |
497 | ||
498 | /* In the normal in-process case non-tip request can never happen */ | |
499 | if (!stateless_rpc) | |
500 | goto error; | |
501 | ||
502 | cmd.argv = argv; | |
503 | cmd.git_cmd = 1; | |
504 | cmd.no_stderr = 1; | |
505 | cmd.in = -1; | |
506 | cmd.out = -1; | |
507 | ||
508 | if (start_command(&cmd)) | |
509 | goto error; | |
510 | ||
511 | /* | |
512 | * If rev-list --stdin encounters an unknown commit, it | |
513 | * terminates, which will cause SIGPIPE in the write loop | |
514 | * below. | |
515 | */ | |
516 | sigchain_push(SIGPIPE, SIG_IGN); | |
517 | ||
518 | namebuf[0] = '^'; | |
519 | namebuf[41] = '\n'; | |
520 | for (i = get_max_object_index(); 0 < i; ) { | |
521 | o = get_indexed_object(--i); | |
2a745324 BH |
522 | if (!o) |
523 | continue; | |
051e4005 JH |
524 | if (!(o->flags & OUR_REF)) |
525 | continue; | |
526 | memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40); | |
527 | if (write_in_full(cmd.in, namebuf, 42) < 0) | |
528 | goto error; | |
529 | } | |
530 | namebuf[40] = '\n'; | |
531 | for (i = 0; i < want_obj.nr; i++) { | |
532 | o = want_obj.objects[i].item; | |
533 | if (o->flags & OUR_REF) | |
534 | continue; | |
535 | memcpy(namebuf, sha1_to_hex(o->sha1), 40); | |
536 | if (write_in_full(cmd.in, namebuf, 41) < 0) | |
537 | goto error; | |
538 | } | |
539 | close(cmd.in); | |
540 | ||
541 | sigchain_pop(SIGPIPE); | |
542 | ||
543 | /* | |
544 | * The commits out of the rev-list are not ancestors of | |
545 | * our ref. | |
546 | */ | |
547 | i = read_in_full(cmd.out, namebuf, 1); | |
548 | if (i) | |
549 | goto error; | |
550 | close(cmd.out); | |
551 | ||
552 | /* | |
553 | * rev-list may have died by encountering a bad commit | |
554 | * in the history, in which case we do want to bail out | |
555 | * even when it showed no commit. | |
556 | */ | |
557 | if (finish_command(&cmd)) | |
558 | goto error; | |
559 | ||
560 | /* All the non-tip ones are ancestors of what we advertised */ | |
561 | return; | |
562 | ||
563 | error: | |
564 | /* Pick one of them (we know there at least is one) */ | |
565 | for (i = 0; i < want_obj.nr; i++) { | |
566 | o = want_obj.objects[i].item; | |
567 | if (!(o->flags & OUR_REF)) | |
568 | die("git upload-pack: not our ref %s", | |
569 | sha1_to_hex(o->sha1)); | |
570 | } | |
571 | } | |
572 | ||
b1e9fff7 | 573 | static void receive_needs(void) |
fb9040cc | 574 | { |
3cd47459 | 575 | struct object_array shallows = OBJECT_ARRAY_INIT; |
fb9040cc | 576 | static char line[1000]; |
016e6ccb | 577 | int len, depth = 0; |
051e4005 | 578 | int has_non_tip = 0; |
fb9040cc | 579 | |
f0cea83f | 580 | shallow_nr = 0; |
49aaddd1 | 581 | if (debug_fd) |
2b7ca830 | 582 | write_str_in_full(debug_fd, "#S\n"); |
fb9040cc | 583 | for (;;) { |
565ebbf7 | 584 | struct object *o; |
f47182c8 | 585 | const char *features; |
6ece0d30 | 586 | unsigned char sha1_buf[20]; |
fb9040cc | 587 | len = packet_read_line(0, line, sizeof(line)); |
960deccb | 588 | reset_timeout(); |
fb9040cc | 589 | if (!len) |
ed09aef0 | 590 | break; |
49aaddd1 SP |
591 | if (debug_fd) |
592 | write_in_full(debug_fd, line, len); | |
e091eb93 | 593 | |
599065a3 | 594 | if (!prefixcmp(line, "shallow ")) { |
ed09aef0 JS |
595 | unsigned char sha1[20]; |
596 | struct object *object; | |
597 | if (get_sha1(line + 8, sha1)) | |
598 | die("invalid shallow line: %s", line); | |
599 | object = parse_object(sha1); | |
600 | if (!object) | |
601 | die("did not find object for %s", line); | |
6293ded3 NTND |
602 | if (object->type != OBJ_COMMIT) |
603 | die("invalid shallow object %s", sha1_to_hex(sha1)); | |
f53514bc | 604 | object->flags |= CLIENT_SHALLOW; |
ed09aef0 JS |
605 | add_object_array(object, NULL, &shallows); |
606 | continue; | |
607 | } | |
599065a3 | 608 | if (!prefixcmp(line, "deepen ")) { |
016e6ccb JS |
609 | char *end; |
610 | depth = strtol(line + 7, &end, 0); | |
611 | if (end == line + 7 || depth <= 0) | |
612 | die("Invalid deepen: %s", line); | |
613 | continue; | |
614 | } | |
599065a3 | 615 | if (prefixcmp(line, "want ") || |
6ece0d30 | 616 | get_sha1_hex(line+5, sha1_buf)) |
7e44c935 | 617 | die("git upload-pack: protocol error, " |
e091eb93 | 618 | "expected to get sha, not '%s'", line); |
f47182c8 JH |
619 | |
620 | features = line + 45; | |
621 | ||
622 | if (parse_feature_request(features, "multi_ack_detailed")) | |
78affc49 | 623 | multi_ack = 2; |
f47182c8 | 624 | else if (parse_feature_request(features, "multi_ack")) |
1bd8c8f0 | 625 | multi_ack = 1; |
f47182c8 | 626 | if (parse_feature_request(features, "no-done")) |
4e10cf9a | 627 | no_done = 1; |
f47182c8 | 628 | if (parse_feature_request(features, "thin-pack")) |
b19696c2 | 629 | use_thin_pack = 1; |
f47182c8 | 630 | if (parse_feature_request(features, "ofs-delta")) |
e4fe4b8e | 631 | use_ofs_delta = 1; |
f47182c8 | 632 | if (parse_feature_request(features, "side-band-64k")) |
d47f3db7 | 633 | use_sideband = LARGE_PACKET_MAX; |
f47182c8 | 634 | else if (parse_feature_request(features, "side-band")) |
d47f3db7 | 635 | use_sideband = DEFAULT_PACKET_MAX; |
f47182c8 | 636 | if (parse_feature_request(features, "no-progress")) |
b0e90897 | 637 | no_progress = 1; |
f47182c8 | 638 | if (parse_feature_request(features, "include-tag")) |
348e390b | 639 | use_include_tag = 1; |
565ebbf7 | 640 | |
f59de5d1 | 641 | o = parse_object(sha1_buf); |
051e4005 | 642 | if (!o) |
9f9aa761 EN |
643 | die("git upload-pack: not our ref %s", |
644 | sha1_to_hex(sha1_buf)); | |
565ebbf7 JH |
645 | if (!(o->flags & WANTED)) { |
646 | o->flags |= WANTED; | |
051e4005 JH |
647 | if (!(o->flags & OUR_REF)) |
648 | has_non_tip = 1; | |
b1e9fff7 | 649 | add_object_array(o, NULL, &want_obj); |
565ebbf7 | 650 | } |
fb9040cc | 651 | } |
49aaddd1 | 652 | if (debug_fd) |
2b7ca830 | 653 | write_str_in_full(debug_fd, "#E\n"); |
9462e3f5 | 654 | |
051e4005 JH |
655 | /* |
656 | * We have sent all our refs already, and the other end | |
657 | * should have chosen out of them. When we are operating | |
658 | * in the stateless RPC mode, however, their choice may | |
659 | * have been based on the set of older refs advertised | |
660 | * by another process that handled the initial request. | |
661 | */ | |
662 | if (has_non_tip) | |
663 | check_non_tip(); | |
664 | ||
9462e3f5 JS |
665 | if (!use_sideband && daemon_mode) |
666 | no_progress = 1; | |
667 | ||
f53514bc JS |
668 | if (depth == 0 && shallows.nr == 0) |
669 | return; | |
016e6ccb | 670 | if (depth > 0) { |
4dcb167f | 671 | struct commit_list *result = NULL, *backup = NULL; |
f53514bc | 672 | int i; |
4dcb167f NTND |
673 | if (depth == INFINITE_DEPTH) |
674 | for (i = 0; i < shallows.nr; i++) { | |
675 | struct object *object = shallows.objects[i].item; | |
676 | object->flags |= NOT_SHALLOW; | |
677 | } | |
678 | else | |
679 | backup = result = | |
680 | get_shallow_commits(&want_obj, depth, | |
681 | SHALLOW, NOT_SHALLOW); | |
016e6ccb | 682 | while (result) { |
f53514bc | 683 | struct object *object = &result->item->object; |
1f2de769 | 684 | if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) { |
f53514bc JS |
685 | packet_write(1, "shallow %s", |
686 | sha1_to_hex(object->sha1)); | |
687 | register_shallow(object->sha1); | |
f0cea83f | 688 | shallow_nr++; |
f53514bc | 689 | } |
016e6ccb JS |
690 | result = result->next; |
691 | } | |
692 | free_commit_list(backup); | |
f53514bc JS |
693 | for (i = 0; i < shallows.nr; i++) { |
694 | struct object *object = shallows.objects[i].item; | |
695 | if (object->flags & NOT_SHALLOW) { | |
696 | struct commit_list *parents; | |
697 | packet_write(1, "unshallow %s", | |
698 | sha1_to_hex(object->sha1)); | |
699 | object->flags &= ~CLIENT_SHALLOW; | |
700 | /* make sure the real parents are parsed */ | |
701 | unregister_shallow(object->sha1); | |
176d45cb | 702 | object->parsed = 0; |
dec38c81 MK |
703 | if (parse_commit((struct commit *)object)) |
704 | die("invalid commit"); | |
f53514bc JS |
705 | parents = ((struct commit *)object)->parents; |
706 | while (parents) { | |
707 | add_object_array(&parents->item->object, | |
708 | NULL, &want_obj); | |
709 | parents = parents->next; | |
710 | } | |
6523078b | 711 | add_object_array(object, NULL, &extra_edge_obj); |
f53514bc JS |
712 | } |
713 | /* make sure commit traversal conforms to client */ | |
714 | register_shallow(object->sha1); | |
715 | } | |
716 | packet_flush(1); | |
717 | } else | |
718 | if (shallows.nr > 0) { | |
719 | int i; | |
720 | for (i = 0; i < shallows.nr; i++) | |
721 | register_shallow(shallows.objects[i].item->sha1); | |
722 | } | |
f0cea83f NE |
723 | |
724 | shallow_nr += shallows.nr; | |
f53514bc | 725 | free(shallows.objects); |
fb9040cc LT |
726 | } |
727 | ||
daebaa78 | 728 | /* return non-zero if the ref is hidden, otherwise 0 */ |
cbbe50db JH |
729 | static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
730 | { | |
731 | struct object *o = lookup_unknown_object(sha1); | |
daebaa78 JH |
732 | |
733 | if (ref_is_hidden(refname)) | |
734 | return 1; | |
cbbe50db JH |
735 | if (!o) |
736 | die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1)); | |
3f1da57f | 737 | o->flags |= OUR_REF; |
cbbe50db JH |
738 | return 0; |
739 | } | |
740 | ||
8da19775 | 741 | static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
def88e9a | 742 | { |
ed09aef0 | 743 | static const char *capabilities = "multi_ack thin-pack side-band" |
348e390b | 744 | " side-band-64k ofs-delta shallow no-progress" |
78affc49 | 745 | " include-tag multi_ack_detailed"; |
6b01ecfe | 746 | const char *refname_nons = strip_namespace(refname); |
435c8332 | 747 | unsigned char peeled[20]; |
b5b16990 | 748 | |
daebaa78 JH |
749 | if (mark_our_ref(refname, sha1, flag, cb_data)) |
750 | return 0; | |
cbbe50db | 751 | |
1f5881bb | 752 | if (capabilities) |
ff5effdf JK |
753 | packet_write(1, "%s %s%c%s%s agent=%s\n", |
754 | sha1_to_hex(sha1), refname_nons, | |
cf2ad8e6 | 755 | 0, capabilities, |
ff5effdf JK |
756 | stateless_rpc ? " no-done" : "", |
757 | git_user_agent_sanitized()); | |
1f5881bb | 758 | else |
6b01ecfe | 759 | packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons); |
1f5881bb | 760 | capabilities = NULL; |
435c8332 JK |
761 | if (!peel_ref(refname, peeled)) |
762 | packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons); | |
def88e9a LT |
763 | return 0; |
764 | } | |
765 | ||
59076eba | 766 | static void upload_pack(void) |
def88e9a | 767 | { |
42526b47 SP |
768 | if (advertise_refs || !stateless_rpc) { |
769 | reset_timeout(); | |
6b01ecfe JT |
770 | head_ref_namespaced(send_ref, NULL); |
771 | for_each_namespaced_ref(send_ref, NULL); | |
42526b47 SP |
772 | packet_flush(1); |
773 | } else { | |
6b01ecfe JT |
774 | head_ref_namespaced(mark_our_ref, NULL); |
775 | for_each_namespaced_ref(mark_our_ref, NULL); | |
42526b47 SP |
776 | } |
777 | if (advertise_refs) | |
778 | return; | |
779 | ||
b1e9fff7 | 780 | receive_needs(); |
59076eba DR |
781 | if (want_obj.nr) { |
782 | get_common_commits(); | |
783 | create_pack_file(); | |
784 | } | |
def88e9a LT |
785 | } |
786 | ||
daebaa78 JH |
787 | static int upload_pack_config(const char *var, const char *value, void *unused) |
788 | { | |
789 | return parse_hide_refs_config(var, value, "uploadpack"); | |
790 | } | |
791 | ||
def88e9a LT |
792 | int main(int argc, char **argv) |
793 | { | |
8d630132 | 794 | char *dir; |
960deccb PA |
795 | int i; |
796 | int strict = 0; | |
797 | ||
5e9637c6 ÆAB |
798 | git_setup_gettext(); |
799 | ||
bbc30f99 | 800 | packet_trace_identity("upload-pack"); |
2fb3f6db | 801 | git_extract_argv0_path(argv[0]); |
dae556bd | 802 | read_replace_refs = 0; |
2fb3f6db | 803 | |
960deccb PA |
804 | for (i = 1; i < argc; i++) { |
805 | char *arg = argv[i]; | |
806 | ||
807 | if (arg[0] != '-') | |
808 | break; | |
42526b47 SP |
809 | if (!strcmp(arg, "--advertise-refs")) { |
810 | advertise_refs = 1; | |
811 | continue; | |
812 | } | |
813 | if (!strcmp(arg, "--stateless-rpc")) { | |
814 | stateless_rpc = 1; | |
815 | continue; | |
816 | } | |
960deccb PA |
817 | if (!strcmp(arg, "--strict")) { |
818 | strict = 1; | |
819 | continue; | |
820 | } | |
cc44c765 | 821 | if (!prefixcmp(arg, "--timeout=")) { |
960deccb | 822 | timeout = atoi(arg+10); |
9462e3f5 | 823 | daemon_mode = 1; |
960deccb PA |
824 | continue; |
825 | } | |
826 | if (!strcmp(arg, "--")) { | |
827 | i++; | |
828 | break; | |
829 | } | |
830 | } | |
a6080a0a | 831 | |
960deccb | 832 | if (i != argc-1) |
def88e9a | 833 | usage(upload_pack_usage); |
04b33055 | 834 | |
e1464ca7 | 835 | setup_path(); |
04b33055 | 836 | |
960deccb | 837 | dir = argv[i]; |
113b9475 | 838 | |
8d630132 | 839 | if (!enter_repo(dir, strict)) |
05ac6b34 | 840 | die("'%s' does not appear to be a git repository", dir); |
a0022eeb JH |
841 | if (is_repository_shallow()) |
842 | die("attempt to fetch/clone from a shallow repository"); | |
daebaa78 | 843 | git_config(upload_pack_config, NULL); |
49aaddd1 SP |
844 | if (getenv("GIT_DEBUG_SEND_PACK")) |
845 | debug_fd = atoi(getenv("GIT_DEBUG_SEND_PACK")); | |
def88e9a LT |
846 | upload_pack(); |
847 | return 0; | |
848 | } |