Convert git-mv to use Git.pm
[git/builtin-gsoc.git] / upload-pack.c
blobb18eb9ba0dbe8b5020aaaa698d98a4d1b76aa49c
1 #include <signal.h>
2 #include <sys/wait.h>
3 #include <sys/poll.h>
4 #include "cache.h"
5 #include "refs.h"
6 #include "pkt-line.h"
7 #include "tag.h"
8 #include "object.h"
9 #include "commit.h"
10 #include "exec_cmd.h"
12 static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
14 #define THEY_HAVE (1U << 0)
15 #define OUR_REF (1U << 1)
16 #define WANTED (1U << 2)
17 #define MAX_HAS 256
18 #define MAX_NEEDS 256
19 static int nr_has = 0, nr_needs = 0, multi_ack = 0, nr_our_refs = 0;
20 static int use_thin_pack = 0;
21 static unsigned char has_sha1[MAX_HAS][20];
22 static unsigned char needs_sha1[MAX_NEEDS][20];
23 static unsigned int timeout = 0;
24 static int use_sideband = 0;
26 static void reset_timeout(void)
28 alarm(timeout);
31 static int strip(char *line, int len)
33 if (len && line[len-1] == '\n')
34 line[--len] = 0;
35 return len;
38 #define PACKET_MAX 1000
39 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
41 ssize_t ssz;
42 const char *p;
44 if (!data) {
45 if (!use_sideband)
46 return 0;
47 packet_flush(1);
50 if (!use_sideband) {
51 if (fd == 3)
52 /* emergency quit */
53 fd = 2;
54 return safe_write(fd, data, sz);
56 p = data;
57 ssz = sz;
58 while (sz) {
59 unsigned n;
60 char hdr[5];
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;
72 return ssz;
75 static void create_pack_file(void)
77 /* Pipes between rev-list to pack-objects, pack-objects to us
78 * and pack-objects error stream for progress bar.
80 int lp_pipe[2], pu_pipe[2], pe_pipe[2];
81 pid_t pid_rev_list, pid_pack_objects;
82 int create_full_pack = (nr_our_refs == nr_needs && !nr_has);
83 char data[8193], progress[128];
84 char abort_msg[] = "aborting due to possible repository "
85 "corruption on the remote side.";
86 int buffered = -1;
88 if (pipe(lp_pipe) < 0)
89 die("git-upload-pack: unable to create pipe");
90 pid_rev_list = fork();
91 if (pid_rev_list < 0)
92 die("git-upload-pack: unable to fork git-rev-list");
94 if (!pid_rev_list) {
95 int i;
96 int args;
97 const char **argv;
98 const char **p;
99 char *buf;
101 if (create_full_pack) {
102 args = 10;
103 use_thin_pack = 0; /* no point doing it */
105 else
106 args = nr_has + nr_needs + 5;
107 p = xmalloc(args * sizeof(char *));
108 argv = (const char **) p;
109 buf = xmalloc(args * 45);
111 dup2(lp_pipe[1], 1);
112 close(0);
113 close(lp_pipe[0]);
114 close(lp_pipe[1]);
115 *p++ = "rev-list";
116 *p++ = use_thin_pack ? "--objects-edge" : "--objects";
117 if (create_full_pack || MAX_NEEDS <= nr_needs)
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;
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;
133 *p++ = NULL;
134 execv_git_cmd(argv);
135 die("git-upload-pack: unable to exec git-rev-list");
138 if (pipe(pu_pipe) < 0)
139 die("git-upload-pack: unable to create pipe");
140 if (pipe(pe_pipe) < 0)
141 die("git-upload-pack: unable to create pipe");
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");
148 if (!pid_pack_objects) {
149 dup2(lp_pipe[0], 0);
150 dup2(pu_pipe[1], 1);
151 dup2(pe_pipe[1], 2);
153 close(lp_pipe[0]);
154 close(lp_pipe[1]);
155 close(pu_pipe[0]);
156 close(pu_pipe[1]);
157 close(pe_pipe[0]);
158 close(pe_pipe[1]);
159 execl_git_cmd("pack-objects", "--stdout", "--progress", NULL);
160 kill(pid_rev_list, SIGKILL);
161 die("git-upload-pack: unable to exec git-pack-objects");
164 close(lp_pipe[0]);
165 close(lp_pipe[1]);
167 /* We read from pe_pipe[0] to capture stderr output for
168 * progress bar, and pu_pipe[0] to capture the pack data.
170 close(pe_pipe[1]);
171 close(pu_pipe[1]);
173 while (1) {
174 const char *who;
175 struct pollfd pfd[2];
176 pid_t pid;
177 int status;
178 ssize_t sz;
179 int pe, pu, pollsize;
181 pollsize = 0;
182 pe = pu = -1;
184 if (0 <= pu_pipe[0]) {
185 pfd[pollsize].fd = pu_pipe[0];
186 pfd[pollsize].events = POLLIN;
187 pu = pollsize;
188 pollsize++;
190 if (0 <= pe_pipe[0]) {
191 pfd[pollsize].fd = pe_pipe[0];
192 pfd[pollsize].events = POLLIN;
193 pe = pollsize;
194 pollsize++;
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);
204 continue;
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.
218 char *cp = data;
219 ssize_t outsz = 0;
220 if (0 <= buffered) {
221 *cp++ = buffered;
222 outsz++;
224 sz = read(pu_pipe[0], cp,
225 sizeof(data) - outsz);
226 if (0 < sz)
228 else if (sz == 0) {
229 close(pu_pipe[0]);
230 pu_pipe[0] = -1;
232 else
233 goto fail;
234 sz += outsz;
235 if (1 < sz) {
236 buffered = data[sz-1] & 0xFF;
237 sz--;
239 else
240 buffered = -1;
241 sz = send_client_data(1, data, sz);
242 if (sz < 0)
243 goto fail;
245 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
246 /* Status ready; we ship that in the side-band
247 * or dump to the standard error.
249 sz = read(pe_pipe[0], progress,
250 sizeof(progress));
251 if (0 < sz)
252 send_client_data(2, progress, sz);
253 else if (sz == 0) {
254 close(pe_pipe[0]);
255 pe_pipe[0] = -1;
257 else
258 goto fail;
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;
276 error("git-upload-pack: we weren't "
277 "waiting for %d", pid);
278 continue;
280 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) {
281 error("git-upload-pack: %s died with error.",
282 who);
283 goto fail;
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;
293 /* both died happily */
294 if (pollsize)
295 continue;
297 /* flush the data */
298 if (0 <= buffered) {
299 data[0] = buffered;
300 sz = send_client_data(1, data, 1);
301 if (sz < 0)
302 goto fail;
303 fprintf(stderr, "flushed.\n");
305 send_client_data(1, NULL, 0);
306 return;
308 fail:
309 if (pid_pack_objects)
310 kill(pid_pack_objects, SIGKILL);
311 if (pid_rev_list)
312 kill(pid_rev_list, SIGKILL);
313 send_client_data(3, abort_msg, sizeof(abort_msg));
314 die("git-upload-pack: %s", abort_msg);
317 static int got_sha1(char *hex, unsigned char *sha1)
319 if (get_sha1_hex(hex, sha1))
320 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
321 if (!has_sha1_file(sha1))
322 return 0;
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));
329 if (o->type == TYPE_COMMIT) {
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;
339 memcpy(has_sha1[nr_has++], sha1, 20);
341 return 1;
344 static int get_common_commits(void)
346 static char line[1000];
347 unsigned char sha1[20], last_sha1[20];
348 int len;
350 track_object_refs = 0;
351 save_commit_buffer = 0;
353 for(;;) {
354 len = packet_read_line(0, line, sizeof(line));
355 reset_timeout();
357 if (!len) {
358 if (nr_has == 0 || multi_ack)
359 packet_write(1, "NAK\n");
360 continue;
362 len = strip(line, len);
363 if (!strncmp(line, "have ", 5)) {
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);
374 continue;
376 if (!strcmp(line, "done")) {
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;
383 packet_write(1, "NAK\n");
384 return -1;
386 die("git-upload-pack: expected SHA1 list, got '%s'", line);
390 static int receive_needs(void)
392 static char line[1000];
393 int len, needs;
395 needs = 0;
396 for (;;) {
397 struct object *o;
398 unsigned char dummy[20], *sha1_buf;
399 len = packet_read_line(0, line, sizeof(line));
400 reset_timeout();
401 if (!len)
402 return needs;
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);
411 else if (needs < MAX_NEEDS)
412 sha1_buf = needs_sha1[needs];
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);
417 if (strstr(line+45, "multi_ack"))
418 multi_ack = 1;
419 if (strstr(line+45, "thin-pack"))
420 use_thin_pack = 1;
421 if (strstr(line+45, "side-band"))
422 use_sideband = 1;
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.
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.
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++;
442 static int send_ref(const char *refname, const unsigned char *sha1)
444 static const char *capabilities = "multi_ack thin-pack side-band";
445 struct object *o = parse_object(sha1);
447 if (!o)
448 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
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;
456 if (!(o->flags & OUR_REF)) {
457 o->flags |= OUR_REF;
458 nr_our_refs++;
460 if (o->type == TYPE_TAG) {
461 o = deref_tag(o, refname, 0);
462 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
464 return 0;
467 static int upload_pack(void)
469 reset_timeout();
470 head_ref(send_ref);
471 for_each_ref(send_ref);
472 packet_flush(1);
473 nr_needs = receive_needs();
474 if (!nr_needs)
475 return 0;
476 get_common_commits();
477 create_pack_file();
478 return 0;
481 int main(int argc, char **argv)
483 char *dir;
484 int i;
485 int strict = 0;
487 for (i = 1; i < argc; i++) {
488 char *arg = argv[i];
490 if (arg[0] != '-')
491 break;
492 if (!strcmp(arg, "--strict")) {
493 strict = 1;
494 continue;
496 if (!strncmp(arg, "--timeout=", 10)) {
497 timeout = atoi(arg+10);
498 continue;
500 if (!strcmp(arg, "--")) {
501 i++;
502 break;
506 if (i != argc-1)
507 usage(upload_pack_usage);
508 dir = argv[i];
510 if (!enter_repo(dir, strict))
511 die("'%s': unable to chdir or not a git archive", dir);
513 upload_pack();
514 return 0;