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