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 static int multi_ack = 0, nr_our_refs = 0;
18 static int use_thin_pack = 0;
19 static struct object_array have_obj;
20 static struct object_array want_obj;
21 static unsigned int timeout = 0;
22 static int use_sideband = 0;
24 static void reset_timeout(void)
29 static int strip(char *line, int len)
31 if (len && line[len-1] == '\n')
36 #define PACKET_MAX 1000
37 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
52 return safe_write(fd, data, sz);
61 if (PACKET_MAX - 5 < n)
63 sprintf(hdr, "%04x", n + 5);
65 safe_write(1, hdr, 5);
73 static void create_pack_file(void)
75 /* Pipes between rev-list to pack-objects, pack-objects to us
76 * and pack-objects error stream for progress bar.
78 int lp_pipe[2], pu_pipe[2], pe_pipe[2];
79 pid_t pid_rev_list, pid_pack_objects;
80 int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr);
81 char data[8193], progress[128];
82 char abort_msg[] = "aborting due to possible repository "
83 "corruption on the remote side.";
86 if (pipe(lp_pipe) < 0)
87 die("git-upload-pack: unable to create pipe");
88 pid_rev_list = fork();
90 die("git-upload-pack: unable to fork git-rev-list");
99 if (create_full_pack) {
101 use_thin_pack = 0; /* no point doing it */
104 args = have_obj.nr + want_obj.nr + 5;
105 p = xmalloc(args * sizeof(char *));
106 argv = (const char **) p;
107 buf = xmalloc(args * 45);
114 *p++ = use_thin_pack ? "--objects-edge" : "--objects";
115 if (create_full_pack)
118 for (i = 0; i < want_obj.nr; i++) {
119 struct object *o = want_obj.objects[i].item;
121 memcpy(buf, sha1_to_hex(o->sha1), 41);
125 if (!create_full_pack)
126 for (i = 0; i < have_obj.nr; i++) {
127 struct object *o = have_obj.objects[i].item;
130 memcpy(buf, sha1_to_hex(o->sha1), 41);
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) {
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");
167 /* We read from pe_pipe[0] to capture stderr output for
168 * progress bar, and pu_pipe[0] to capture the pack data.
175 struct pollfd pfd[2];
179 int pe, pu, pollsize;
184 if (0 <= pu_pipe[0]) {
185 pfd[pollsize].fd = pu_pipe[0];
186 pfd[pollsize].events = POLLIN;
190 if (0 <= pe_pipe[0]) {
191 pfd[pollsize].fd = pe_pipe[0];
192 pfd[pollsize].events = POLLIN;
198 if (poll(pfd, pollsize, -1) < 0) {
199 if (errno != EINTR) {
200 error("poll failed, resuming: %s",
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.
224 sz = read(pu_pipe[0], cp,
225 sizeof(data) - outsz);
236 buffered = data[sz-1] & 0xFF;
241 sz = send_client_data(1, data, sz);
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,
252 send_client_data(2, progress, sz);
262 /* See if the children are still there */
263 if (pid_rev_list || pid_pack_objects) {
264 pid = waitpid(-1, &status, WNOHANG);
267 who = ((pid == pid_rev_list) ? "git-rev-list" :
268 (pid == pid_pack_objects) ? "git-pack-objects" :
272 error("git-upload-pack: %s",
276 error("git-upload-pack: we weren't "
277 "waiting for %d", pid);
280 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) {
281 error("git-upload-pack: %s died with error.",
285 if (pid == pid_rev_list)
287 if (pid == pid_pack_objects)
288 pid_pack_objects = 0;
289 if (pid_rev_list || pid_pack_objects)
293 /* both died happily */
300 sz = send_client_data(1, data, 1);
303 fprintf(stderr, "flushed.\n");
305 send_client_data(1, NULL, 0);
309 if (pid_pack_objects)
310 kill(pid_pack_objects, SIGKILL);
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)
321 if (get_sha1_hex(hex, sha1))
322 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
323 if (!has_sha1_file(sha1))
326 o = lookup_object(sha1);
327 if (!(o && o->parsed))
328 o = parse_object(sha1);
330 die("oops (%s)", sha1_to_hex(sha1));
331 if (o->type == TYPE_COMMIT) {
332 struct commit_list *parents;
333 if (o->flags & THEY_HAVE)
335 o->flags |= THEY_HAVE;
336 for (parents = ((struct commit*)o)->parents;
338 parents = parents->next)
339 parents->item->object.flags |= THEY_HAVE;
341 add_object_array(o, NULL, &have_obj);
345 static int get_common_commits(void)
347 static char line[1000];
348 unsigned char sha1[20], last_sha1[20];
351 track_object_refs = 0;
352 save_commit_buffer = 0;
355 len = packet_read_line(0, line, sizeof(line));
359 if (have_obj.nr == 0 || multi_ack)
360 packet_write(1, "NAK\n");
363 len = strip(line, len);
364 if (!strncmp(line, "have ", 5)) {
365 if (got_sha1(line+5, sha1) &&
366 (multi_ack || have_obj.nr == 1)) {
367 packet_write(1, "ACK %s%s\n",
369 multi_ack ? " continue" : "");
371 memcpy(last_sha1, sha1, 20);
375 if (!strcmp(line, "done")) {
376 if (have_obj.nr > 0) {
378 packet_write(1, "ACK %s\n",
379 sha1_to_hex(last_sha1));
382 packet_write(1, "NAK\n");
385 die("git-upload-pack: expected SHA1 list, got '%s'", line);
389 static void receive_needs(void)
391 static char line[1000];
396 unsigned char sha1_buf[20];
397 len = packet_read_line(0, line, sizeof(line));
402 if (strncmp("want ", line, 5) ||
403 get_sha1_hex(line+5, sha1_buf))
404 die("git-upload-pack: protocol error, "
405 "expected to get sha, not '%s'", line);
406 if (strstr(line+45, "multi_ack"))
408 if (strstr(line+45, "thin-pack"))
410 if (strstr(line+45, "side-band"))
413 /* We have sent all our refs already, and the other end
414 * should have chosen out of them; otherwise they are
415 * asking for nonsense.
417 * Hmph. We may later want to allow "want" line that
418 * asks for something like "master~10" (symbolic)...
419 * would it make sense? I don't know.
421 o = lookup_object(sha1_buf);
422 if (!o || !(o->flags & OUR_REF))
423 die("git-upload-pack: not our ref %s", line+5);
424 if (!(o->flags & WANTED)) {
426 add_object_array(o, NULL, &want_obj);
431 static int send_ref(const char *refname, const unsigned char *sha1)
433 static const char *capabilities = "multi_ack thin-pack side-band";
434 struct object *o = parse_object(sha1);
437 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
440 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
443 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
445 if (!(o->flags & OUR_REF)) {
449 if (o->type == TYPE_TAG) {
450 o = deref_tag(o, refname, 0);
451 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
456 static int upload_pack(void)
460 for_each_ref(send_ref);
465 get_common_commits();
470 int main(int argc, char **argv)
476 for (i = 1; i < argc; i++) {
481 if (!strcmp(arg, "--strict")) {
485 if (!strncmp(arg, "--timeout=", 10)) {
486 timeout = atoi(arg+10);
489 if (!strcmp(arg, "--")) {
496 usage(upload_pack_usage);
499 if (!enter_repo(dir, strict))
500 die("'%s': unable to chdir or not a git archive", dir);