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