allow cloning a repository "shallowly"
[git.git] / upload-pack.c
blobebe1e5ae4d12eb32dbd14eb40ee1c1e5d21bb100
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"
16 static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
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)
25 static unsigned long oldest_have;
27 static int multi_ack, nr_our_refs;
28 static int use_thin_pack, use_ofs_delta;
29 static struct object_array have_obj;
30 static struct object_array want_obj;
31 static unsigned int timeout;
32 /* 0 for no sideband,
33 * otherwise maximum packet size (up to 65520 bytes).
35 static int use_sideband;
37 static void reset_timeout(void)
39 alarm(timeout);
42 static int strip(char *line, int len)
44 if (len && line[len-1] == '\n')
45 line[--len] = 0;
46 return len;
49 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
51 if (use_sideband)
52 return send_sideband(1, fd, data, sz, use_sideband);
53 if (fd == 3)
54 /* emergency quit */
55 fd = 2;
56 if (fd == 2) {
57 xwrite(fd, data, sz);
58 return sz;
60 return safe_write(fd, data, sz);
63 FILE *pack_pipe = NULL;
64 static void show_commit(struct commit *commit)
66 if (commit->object.flags & BOUNDARY)
67 fputc('-', pack_pipe);
68 if (fputs(sha1_to_hex(commit->object.sha1), pack_pipe) < 0)
69 die("broken output pipe");
70 fputc('\n', pack_pipe);
71 fflush(pack_pipe);
72 free(commit->buffer);
73 commit->buffer = NULL;
76 static void show_object(struct object_array_entry *p)
78 /* An object with name "foo\n0000000..." can be used to
79 * confuse downstream git-pack-objects very badly.
81 const char *ep = strchr(p->name, '\n');
82 if (ep) {
83 fprintf(pack_pipe, "%s %.*s\n", sha1_to_hex(p->item->sha1),
84 (int) (ep - p->name),
85 p->name);
87 else
88 fprintf(pack_pipe, "%s %s\n",
89 sha1_to_hex(p->item->sha1), p->name);
92 static void show_edge(struct commit *commit)
94 fprintf(pack_pipe, "-%s\n", sha1_to_hex(commit->object.sha1));
97 static void create_pack_file(void)
99 /* Pipes between rev-list to pack-objects, pack-objects to us
100 * and pack-objects error stream for progress bar.
102 int lp_pipe[2], pu_pipe[2], pe_pipe[2];
103 pid_t pid_rev_list, pid_pack_objects;
104 int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr);
105 char data[8193], progress[128];
106 char abort_msg[] = "aborting due to possible repository "
107 "corruption on the remote side.";
108 int buffered = -1;
110 if (pipe(lp_pipe) < 0)
111 die("git-upload-pack: unable to create pipe");
112 pid_rev_list = fork();
113 if (pid_rev_list < 0)
114 die("git-upload-pack: unable to fork git-rev-list");
116 if (!pid_rev_list) {
117 int i;
118 struct rev_info revs;
120 pack_pipe = fdopen(lp_pipe[1], "w");
122 if (create_full_pack)
123 use_thin_pack = 0; /* no point doing it */
124 init_revisions(&revs, NULL);
125 revs.tag_objects = 1;
126 revs.tree_objects = 1;
127 revs.blob_objects = 1;
128 if (use_thin_pack)
129 revs.edge_hint = 1;
131 if (create_full_pack) {
132 const char *args[] = {"rev-list", "--all", NULL};
133 setup_revisions(2, args, &revs, NULL);
134 } else {
135 for (i = 0; i < want_obj.nr; i++) {
136 struct object *o = want_obj.objects[i].item;
137 add_pending_object(&revs, o, NULL);
139 for (i = 0; i < have_obj.nr; i++) {
140 struct object *o = have_obj.objects[i].item;
141 o->flags |= UNINTERESTING;
142 add_pending_object(&revs, o, NULL);
144 setup_revisions(0, NULL, &revs, NULL);
146 prepare_revision_walk(&revs);
147 mark_edges_uninteresting(revs.commits, &revs, show_edge);
148 traverse_commit_list(&revs, show_commit, show_object);
149 exit(0);
152 if (pipe(pu_pipe) < 0)
153 die("git-upload-pack: unable to create pipe");
154 if (pipe(pe_pipe) < 0)
155 die("git-upload-pack: unable to create pipe");
156 pid_pack_objects = fork();
157 if (pid_pack_objects < 0) {
158 /* daemon sets things up to ignore TERM */
159 kill(pid_rev_list, SIGKILL);
160 die("git-upload-pack: unable to fork git-pack-objects");
162 if (!pid_pack_objects) {
163 dup2(lp_pipe[0], 0);
164 dup2(pu_pipe[1], 1);
165 dup2(pe_pipe[1], 2);
167 close(lp_pipe[0]);
168 close(lp_pipe[1]);
169 close(pu_pipe[0]);
170 close(pu_pipe[1]);
171 close(pe_pipe[0]);
172 close(pe_pipe[1]);
173 execl_git_cmd("pack-objects", "--stdout", "--progress",
174 use_ofs_delta ? "--delta-base-offset" : NULL,
175 NULL);
176 kill(pid_rev_list, SIGKILL);
177 die("git-upload-pack: unable to exec git-pack-objects");
180 close(lp_pipe[0]);
181 close(lp_pipe[1]);
183 /* We read from pe_pipe[0] to capture stderr output for
184 * progress bar, and pu_pipe[0] to capture the pack data.
186 close(pe_pipe[1]);
187 close(pu_pipe[1]);
189 while (1) {
190 const char *who;
191 struct pollfd pfd[2];
192 pid_t pid;
193 int status;
194 ssize_t sz;
195 int pe, pu, pollsize;
197 reset_timeout();
199 pollsize = 0;
200 pe = pu = -1;
202 if (0 <= pu_pipe[0]) {
203 pfd[pollsize].fd = pu_pipe[0];
204 pfd[pollsize].events = POLLIN;
205 pu = pollsize;
206 pollsize++;
208 if (0 <= pe_pipe[0]) {
209 pfd[pollsize].fd = pe_pipe[0];
210 pfd[pollsize].events = POLLIN;
211 pe = pollsize;
212 pollsize++;
215 if (pollsize) {
216 if (poll(pfd, pollsize, -1) < 0) {
217 if (errno != EINTR) {
218 error("poll failed, resuming: %s",
219 strerror(errno));
220 sleep(1);
222 continue;
224 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
225 /* Data ready; we keep the last byte
226 * to ourselves in case we detect
227 * broken rev-list, so that we can
228 * leave the stream corrupted. This
229 * is unfortunate -- unpack-objects
230 * would happily accept a valid pack
231 * data with trailing garbage, so
232 * appending garbage after we pass all
233 * the pack data is not good enough to
234 * signal breakage to downstream.
236 char *cp = data;
237 ssize_t outsz = 0;
238 if (0 <= buffered) {
239 *cp++ = buffered;
240 outsz++;
242 sz = read(pu_pipe[0], cp,
243 sizeof(data) - outsz);
244 if (0 < sz)
246 else if (sz == 0) {
247 close(pu_pipe[0]);
248 pu_pipe[0] = -1;
250 else
251 goto fail;
252 sz += outsz;
253 if (1 < sz) {
254 buffered = data[sz-1] & 0xFF;
255 sz--;
257 else
258 buffered = -1;
259 sz = send_client_data(1, data, sz);
260 if (sz < 0)
261 goto fail;
263 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
264 /* Status ready; we ship that in the side-band
265 * or dump to the standard error.
267 sz = read(pe_pipe[0], progress,
268 sizeof(progress));
269 if (0 < sz)
270 send_client_data(2, progress, sz);
271 else if (sz == 0) {
272 close(pe_pipe[0]);
273 pe_pipe[0] = -1;
275 else
276 goto fail;
280 /* See if the children are still there */
281 if (pid_rev_list || pid_pack_objects) {
282 pid = waitpid(-1, &status, WNOHANG);
283 if (!pid)
284 continue;
285 who = ((pid == pid_rev_list) ? "git-rev-list" :
286 (pid == pid_pack_objects) ? "git-pack-objects" :
287 NULL);
288 if (!who) {
289 if (pid < 0) {
290 error("git-upload-pack: %s",
291 strerror(errno));
292 goto fail;
294 error("git-upload-pack: we weren't "
295 "waiting for %d", pid);
296 continue;
298 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) {
299 error("git-upload-pack: %s died with error.",
300 who);
301 goto fail;
303 if (pid == pid_rev_list)
304 pid_rev_list = 0;
305 if (pid == pid_pack_objects)
306 pid_pack_objects = 0;
307 if (pid_rev_list || pid_pack_objects)
308 continue;
311 /* both died happily */
312 if (pollsize)
313 continue;
315 /* flush the data */
316 if (0 <= buffered) {
317 data[0] = buffered;
318 sz = send_client_data(1, data, 1);
319 if (sz < 0)
320 goto fail;
321 fprintf(stderr, "flushed.\n");
323 if (use_sideband)
324 packet_flush(1);
325 return;
327 fail:
328 if (pid_pack_objects)
329 kill(pid_pack_objects, SIGKILL);
330 if (pid_rev_list)
331 kill(pid_rev_list, SIGKILL);
332 send_client_data(3, abort_msg, sizeof(abort_msg));
333 die("git-upload-pack: %s", abort_msg);
336 static int got_sha1(char *hex, unsigned char *sha1)
338 struct object *o;
339 int we_knew_they_have = 0;
341 if (get_sha1_hex(hex, sha1))
342 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
343 if (!has_sha1_file(sha1))
344 return -1;
346 o = lookup_object(sha1);
347 if (!(o && o->parsed))
348 o = parse_object(sha1);
349 if (!o)
350 die("oops (%s)", sha1_to_hex(sha1));
351 if (o->type == OBJ_COMMIT) {
352 struct commit_list *parents;
353 struct commit *commit = (struct commit *)o;
354 if (o->flags & THEY_HAVE)
355 we_knew_they_have = 1;
356 else
357 o->flags |= THEY_HAVE;
358 if (!oldest_have || (commit->date < oldest_have))
359 oldest_have = commit->date;
360 for (parents = commit->parents;
361 parents;
362 parents = parents->next)
363 parents->item->object.flags |= THEY_HAVE;
365 if (!we_knew_they_have) {
366 add_object_array(o, NULL, &have_obj);
367 return 1;
369 return 0;
372 static int reachable(struct commit *want)
374 struct commit_list *work = NULL;
376 insert_by_date(want, &work);
377 while (work) {
378 struct commit_list *list = work->next;
379 struct commit *commit = work->item;
380 free(work);
381 work = list;
383 if (commit->object.flags & THEY_HAVE) {
384 want->object.flags |= COMMON_KNOWN;
385 break;
387 if (!commit->object.parsed)
388 parse_object(commit->object.sha1);
389 if (commit->object.flags & REACHABLE)
390 continue;
391 commit->object.flags |= REACHABLE;
392 if (commit->date < oldest_have)
393 continue;
394 for (list = commit->parents; list; list = list->next) {
395 struct commit *parent = list->item;
396 if (!(parent->object.flags & REACHABLE))
397 insert_by_date(parent, &work);
400 want->object.flags |= REACHABLE;
401 clear_commit_marks(want, REACHABLE);
402 free_commit_list(work);
403 return (want->object.flags & COMMON_KNOWN);
406 static int ok_to_give_up(void)
408 int i;
410 if (!have_obj.nr)
411 return 0;
413 for (i = 0; i < want_obj.nr; i++) {
414 struct object *want = want_obj.objects[i].item;
416 if (want->flags & COMMON_KNOWN)
417 continue;
418 want = deref_tag(want, "a want line", 0);
419 if (!want || want->type != OBJ_COMMIT) {
420 /* no way to tell if this is reachable by
421 * looking at the ancestry chain alone, so
422 * leave a note to ourselves not to worry about
423 * this object anymore.
425 want_obj.objects[i].item->flags |= COMMON_KNOWN;
426 continue;
428 if (!reachable((struct commit *)want))
429 return 0;
431 return 1;
434 static int get_common_commits(void)
436 static char line[1000];
437 unsigned char sha1[20];
438 char hex[41], last_hex[41];
439 int len;
441 track_object_refs = 0;
442 save_commit_buffer = 0;
444 for(;;) {
445 len = packet_read_line(0, line, sizeof(line));
446 reset_timeout();
448 if (!len) {
449 if (have_obj.nr == 0 || multi_ack)
450 packet_write(1, "NAK\n");
451 continue;
453 len = strip(line, len);
454 if (!strncmp(line, "have ", 5)) {
455 switch (got_sha1(line+5, sha1)) {
456 case -1: /* they have what we do not */
457 if (multi_ack && ok_to_give_up())
458 packet_write(1, "ACK %s continue\n",
459 sha1_to_hex(sha1));
460 break;
461 default:
462 memcpy(hex, sha1_to_hex(sha1), 41);
463 if (multi_ack) {
464 const char *msg = "ACK %s continue\n";
465 packet_write(1, msg, hex);
466 memcpy(last_hex, hex, 41);
468 else if (have_obj.nr == 1)
469 packet_write(1, "ACK %s\n", hex);
470 break;
472 continue;
474 if (!strcmp(line, "done")) {
475 if (have_obj.nr > 0) {
476 if (multi_ack)
477 packet_write(1, "ACK %s\n", last_hex);
478 return 0;
480 packet_write(1, "NAK\n");
481 return -1;
483 die("git-upload-pack: expected SHA1 list, got '%s'", line);
487 static void receive_needs(void)
489 struct object_array shallows = {0, 0, NULL};
490 static char line[1000];
491 int len, depth = 0;
493 for (;;) {
494 struct object *o;
495 unsigned char sha1_buf[20];
496 len = packet_read_line(0, line, sizeof(line));
497 reset_timeout();
498 if (!len)
499 break;
501 if (!strncmp("shallow ", line, 8)) {
502 unsigned char sha1[20];
503 struct object *object;
504 if (get_sha1(line + 8, sha1))
505 die("invalid shallow line: %s", line);
506 object = parse_object(sha1);
507 if (!object)
508 die("did not find object for %s", line);
509 add_object_array(object, NULL, &shallows);
510 continue;
512 if (!strncmp("deepen ", line, 7)) {
513 char *end;
514 depth = strtol(line + 7, &end, 0);
515 if (end == line + 7 || depth <= 0)
516 die("Invalid deepen: %s", line);
517 continue;
519 if (strncmp("want ", line, 5) ||
520 get_sha1_hex(line+5, sha1_buf))
521 die("git-upload-pack: protocol error, "
522 "expected to get sha, not '%s'", line);
523 if (strstr(line+45, "multi_ack"))
524 multi_ack = 1;
525 if (strstr(line+45, "thin-pack"))
526 use_thin_pack = 1;
527 if (strstr(line+45, "ofs-delta"))
528 use_ofs_delta = 1;
529 if (strstr(line+45, "side-band-64k"))
530 use_sideband = LARGE_PACKET_MAX;
531 else if (strstr(line+45, "side-band"))
532 use_sideband = DEFAULT_PACKET_MAX;
534 /* We have sent all our refs already, and the other end
535 * should have chosen out of them; otherwise they are
536 * asking for nonsense.
538 * Hmph. We may later want to allow "want" line that
539 * asks for something like "master~10" (symbolic)...
540 * would it make sense? I don't know.
542 o = lookup_object(sha1_buf);
543 if (!o || !(o->flags & OUR_REF))
544 die("git-upload-pack: not our ref %s", line+5);
545 if (!(o->flags & WANTED)) {
546 o->flags |= WANTED;
547 add_object_array(o, NULL, &want_obj);
550 if (depth > 0) {
551 struct commit_list *result, *backup;
552 if (shallows.nr > 0)
553 die("Deepening a shallow repository not yet supported");
554 backup = result = get_shallow_commits(&want_obj, depth);
555 while (result) {
556 packet_write(1, "shallow %s",
557 sha1_to_hex(result->item->object.sha1));
558 result = result->next;
560 free_commit_list(backup);
562 if (shallows.nr > 0) {
563 int i;
564 for (i = 0; i < shallows.nr; i++)
565 register_shallow(shallows.objects[i].item->sha1);
569 static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
571 static const char *capabilities = "multi_ack thin-pack side-band"
572 " side-band-64k ofs-delta shallow";
573 struct object *o = parse_object(sha1);
575 if (!o)
576 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
578 if (capabilities)
579 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
580 0, capabilities);
581 else
582 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
583 capabilities = NULL;
584 if (!(o->flags & OUR_REF)) {
585 o->flags |= OUR_REF;
586 nr_our_refs++;
588 if (o->type == OBJ_TAG) {
589 o = deref_tag(o, refname, 0);
590 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
592 return 0;
595 static void upload_pack(void)
597 reset_timeout();
598 head_ref(send_ref, NULL);
599 for_each_ref(send_ref, NULL);
600 packet_flush(1);
601 receive_needs();
602 if (want_obj.nr) {
603 get_common_commits();
604 create_pack_file();
608 int main(int argc, char **argv)
610 char *dir;
611 int i;
612 int strict = 0;
614 for (i = 1; i < argc; i++) {
615 char *arg = argv[i];
617 if (arg[0] != '-')
618 break;
619 if (!strcmp(arg, "--strict")) {
620 strict = 1;
621 continue;
623 if (!strncmp(arg, "--timeout=", 10)) {
624 timeout = atoi(arg+10);
625 continue;
627 if (!strcmp(arg, "--")) {
628 i++;
629 break;
633 if (i != argc-1)
634 usage(upload_pack_usage);
635 dir = argv[i];
637 if (!enter_repo(dir, strict))
638 die("'%s': unable to chdir or not a git archive", dir);
640 upload_pack();
641 return 0;