shallow clone: unparse and reparse an unshallowed commit
[git] / upload-pack.c
1 #include <signal.h>
2 #include <sys/wait.h>
3 #include <sys/poll.h>
4 #include "cache.h"
5 #include "refs.h"
6 #include "pkt-line.h"
7 #include "sideband.h"
8 #include "tag.h"
9 #include "object.h"
10 #include "commit.h"
11 #include "exec_cmd.h"
12 #include "diff.h"
13 #include "revision.h"
14 #include "list-objects.h"
15
16 static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
17
18 /* bits #0..7 in revision.h, #8..10 in commit.c */
19 #define THEY_HAVE       (1u << 11)
20 #define OUR_REF         (1u << 12)
21 #define WANTED          (1u << 13)
22 #define COMMON_KNOWN    (1u << 14)
23 #define REACHABLE       (1u << 15)
24
25 #define SHALLOW         (1u << 16)
26 #define NOT_SHALLOW     (1u << 17)
27 #define CLIENT_SHALLOW  (1u << 18)
28
29 static unsigned long oldest_have;
30
31 static int multi_ack, nr_our_refs;
32 static int use_thin_pack, use_ofs_delta;
33 static struct object_array have_obj;
34 static struct object_array want_obj;
35 static unsigned int timeout;
36 /* 0 for no sideband,
37  * otherwise maximum packet size (up to 65520 bytes).
38  */
39 static int use_sideband;
40
41 static void reset_timeout(void)
42 {
43         alarm(timeout);
44 }
45
46 static int strip(char *line, int len)
47 {
48         if (len && line[len-1] == '\n')
49                 line[--len] = 0;
50         return len;
51 }
52
53 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
54 {
55         if (use_sideband)
56                 return send_sideband(1, fd, data, sz, use_sideband);
57         if (fd == 3)
58                 /* emergency quit */
59                 fd = 2;
60         if (fd == 2) {
61                 xwrite(fd, data, sz);
62                 return sz;
63         }
64         return safe_write(fd, data, sz);
65 }
66
67 FILE *pack_pipe = NULL;
68 static void show_commit(struct commit *commit)
69 {
70         if (commit->object.flags & BOUNDARY)
71                 fputc('-', pack_pipe);
72         if (fputs(sha1_to_hex(commit->object.sha1), pack_pipe) < 0)
73                 die("broken output pipe");
74         fputc('\n', pack_pipe);
75         fflush(pack_pipe);
76         free(commit->buffer);
77         commit->buffer = NULL;
78 }
79
80 static void show_object(struct object_array_entry *p)
81 {
82         /* An object with name "foo\n0000000..." can be used to
83          * confuse downstream git-pack-objects very badly.
84          */
85         const char *ep = strchr(p->name, '\n');
86         if (ep) {
87                 fprintf(pack_pipe, "%s %.*s\n", sha1_to_hex(p->item->sha1),
88                        (int) (ep - p->name),
89                        p->name);
90         }
91         else
92                 fprintf(pack_pipe, "%s %s\n",
93                                 sha1_to_hex(p->item->sha1), p->name);
94 }
95
96 static void show_edge(struct commit *commit)
97 {
98         fprintf(pack_pipe, "-%s\n", sha1_to_hex(commit->object.sha1));
99 }
100
101 static void create_pack_file(void)
102 {
103         /* Pipes between rev-list to pack-objects, pack-objects to us
104          * and pack-objects error stream for progress bar.
105          */
106         int lp_pipe[2], pu_pipe[2], pe_pipe[2];
107         pid_t pid_rev_list, pid_pack_objects;
108         int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr);
109         char data[8193], progress[128];
110         char abort_msg[] = "aborting due to possible repository "
111                 "corruption on the remote side.";
112         int buffered = -1;
113
114         if (pipe(lp_pipe) < 0)
115                 die("git-upload-pack: unable to create pipe");
116         pid_rev_list = fork();
117         if (pid_rev_list < 0)
118                 die("git-upload-pack: unable to fork git-rev-list");
119
120         if (!pid_rev_list) {
121                 int i;
122                 struct rev_info revs;
123
124                 pack_pipe = fdopen(lp_pipe[1], "w");
125
126                 if (create_full_pack)
127                         use_thin_pack = 0; /* no point doing it */
128                 init_revisions(&revs, NULL);
129                 revs.tag_objects = 1;
130                 revs.tree_objects = 1;
131                 revs.blob_objects = 1;
132                 if (use_thin_pack)
133                         revs.edge_hint = 1;
134
135                 if (create_full_pack) {
136                         const char *args[] = {"rev-list", "--all", NULL};
137                         setup_revisions(2, args, &revs, NULL);
138                 } else {
139                         for (i = 0; i < want_obj.nr; i++) {
140                                 struct object *o = want_obj.objects[i].item;
141                                 /* why??? */
142                                 o->flags &= ~UNINTERESTING;
143                                 add_pending_object(&revs, o, NULL);
144                         }
145                         for (i = 0; i < have_obj.nr; i++) {
146                                 struct object *o = have_obj.objects[i].item;
147                                 o->flags |= UNINTERESTING;
148                                 add_pending_object(&revs, o, NULL);
149                         }
150                         setup_revisions(0, NULL, &revs, NULL);
151                 }
152                 prepare_revision_walk(&revs);
153                 mark_edges_uninteresting(revs.commits, &revs, show_edge);
154                 traverse_commit_list(&revs, show_commit, show_object);
155                 exit(0);
156         }
157
158         if (pipe(pu_pipe) < 0)
159                 die("git-upload-pack: unable to create pipe");
160         if (pipe(pe_pipe) < 0)
161                 die("git-upload-pack: unable to create pipe");
162         pid_pack_objects = fork();
163         if (pid_pack_objects < 0) {
164                 /* daemon sets things up to ignore TERM */
165                 kill(pid_rev_list, SIGKILL);
166                 die("git-upload-pack: unable to fork git-pack-objects");
167         }
168         if (!pid_pack_objects) {
169                 dup2(lp_pipe[0], 0);
170                 dup2(pu_pipe[1], 1);
171                 dup2(pe_pipe[1], 2);
172
173                 close(lp_pipe[0]);
174                 close(lp_pipe[1]);
175                 close(pu_pipe[0]);
176                 close(pu_pipe[1]);
177                 close(pe_pipe[0]);
178                 close(pe_pipe[1]);
179                 execl_git_cmd("pack-objects", "--stdout", "--progress",
180                               use_ofs_delta ? "--delta-base-offset" : NULL,
181                               NULL);
182                 kill(pid_rev_list, SIGKILL);
183                 die("git-upload-pack: unable to exec git-pack-objects");
184         }
185
186         close(lp_pipe[0]);
187         close(lp_pipe[1]);
188
189         /* We read from pe_pipe[0] to capture stderr output for
190          * progress bar, and pu_pipe[0] to capture the pack data.
191          */
192         close(pe_pipe[1]);
193         close(pu_pipe[1]);
194
195         while (1) {
196                 const char *who;
197                 struct pollfd pfd[2];
198                 pid_t pid;
199                 int status;
200                 ssize_t sz;
201                 int pe, pu, pollsize;
202
203                 reset_timeout();
204
205                 pollsize = 0;
206                 pe = pu = -1;
207
208                 if (0 <= pu_pipe[0]) {
209                         pfd[pollsize].fd = pu_pipe[0];
210                         pfd[pollsize].events = POLLIN;
211                         pu = pollsize;
212                         pollsize++;
213                 }
214                 if (0 <= pe_pipe[0]) {
215                         pfd[pollsize].fd = pe_pipe[0];
216                         pfd[pollsize].events = POLLIN;
217                         pe = pollsize;
218                         pollsize++;
219                 }
220
221                 if (pollsize) {
222                         if (poll(pfd, pollsize, -1) < 0) {
223                                 if (errno != EINTR) {
224                                         error("poll failed, resuming: %s",
225                                               strerror(errno));
226                                         sleep(1);
227                                 }
228                                 continue;
229                         }
230                         if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
231                                 /* Data ready; we keep the last byte
232                                  * to ourselves in case we detect
233                                  * broken rev-list, so that we can
234                                  * leave the stream corrupted.  This
235                                  * is unfortunate -- unpack-objects
236                                  * would happily accept a valid pack
237                                  * data with trailing garbage, so
238                                  * appending garbage after we pass all
239                                  * the pack data is not good enough to
240                                  * signal breakage to downstream.
241                                  */
242                                 char *cp = data;
243                                 ssize_t outsz = 0;
244                                 if (0 <= buffered) {
245                                         *cp++ = buffered;
246                                         outsz++;
247                                 }
248                                 sz = read(pu_pipe[0], cp,
249                                           sizeof(data) - outsz);
250                                 if (0 < sz)
251                                                 ;
252                                 else if (sz == 0) {
253                                         close(pu_pipe[0]);
254                                         pu_pipe[0] = -1;
255                                 }
256                                 else
257                                         goto fail;
258                                 sz += outsz;
259                                 if (1 < sz) {
260                                         buffered = data[sz-1] & 0xFF;
261                                         sz--;
262                                 }
263                                 else
264                                         buffered = -1;
265                                 sz = send_client_data(1, data, sz);
266                                 if (sz < 0)
267                                         goto fail;
268                         }
269                         if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
270                                 /* Status ready; we ship that in the side-band
271                                  * or dump to the standard error.
272                                  */
273                                 sz = read(pe_pipe[0], progress,
274                                           sizeof(progress));
275                                 if (0 < sz)
276                                         send_client_data(2, progress, sz);
277                                 else if (sz == 0) {
278                                         close(pe_pipe[0]);
279                                         pe_pipe[0] = -1;
280                                 }
281                                 else
282                                         goto fail;
283                         }
284                 }
285
286                 /* See if the children are still there */
287                 if (pid_rev_list || pid_pack_objects) {
288                         pid = waitpid(-1, &status, WNOHANG);
289                         if (!pid)
290                                 continue;
291                         who = ((pid == pid_rev_list) ? "git-rev-list" :
292                                (pid == pid_pack_objects) ? "git-pack-objects" :
293                                NULL);
294                         if (!who) {
295                                 if (pid < 0) {
296                                         error("git-upload-pack: %s",
297                                               strerror(errno));
298                                         goto fail;
299                                 }
300                                 error("git-upload-pack: we weren't "
301                                       "waiting for %d", pid);
302                                 continue;
303                         }
304                         if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) {
305                                 error("git-upload-pack: %s died with error.",
306                                       who);
307                                 goto fail;
308                         }
309                         if (pid == pid_rev_list)
310                                 pid_rev_list = 0;
311                         if (pid == pid_pack_objects)
312                                 pid_pack_objects = 0;
313                         if (pid_rev_list || pid_pack_objects)
314                                 continue;
315                 }
316
317                 /* both died happily */
318                 if (pollsize)
319                         continue;
320
321                 /* flush the data */
322                 if (0 <= buffered) {
323                         data[0] = buffered;
324                         sz = send_client_data(1, data, 1);
325                         if (sz < 0)
326                                 goto fail;
327                         fprintf(stderr, "flushed.\n");
328                 }
329                 if (use_sideband)
330                         packet_flush(1);
331                 return;
332         }
333  fail:
334         if (pid_pack_objects)
335                 kill(pid_pack_objects, SIGKILL);
336         if (pid_rev_list)
337                 kill(pid_rev_list, SIGKILL);
338         send_client_data(3, abort_msg, sizeof(abort_msg));
339         die("git-upload-pack: %s", abort_msg);
340 }
341
342 static int got_sha1(char *hex, unsigned char *sha1)
343 {
344         struct object *o;
345         int we_knew_they_have = 0;
346
347         if (get_sha1_hex(hex, sha1))
348                 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
349         if (!has_sha1_file(sha1))
350                 return -1;
351
352         o = lookup_object(sha1);
353         if (!(o && o->parsed))
354                 o = parse_object(sha1);
355         if (!o)
356                 die("oops (%s)", sha1_to_hex(sha1));
357         if (o->type == OBJ_COMMIT) {
358                 struct commit_list *parents;
359                 struct commit *commit = (struct commit *)o;
360                 if (o->flags & THEY_HAVE)
361                         we_knew_they_have = 1;
362                 else
363                         o->flags |= THEY_HAVE;
364                 if (!oldest_have || (commit->date < oldest_have))
365                         oldest_have = commit->date;
366                 for (parents = commit->parents;
367                      parents;
368                      parents = parents->next)
369                         parents->item->object.flags |= THEY_HAVE;
370         }
371         if (!we_knew_they_have) {
372                 add_object_array(o, NULL, &have_obj);
373                 return 1;
374         }
375         return 0;
376 }
377
378 static int reachable(struct commit *want)
379 {
380         struct commit_list *work = NULL;
381
382         insert_by_date(want, &work);
383         while (work) {
384                 struct commit_list *list = work->next;
385                 struct commit *commit = work->item;
386                 free(work);
387                 work = list;
388
389                 if (commit->object.flags & THEY_HAVE) {
390                         want->object.flags |= COMMON_KNOWN;
391                         break;
392                 }
393                 if (!commit->object.parsed)
394                         parse_object(commit->object.sha1);
395                 if (commit->object.flags & REACHABLE)
396                         continue;
397                 commit->object.flags |= REACHABLE;
398                 if (commit->date < oldest_have)
399                         continue;
400                 for (list = commit->parents; list; list = list->next) {
401                         struct commit *parent = list->item;
402                         if (!(parent->object.flags & REACHABLE))
403                                 insert_by_date(parent, &work);
404                 }
405         }
406         want->object.flags |= REACHABLE;
407         clear_commit_marks(want, REACHABLE);
408         free_commit_list(work);
409         return (want->object.flags & COMMON_KNOWN);
410 }
411
412 static int ok_to_give_up(void)
413 {
414         int i;
415
416         if (!have_obj.nr)
417                 return 0;
418
419         for (i = 0; i < want_obj.nr; i++) {
420                 struct object *want = want_obj.objects[i].item;
421
422                 if (want->flags & COMMON_KNOWN)
423                         continue;
424                 want = deref_tag(want, "a want line", 0);
425                 if (!want || want->type != OBJ_COMMIT) {
426                         /* no way to tell if this is reachable by
427                          * looking at the ancestry chain alone, so
428                          * leave a note to ourselves not to worry about
429                          * this object anymore.
430                          */
431                         want_obj.objects[i].item->flags |= COMMON_KNOWN;
432                         continue;
433                 }
434                 if (!reachable((struct commit *)want))
435                         return 0;
436         }
437         return 1;
438 }
439
440 static int get_common_commits(void)
441 {
442         static char line[1000];
443         unsigned char sha1[20];
444         char hex[41], last_hex[41];
445         int len;
446
447         track_object_refs = 0;
448         save_commit_buffer = 0;
449
450         for(;;) {
451                 len = packet_read_line(0, line, sizeof(line));
452                 reset_timeout();
453
454                 if (!len) {
455                         if (have_obj.nr == 0 || multi_ack)
456                                 packet_write(1, "NAK\n");
457                         continue;
458                 }
459                 len = strip(line, len);
460                 if (!strncmp(line, "have ", 5)) {
461                         switch (got_sha1(line+5, sha1)) {
462                         case -1: /* they have what we do not */
463                                 if (multi_ack && ok_to_give_up())
464                                         packet_write(1, "ACK %s continue\n",
465                                                      sha1_to_hex(sha1));
466                                 break;
467                         default:
468                                 memcpy(hex, sha1_to_hex(sha1), 41);
469                                 if (multi_ack) {
470                                         const char *msg = "ACK %s continue\n";
471                                         packet_write(1, msg, hex);
472                                         memcpy(last_hex, hex, 41);
473                                 }
474                                 else if (have_obj.nr == 1)
475                                         packet_write(1, "ACK %s\n", hex);
476                                 break;
477                         }
478                         continue;
479                 }
480                 if (!strcmp(line, "done")) {
481                         if (have_obj.nr > 0) {
482                                 if (multi_ack)
483                                         packet_write(1, "ACK %s\n", last_hex);
484                                 return 0;
485                         }
486                         packet_write(1, "NAK\n");
487                         return -1;
488                 }
489                 die("git-upload-pack: expected SHA1 list, got '%s'", line);
490         }
491 }
492
493 static void receive_needs(void)
494 {
495         struct object_array shallows = {0, 0, NULL};
496         static char line[1000];
497         int len, depth = 0;
498
499         for (;;) {
500                 struct object *o;
501                 unsigned char sha1_buf[20];
502                 len = packet_read_line(0, line, sizeof(line));
503                 reset_timeout();
504                 if (!len)
505                         break;
506
507                 if (!strncmp("shallow ", line, 8)) {
508                         unsigned char sha1[20];
509                         struct object *object;
510                         use_thin_pack = 0;
511                         if (get_sha1(line + 8, sha1))
512                                 die("invalid shallow line: %s", line);
513                         object = parse_object(sha1);
514                         if (!object)
515                                 die("did not find object for %s", line);
516                         object->flags |= CLIENT_SHALLOW;
517                         add_object_array(object, NULL, &shallows);
518                         continue;
519                 }
520                 if (!strncmp("deepen ", line, 7)) {
521                         char *end;
522                         use_thin_pack = 0;
523                         depth = strtol(line + 7, &end, 0);
524                         if (end == line + 7 || depth <= 0)
525                                 die("Invalid deepen: %s", line);
526                         continue;
527                 }
528                 if (strncmp("want ", line, 5) ||
529                     get_sha1_hex(line+5, sha1_buf))
530                         die("git-upload-pack: protocol error, "
531                             "expected to get sha, not '%s'", line);
532                 if (strstr(line+45, "multi_ack"))
533                         multi_ack = 1;
534                 if (strstr(line+45, "thin-pack"))
535                         use_thin_pack = 1;
536                 if (strstr(line+45, "ofs-delta"))
537                         use_ofs_delta = 1;
538                 if (strstr(line+45, "side-band-64k"))
539                         use_sideband = LARGE_PACKET_MAX;
540                 else if (strstr(line+45, "side-band"))
541                         use_sideband = DEFAULT_PACKET_MAX;
542
543                 /* We have sent all our refs already, and the other end
544                  * should have chosen out of them; otherwise they are
545                  * asking for nonsense.
546                  *
547                  * Hmph.  We may later want to allow "want" line that
548                  * asks for something like "master~10" (symbolic)...
549                  * would it make sense?  I don't know.
550                  */
551                 o = lookup_object(sha1_buf);
552                 if (!o || !(o->flags & OUR_REF))
553                         die("git-upload-pack: not our ref %s", line+5);
554                 if (!(o->flags & WANTED)) {
555                         o->flags |= WANTED;
556                         add_object_array(o, NULL, &want_obj);
557                 }
558         }
559         if (depth == 0 && shallows.nr == 0)
560                 return;
561         if (depth > 0) {
562                 struct commit_list *result, *backup;
563                 int i;
564                 backup = result = get_shallow_commits(&want_obj, depth,
565                         SHALLOW, NOT_SHALLOW);
566                 while (result) {
567                         struct object *object = &result->item->object;
568                         if (!(object->flags & CLIENT_SHALLOW)) {
569                                 packet_write(1, "shallow %s",
570                                                 sha1_to_hex(object->sha1));
571                                 register_shallow(object->sha1);
572                         }
573                         result = result->next;
574                 }
575                 free_commit_list(backup);
576                 for (i = 0; i < shallows.nr; i++) {
577                         struct object *object = shallows.objects[i].item;
578                         if (object->flags & NOT_SHALLOW) {
579                                 struct commit_list *parents;
580                                 packet_write(1, "unshallow %s",
581                                         sha1_to_hex(object->sha1));
582                                 object->flags &= ~CLIENT_SHALLOW;
583                                 /* make sure the real parents are parsed */
584                                 unregister_shallow(object->sha1);
585                                 object->parsed = 0;
586                                 parse_commit((struct commit *)object);
587                                 parents = ((struct commit *)object)->parents;
588                                 while (parents) {
589                                         add_object_array(&parents->item->object,
590                                                         NULL, &want_obj);
591                                         parents = parents->next;
592                                 }
593                         }
594                         /* make sure commit traversal conforms to client */
595                         register_shallow(object->sha1);
596                 }
597                 packet_flush(1);
598         } else
599                 if (shallows.nr > 0) {
600                         int i;
601                         for (i = 0; i < shallows.nr; i++)
602                                 register_shallow(shallows.objects[i].item->sha1);
603                 }
604         free(shallows.objects);
605 }
606
607 static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
608 {
609         static const char *capabilities = "multi_ack thin-pack side-band"
610                 " side-band-64k ofs-delta shallow";
611         struct object *o = parse_object(sha1);
612
613         if (!o)
614                 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
615
616         if (capabilities)
617                 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
618                         0, capabilities);
619         else
620                 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
621         capabilities = NULL;
622         if (!(o->flags & OUR_REF)) {
623                 o->flags |= OUR_REF;
624                 nr_our_refs++;
625         }
626         if (o->type == OBJ_TAG) {
627                 o = deref_tag(o, refname, 0);
628                 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
629         }
630         return 0;
631 }
632
633 static void upload_pack(void)
634 {
635         reset_timeout();
636         head_ref(send_ref, NULL);
637         for_each_ref(send_ref, NULL);
638         packet_flush(1);
639         receive_needs();
640         if (want_obj.nr) {
641                 get_common_commits();
642                 create_pack_file();
643         }
644 }
645
646 int main(int argc, char **argv)
647 {
648         char *dir;
649         int i;
650         int strict = 0;
651
652         for (i = 1; i < argc; i++) {
653                 char *arg = argv[i];
654
655                 if (arg[0] != '-')
656                         break;
657                 if (!strcmp(arg, "--strict")) {
658                         strict = 1;
659                         continue;
660                 }
661                 if (!strncmp(arg, "--timeout=", 10)) {
662                         timeout = atoi(arg+10);
663                         continue;
664                 }
665                 if (!strcmp(arg, "--")) {
666                         i++;
667                         break;
668                 }
669         }
670         
671         if (i != argc-1)
672                 usage(upload_pack_usage);
673         dir = argv[i];
674
675         if (!enter_repo(dir, strict))
676                 die("'%s': unable to chdir or not a git archive", dir);
677
678         upload_pack();
679         return 0;
680 }