support fetching into a shallow repository
[git/git.git] / upload-pack.c
CommitLineData
e3a56298
JH
1#include <signal.h>
2#include <sys/wait.h>
3#include <sys/poll.h>
def88e9a
LT
4#include "cache.h"
5#include "refs.h"
6#include "pkt-line.h"
958c24b1 7#include "sideband.h"
f6b42a81
JH
8#include "tag.h"
9#include "object.h"
f0243f26 10#include "commit.h"
77cb17e9 11#include "exec_cmd.h"
9b8dc263
JS
12#include "diff.h"
13#include "revision.h"
14#include "list-objects.h"
def88e9a 15
960deccb 16static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
def88e9a 17
937a515a
JH
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
3fbe2d54 25static unsigned long oldest_have;
937a515a 26
96f1e58f 27static int multi_ack, nr_our_refs;
e4fe4b8e 28static int use_thin_pack, use_ofs_delta;
b1e9fff7
JH
29static struct object_array have_obj;
30static struct object_array want_obj;
96f1e58f 31static unsigned int timeout;
d47f3db7
JH
32/* 0 for no sideband,
33 * otherwise maximum packet size (up to 65520 bytes).
34 */
96f1e58f 35static int use_sideband;
960deccb
PA
36
37static void reset_timeout(void)
38{
39 alarm(timeout);
40}
fb9040cc 41
75bfc6c2
LT
42static int strip(char *line, int len)
43{
44 if (len && line[len-1] == '\n')
45 line[--len] = 0;
46 return len;
47}
48
583b7ea3
JH
49static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
50{
958c24b1 51 if (use_sideband)
d47f3db7 52 return send_sideband(1, fd, data, sz, use_sideband);
958c24b1
JH
53 if (fd == 3)
54 /* emergency quit */
55 fd = 2;
56 if (fd == 2) {
57 xwrite(fd, data, sz);
58 return sz;
583b7ea3 59 }
958c24b1 60 return safe_write(fd, data, sz);
583b7ea3
JH
61}
62
9b8dc263
JS
63FILE *pack_pipe = NULL;
64static void show_commit(struct commit *commit)
65{
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;
74}
75
76static void show_object(struct object_array_entry *p)
77{
78 /* An object with name "foo\n0000000..." can be used to
79 * confuse downstream git-pack-objects very badly.
80 */
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);
86 }
87 else
88 fprintf(pack_pipe, "%s %s\n",
89 sha1_to_hex(p->item->sha1), p->name);
90}
91
92static void show_edge(struct commit *commit)
93{
94 fprintf(pack_pipe, "-%s\n", sha1_to_hex(commit->object.sha1));
95}
96
fb9040cc
LT
97static void create_pack_file(void)
98{
363b7817
JH
99 /* Pipes between rev-list to pack-objects, pack-objects to us
100 * and pack-objects error stream for progress bar.
101 */
102 int lp_pipe[2], pu_pipe[2], pe_pipe[2];
b1c71b72 103 pid_t pid_rev_list, pid_pack_objects;
b1e9fff7 104 int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr);
363b7817 105 char data[8193], progress[128];
583b7ea3
JH
106 char abort_msg[] = "aborting due to possible repository "
107 "corruption on the remote side.";
b1c71b72 108 int buffered = -1;
75bfc6c2 109
b1c71b72 110 if (pipe(lp_pipe) < 0)
75bfc6c2 111 die("git-upload-pack: unable to create pipe");
b1c71b72
JH
112 pid_rev_list = fork();
113 if (pid_rev_list < 0)
75bfc6c2
LT
114 die("git-upload-pack: unable to fork git-rev-list");
115
b1c71b72 116 if (!pid_rev_list) {
75bfc6c2 117 int i;
9b8dc263
JS
118 struct rev_info revs;
119
120 pack_pipe = fdopen(lp_pipe[1], "w");
e091eb93 121
b1e9fff7 122 if (create_full_pack)
9b8dc263
JS
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;
130
131 if (create_full_pack) {
132 const char *args[] = {"rev-list", "--all", NULL};
133 setup_revisions(2, args, &revs, NULL);
134 } else {
b1e9fff7
JH
135 for (i = 0; i < want_obj.nr; i++) {
136 struct object *o = want_obj.objects[i].item;
9b8dc263 137 add_pending_object(&revs, o, NULL);
e091eb93 138 }
b1e9fff7
JH
139 for (i = 0; i < have_obj.nr; i++) {
140 struct object *o = have_obj.objects[i].item;
9b8dc263
JS
141 o->flags |= UNINTERESTING;
142 add_pending_object(&revs, o, NULL);
b5c367f7 143 }
9b8dc263
JS
144 setup_revisions(0, NULL, &revs, NULL);
145 }
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);
75bfc6c2 150 }
b1c71b72
JH
151
152 if (pipe(pu_pipe) < 0)
153 die("git-upload-pack: unable to create pipe");
363b7817
JH
154 if (pipe(pe_pipe) < 0)
155 die("git-upload-pack: unable to create pipe");
b1c71b72
JH
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");
161 }
162 if (!pid_pack_objects) {
163 dup2(lp_pipe[0], 0);
164 dup2(pu_pipe[1], 1);
363b7817 165 dup2(pe_pipe[1], 2);
b1c71b72
JH
166
167 close(lp_pipe[0]);
168 close(lp_pipe[1]);
169 close(pu_pipe[0]);
170 close(pu_pipe[1]);
363b7817
JH
171 close(pe_pipe[0]);
172 close(pe_pipe[1]);
e4fe4b8e
NP
173 execl_git_cmd("pack-objects", "--stdout", "--progress",
174 use_ofs_delta ? "--delta-base-offset" : NULL,
175 NULL);
b1c71b72
JH
176 kill(pid_rev_list, SIGKILL);
177 die("git-upload-pack: unable to exec git-pack-objects");
178 }
179
180 close(lp_pipe[0]);
181 close(lp_pipe[1]);
182
363b7817
JH
183 /* We read from pe_pipe[0] to capture stderr output for
184 * progress bar, and pu_pipe[0] to capture the pack data.
b1c71b72 185 */
363b7817 186 close(pe_pipe[1]);
b1c71b72
JH
187 close(pu_pipe[1]);
188
189 while (1) {
190 const char *who;
191 struct pollfd pfd[2];
192 pid_t pid;
193 int status;
194 ssize_t sz;
363b7817 195 int pe, pu, pollsize;
b1c71b72 196
0d516ada
ML
197 reset_timeout();
198
b1c71b72 199 pollsize = 0;
363b7817 200 pe = pu = -1;
b1c71b72
JH
201
202 if (0 <= pu_pipe[0]) {
203 pfd[pollsize].fd = pu_pipe[0];
204 pfd[pollsize].events = POLLIN;
205 pu = pollsize;
206 pollsize++;
207 }
363b7817
JH
208 if (0 <= pe_pipe[0]) {
209 pfd[pollsize].fd = pe_pipe[0];
210 pfd[pollsize].events = POLLIN;
211 pe = pollsize;
212 pollsize++;
213 }
b1c71b72
JH
214
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);
221 }
222 continue;
223 }
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.
235 */
236 char *cp = data;
237 ssize_t outsz = 0;
238 if (0 <= buffered) {
239 *cp++ = buffered;
240 outsz++;
241 }
242 sz = read(pu_pipe[0], cp,
243 sizeof(data) - outsz);
244 if (0 < sz)
245 ;
246 else if (sz == 0) {
247 close(pu_pipe[0]);
248 pu_pipe[0] = -1;
249 }
250 else
251 goto fail;
252 sz += outsz;
253 if (1 < sz) {
254 buffered = data[sz-1] & 0xFF;
255 sz--;
256 }
257 else
258 buffered = -1;
583b7ea3 259 sz = send_client_data(1, data, sz);
b1c71b72
JH
260 if (sz < 0)
261 goto fail;
262 }
363b7817 263 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
583b7ea3
JH
264 /* Status ready; we ship that in the side-band
265 * or dump to the standard error.
363b7817
JH
266 */
267 sz = read(pe_pipe[0], progress,
268 sizeof(progress));
269 if (0 < sz)
583b7ea3 270 send_client_data(2, progress, sz);
363b7817
JH
271 else if (sz == 0) {
272 close(pe_pipe[0]);
273 pe_pipe[0] = -1;
274 }
275 else
276 goto fail;
277 }
b1c71b72
JH
278 }
279
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;
293 }
294 error("git-upload-pack: we weren't "
295 "waiting for %d", pid);
296 continue;
297 }
298 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) {
299 error("git-upload-pack: %s died with error.",
300 who);
301 goto fail;
302 }
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;
309 }
310
311 /* both died happily */
312 if (pollsize)
313 continue;
314
315 /* flush the data */
316 if (0 <= buffered) {
317 data[0] = buffered;
583b7ea3 318 sz = send_client_data(1, data, 1);
b1c71b72
JH
319 if (sz < 0)
320 goto fail;
321 fprintf(stderr, "flushed.\n");
322 }
958c24b1
JH
323 if (use_sideband)
324 packet_flush(1);
b1c71b72
JH
325 return;
326 }
327 fail:
328 if (pid_pack_objects)
329 kill(pid_pack_objects, SIGKILL);
330 if (pid_rev_list)
331 kill(pid_rev_list, SIGKILL);
583b7ea3
JH
332 send_client_data(3, abort_msg, sizeof(abort_msg));
333 die("git-upload-pack: %s", abort_msg);
fb9040cc
LT
334}
335
def88e9a
LT
336static int got_sha1(char *hex, unsigned char *sha1)
337{
b1e9fff7 338 struct object *o;
937a515a 339 int we_knew_they_have = 0;
b1e9fff7 340
def88e9a
LT
341 if (get_sha1_hex(hex, sha1))
342 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
fb9040cc 343 if (!has_sha1_file(sha1))
937a515a 344 return -1;
b1e9fff7
JH
345
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));
182a8dab 351 if (o->type == OBJ_COMMIT) {
b1e9fff7 352 struct commit_list *parents;
937a515a 353 struct commit *commit = (struct commit *)o;
b1e9fff7 354 if (o->flags & THEY_HAVE)
937a515a
JH
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;
b1e9fff7
JH
361 parents;
362 parents = parents->next)
363 parents->item->object.flags |= THEY_HAVE;
fb9040cc 364 }
937a515a
JH
365 if (!we_knew_they_have) {
366 add_object_array(o, NULL, &have_obj);
367 return 1;
368 }
369 return 0;
370}
371
372static int reachable(struct commit *want)
373{
374 struct commit_list *work = NULL;
375
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;
382
383 if (commit->object.flags & THEY_HAVE) {
384 want->object.flags |= COMMON_KNOWN;
385 break;
386 }
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);
398 }
399 }
400 want->object.flags |= REACHABLE;
401 clear_commit_marks(want, REACHABLE);
402 free_commit_list(work);
403 return (want->object.flags & COMMON_KNOWN);
404}
405
406static int ok_to_give_up(void)
407{
408 int i;
409
410 if (!have_obj.nr)
411 return 0;
412
413 for (i = 0; i < want_obj.nr; i++) {
414 struct object *want = want_obj.objects[i].item;
415
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.
424 */
425 want_obj.objects[i].item->flags |= COMMON_KNOWN;
426 continue;
427 }
428 if (!reachable((struct commit *)want))
429 return 0;
430 }
fb9040cc 431 return 1;
def88e9a
LT
432}
433
434static int get_common_commits(void)
435{
436 static char line[1000];
c04c4e57
JH
437 unsigned char sha1[20];
438 char hex[41], last_hex[41];
def88e9a
LT
439 int len;
440
f0243f26
JS
441 track_object_refs = 0;
442 save_commit_buffer = 0;
443
def88e9a
LT
444 for(;;) {
445 len = packet_read_line(0, line, sizeof(line));
960deccb 446 reset_timeout();
def88e9a
LT
447
448 if (!len) {
b1e9fff7 449 if (have_obj.nr == 0 || multi_ack)
1bd8c8f0 450 packet_write(1, "NAK\n");
def88e9a
LT
451 continue;
452 }
75bfc6c2 453 len = strip(line, len);
def88e9a 454 if (!strncmp(line, "have ", 5)) {
937a515a
JH
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:
c04c4e57
JH
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);
467 }
468 else if (have_obj.nr == 1)
469 packet_write(1, "ACK %s\n", hex);
937a515a 470 break;
af2d3aa4 471 }
def88e9a
LT
472 continue;
473 }
474 if (!strcmp(line, "done")) {
b1e9fff7 475 if (have_obj.nr > 0) {
1bd8c8f0 476 if (multi_ack)
c04c4e57 477 packet_write(1, "ACK %s\n", last_hex);
1bd8c8f0
JS
478 return 0;
479 }
def88e9a
LT
480 packet_write(1, "NAK\n");
481 return -1;
482 }
483 die("git-upload-pack: expected SHA1 list, got '%s'", line);
484 }
def88e9a
LT
485}
486
b1e9fff7 487static void receive_needs(void)
fb9040cc 488{
ed09aef0 489 struct object_array shallows = {0, 0, NULL};
fb9040cc 490 static char line[1000];
b1e9fff7 491 int len;
fb9040cc 492
fb9040cc 493 for (;;) {
565ebbf7 494 struct object *o;
6ece0d30 495 unsigned char sha1_buf[20];
fb9040cc 496 len = packet_read_line(0, line, sizeof(line));
960deccb 497 reset_timeout();
fb9040cc 498 if (!len)
ed09aef0 499 break;
e091eb93 500
ed09aef0
JS
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;
511 }
6ece0d30
JH
512 if (strncmp("want ", line, 5) ||
513 get_sha1_hex(line+5, sha1_buf))
e091eb93
JH
514 die("git-upload-pack: protocol error, "
515 "expected to get sha, not '%s'", line);
1bd8c8f0
JS
516 if (strstr(line+45, "multi_ack"))
517 multi_ack = 1;
b19696c2
JH
518 if (strstr(line+45, "thin-pack"))
519 use_thin_pack = 1;
e4fe4b8e
NP
520 if (strstr(line+45, "ofs-delta"))
521 use_ofs_delta = 1;
d47f3db7
JH
522 if (strstr(line+45, "side-band-64k"))
523 use_sideband = LARGE_PACKET_MAX;
524 else if (strstr(line+45, "side-band"))
525 use_sideband = DEFAULT_PACKET_MAX;
565ebbf7
JH
526
527 /* We have sent all our refs already, and the other end
528 * should have chosen out of them; otherwise they are
529 * asking for nonsense.
530 *
531 * Hmph. We may later want to allow "want" line that
532 * asks for something like "master~10" (symbolic)...
533 * would it make sense? I don't know.
534 */
535 o = lookup_object(sha1_buf);
536 if (!o || !(o->flags & OUR_REF))
537 die("git-upload-pack: not our ref %s", line+5);
538 if (!(o->flags & WANTED)) {
539 o->flags |= WANTED;
b1e9fff7 540 add_object_array(o, NULL, &want_obj);
565ebbf7 541 }
fb9040cc 542 }
ed09aef0
JS
543 if (shallows.nr > 0) {
544 int i;
545 for (i = 0; i < shallows.nr; i++)
546 register_shallow(shallows.objects[i].item->sha1);
547 }
fb9040cc
LT
548}
549
8da19775 550static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
def88e9a 551{
ed09aef0
JS
552 static const char *capabilities = "multi_ack thin-pack side-band"
553 " side-band-64k ofs-delta shallow";
f6b42a81
JH
554 struct object *o = parse_object(sha1);
555
b5b16990
CW
556 if (!o)
557 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
558
1f5881bb
JS
559 if (capabilities)
560 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
561 0, capabilities);
562 else
563 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
564 capabilities = NULL;
565ebbf7
JH
565 if (!(o->flags & OUR_REF)) {
566 o->flags |= OUR_REF;
567 nr_our_refs++;
568 }
1974632c 569 if (o->type == OBJ_TAG) {
9534f40b 570 o = deref_tag(o, refname, 0);
f6b42a81
JH
571 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
572 }
def88e9a
LT
573 return 0;
574}
575
59076eba 576static void upload_pack(void)
def88e9a 577{
960deccb 578 reset_timeout();
cb5d709f
JH
579 head_ref(send_ref, NULL);
580 for_each_ref(send_ref, NULL);
def88e9a 581 packet_flush(1);
b1e9fff7 582 receive_needs();
59076eba
DR
583 if (want_obj.nr) {
584 get_common_commits();
585 create_pack_file();
586 }
def88e9a
LT
587}
588
589int main(int argc, char **argv)
590{
8d630132 591 char *dir;
960deccb
PA
592 int i;
593 int strict = 0;
594
595 for (i = 1; i < argc; i++) {
596 char *arg = argv[i];
597
598 if (arg[0] != '-')
599 break;
600 if (!strcmp(arg, "--strict")) {
601 strict = 1;
602 continue;
603 }
604 if (!strncmp(arg, "--timeout=", 10)) {
605 timeout = atoi(arg+10);
606 continue;
607 }
608 if (!strcmp(arg, "--")) {
609 i++;
610 break;
611 }
612 }
613
614 if (i != argc-1)
def88e9a 615 usage(upload_pack_usage);
960deccb 616 dir = argv[i];
113b9475 617
8d630132
AE
618 if (!enter_repo(dir, strict))
619 die("'%s': unable to chdir or not a git archive", dir);
960deccb 620
def88e9a
LT
621 upload_pack();
622 return 0;
623}