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