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