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