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