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