pkt-line: teach packet_read_line to chomp newlines
[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         static char line[1000];
412         unsigned char sha1[20];
413         char last_hex[41];
414         int got_common = 0;
415         int got_other = 0;
416         int sent_ready = 0;
417
418         save_commit_buffer = 0;
419
420         for (;;) {
421                 int len = packet_read_line(0, line, sizeof(line));
422                 reset_timeout();
423
424                 if (!len) {
425                         if (multi_ack == 2 && got_common
426                             && !got_other && ok_to_give_up()) {
427                                 sent_ready = 1;
428                                 packet_write(1, "ACK %s ready\n", last_hex);
429                         }
430                         if (have_obj.nr == 0 || multi_ack)
431                                 packet_write(1, "NAK\n");
432
433                         if (no_done && sent_ready) {
434                                 packet_write(1, "ACK %s\n", last_hex);
435                                 return 0;
436                         }
437                         if (stateless_rpc)
438                                 exit(0);
439                         got_common = 0;
440                         got_other = 0;
441                         continue;
442                 }
443                 if (!prefixcmp(line, "have ")) {
444                         switch (got_sha1(line+5, sha1)) {
445                         case -1: /* they have what we do not */
446                                 got_other = 1;
447                                 if (multi_ack && ok_to_give_up()) {
448                                         const char *hex = sha1_to_hex(sha1);
449                                         if (multi_ack == 2) {
450                                                 sent_ready = 1;
451                                                 packet_write(1, "ACK %s ready\n", hex);
452                                         } else
453                                                 packet_write(1, "ACK %s continue\n", hex);
454                                 }
455                                 break;
456                         default:
457                                 got_common = 1;
458                                 memcpy(last_hex, sha1_to_hex(sha1), 41);
459                                 if (multi_ack == 2)
460                                         packet_write(1, "ACK %s common\n", last_hex);
461                                 else if (multi_ack)
462                                         packet_write(1, "ACK %s continue\n", last_hex);
463                                 else if (have_obj.nr == 1)
464                                         packet_write(1, "ACK %s\n", last_hex);
465                                 break;
466                         }
467                         continue;
468                 }
469                 if (!strcmp(line, "done")) {
470                         if (have_obj.nr > 0) {
471                                 if (multi_ack)
472                                         packet_write(1, "ACK %s\n", last_hex);
473                                 return 0;
474                         }
475                         packet_write(1, "NAK\n");
476                         return -1;
477                 }
478                 die("git upload-pack: expected SHA1 list, got '%s'", line);
479         }
480 }
481
482 static void check_non_tip(void)
483 {
484         static const char *argv[] = {
485                 "rev-list", "--stdin", NULL,
486         };
487         static struct child_process cmd;
488         struct object *o;
489         char namebuf[42]; /* ^ + SHA-1 + LF */
490         int i;
491
492         /* In the normal in-process case non-tip request can never happen */
493         if (!stateless_rpc)
494                 goto error;
495
496         cmd.argv = argv;
497         cmd.git_cmd = 1;
498         cmd.no_stderr = 1;
499         cmd.in = -1;
500         cmd.out = -1;
501
502         if (start_command(&cmd))
503                 goto error;
504
505         /*
506          * If rev-list --stdin encounters an unknown commit, it
507          * terminates, which will cause SIGPIPE in the write loop
508          * below.
509          */
510         sigchain_push(SIGPIPE, SIG_IGN);
511
512         namebuf[0] = '^';
513         namebuf[41] = '\n';
514         for (i = get_max_object_index(); 0 < i; ) {
515                 o = get_indexed_object(--i);
516                 if (!o)
517                         continue;
518                 if (!(o->flags & OUR_REF))
519                         continue;
520                 memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40);
521                 if (write_in_full(cmd.in, namebuf, 42) < 0)
522                         goto error;
523         }
524         namebuf[40] = '\n';
525         for (i = 0; i < want_obj.nr; i++) {
526                 o = want_obj.objects[i].item;
527                 if (o->flags & OUR_REF)
528                         continue;
529                 memcpy(namebuf, sha1_to_hex(o->sha1), 40);
530                 if (write_in_full(cmd.in, namebuf, 41) < 0)
531                         goto error;
532         }
533         close(cmd.in);
534
535         sigchain_pop(SIGPIPE);
536
537         /*
538          * The commits out of the rev-list are not ancestors of
539          * our ref.
540          */
541         i = read_in_full(cmd.out, namebuf, 1);
542         if (i)
543                 goto error;
544         close(cmd.out);
545
546         /*
547          * rev-list may have died by encountering a bad commit
548          * in the history, in which case we do want to bail out
549          * even when it showed no commit.
550          */
551         if (finish_command(&cmd))
552                 goto error;
553
554         /* All the non-tip ones are ancestors of what we advertised */
555         return;
556
557 error:
558         /* Pick one of them (we know there at least is one) */
559         for (i = 0; i < want_obj.nr; i++) {
560                 o = want_obj.objects[i].item;
561                 if (!(o->flags & OUR_REF))
562                         die("git upload-pack: not our ref %s",
563                             sha1_to_hex(o->sha1));
564         }
565 }
566
567 static void receive_needs(void)
568 {
569         struct object_array shallows = OBJECT_ARRAY_INIT;
570         static char line[1000];
571         int len, depth = 0;
572         int has_non_tip = 0;
573
574         shallow_nr = 0;
575         for (;;) {
576                 struct object *o;
577                 const char *features;
578                 unsigned char sha1_buf[20];
579                 len = packet_read_line(0, line, sizeof(line));
580                 reset_timeout();
581                 if (!len)
582                         break;
583
584                 if (!prefixcmp(line, "shallow ")) {
585                         unsigned char sha1[20];
586                         struct object *object;
587                         if (get_sha1_hex(line + 8, sha1))
588                                 die("invalid shallow line: %s", line);
589                         object = parse_object(sha1);
590                         if (!object)
591                                 die("did not find object for %s", line);
592                         if (object->type != OBJ_COMMIT)
593                                 die("invalid shallow object %s", sha1_to_hex(sha1));
594                         if (!(object->flags & CLIENT_SHALLOW)) {
595                                 object->flags |= CLIENT_SHALLOW;
596                                 add_object_array(object, NULL, &shallows);
597                         }
598                         continue;
599                 }
600                 if (!prefixcmp(line, "deepen ")) {
601                         char *end;
602                         depth = strtol(line + 7, &end, 0);
603                         if (end == line + 7 || depth <= 0)
604                                 die("Invalid deepen: %s", line);
605                         continue;
606                 }
607                 if (prefixcmp(line, "want ") ||
608                     get_sha1_hex(line+5, sha1_buf))
609                         die("git upload-pack: protocol error, "
610                             "expected to get sha, not '%s'", line);
611
612                 features = line + 45;
613
614                 if (parse_feature_request(features, "multi_ack_detailed"))
615                         multi_ack = 2;
616                 else if (parse_feature_request(features, "multi_ack"))
617                         multi_ack = 1;
618                 if (parse_feature_request(features, "no-done"))
619                         no_done = 1;
620                 if (parse_feature_request(features, "thin-pack"))
621                         use_thin_pack = 1;
622                 if (parse_feature_request(features, "ofs-delta"))
623                         use_ofs_delta = 1;
624                 if (parse_feature_request(features, "side-band-64k"))
625                         use_sideband = LARGE_PACKET_MAX;
626                 else if (parse_feature_request(features, "side-band"))
627                         use_sideband = DEFAULT_PACKET_MAX;
628                 if (parse_feature_request(features, "no-progress"))
629                         no_progress = 1;
630                 if (parse_feature_request(features, "include-tag"))
631                         use_include_tag = 1;
632
633                 o = lookup_object(sha1_buf);
634                 if (!o)
635                         die("git upload-pack: not our ref %s",
636                             sha1_to_hex(sha1_buf));
637                 if (!(o->flags & WANTED)) {
638                         o->flags |= WANTED;
639                         if (!(o->flags & OUR_REF))
640                                 has_non_tip = 1;
641                         add_object_array(o, NULL, &want_obj);
642                 }
643         }
644
645         /*
646          * We have sent all our refs already, and the other end
647          * should have chosen out of them. When we are operating
648          * in the stateless RPC mode, however, their choice may
649          * have been based on the set of older refs advertised
650          * by another process that handled the initial request.
651          */
652         if (has_non_tip)
653                 check_non_tip();
654
655         if (!use_sideband && daemon_mode)
656                 no_progress = 1;
657
658         if (depth == 0 && shallows.nr == 0)
659                 return;
660         if (depth > 0) {
661                 struct commit_list *result = NULL, *backup = NULL;
662                 int i;
663                 if (depth == INFINITE_DEPTH)
664                         for (i = 0; i < shallows.nr; i++) {
665                                 struct object *object = shallows.objects[i].item;
666                                 object->flags |= NOT_SHALLOW;
667                         }
668                 else
669                         backup = result =
670                                 get_shallow_commits(&want_obj, depth,
671                                                     SHALLOW, NOT_SHALLOW);
672                 while (result) {
673                         struct object *object = &result->item->object;
674                         if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
675                                 packet_write(1, "shallow %s",
676                                                 sha1_to_hex(object->sha1));
677                                 register_shallow(object->sha1);
678                                 shallow_nr++;
679                         }
680                         result = result->next;
681                 }
682                 free_commit_list(backup);
683                 for (i = 0; i < shallows.nr; i++) {
684                         struct object *object = shallows.objects[i].item;
685                         if (object->flags & NOT_SHALLOW) {
686                                 struct commit_list *parents;
687                                 packet_write(1, "unshallow %s",
688                                         sha1_to_hex(object->sha1));
689                                 object->flags &= ~CLIENT_SHALLOW;
690                                 /* make sure the real parents are parsed */
691                                 unregister_shallow(object->sha1);
692                                 object->parsed = 0;
693                                 if (parse_commit((struct commit *)object))
694                                         die("invalid commit");
695                                 parents = ((struct commit *)object)->parents;
696                                 while (parents) {
697                                         add_object_array(&parents->item->object,
698                                                         NULL, &want_obj);
699                                         parents = parents->next;
700                                 }
701                                 add_object_array(object, NULL, &extra_edge_obj);
702                         }
703                         /* make sure commit traversal conforms to client */
704                         register_shallow(object->sha1);
705                 }
706                 packet_flush(1);
707         } else
708                 if (shallows.nr > 0) {
709                         int i;
710                         for (i = 0; i < shallows.nr; i++)
711                                 register_shallow(shallows.objects[i].item->sha1);
712                 }
713
714         shallow_nr += shallows.nr;
715         free(shallows.objects);
716 }
717
718 /* return non-zero if the ref is hidden, otherwise 0 */
719 static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
720 {
721         struct object *o = lookup_unknown_object(sha1);
722
723         if (ref_is_hidden(refname))
724                 return 1;
725         if (!o)
726                 die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1));
727         o->flags |= OUR_REF;
728         return 0;
729 }
730
731 static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
732 {
733         static const char *capabilities = "multi_ack thin-pack side-band"
734                 " side-band-64k ofs-delta shallow no-progress"
735                 " include-tag multi_ack_detailed";
736         const char *refname_nons = strip_namespace(refname);
737         unsigned char peeled[20];
738
739         if (mark_our_ref(refname, sha1, flag, cb_data))
740                 return 0;
741
742         if (capabilities)
743                 packet_write(1, "%s %s%c%s%s agent=%s\n",
744                              sha1_to_hex(sha1), refname_nons,
745                              0, capabilities,
746                              stateless_rpc ? " no-done" : "",
747                              git_user_agent_sanitized());
748         else
749                 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
750         capabilities = NULL;
751         if (!peel_ref(refname, peeled))
752                 packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons);
753         return 0;
754 }
755
756 static void upload_pack(void)
757 {
758         if (advertise_refs || !stateless_rpc) {
759                 reset_timeout();
760                 head_ref_namespaced(send_ref, NULL);
761                 for_each_namespaced_ref(send_ref, NULL);
762                 packet_flush(1);
763         } else {
764                 head_ref_namespaced(mark_our_ref, NULL);
765                 for_each_namespaced_ref(mark_our_ref, NULL);
766         }
767         if (advertise_refs)
768                 return;
769
770         receive_needs();
771         if (want_obj.nr) {
772                 get_common_commits();
773                 create_pack_file();
774         }
775 }
776
777 static int upload_pack_config(const char *var, const char *value, void *unused)
778 {
779         return parse_hide_refs_config(var, value, "uploadpack");
780 }
781
782 int main(int argc, char **argv)
783 {
784         char *dir;
785         int i;
786         int strict = 0;
787
788         git_setup_gettext();
789
790         packet_trace_identity("upload-pack");
791         git_extract_argv0_path(argv[0]);
792         read_replace_refs = 0;
793
794         for (i = 1; i < argc; i++) {
795                 char *arg = argv[i];
796
797                 if (arg[0] != '-')
798                         break;
799                 if (!strcmp(arg, "--advertise-refs")) {
800                         advertise_refs = 1;
801                         continue;
802                 }
803                 if (!strcmp(arg, "--stateless-rpc")) {
804                         stateless_rpc = 1;
805                         continue;
806                 }
807                 if (!strcmp(arg, "--strict")) {
808                         strict = 1;
809                         continue;
810                 }
811                 if (!prefixcmp(arg, "--timeout=")) {
812                         timeout = atoi(arg+10);
813                         daemon_mode = 1;
814                         continue;
815                 }
816                 if (!strcmp(arg, "--")) {
817                         i++;
818                         break;
819                 }
820         }
821
822         if (i != argc-1)
823                 usage(upload_pack_usage);
824
825         setup_path();
826
827         dir = argv[i];
828
829         if (!enter_repo(dir, strict))
830                 die("'%s' does not appear to be a git repository", dir);
831         if (is_repository_shallow())
832                 die("attempt to fetch/clone from a shallow repository");
833         git_config(upload_pack_config, NULL);
834         upload_pack();
835         return 0;
836 }