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