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