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