upload-pack: stop the other side when they have more roots than we do.
[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"
f6b42a81
JH
7#include "tag.h"
8#include "object.h"
f0243f26 9#include "commit.h"
77cb17e9 10#include "exec_cmd.h"
def88e9a 11
960deccb 12static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
def88e9a 13
937a515a
JH
14/* bits #0..7 in revision.h, #8..10 in commit.c */
15#define THEY_HAVE (1u << 11)
16#define OUR_REF (1u << 12)
17#define WANTED (1u << 13)
18#define COMMON_KNOWN (1u << 14)
19#define REACHABLE (1u << 15)
20
21static unsigned long oldest_have = 0;
22
b1e9fff7 23static int multi_ack = 0, nr_our_refs = 0;
b19696c2 24static int use_thin_pack = 0;
b1e9fff7
JH
25static struct object_array have_obj;
26static struct object_array want_obj;
960deccb 27static unsigned int timeout = 0;
583b7ea3 28static int use_sideband = 0;
960deccb
PA
29
30static void reset_timeout(void)
31{
32 alarm(timeout);
33}
fb9040cc 34
75bfc6c2
LT
35static int strip(char *line, int len)
36{
37 if (len && line[len-1] == '\n')
38 line[--len] = 0;
39 return len;
40}
41
583b7ea3
JH
42#define PACKET_MAX 1000
43static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
44{
45 ssize_t ssz;
46 const char *p;
47
48 if (!data) {
49 if (!use_sideband)
50 return 0;
51 packet_flush(1);
52 }
53
54 if (!use_sideband) {
55 if (fd == 3)
56 /* emergency quit */
57 fd = 2;
5f490ce0
ML
58 if (fd == 2) {
59 xwrite(fd, data, sz);
60 return sz;
61 }
583b7ea3
JH
62 return safe_write(fd, data, sz);
63 }
64 p = data;
65 ssz = sz;
66 while (sz) {
67 unsigned n;
68 char hdr[5];
69
70 n = sz;
71 if (PACKET_MAX - 5 < n)
72 n = PACKET_MAX - 5;
73 sprintf(hdr, "%04x", n + 5);
74 hdr[4] = fd;
75 safe_write(1, hdr, 5);
76 safe_write(1, p, n);
77 p += n;
78 sz -= n;
79 }
80 return ssz;
81}
82
fb9040cc
LT
83static void create_pack_file(void)
84{
363b7817
JH
85 /* Pipes between rev-list to pack-objects, pack-objects to us
86 * and pack-objects error stream for progress bar.
87 */
88 int lp_pipe[2], pu_pipe[2], pe_pipe[2];
b1c71b72 89 pid_t pid_rev_list, pid_pack_objects;
b1e9fff7 90 int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr);
363b7817 91 char data[8193], progress[128];
583b7ea3
JH
92 char abort_msg[] = "aborting due to possible repository "
93 "corruption on the remote side.";
b1c71b72 94 int buffered = -1;
75bfc6c2 95
b1c71b72 96 if (pipe(lp_pipe) < 0)
75bfc6c2 97 die("git-upload-pack: unable to create pipe");
b1c71b72
JH
98 pid_rev_list = fork();
99 if (pid_rev_list < 0)
75bfc6c2
LT
100 die("git-upload-pack: unable to fork git-rev-list");
101
b1c71b72 102 if (!pid_rev_list) {
75bfc6c2 103 int i;
e091eb93 104 int args;
9201c707 105 const char **argv;
554fe20d 106 const char **p;
e091eb93 107 char *buf;
e091eb93 108
b19696c2 109 if (create_full_pack) {
565ebbf7 110 args = 10;
b19696c2
JH
111 use_thin_pack = 0; /* no point doing it */
112 }
e091eb93 113 else
b1e9fff7 114 args = have_obj.nr + want_obj.nr + 5;
9201c707
JH
115 p = xmalloc(args * sizeof(char *));
116 argv = (const char **) p;
e091eb93 117 buf = xmalloc(args * 45);
75bfc6c2 118
b1c71b72 119 dup2(lp_pipe[1], 1);
75bfc6c2 120 close(0);
b1c71b72
JH
121 close(lp_pipe[0]);
122 close(lp_pipe[1]);
77cb17e9 123 *p++ = "rev-list";
b19696c2 124 *p++ = use_thin_pack ? "--objects-edge" : "--objects";
b1e9fff7 125 if (create_full_pack)
e091eb93
JH
126 *p++ = "--all";
127 else {
b1e9fff7
JH
128 for (i = 0; i < want_obj.nr; i++) {
129 struct object *o = want_obj.objects[i].item;
e091eb93 130 *p++ = buf;
b1e9fff7 131 memcpy(buf, sha1_to_hex(o->sha1), 41);
e091eb93
JH
132 buf += 41;
133 }
75bfc6c2 134 }
b5c367f7 135 if (!create_full_pack)
b1e9fff7
JH
136 for (i = 0; i < have_obj.nr; i++) {
137 struct object *o = have_obj.objects[i].item;
b5c367f7
JS
138 *p++ = buf;
139 *buf++ = '^';
b1e9fff7 140 memcpy(buf, sha1_to_hex(o->sha1), 41);
b5c367f7
JS
141 buf += 41;
142 }
75bfc6c2 143 *p++ = NULL;
77cb17e9 144 execv_git_cmd(argv);
75bfc6c2
LT
145 die("git-upload-pack: unable to exec git-rev-list");
146 }
b1c71b72
JH
147
148 if (pipe(pu_pipe) < 0)
149 die("git-upload-pack: unable to create pipe");
363b7817
JH
150 if (pipe(pe_pipe) < 0)
151 die("git-upload-pack: unable to create pipe");
b1c71b72
JH
152 pid_pack_objects = fork();
153 if (pid_pack_objects < 0) {
154 /* daemon sets things up to ignore TERM */
155 kill(pid_rev_list, SIGKILL);
156 die("git-upload-pack: unable to fork git-pack-objects");
157 }
158 if (!pid_pack_objects) {
159 dup2(lp_pipe[0], 0);
160 dup2(pu_pipe[1], 1);
363b7817 161 dup2(pe_pipe[1], 2);
b1c71b72
JH
162
163 close(lp_pipe[0]);
164 close(lp_pipe[1]);
165 close(pu_pipe[0]);
166 close(pu_pipe[1]);
363b7817
JH
167 close(pe_pipe[0]);
168 close(pe_pipe[1]);
169 execl_git_cmd("pack-objects", "--stdout", "--progress", NULL);
b1c71b72
JH
170 kill(pid_rev_list, SIGKILL);
171 die("git-upload-pack: unable to exec git-pack-objects");
172 }
173
174 close(lp_pipe[0]);
175 close(lp_pipe[1]);
176
363b7817
JH
177 /* We read from pe_pipe[0] to capture stderr output for
178 * progress bar, and pu_pipe[0] to capture the pack data.
b1c71b72 179 */
363b7817 180 close(pe_pipe[1]);
b1c71b72
JH
181 close(pu_pipe[1]);
182
183 while (1) {
184 const char *who;
185 struct pollfd pfd[2];
186 pid_t pid;
187 int status;
188 ssize_t sz;
363b7817 189 int pe, pu, pollsize;
b1c71b72 190
0d516ada
ML
191 reset_timeout();
192
b1c71b72 193 pollsize = 0;
363b7817 194 pe = pu = -1;
b1c71b72
JH
195
196 if (0 <= pu_pipe[0]) {
197 pfd[pollsize].fd = pu_pipe[0];
198 pfd[pollsize].events = POLLIN;
199 pu = pollsize;
200 pollsize++;
201 }
363b7817
JH
202 if (0 <= pe_pipe[0]) {
203 pfd[pollsize].fd = pe_pipe[0];
204 pfd[pollsize].events = POLLIN;
205 pe = pollsize;
206 pollsize++;
207 }
b1c71b72
JH
208
209 if (pollsize) {
210 if (poll(pfd, pollsize, -1) < 0) {
211 if (errno != EINTR) {
212 error("poll failed, resuming: %s",
213 strerror(errno));
214 sleep(1);
215 }
216 continue;
217 }
218 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
219 /* Data ready; we keep the last byte
220 * to ourselves in case we detect
221 * broken rev-list, so that we can
222 * leave the stream corrupted. This
223 * is unfortunate -- unpack-objects
224 * would happily accept a valid pack
225 * data with trailing garbage, so
226 * appending garbage after we pass all
227 * the pack data is not good enough to
228 * signal breakage to downstream.
229 */
230 char *cp = data;
231 ssize_t outsz = 0;
232 if (0 <= buffered) {
233 *cp++ = buffered;
234 outsz++;
235 }
236 sz = read(pu_pipe[0], cp,
237 sizeof(data) - outsz);
238 if (0 < sz)
239 ;
240 else if (sz == 0) {
241 close(pu_pipe[0]);
242 pu_pipe[0] = -1;
243 }
244 else
245 goto fail;
246 sz += outsz;
247 if (1 < sz) {
248 buffered = data[sz-1] & 0xFF;
249 sz--;
250 }
251 else
252 buffered = -1;
583b7ea3 253 sz = send_client_data(1, data, sz);
b1c71b72
JH
254 if (sz < 0)
255 goto fail;
256 }
363b7817 257 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
583b7ea3
JH
258 /* Status ready; we ship that in the side-band
259 * or dump to the standard error.
363b7817
JH
260 */
261 sz = read(pe_pipe[0], progress,
262 sizeof(progress));
263 if (0 < sz)
583b7ea3 264 send_client_data(2, progress, sz);
363b7817
JH
265 else if (sz == 0) {
266 close(pe_pipe[0]);
267 pe_pipe[0] = -1;
268 }
269 else
270 goto fail;
271 }
b1c71b72
JH
272 }
273
274 /* See if the children are still there */
275 if (pid_rev_list || pid_pack_objects) {
276 pid = waitpid(-1, &status, WNOHANG);
277 if (!pid)
278 continue;
279 who = ((pid == pid_rev_list) ? "git-rev-list" :
280 (pid == pid_pack_objects) ? "git-pack-objects" :
281 NULL);
282 if (!who) {
283 if (pid < 0) {
284 error("git-upload-pack: %s",
285 strerror(errno));
286 goto fail;
287 }
288 error("git-upload-pack: we weren't "
289 "waiting for %d", pid);
290 continue;
291 }
292 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) {
293 error("git-upload-pack: %s died with error.",
294 who);
295 goto fail;
296 }
297 if (pid == pid_rev_list)
298 pid_rev_list = 0;
299 if (pid == pid_pack_objects)
300 pid_pack_objects = 0;
301 if (pid_rev_list || pid_pack_objects)
302 continue;
303 }
304
305 /* both died happily */
306 if (pollsize)
307 continue;
308
309 /* flush the data */
310 if (0 <= buffered) {
311 data[0] = buffered;
583b7ea3 312 sz = send_client_data(1, data, 1);
b1c71b72
JH
313 if (sz < 0)
314 goto fail;
315 fprintf(stderr, "flushed.\n");
316 }
583b7ea3 317 send_client_data(1, NULL, 0);
b1c71b72
JH
318 return;
319 }
320 fail:
321 if (pid_pack_objects)
322 kill(pid_pack_objects, SIGKILL);
323 if (pid_rev_list)
324 kill(pid_rev_list, SIGKILL);
583b7ea3
JH
325 send_client_data(3, abort_msg, sizeof(abort_msg));
326 die("git-upload-pack: %s", abort_msg);
fb9040cc
LT
327}
328
def88e9a
LT
329static int got_sha1(char *hex, unsigned char *sha1)
330{
b1e9fff7 331 struct object *o;
937a515a 332 int we_knew_they_have = 0;
b1e9fff7 333
def88e9a
LT
334 if (get_sha1_hex(hex, sha1))
335 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
fb9040cc 336 if (!has_sha1_file(sha1))
937a515a 337 return -1;
b1e9fff7
JH
338
339 o = lookup_object(sha1);
340 if (!(o && o->parsed))
341 o = parse_object(sha1);
342 if (!o)
343 die("oops (%s)", sha1_to_hex(sha1));
182a8dab 344 if (o->type == OBJ_COMMIT) {
b1e9fff7 345 struct commit_list *parents;
937a515a 346 struct commit *commit = (struct commit *)o;
b1e9fff7 347 if (o->flags & THEY_HAVE)
937a515a
JH
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;
b1e9fff7
JH
354 parents;
355 parents = parents->next)
356 parents->item->object.flags |= THEY_HAVE;
fb9040cc 357 }
937a515a
JH
358 if (!we_knew_they_have) {
359 add_object_array(o, NULL, &have_obj);
360 return 1;
361 }
362 return 0;
363}
364
365static int reachable(struct commit *want)
366{
367 struct commit_list *work = NULL;
368
369 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 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
399static 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 }
fb9040cc 424 return 1;
def88e9a
LT
425}
426
427static int get_common_commits(void)
428{
429 static char line[1000];
c04c4e57
JH
430 unsigned char sha1[20];
431 char hex[41], last_hex[41];
def88e9a
LT
432 int len;
433
f0243f26
JS
434 track_object_refs = 0;
435 save_commit_buffer = 0;
436
def88e9a
LT
437 for(;;) {
438 len = packet_read_line(0, line, sizeof(line));
960deccb 439 reset_timeout();
def88e9a
LT
440
441 if (!len) {
b1e9fff7 442 if (have_obj.nr == 0 || multi_ack)
1bd8c8f0 443 packet_write(1, "NAK\n");
def88e9a
LT
444 continue;
445 }
75bfc6c2 446 len = strip(line, len);
def88e9a 447 if (!strncmp(line, "have ", 5)) {
937a515a
JH
448 switch (got_sha1(line+5, sha1)) {
449 case -1: /* they have what we do not */
450 if (multi_ack && ok_to_give_up())
451 packet_write(1, "ACK %s continue\n",
452 sha1_to_hex(sha1));
453 break;
454 default:
c04c4e57
JH
455 memcpy(hex, sha1_to_hex(sha1), 41);
456 if (multi_ack) {
457 const char *msg = "ACK %s continue\n";
458 packet_write(1, msg, hex);
459 memcpy(last_hex, hex, 41);
460 }
461 else if (have_obj.nr == 1)
462 packet_write(1, "ACK %s\n", hex);
937a515a 463 break;
af2d3aa4 464 }
def88e9a
LT
465 continue;
466 }
467 if (!strcmp(line, "done")) {
b1e9fff7 468 if (have_obj.nr > 0) {
1bd8c8f0 469 if (multi_ack)
c04c4e57 470 packet_write(1, "ACK %s\n", last_hex);
1bd8c8f0
JS
471 return 0;
472 }
def88e9a
LT
473 packet_write(1, "NAK\n");
474 return -1;
475 }
476 die("git-upload-pack: expected SHA1 list, got '%s'", line);
477 }
def88e9a
LT
478}
479
b1e9fff7 480static void receive_needs(void)
fb9040cc
LT
481{
482 static char line[1000];
b1e9fff7 483 int len;
fb9040cc 484
fb9040cc 485 for (;;) {
565ebbf7 486 struct object *o;
6ece0d30 487 unsigned char sha1_buf[20];
fb9040cc 488 len = packet_read_line(0, line, sizeof(line));
960deccb 489 reset_timeout();
fb9040cc 490 if (!len)
b1e9fff7 491 return;
e091eb93 492
6ece0d30
JH
493 if (strncmp("want ", line, 5) ||
494 get_sha1_hex(line+5, sha1_buf))
e091eb93
JH
495 die("git-upload-pack: protocol error, "
496 "expected to get sha, not '%s'", line);
1bd8c8f0
JS
497 if (strstr(line+45, "multi_ack"))
498 multi_ack = 1;
b19696c2
JH
499 if (strstr(line+45, "thin-pack"))
500 use_thin_pack = 1;
583b7ea3
JH
501 if (strstr(line+45, "side-band"))
502 use_sideband = 1;
565ebbf7
JH
503
504 /* We have sent all our refs already, and the other end
505 * should have chosen out of them; otherwise they are
506 * asking for nonsense.
507 *
508 * Hmph. We may later want to allow "want" line that
509 * asks for something like "master~10" (symbolic)...
510 * would it make sense? I don't know.
511 */
512 o = lookup_object(sha1_buf);
513 if (!o || !(o->flags & OUR_REF))
514 die("git-upload-pack: not our ref %s", line+5);
515 if (!(o->flags & WANTED)) {
516 o->flags |= WANTED;
b1e9fff7 517 add_object_array(o, NULL, &want_obj);
565ebbf7 518 }
fb9040cc
LT
519 }
520}
521
def88e9a
LT
522static int send_ref(const char *refname, const unsigned char *sha1)
523{
554fe20d 524 static const char *capabilities = "multi_ack thin-pack side-band";
f6b42a81
JH
525 struct object *o = parse_object(sha1);
526
b5b16990
CW
527 if (!o)
528 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
529
1f5881bb
JS
530 if (capabilities)
531 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
532 0, capabilities);
533 else
534 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
535 capabilities = NULL;
565ebbf7
JH
536 if (!(o->flags & OUR_REF)) {
537 o->flags |= OUR_REF;
538 nr_our_refs++;
539 }
1974632c 540 if (o->type == OBJ_TAG) {
9534f40b 541 o = deref_tag(o, refname, 0);
f6b42a81
JH
542 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
543 }
def88e9a
LT
544 return 0;
545}
546
547static int upload_pack(void)
548{
960deccb 549 reset_timeout();
723c31fe 550 head_ref(send_ref);
def88e9a
LT
551 for_each_ref(send_ref);
552 packet_flush(1);
b1e9fff7
JH
553 receive_needs();
554 if (!want_obj.nr)
fb9040cc 555 return 0;
def88e9a 556 get_common_commits();
fb9040cc 557 create_pack_file();
def88e9a
LT
558 return 0;
559}
560
561int main(int argc, char **argv)
562{
8d630132 563 char *dir;
960deccb
PA
564 int i;
565 int strict = 0;
566
567 for (i = 1; i < argc; i++) {
568 char *arg = argv[i];
569
570 if (arg[0] != '-')
571 break;
572 if (!strcmp(arg, "--strict")) {
573 strict = 1;
574 continue;
575 }
576 if (!strncmp(arg, "--timeout=", 10)) {
577 timeout = atoi(arg+10);
578 continue;
579 }
580 if (!strcmp(arg, "--")) {
581 i++;
582 break;
583 }
584 }
585
586 if (i != argc-1)
def88e9a 587 usage(upload_pack_usage);
960deccb 588 dir = argv[i];
113b9475 589
8d630132
AE
590 if (!enter_repo(dir, strict))
591 die("'%s': unable to chdir or not a git archive", dir);
960deccb 592
def88e9a
LT
593 upload_pack();
594 return 0;
595}