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