]> git.ipfire.org Git - thirdparty/git.git/blame - upload-pack.c
upload-pack: no longer call rev-list
[thirdparty/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
LT
488{
489 static char line[1000];
b1e9fff7 490 int len;
fb9040cc 491
fb9040cc 492 for (;;) {
565ebbf7 493 struct object *o;
6ece0d30 494 unsigned char sha1_buf[20];
fb9040cc 495 len = packet_read_line(0, line, sizeof(line));
960deccb 496 reset_timeout();
fb9040cc 497 if (!len)
b1e9fff7 498 return;
e091eb93 499
6ece0d30
JH
500 if (strncmp("want ", line, 5) ||
501 get_sha1_hex(line+5, sha1_buf))
e091eb93
JH
502 die("git-upload-pack: protocol error, "
503 "expected to get sha, not '%s'", line);
1bd8c8f0
JS
504 if (strstr(line+45, "multi_ack"))
505 multi_ack = 1;
b19696c2
JH
506 if (strstr(line+45, "thin-pack"))
507 use_thin_pack = 1;
e4fe4b8e
NP
508 if (strstr(line+45, "ofs-delta"))
509 use_ofs_delta = 1;
d47f3db7
JH
510 if (strstr(line+45, "side-band-64k"))
511 use_sideband = LARGE_PACKET_MAX;
512 else if (strstr(line+45, "side-band"))
513 use_sideband = DEFAULT_PACKET_MAX;
565ebbf7
JH
514
515 /* We have sent all our refs already, and the other end
516 * should have chosen out of them; otherwise they are
517 * asking for nonsense.
518 *
519 * Hmph. We may later want to allow "want" line that
520 * asks for something like "master~10" (symbolic)...
521 * would it make sense? I don't know.
522 */
523 o = lookup_object(sha1_buf);
524 if (!o || !(o->flags & OUR_REF))
525 die("git-upload-pack: not our ref %s", line+5);
526 if (!(o->flags & WANTED)) {
527 o->flags |= WANTED;
b1e9fff7 528 add_object_array(o, NULL, &want_obj);
565ebbf7 529 }
fb9040cc
LT
530 }
531}
532
8da19775 533static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
def88e9a 534{
e4fe4b8e 535 static const char *capabilities = "multi_ack thin-pack side-band side-band-64k ofs-delta";
f6b42a81
JH
536 struct object *o = parse_object(sha1);
537
b5b16990
CW
538 if (!o)
539 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
540
1f5881bb
JS
541 if (capabilities)
542 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
543 0, capabilities);
544 else
545 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
546 capabilities = NULL;
565ebbf7
JH
547 if (!(o->flags & OUR_REF)) {
548 o->flags |= OUR_REF;
549 nr_our_refs++;
550 }
1974632c 551 if (o->type == OBJ_TAG) {
9534f40b 552 o = deref_tag(o, refname, 0);
f6b42a81
JH
553 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
554 }
def88e9a
LT
555 return 0;
556}
557
59076eba 558static void upload_pack(void)
def88e9a 559{
960deccb 560 reset_timeout();
cb5d709f
JH
561 head_ref(send_ref, NULL);
562 for_each_ref(send_ref, NULL);
def88e9a 563 packet_flush(1);
b1e9fff7 564 receive_needs();
59076eba
DR
565 if (want_obj.nr) {
566 get_common_commits();
567 create_pack_file();
568 }
def88e9a
LT
569}
570
571int main(int argc, char **argv)
572{
8d630132 573 char *dir;
960deccb
PA
574 int i;
575 int strict = 0;
576
577 for (i = 1; i < argc; i++) {
578 char *arg = argv[i];
579
580 if (arg[0] != '-')
581 break;
582 if (!strcmp(arg, "--strict")) {
583 strict = 1;
584 continue;
585 }
586 if (!strncmp(arg, "--timeout=", 10)) {
587 timeout = atoi(arg+10);
588 continue;
589 }
590 if (!strcmp(arg, "--")) {
591 i++;
592 break;
593 }
594 }
595
596 if (i != argc-1)
def88e9a 597 usage(upload_pack_usage);
960deccb 598 dir = argv[i];
113b9475 599
8d630132
AE
600 if (!enter_repo(dir, strict))
601 die("'%s': unable to chdir or not a git archive", dir);
960deccb 602
def88e9a
LT
603 upload_pack();
604 return 0;
605}