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