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)
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)
31 static int strip(char *line, int len)
33 if (len && line[len-1] == '\n')
38 #define PACKET_MAX 1000
39 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
58 return safe_write(fd, data, sz);
67 if (PACKET_MAX - 5 < n)
69 sprintf(hdr, "%04x", n + 5);
71 safe_write(1, hdr, 5);
79 static void create_pack_file(void)
81 /* Pipes between rev-list to pack-objects, pack-objects to us
82 * and pack-objects error stream for progress bar.
84 int lp_pipe[2], pu_pipe[2], pe_pipe[2];
85 pid_t pid_rev_list, pid_pack_objects;
86 int create_full_pack = (nr_our_refs == nr_needs && !nr_has);
87 char data[8193], progress[128];
88 char abort_msg[] = "aborting due to possible repository "
89 "corruption on the remote side.";
92 if (pipe(lp_pipe) < 0)
93 die("git-upload-pack: unable to create pipe");
94 pid_rev_list = fork();
96 die("git-upload-pack: unable to fork git-rev-list");
105 if (create_full_pack) {
107 use_thin_pack = 0; /* no point doing it */
110 args = nr_has + nr_needs + 5;
111 p = xmalloc(args * sizeof(char *));
112 argv = (const char **) p;
113 buf = xmalloc(args * 45);
120 *p++ = use_thin_pack ? "--objects-edge" : "--objects";
121 if (create_full_pack || MAX_NEEDS <= nr_needs)
124 for (i = 0; i < nr_needs; i++) {
126 memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
130 if (!create_full_pack)
131 for (i = 0; i < nr_has; i++) {
134 memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
139 die("git-upload-pack: unable to exec git-rev-list");
142 if (pipe(pu_pipe) < 0)
143 die("git-upload-pack: unable to create pipe");
144 if (pipe(pe_pipe) < 0)
145 die("git-upload-pack: unable to create pipe");
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");
152 if (!pid_pack_objects) {
163 execl_git_cmd("pack-objects", "--stdout", "--progress", NULL);
164 kill(pid_rev_list, SIGKILL);
165 die("git-upload-pack: unable to exec git-pack-objects");
171 /* We read from pe_pipe[0] to capture stderr output for
172 * progress bar, and pu_pipe[0] to capture the pack data.
179 struct pollfd pfd[2];
183 int pe, pu, pollsize;
188 if (0 <= pu_pipe[0]) {
189 pfd[pollsize].fd = pu_pipe[0];
190 pfd[pollsize].events = POLLIN;
194 if (0 <= pe_pipe[0]) {
195 pfd[pollsize].fd = pe_pipe[0];
196 pfd[pollsize].events = POLLIN;
202 if (poll(pfd, pollsize, -1) < 0) {
203 if (errno != EINTR) {
204 error("poll failed, resuming: %s",
210 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
211 /* Data ready; we keep the last byte
212 * to ourselves in case we detect
213 * broken rev-list, so that we can
214 * leave the stream corrupted. This
215 * is unfortunate -- unpack-objects
216 * would happily accept a valid pack
217 * data with trailing garbage, so
218 * appending garbage after we pass all
219 * the pack data is not good enough to
220 * signal breakage to downstream.
228 sz = read(pu_pipe[0], cp,
229 sizeof(data) - outsz);
240 buffered = data[sz-1] & 0xFF;
245 sz = send_client_data(1, data, sz);
249 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
250 /* Status ready; we ship that in the side-band
251 * or dump to the standard error.
253 sz = read(pe_pipe[0], progress,
256 send_client_data(2, progress, sz);
266 /* See if the children are still there */
267 if (pid_rev_list || pid_pack_objects) {
268 pid = waitpid(-1, &status, WNOHANG);
271 who = ((pid == pid_rev_list) ? "git-rev-list" :
272 (pid == pid_pack_objects) ? "git-pack-objects" :
276 error("git-upload-pack: %s",
280 error("git-upload-pack: we weren't "
281 "waiting for %d", pid);
284 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) {
285 error("git-upload-pack: %s died with error.",
289 if (pid == pid_rev_list)
291 if (pid == pid_pack_objects)
292 pid_pack_objects = 0;
293 if (pid_rev_list || pid_pack_objects)
297 /* both died happily */
304 sz = send_client_data(1, data, 1);
307 fprintf(stderr, "flushed.\n");
309 send_client_data(1, NULL, 0);
313 if (pid_pack_objects)
314 kill(pid_pack_objects, SIGKILL);
316 kill(pid_rev_list, SIGKILL);
317 send_client_data(3, abort_msg, sizeof(abort_msg));
318 die("git-upload-pack: %s", abort_msg);
321 static int got_sha1(char *hex, unsigned char *sha1)
323 if (get_sha1_hex(hex, sha1))
324 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
325 if (!has_sha1_file(sha1))
327 if (nr_has < MAX_HAS) {
328 struct object *o = lookup_object(sha1);
329 if (!(o && o->parsed))
330 o = parse_object(sha1);
332 die("oops (%s)", sha1_to_hex(sha1));
333 if (o->type == OBJ_COMMIT) {
334 struct commit_list *parents;
335 if (o->flags & THEY_HAVE)
337 o->flags |= THEY_HAVE;
338 for (parents = ((struct commit*)o)->parents;
340 parents = parents->next)
341 parents->item->object.flags |= THEY_HAVE;
343 memcpy(has_sha1[nr_has++], sha1, 20);
348 static int get_common_commits(void)
350 static char line[1000];
351 unsigned char sha1[20], last_sha1[20];
354 track_object_refs = 0;
355 save_commit_buffer = 0;
358 len = packet_read_line(0, line, sizeof(line));
362 if (nr_has == 0 || multi_ack)
363 packet_write(1, "NAK\n");
366 len = strip(line, len);
367 if (!strncmp(line, "have ", 5)) {
368 if (got_sha1(line+5, sha1) &&
369 (multi_ack || nr_has == 1)) {
370 if (nr_has >= MAX_HAS)
372 packet_write(1, "ACK %s%s\n",
374 multi_ack ? " continue" : "");
376 memcpy(last_sha1, sha1, 20);
380 if (!strcmp(line, "done")) {
383 packet_write(1, "ACK %s\n",
384 sha1_to_hex(last_sha1));
387 packet_write(1, "NAK\n");
390 die("git-upload-pack: expected SHA1 list, got '%s'", line);
394 static int receive_needs(void)
396 static char line[1000];
402 unsigned char dummy[20], *sha1_buf;
403 len = packet_read_line(0, line, sizeof(line));
409 if (needs == MAX_NEEDS) {
411 "warning: supporting only a max of %d requests. "
412 "sending everything instead.\n",
415 else if (needs < MAX_NEEDS)
416 sha1_buf = needs_sha1[needs];
418 if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
419 die("git-upload-pack: protocol error, "
420 "expected to get sha, not '%s'", line);
421 if (strstr(line+45, "multi_ack"))
423 if (strstr(line+45, "thin-pack"))
425 if (strstr(line+45, "side-band"))
428 /* We have sent all our refs already, and the other end
429 * should have chosen out of them; otherwise they are
430 * asking for nonsense.
432 * Hmph. We may later want to allow "want" line that
433 * asks for something like "master~10" (symbolic)...
434 * would it make sense? I don't know.
436 o = lookup_object(sha1_buf);
437 if (!o || !(o->flags & OUR_REF))
438 die("git-upload-pack: not our ref %s", line+5);
439 if (!(o->flags & WANTED)) {
446 static int send_ref(const char *refname, const unsigned char *sha1)
448 static const char *capabilities = "multi_ack thin-pack side-band";
449 struct object *o = parse_object(sha1);
452 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
455 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
458 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
460 if (!(o->flags & OUR_REF)) {
464 if (o->type == OBJ_TAG) {
465 o = deref_tag(o, refname, 0);
466 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
471 static int upload_pack(void)
475 for_each_ref(send_ref);
477 nr_needs = receive_needs();
480 get_common_commits();
485 int main(int argc, char **argv)
491 for (i = 1; i < argc; i++) {
496 if (!strcmp(arg, "--strict")) {
500 if (!strncmp(arg, "--timeout=", 10)) {
501 timeout = atoi(arg+10);
504 if (!strcmp(arg, "--")) {
511 usage(upload_pack_usage);
514 if (!enter_repo(dir, strict))
515 die("'%s': unable to chdir or not a git archive", dir);