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