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