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