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