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