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