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