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