Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "refs.h" |
| 3 | #include "pkt-line.h" |
Junio C Hamano | 958c24b | 2006-09-10 03:20:24 -0700 | [diff] [blame] | 4 | #include "sideband.h" |
Junio C Hamano | f6b42a8 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 5 | #include "tag.h" |
| 6 | #include "object.h" |
Johannes Schindelin | f0243f2 | 2005-10-28 04:48:32 +0200 | [diff] [blame] | 7 | #include "commit.h" |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 8 | #include "exec_cmd.h" |
Johannes Schindelin | 9b8dc26 | 2006-10-30 20:08:43 +0100 | [diff] [blame] | 9 | #include "diff.h" |
| 10 | #include "revision.h" |
| 11 | #include "list-objects.h" |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 12 | #include "run-command.h" |
Junio C Hamano | 47a5918 | 2013-07-08 13:56:53 -0700 | [diff] [blame] | 13 | #include "connect.h" |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 14 | #include "sigchain.h" |
Jeff King | ff5effd | 2012-08-03 12:19:16 -0400 | [diff] [blame] | 15 | #include "version.h" |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 16 | #include "string-list.h" |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 17 | |
Štěpán Němec | 62b4698 | 2010-10-08 19:31:15 +0200 | [diff] [blame] | 18 | static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>"; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 19 | |
Nguyễn Thái Ngọc Duy | 208acbf | 2014-03-25 20:23:26 +0700 | [diff] [blame] | 20 | /* Remember to update object flag allocation in object.h */ |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 21 | #define THEY_HAVE (1u << 11) |
| 22 | #define OUR_REF (1u << 12) |
| 23 | #define WANTED (1u << 13) |
| 24 | #define COMMON_KNOWN (1u << 14) |
| 25 | #define REACHABLE (1u << 15) |
| 26 | |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 27 | #define SHALLOW (1u << 16) |
| 28 | #define NOT_SHALLOW (1u << 17) |
| 29 | #define CLIENT_SHALLOW (1u << 18) |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 30 | #define HIDDEN_REF (1u << 19) |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 31 | |
Junio C Hamano | 3fbe2d5 | 2006-11-24 02:34:27 -0800 | [diff] [blame] | 32 | static unsigned long oldest_have; |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 33 | |
Junio C Hamano | 3f1da57 | 2013-01-28 20:45:43 -0800 | [diff] [blame] | 34 | static int multi_ack; |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 35 | static int no_done; |
Shawn O. Pearce | 348e390 | 2008-03-03 22:27:33 -0500 | [diff] [blame] | 36 | static int use_thin_pack, use_ofs_delta, use_include_tag; |
Johannes Sixt | 9462e3f | 2009-06-16 20:41:16 +0200 | [diff] [blame] | 37 | static int no_progress, daemon_mode; |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 38 | static int allow_tip_sha1_in_want; |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 39 | static int shallow_nr; |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 40 | static struct object_array have_obj; |
| 41 | static struct object_array want_obj; |
Nicolas Pitre | 6523078 | 2009-09-03 19:08:33 -0400 | [diff] [blame] | 42 | static struct object_array extra_edge_obj; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 43 | static unsigned int timeout; |
Jeff King | 115dedd | 2013-09-08 05:02:06 -0400 | [diff] [blame] | 44 | static int keepalive = 5; |
Junio C Hamano | d47f3db | 2006-09-10 16:27:08 -0700 | [diff] [blame] | 45 | /* 0 for no sideband, |
| 46 | * otherwise maximum packet size (up to 65520 bytes). |
| 47 | */ |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 48 | static int use_sideband; |
Shawn O. Pearce | 42526b4 | 2009-10-30 17:47:33 -0700 | [diff] [blame] | 49 | static int advertise_refs; |
| 50 | static int stateless_rpc; |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 51 | |
| 52 | static void reset_timeout(void) |
| 53 | { |
| 54 | alarm(timeout); |
| 55 | } |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 56 | |
Junio C Hamano | 583b7ea | 2006-06-21 00:30:21 -0700 | [diff] [blame] | 57 | static ssize_t send_client_data(int fd, const char *data, ssize_t sz) |
| 58 | { |
Junio C Hamano | 958c24b | 2006-09-10 03:20:24 -0700 | [diff] [blame] | 59 | if (use_sideband) |
Junio C Hamano | d47f3db | 2006-09-10 16:27:08 -0700 | [diff] [blame] | 60 | return send_sideband(1, fd, data, sz, use_sideband); |
Junio C Hamano | 958c24b | 2006-09-10 03:20:24 -0700 | [diff] [blame] | 61 | if (fd == 3) |
| 62 | /* emergency quit */ |
| 63 | fd = 2; |
| 64 | if (fd == 2) { |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 65 | /* XXX: are we happy to lose stuff here? */ |
Junio C Hamano | 958c24b | 2006-09-10 03:20:24 -0700 | [diff] [blame] | 66 | xwrite(fd, data, sz); |
| 67 | return sz; |
Junio C Hamano | 583b7ea | 2006-06-21 00:30:21 -0700 | [diff] [blame] | 68 | } |
Jeff King | cdf4fb8 | 2013-02-20 15:01:56 -0500 | [diff] [blame] | 69 | write_or_die(fd, data, sz); |
| 70 | return sz; |
Junio C Hamano | 583b7ea | 2006-06-21 00:30:21 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Nguyễn Thái Ngọc Duy | b790e0f | 2014-03-11 19:59:46 +0700 | [diff] [blame] | 73 | static int write_one_shallow(const struct commit_graft *graft, void *cb_data) |
| 74 | { |
| 75 | FILE *fp = cb_data; |
| 76 | if (graft->nr_parent == -1) |
| 77 | fprintf(fp, "--shallow %s\n", sha1_to_hex(graft->sha1)); |
| 78 | return 0; |
| 79 | } |
| 80 | |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 81 | static void create_pack_file(void) |
| 82 | { |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 83 | struct child_process pack_objects = CHILD_PROCESS_INIT; |
Junio C Hamano | 363b781 | 2006-06-20 22:48:23 -0700 | [diff] [blame] | 84 | char data[8193], progress[128]; |
Junio C Hamano | 583b7ea | 2006-06-21 00:30:21 -0700 | [diff] [blame] | 85 | char abort_msg[] = "aborting due to possible repository " |
| 86 | "corruption on the remote side."; |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 87 | int buffered = -1; |
Junio C Hamano | 1456b04 | 2009-12-10 12:17:11 -0800 | [diff] [blame] | 88 | ssize_t sz; |
Nguyễn Thái Ngọc Duy | cdab485 | 2013-08-16 16:52:05 +0700 | [diff] [blame] | 89 | const char *argv[12]; |
| 90 | int i, arg = 0; |
| 91 | FILE *pipe_fd; |
Linus Torvalds | 75bfc6c | 2005-07-04 16:35:13 -0700 | [diff] [blame] | 92 | |
Nguyễn Thái Ngọc Duy | cdab485 | 2013-08-16 16:52:05 +0700 | [diff] [blame] | 93 | if (shallow_nr) { |
Nguyễn Thái Ngọc Duy | cdab485 | 2013-08-16 16:52:05 +0700 | [diff] [blame] | 94 | argv[arg++] = "--shallow-file"; |
Nguyễn Thái Ngọc Duy | b790e0f | 2014-03-11 19:59:46 +0700 | [diff] [blame] | 95 | argv[arg++] = ""; |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 96 | } |
Nguyễn Thái Ngọc Duy | cdab485 | 2013-08-16 16:52:05 +0700 | [diff] [blame] | 97 | argv[arg++] = "pack-objects"; |
| 98 | argv[arg++] = "--revs"; |
| 99 | if (use_thin_pack) |
| 100 | argv[arg++] = "--thin"; |
Linus Torvalds | 75bfc6c | 2005-07-04 16:35:13 -0700 | [diff] [blame] | 101 | |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 102 | argv[arg++] = "--stdout"; |
| 103 | if (!no_progress) |
| 104 | argv[arg++] = "--progress"; |
| 105 | if (use_ofs_delta) |
| 106 | argv[arg++] = "--delta-base-offset"; |
Shawn O. Pearce | 348e390 | 2008-03-03 22:27:33 -0500 | [diff] [blame] | 107 | if (use_include_tag) |
| 108 | argv[arg++] = "--include-tag"; |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 109 | argv[arg++] = NULL; |
| 110 | |
Jeff King | b961219 | 2011-04-06 17:33:33 -0400 | [diff] [blame] | 111 | pack_objects.in = -1; |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 112 | pack_objects.out = -1; |
| 113 | pack_objects.err = -1; |
| 114 | pack_objects.git_cmd = 1; |
| 115 | pack_objects.argv = argv; |
Johannes Sixt | 21edd3f | 2007-10-19 21:48:03 +0200 | [diff] [blame] | 116 | |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 117 | if (start_command(&pack_objects)) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 118 | die("git upload-pack: unable to fork git-pack-objects"); |
Johannes Schindelin | 83a5ad6 | 2007-02-20 03:01:44 +0100 | [diff] [blame] | 119 | |
Nguyễn Thái Ngọc Duy | cdab485 | 2013-08-16 16:52:05 +0700 | [diff] [blame] | 120 | pipe_fd = xfdopen(pack_objects.in, "w"); |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 121 | |
Nguyễn Thái Ngọc Duy | b790e0f | 2014-03-11 19:59:46 +0700 | [diff] [blame] | 122 | if (shallow_nr) |
| 123 | for_each_commit_graft(write_one_shallow, pipe_fd); |
| 124 | |
Nguyễn Thái Ngọc Duy | cdab485 | 2013-08-16 16:52:05 +0700 | [diff] [blame] | 125 | for (i = 0; i < want_obj.nr; i++) |
| 126 | fprintf(pipe_fd, "%s\n", |
| 127 | sha1_to_hex(want_obj.objects[i].item->sha1)); |
| 128 | fprintf(pipe_fd, "--not\n"); |
| 129 | for (i = 0; i < have_obj.nr; i++) |
| 130 | fprintf(pipe_fd, "%s\n", |
| 131 | sha1_to_hex(have_obj.objects[i].item->sha1)); |
| 132 | for (i = 0; i < extra_edge_obj.nr; i++) |
| 133 | fprintf(pipe_fd, "%s\n", |
| 134 | sha1_to_hex(extra_edge_obj.objects[i].item->sha1)); |
| 135 | fprintf(pipe_fd, "\n"); |
| 136 | fflush(pipe_fd); |
| 137 | fclose(pipe_fd); |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 138 | |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 139 | /* We read from pack_objects.err to capture stderr output for |
| 140 | * progress bar, and pack_objects.out to capture the pack data. |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 141 | */ |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 142 | |
| 143 | while (1) { |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 144 | struct pollfd pfd[2]; |
Junio C Hamano | 363b781 | 2006-06-20 22:48:23 -0700 | [diff] [blame] | 145 | int pe, pu, pollsize; |
Jeff King | 05e9515 | 2013-09-08 05:01:31 -0400 | [diff] [blame] | 146 | int ret; |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 147 | |
Matthias Lederhofer | 0d516ad | 2006-07-18 19:14:51 +0200 | [diff] [blame] | 148 | reset_timeout(); |
| 149 | |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 150 | pollsize = 0; |
Junio C Hamano | 363b781 | 2006-06-20 22:48:23 -0700 | [diff] [blame] | 151 | pe = pu = -1; |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 152 | |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 153 | if (0 <= pack_objects.out) { |
| 154 | pfd[pollsize].fd = pack_objects.out; |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 155 | pfd[pollsize].events = POLLIN; |
| 156 | pu = pollsize; |
| 157 | pollsize++; |
| 158 | } |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 159 | if (0 <= pack_objects.err) { |
| 160 | pfd[pollsize].fd = pack_objects.err; |
Junio C Hamano | 363b781 | 2006-06-20 22:48:23 -0700 | [diff] [blame] | 161 | pfd[pollsize].events = POLLIN; |
| 162 | pe = pollsize; |
| 163 | pollsize++; |
| 164 | } |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 165 | |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 166 | if (!pollsize) |
| 167 | break; |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 168 | |
Edward Thomson | 6c71f8b | 2014-08-22 15:19:11 +0000 | [diff] [blame] | 169 | ret = poll(pfd, pollsize, |
| 170 | keepalive < 0 ? -1 : 1000 * keepalive); |
| 171 | |
Jeff King | 05e9515 | 2013-09-08 05:01:31 -0400 | [diff] [blame] | 172 | if (ret < 0) { |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 173 | if (errno != EINTR) { |
| 174 | error("poll failed, resuming: %s", |
| 175 | strerror(errno)); |
| 176 | sleep(1); |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 177 | } |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 178 | continue; |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 179 | } |
Nicolas Pitre | 6b59f51 | 2009-11-11 17:24:42 -0500 | [diff] [blame] | 180 | if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) { |
| 181 | /* Status ready; we ship that in the side-band |
| 182 | * or dump to the standard error. |
| 183 | */ |
| 184 | sz = xread(pack_objects.err, progress, |
| 185 | sizeof(progress)); |
| 186 | if (0 < sz) |
| 187 | send_client_data(2, progress, sz); |
| 188 | else if (sz == 0) { |
| 189 | close(pack_objects.err); |
| 190 | pack_objects.err = -1; |
| 191 | } |
| 192 | else |
| 193 | goto fail; |
| 194 | /* give priority to status messages */ |
| 195 | continue; |
| 196 | } |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 197 | if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) { |
| 198 | /* Data ready; we keep the last byte to ourselves |
| 199 | * in case we detect broken rev-list, so that we |
| 200 | * can leave the stream corrupted. This is |
| 201 | * unfortunate -- unpack-objects would happily |
| 202 | * accept a valid packdata with trailing garbage, |
| 203 | * so appending garbage after we pass all the |
| 204 | * pack data is not good enough to signal |
| 205 | * breakage to downstream. |
| 206 | */ |
| 207 | char *cp = data; |
| 208 | ssize_t outsz = 0; |
| 209 | if (0 <= buffered) { |
| 210 | *cp++ = buffered; |
| 211 | outsz++; |
| 212 | } |
| 213 | sz = xread(pack_objects.out, cp, |
| 214 | sizeof(data) - outsz); |
| 215 | if (0 < sz) |
Junio C Hamano | 1456b04 | 2009-12-10 12:17:11 -0800 | [diff] [blame] | 216 | ; |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 217 | else if (sz == 0) { |
| 218 | close(pack_objects.out); |
| 219 | pack_objects.out = -1; |
| 220 | } |
| 221 | else |
| 222 | goto fail; |
| 223 | sz += outsz; |
| 224 | if (1 < sz) { |
| 225 | buffered = data[sz-1] & 0xFF; |
| 226 | sz--; |
| 227 | } |
| 228 | else |
| 229 | buffered = -1; |
| 230 | sz = send_client_data(1, data, sz); |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 231 | if (sz < 0) |
| 232 | goto fail; |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 233 | } |
Jeff King | 05e9515 | 2013-09-08 05:01:31 -0400 | [diff] [blame] | 234 | |
| 235 | /* |
| 236 | * We hit the keepalive timeout without saying anything; send |
| 237 | * an empty message on the data sideband just to let the other |
| 238 | * side know we're still working on it, but don't have any data |
| 239 | * yet. |
| 240 | * |
| 241 | * If we don't have a sideband channel, there's no room in the |
| 242 | * protocol to say anything, so those clients are just out of |
| 243 | * luck. |
| 244 | */ |
| 245 | if (!ret && use_sideband) { |
| 246 | static const char buf[] = "0005\1"; |
| 247 | write_or_die(1, buf, 5); |
| 248 | } |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 249 | } |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 250 | |
| 251 | if (finish_command(&pack_objects)) { |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 252 | error("git upload-pack: git-pack-objects died with error."); |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 253 | goto fail; |
| 254 | } |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 255 | |
| 256 | /* flush the data */ |
| 257 | if (0 <= buffered) { |
| 258 | data[0] = buffered; |
| 259 | sz = send_client_data(1, data, 1); |
| 260 | if (sz < 0) |
| 261 | goto fail; |
| 262 | fprintf(stderr, "flushed.\n"); |
| 263 | } |
| 264 | if (use_sideband) |
| 265 | packet_flush(1); |
| 266 | return; |
| 267 | |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 268 | fail: |
Junio C Hamano | 583b7ea | 2006-06-21 00:30:21 -0700 | [diff] [blame] | 269 | send_client_data(3, abort_msg, sizeof(abort_msg)); |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 270 | die("git upload-pack: %s", abort_msg); |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 273 | static int got_sha1(char *hex, unsigned char *sha1) |
| 274 | { |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 275 | struct object *o; |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 276 | int we_knew_they_have = 0; |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 277 | |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 278 | if (get_sha1_hex(hex, sha1)) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 279 | die("git upload-pack: expected SHA1 object, got '%s'", hex); |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 280 | if (!has_sha1_file(sha1)) |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 281 | return -1; |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 282 | |
Jeff King | a6eec12 | 2013-03-16 06:25:25 -0400 | [diff] [blame] | 283 | o = parse_object(sha1); |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 284 | if (!o) |
| 285 | die("oops (%s)", sha1_to_hex(sha1)); |
Junio C Hamano | 182a8da | 2006-08-12 22:16:51 -0700 | [diff] [blame] | 286 | if (o->type == OBJ_COMMIT) { |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 287 | struct commit_list *parents; |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 288 | struct commit *commit = (struct commit *)o; |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 289 | if (o->flags & THEY_HAVE) |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 290 | we_knew_they_have = 1; |
| 291 | else |
| 292 | o->flags |= THEY_HAVE; |
| 293 | if (!oldest_have || (commit->date < oldest_have)) |
| 294 | oldest_have = commit->date; |
| 295 | for (parents = commit->parents; |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 296 | parents; |
| 297 | parents = parents->next) |
| 298 | parents->item->object.flags |= THEY_HAVE; |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 299 | } |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 300 | if (!we_knew_they_have) { |
| 301 | add_object_array(o, NULL, &have_obj); |
| 302 | return 1; |
| 303 | } |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static int reachable(struct commit *want) |
| 308 | { |
| 309 | struct commit_list *work = NULL; |
| 310 | |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 311 | commit_list_insert_by_date(want, &work); |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 312 | while (work) { |
| 313 | struct commit_list *list = work->next; |
| 314 | struct commit *commit = work->item; |
| 315 | free(work); |
| 316 | work = list; |
| 317 | |
| 318 | if (commit->object.flags & THEY_HAVE) { |
| 319 | want->object.flags |= COMMON_KNOWN; |
| 320 | break; |
| 321 | } |
| 322 | if (!commit->object.parsed) |
| 323 | parse_object(commit->object.sha1); |
| 324 | if (commit->object.flags & REACHABLE) |
| 325 | continue; |
| 326 | commit->object.flags |= REACHABLE; |
| 327 | if (commit->date < oldest_have) |
| 328 | continue; |
| 329 | for (list = commit->parents; list; list = list->next) { |
| 330 | struct commit *parent = list->item; |
| 331 | if (!(parent->object.flags & REACHABLE)) |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 332 | commit_list_insert_by_date(parent, &work); |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | want->object.flags |= REACHABLE; |
| 336 | clear_commit_marks(want, REACHABLE); |
| 337 | free_commit_list(work); |
| 338 | return (want->object.flags & COMMON_KNOWN); |
| 339 | } |
| 340 | |
| 341 | static int ok_to_give_up(void) |
| 342 | { |
| 343 | int i; |
| 344 | |
| 345 | if (!have_obj.nr) |
| 346 | return 0; |
| 347 | |
| 348 | for (i = 0; i < want_obj.nr; i++) { |
| 349 | struct object *want = want_obj.objects[i].item; |
| 350 | |
| 351 | if (want->flags & COMMON_KNOWN) |
| 352 | continue; |
| 353 | want = deref_tag(want, "a want line", 0); |
| 354 | if (!want || want->type != OBJ_COMMIT) { |
| 355 | /* no way to tell if this is reachable by |
| 356 | * looking at the ancestry chain alone, so |
| 357 | * leave a note to ourselves not to worry about |
| 358 | * this object anymore. |
| 359 | */ |
| 360 | want_obj.objects[i].item->flags |= COMMON_KNOWN; |
| 361 | continue; |
| 362 | } |
| 363 | if (!reachable((struct commit *)want)) |
| 364 | return 0; |
| 365 | } |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 366 | return 1; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static int get_common_commits(void) |
| 370 | { |
Junio C Hamano | c04c4e5 | 2006-07-05 18:12:12 -0700 | [diff] [blame] | 371 | unsigned char sha1[20]; |
Shawn O. Pearce | 78affc4 | 2009-10-30 17:47:25 -0700 | [diff] [blame] | 372 | char last_hex[41]; |
Shawn O. Pearce | 49bee71 | 2011-03-14 16:48:39 -0700 | [diff] [blame] | 373 | int got_common = 0; |
| 374 | int got_other = 0; |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 375 | int sent_ready = 0; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 376 | |
Johannes Schindelin | f0243f2 | 2005-10-28 04:48:32 +0200 | [diff] [blame] | 377 | save_commit_buffer = 0; |
| 378 | |
Brian Gianforcaro | eeefa7c | 2009-09-01 01:35:10 -0400 | [diff] [blame] | 379 | for (;;) { |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 380 | char *line = packet_read_line(0, NULL); |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 381 | reset_timeout(); |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 382 | |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 383 | if (!line) { |
Shawn O. Pearce | 49bee71 | 2011-03-14 16:48:39 -0700 | [diff] [blame] | 384 | if (multi_ack == 2 && got_common |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 385 | && !got_other && ok_to_give_up()) { |
| 386 | sent_ready = 1; |
Shawn O. Pearce | 49bee71 | 2011-03-14 16:48:39 -0700 | [diff] [blame] | 387 | packet_write(1, "ACK %s ready\n", last_hex); |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 388 | } |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 389 | if (have_obj.nr == 0 || multi_ack) |
Johannes Schindelin | 1bd8c8f | 2005-10-28 04:49:16 +0200 | [diff] [blame] | 390 | packet_write(1, "NAK\n"); |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 391 | |
| 392 | if (no_done && sent_ready) { |
| 393 | packet_write(1, "ACK %s\n", last_hex); |
| 394 | return 0; |
| 395 | } |
Shawn O. Pearce | 42526b4 | 2009-10-30 17:47:33 -0700 | [diff] [blame] | 396 | if (stateless_rpc) |
| 397 | exit(0); |
Shawn O. Pearce | 49bee71 | 2011-03-14 16:48:39 -0700 | [diff] [blame] | 398 | got_common = 0; |
| 399 | got_other = 0; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 400 | continue; |
| 401 | } |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 402 | if (starts_with(line, "have ")) { |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 403 | switch (got_sha1(line+5, sha1)) { |
| 404 | case -1: /* they have what we do not */ |
Shawn O. Pearce | 49bee71 | 2011-03-14 16:48:39 -0700 | [diff] [blame] | 405 | got_other = 1; |
Shawn O. Pearce | 78affc4 | 2009-10-30 17:47:25 -0700 | [diff] [blame] | 406 | if (multi_ack && ok_to_give_up()) { |
| 407 | const char *hex = sha1_to_hex(sha1); |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 408 | if (multi_ack == 2) { |
| 409 | sent_ready = 1; |
Shawn O. Pearce | 78affc4 | 2009-10-30 17:47:25 -0700 | [diff] [blame] | 410 | packet_write(1, "ACK %s ready\n", hex); |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 411 | } else |
Shawn O. Pearce | 78affc4 | 2009-10-30 17:47:25 -0700 | [diff] [blame] | 412 | packet_write(1, "ACK %s continue\n", hex); |
| 413 | } |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 414 | break; |
| 415 | default: |
Shawn O. Pearce | 49bee71 | 2011-03-14 16:48:39 -0700 | [diff] [blame] | 416 | got_common = 1; |
Shawn O. Pearce | 78affc4 | 2009-10-30 17:47:25 -0700 | [diff] [blame] | 417 | memcpy(last_hex, sha1_to_hex(sha1), 41); |
| 418 | if (multi_ack == 2) |
| 419 | packet_write(1, "ACK %s common\n", last_hex); |
| 420 | else if (multi_ack) |
| 421 | packet_write(1, "ACK %s continue\n", last_hex); |
Junio C Hamano | c04c4e5 | 2006-07-05 18:12:12 -0700 | [diff] [blame] | 422 | else if (have_obj.nr == 1) |
Shawn O. Pearce | 78affc4 | 2009-10-30 17:47:25 -0700 | [diff] [blame] | 423 | packet_write(1, "ACK %s\n", last_hex); |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 424 | break; |
Junio C Hamano | af2d3aa | 2005-10-25 14:55:24 -0700 | [diff] [blame] | 425 | } |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 426 | continue; |
| 427 | } |
| 428 | if (!strcmp(line, "done")) { |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 429 | if (have_obj.nr > 0) { |
Johannes Schindelin | 1bd8c8f | 2005-10-28 04:49:16 +0200 | [diff] [blame] | 430 | if (multi_ack) |
Junio C Hamano | c04c4e5 | 2006-07-05 18:12:12 -0700 | [diff] [blame] | 431 | packet_write(1, "ACK %s\n", last_hex); |
Johannes Schindelin | 1bd8c8f | 2005-10-28 04:49:16 +0200 | [diff] [blame] | 432 | return 0; |
| 433 | } |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 434 | packet_write(1, "NAK\n"); |
| 435 | return -1; |
| 436 | } |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 437 | die("git upload-pack: expected SHA1 list, got '%s'", line); |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 438 | } |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 441 | static int is_our_ref(struct object *o) |
| 442 | { |
| 443 | return o->flags & |
| 444 | ((allow_tip_sha1_in_want ? HIDDEN_REF : 0) | OUR_REF); |
| 445 | } |
| 446 | |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 447 | static void check_non_tip(void) |
| 448 | { |
| 449 | static const char *argv[] = { |
| 450 | "rev-list", "--stdin", NULL, |
| 451 | }; |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 452 | static struct child_process cmd = CHILD_PROCESS_INIT; |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 453 | struct object *o; |
| 454 | char namebuf[42]; /* ^ + SHA-1 + LF */ |
| 455 | int i; |
| 456 | |
| 457 | /* In the normal in-process case non-tip request can never happen */ |
| 458 | if (!stateless_rpc) |
| 459 | goto error; |
| 460 | |
| 461 | cmd.argv = argv; |
| 462 | cmd.git_cmd = 1; |
| 463 | cmd.no_stderr = 1; |
| 464 | cmd.in = -1; |
| 465 | cmd.out = -1; |
| 466 | |
| 467 | if (start_command(&cmd)) |
| 468 | goto error; |
| 469 | |
| 470 | /* |
| 471 | * If rev-list --stdin encounters an unknown commit, it |
| 472 | * terminates, which will cause SIGPIPE in the write loop |
| 473 | * below. |
| 474 | */ |
| 475 | sigchain_push(SIGPIPE, SIG_IGN); |
| 476 | |
| 477 | namebuf[0] = '^'; |
| 478 | namebuf[41] = '\n'; |
| 479 | for (i = get_max_object_index(); 0 < i; ) { |
| 480 | o = get_indexed_object(--i); |
Brian Harring | 2a74532 | 2011-08-23 22:47:17 -0700 | [diff] [blame] | 481 | if (!o) |
| 482 | continue; |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 483 | if (!is_our_ref(o)) |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 484 | continue; |
| 485 | memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40); |
| 486 | if (write_in_full(cmd.in, namebuf, 42) < 0) |
| 487 | goto error; |
| 488 | } |
| 489 | namebuf[40] = '\n'; |
| 490 | for (i = 0; i < want_obj.nr; i++) { |
| 491 | o = want_obj.objects[i].item; |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 492 | if (is_our_ref(o)) |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 493 | continue; |
| 494 | memcpy(namebuf, sha1_to_hex(o->sha1), 40); |
| 495 | if (write_in_full(cmd.in, namebuf, 41) < 0) |
| 496 | goto error; |
| 497 | } |
| 498 | close(cmd.in); |
| 499 | |
| 500 | sigchain_pop(SIGPIPE); |
| 501 | |
| 502 | /* |
| 503 | * The commits out of the rev-list are not ancestors of |
| 504 | * our ref. |
| 505 | */ |
| 506 | i = read_in_full(cmd.out, namebuf, 1); |
| 507 | if (i) |
| 508 | goto error; |
| 509 | close(cmd.out); |
| 510 | |
| 511 | /* |
| 512 | * rev-list may have died by encountering a bad commit |
| 513 | * in the history, in which case we do want to bail out |
| 514 | * even when it showed no commit. |
| 515 | */ |
| 516 | if (finish_command(&cmd)) |
| 517 | goto error; |
| 518 | |
| 519 | /* All the non-tip ones are ancestors of what we advertised */ |
| 520 | return; |
| 521 | |
| 522 | error: |
| 523 | /* Pick one of them (we know there at least is one) */ |
| 524 | for (i = 0; i < want_obj.nr; i++) { |
| 525 | o = want_obj.objects[i].item; |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 526 | if (!is_our_ref(o)) |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 527 | die("git upload-pack: not our ref %s", |
| 528 | sha1_to_hex(o->sha1)); |
| 529 | } |
| 530 | } |
| 531 | |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 532 | static void receive_needs(void) |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 533 | { |
Thiago Farina | 3cd4745 | 2010-08-28 23:04:17 -0300 | [diff] [blame] | 534 | struct object_array shallows = OBJECT_ARRAY_INIT; |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 535 | int depth = 0; |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 536 | int has_non_tip = 0; |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 537 | |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 538 | shallow_nr = 0; |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 539 | for (;;) { |
Junio C Hamano | 565ebbf | 2005-10-24 18:59:18 -0700 | [diff] [blame] | 540 | struct object *o; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 541 | const char *features; |
Junio C Hamano | 6ece0d3 | 2006-07-05 17:41:39 -0700 | [diff] [blame] | 542 | unsigned char sha1_buf[20]; |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 543 | char *line = packet_read_line(0, NULL); |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 544 | reset_timeout(); |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 545 | if (!line) |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 546 | break; |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 547 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 548 | if (starts_with(line, "shallow ")) { |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 549 | unsigned char sha1[20]; |
| 550 | struct object *object; |
Jeff King | b7b0217 | 2013-02-20 14:53:33 -0500 | [diff] [blame] | 551 | if (get_sha1_hex(line + 8, sha1)) |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 552 | die("invalid shallow line: %s", line); |
| 553 | object = parse_object(sha1); |
| 554 | if (!object) |
Michael Heemskerk | af04fa2 | 2013-04-28 22:32:04 -0700 | [diff] [blame] | 555 | continue; |
Nguyễn Thái Ngọc Duy | 6293ded | 2013-01-08 18:32:36 +0700 | [diff] [blame] | 556 | if (object->type != OBJ_COMMIT) |
| 557 | die("invalid shallow object %s", sha1_to_hex(sha1)); |
Jeff King | e58e57e | 2013-02-20 14:54:57 -0500 | [diff] [blame] | 558 | if (!(object->flags & CLIENT_SHALLOW)) { |
| 559 | object->flags |= CLIENT_SHALLOW; |
| 560 | add_object_array(object, NULL, &shallows); |
| 561 | } |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 562 | continue; |
| 563 | } |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 564 | if (starts_with(line, "deepen ")) { |
Johannes Schindelin | 016e6cc | 2006-10-30 20:09:29 +0100 | [diff] [blame] | 565 | char *end; |
| 566 | depth = strtol(line + 7, &end, 0); |
| 567 | if (end == line + 7 || depth <= 0) |
| 568 | die("Invalid deepen: %s", line); |
| 569 | continue; |
| 570 | } |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 571 | if (!starts_with(line, "want ") || |
Junio C Hamano | 6ece0d3 | 2006-07-05 17:41:39 -0700 | [diff] [blame] | 572 | get_sha1_hex(line+5, sha1_buf)) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 573 | die("git upload-pack: protocol error, " |
Junio C Hamano | e091eb9 | 2005-10-05 14:49:54 -0700 | [diff] [blame] | 574 | "expected to get sha, not '%s'", line); |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 575 | |
| 576 | features = line + 45; |
| 577 | |
| 578 | if (parse_feature_request(features, "multi_ack_detailed")) |
Shawn O. Pearce | 78affc4 | 2009-10-30 17:47:25 -0700 | [diff] [blame] | 579 | multi_ack = 2; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 580 | else if (parse_feature_request(features, "multi_ack")) |
Johannes Schindelin | 1bd8c8f | 2005-10-28 04:49:16 +0200 | [diff] [blame] | 581 | multi_ack = 1; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 582 | if (parse_feature_request(features, "no-done")) |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 583 | no_done = 1; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 584 | if (parse_feature_request(features, "thin-pack")) |
Junio C Hamano | b19696c | 2006-02-20 00:38:39 -0800 | [diff] [blame] | 585 | use_thin_pack = 1; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 586 | if (parse_feature_request(features, "ofs-delta")) |
Nicolas Pitre | e4fe4b8 | 2006-09-26 11:27:39 -0400 | [diff] [blame] | 587 | use_ofs_delta = 1; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 588 | if (parse_feature_request(features, "side-band-64k")) |
Junio C Hamano | d47f3db | 2006-09-10 16:27:08 -0700 | [diff] [blame] | 589 | use_sideband = LARGE_PACKET_MAX; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 590 | else if (parse_feature_request(features, "side-band")) |
Junio C Hamano | d47f3db | 2006-09-10 16:27:08 -0700 | [diff] [blame] | 591 | use_sideband = DEFAULT_PACKET_MAX; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 592 | if (parse_feature_request(features, "no-progress")) |
Johannes Schindelin | b0e9089 | 2007-02-23 20:03:10 +0100 | [diff] [blame] | 593 | no_progress = 1; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 594 | if (parse_feature_request(features, "include-tag")) |
Shawn O. Pearce | 348e390 | 2008-03-03 22:27:33 -0500 | [diff] [blame] | 595 | use_include_tag = 1; |
Junio C Hamano | 565ebbf | 2005-10-24 18:59:18 -0700 | [diff] [blame] | 596 | |
Jeff King | f59de5d | 2013-03-16 06:28:30 -0400 | [diff] [blame] | 597 | o = parse_object(sha1_buf); |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 598 | if (!o) |
Elijah Newren | 9f9aa76 | 2010-07-31 14:11:46 -0600 | [diff] [blame] | 599 | die("git upload-pack: not our ref %s", |
| 600 | sha1_to_hex(sha1_buf)); |
Junio C Hamano | 565ebbf | 2005-10-24 18:59:18 -0700 | [diff] [blame] | 601 | if (!(o->flags & WANTED)) { |
| 602 | o->flags |= WANTED; |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 603 | if (!is_our_ref(o)) |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 604 | has_non_tip = 1; |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 605 | add_object_array(o, NULL, &want_obj); |
Junio C Hamano | 565ebbf | 2005-10-24 18:59:18 -0700 | [diff] [blame] | 606 | } |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 607 | } |
Johannes Sixt | 9462e3f | 2009-06-16 20:41:16 +0200 | [diff] [blame] | 608 | |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 609 | /* |
| 610 | * We have sent all our refs already, and the other end |
| 611 | * should have chosen out of them. When we are operating |
| 612 | * in the stateless RPC mode, however, their choice may |
| 613 | * have been based on the set of older refs advertised |
| 614 | * by another process that handled the initial request. |
| 615 | */ |
| 616 | if (has_non_tip) |
| 617 | check_non_tip(); |
| 618 | |
Johannes Sixt | 9462e3f | 2009-06-16 20:41:16 +0200 | [diff] [blame] | 619 | if (!use_sideband && daemon_mode) |
| 620 | no_progress = 1; |
| 621 | |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 622 | if (depth == 0 && shallows.nr == 0) |
| 623 | return; |
Johannes Schindelin | 016e6cc | 2006-10-30 20:09:29 +0100 | [diff] [blame] | 624 | if (depth > 0) { |
Nguyễn Thái Ngọc Duy | 4dcb167 | 2013-01-11 16:05:46 +0700 | [diff] [blame] | 625 | struct commit_list *result = NULL, *backup = NULL; |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 626 | int i; |
Nguyễn Thái Ngọc Duy | 79d3a23 | 2013-12-05 20:02:41 +0700 | [diff] [blame] | 627 | if (depth == INFINITE_DEPTH && !is_repository_shallow()) |
Nguyễn Thái Ngọc Duy | 4dcb167 | 2013-01-11 16:05:46 +0700 | [diff] [blame] | 628 | for (i = 0; i < shallows.nr; i++) { |
| 629 | struct object *object = shallows.objects[i].item; |
| 630 | object->flags |= NOT_SHALLOW; |
| 631 | } |
| 632 | else |
| 633 | backup = result = |
| 634 | get_shallow_commits(&want_obj, depth, |
| 635 | SHALLOW, NOT_SHALLOW); |
Johannes Schindelin | 016e6cc | 2006-10-30 20:09:29 +0100 | [diff] [blame] | 636 | while (result) { |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 637 | struct object *object = &result->item->object; |
Alexandre Julliard | 1f2de76 | 2006-11-24 15:58:25 +0100 | [diff] [blame] | 638 | if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) { |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 639 | packet_write(1, "shallow %s", |
| 640 | sha1_to_hex(object->sha1)); |
| 641 | register_shallow(object->sha1); |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 642 | shallow_nr++; |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 643 | } |
Johannes Schindelin | 016e6cc | 2006-10-30 20:09:29 +0100 | [diff] [blame] | 644 | result = result->next; |
| 645 | } |
| 646 | free_commit_list(backup); |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 647 | for (i = 0; i < shallows.nr; i++) { |
| 648 | struct object *object = shallows.objects[i].item; |
| 649 | if (object->flags & NOT_SHALLOW) { |
| 650 | struct commit_list *parents; |
| 651 | packet_write(1, "unshallow %s", |
| 652 | sha1_to_hex(object->sha1)); |
| 653 | object->flags &= ~CLIENT_SHALLOW; |
| 654 | /* make sure the real parents are parsed */ |
| 655 | unregister_shallow(object->sha1); |
Junio C Hamano | 176d45c | 2006-11-13 22:47:46 -0800 | [diff] [blame] | 656 | object->parsed = 0; |
Jeff King | 367068e | 2013-10-24 04:54:01 -0400 | [diff] [blame] | 657 | parse_commit_or_die((struct commit *)object); |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 658 | parents = ((struct commit *)object)->parents; |
| 659 | while (parents) { |
| 660 | add_object_array(&parents->item->object, |
| 661 | NULL, &want_obj); |
| 662 | parents = parents->next; |
| 663 | } |
Nicolas Pitre | 6523078 | 2009-09-03 19:08:33 -0400 | [diff] [blame] | 664 | add_object_array(object, NULL, &extra_edge_obj); |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 665 | } |
| 666 | /* make sure commit traversal conforms to client */ |
| 667 | register_shallow(object->sha1); |
| 668 | } |
| 669 | packet_flush(1); |
| 670 | } else |
| 671 | if (shallows.nr > 0) { |
| 672 | int i; |
| 673 | for (i = 0; i < shallows.nr; i++) |
| 674 | register_shallow(shallows.objects[i].item->sha1); |
| 675 | } |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 676 | |
| 677 | shallow_nr += shallows.nr; |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 678 | free(shallows.objects); |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 681 | /* return non-zero if the ref is hidden, otherwise 0 */ |
Junio C Hamano | cbbe50d | 2013-01-18 15:48:49 -0800 | [diff] [blame] | 682 | static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
| 683 | { |
| 684 | struct object *o = lookup_unknown_object(sha1); |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 685 | |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 686 | if (ref_is_hidden(refname)) { |
| 687 | o->flags |= HIDDEN_REF; |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 688 | return 1; |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 689 | } |
Junio C Hamano | cbbe50d | 2013-01-18 15:48:49 -0800 | [diff] [blame] | 690 | if (!o) |
| 691 | die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1)); |
Junio C Hamano | 3f1da57 | 2013-01-28 20:45:43 -0800 | [diff] [blame] | 692 | o->flags |= OUR_REF; |
Junio C Hamano | cbbe50d | 2013-01-18 15:48:49 -0800 | [diff] [blame] | 693 | return 0; |
| 694 | } |
| 695 | |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 696 | static void format_symref_info(struct strbuf *buf, struct string_list *symref) |
| 697 | { |
| 698 | struct string_list_item *item; |
| 699 | |
| 700 | if (!symref->nr) |
| 701 | return; |
| 702 | for_each_string_list_item(item, symref) |
| 703 | strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util); |
| 704 | } |
| 705 | |
Junio C Hamano | 8da1977 | 2006-09-20 22:02:01 -0700 | [diff] [blame] | 706 | static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 707 | { |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 708 | static const char *capabilities = "multi_ack thin-pack side-band" |
Shawn O. Pearce | 348e390 | 2008-03-03 22:27:33 -0500 | [diff] [blame] | 709 | " side-band-64k ofs-delta shallow no-progress" |
Shawn O. Pearce | 78affc4 | 2009-10-30 17:47:25 -0700 | [diff] [blame] | 710 | " include-tag multi_ack_detailed"; |
Josh Triplett | 6b01ecf | 2011-07-08 16:13:32 -0700 | [diff] [blame] | 711 | const char *refname_nons = strip_namespace(refname); |
Jeff King | 435c833 | 2012-10-04 04:03:33 -0400 | [diff] [blame] | 712 | unsigned char peeled[20]; |
Carl Worth | b5b1699 | 2006-02-17 16:14:52 -0800 | [diff] [blame] | 713 | |
Junio C Hamano | a4d695d | 2013-09-17 16:03:32 -0700 | [diff] [blame] | 714 | if (mark_our_ref(refname, sha1, flag, NULL)) |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 715 | return 0; |
Junio C Hamano | cbbe50d | 2013-01-18 15:48:49 -0800 | [diff] [blame] | 716 | |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 717 | if (capabilities) { |
| 718 | struct strbuf symref_info = STRBUF_INIT; |
| 719 | |
| 720 | format_symref_info(&symref_info, cb_data); |
| 721 | packet_write(1, "%s %s%c%s%s%s%s agent=%s\n", |
Jeff King | ff5effd | 2012-08-03 12:19:16 -0400 | [diff] [blame] | 722 | sha1_to_hex(sha1), refname_nons, |
Junio C Hamano | cf2ad8e | 2011-03-29 10:24:59 -0700 | [diff] [blame] | 723 | 0, capabilities, |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 724 | allow_tip_sha1_in_want ? " allow-tip-sha1-in-want" : "", |
Jeff King | ff5effd | 2012-08-03 12:19:16 -0400 | [diff] [blame] | 725 | stateless_rpc ? " no-done" : "", |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 726 | symref_info.buf, |
Jeff King | ff5effd | 2012-08-03 12:19:16 -0400 | [diff] [blame] | 727 | git_user_agent_sanitized()); |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 728 | strbuf_release(&symref_info); |
| 729 | } else { |
Josh Triplett | 6b01ecf | 2011-07-08 16:13:32 -0700 | [diff] [blame] | 730 | packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons); |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 731 | } |
Johannes Schindelin | 1f5881b | 2005-10-28 05:56:41 +0200 | [diff] [blame] | 732 | capabilities = NULL; |
Jeff King | 435c833 | 2012-10-04 04:03:33 -0400 | [diff] [blame] | 733 | if (!peel_ref(refname, peeled)) |
| 734 | packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons); |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 735 | return 0; |
| 736 | } |
| 737 | |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 738 | static int find_symref(const char *refname, const unsigned char *sha1, int flag, |
| 739 | void *cb_data) |
| 740 | { |
| 741 | const char *symref_target; |
| 742 | struct string_list_item *item; |
| 743 | unsigned char unused[20]; |
| 744 | |
| 745 | if ((flag & REF_ISSYMREF) == 0) |
| 746 | return 0; |
Ronnie Sahlberg | 7695d11 | 2014-07-15 12:59:36 -0700 | [diff] [blame] | 747 | symref_target = resolve_ref_unsafe(refname, 0, unused, &flag); |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 748 | if (!symref_target || (flag & REF_ISSYMREF) == 0) |
| 749 | die("'%s' is a symref but it is not?", refname); |
| 750 | item = string_list_append(cb_data, refname); |
| 751 | item->util = xstrdup(symref_target); |
| 752 | return 0; |
| 753 | } |
| 754 | |
David Rientjes | 59076eb | 2006-08-14 13:40:51 -0700 | [diff] [blame] | 755 | static void upload_pack(void) |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 756 | { |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 757 | struct string_list symref = STRING_LIST_INIT_DUP; |
| 758 | |
| 759 | head_ref_namespaced(find_symref, &symref); |
| 760 | |
Shawn O. Pearce | 42526b4 | 2009-10-30 17:47:33 -0700 | [diff] [blame] | 761 | if (advertise_refs || !stateless_rpc) { |
| 762 | reset_timeout(); |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 763 | head_ref_namespaced(send_ref, &symref); |
| 764 | for_each_namespaced_ref(send_ref, &symref); |
Nguyễn Thái Ngọc Duy | ad49136 | 2013-12-05 20:02:32 +0700 | [diff] [blame] | 765 | advertise_shallow_grafts(1); |
Shawn O. Pearce | 42526b4 | 2009-10-30 17:47:33 -0700 | [diff] [blame] | 766 | packet_flush(1); |
| 767 | } else { |
Josh Triplett | 6b01ecf | 2011-07-08 16:13:32 -0700 | [diff] [blame] | 768 | head_ref_namespaced(mark_our_ref, NULL); |
| 769 | for_each_namespaced_ref(mark_our_ref, NULL); |
Shawn O. Pearce | 42526b4 | 2009-10-30 17:47:33 -0700 | [diff] [blame] | 770 | } |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 771 | string_list_clear(&symref, 1); |
Shawn O. Pearce | 42526b4 | 2009-10-30 17:47:33 -0700 | [diff] [blame] | 772 | if (advertise_refs) |
| 773 | return; |
| 774 | |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 775 | receive_needs(); |
David Rientjes | 59076eb | 2006-08-14 13:40:51 -0700 | [diff] [blame] | 776 | if (want_obj.nr) { |
| 777 | get_common_commits(); |
| 778 | create_pack_file(); |
| 779 | } |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 782 | static int upload_pack_config(const char *var, const char *value, void *unused) |
| 783 | { |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 784 | if (!strcmp("uploadpack.allowtipsha1inwant", var)) |
| 785 | allow_tip_sha1_in_want = git_config_bool(var, value); |
Jeff King | 05e9515 | 2013-09-08 05:01:31 -0400 | [diff] [blame] | 786 | else if (!strcmp("uploadpack.keepalive", var)) { |
| 787 | keepalive = git_config_int(var, value); |
| 788 | if (!keepalive) |
| 789 | keepalive = -1; |
| 790 | } |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 791 | return parse_hide_refs_config(var, value, "uploadpack"); |
| 792 | } |
| 793 | |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 794 | int main(int argc, char **argv) |
| 795 | { |
Andreas Ericsson | 8d63013 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 796 | char *dir; |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 797 | int i; |
| 798 | int strict = 0; |
| 799 | |
Ævar Arnfjörð Bjarmason | 5e9637c | 2011-11-18 00:14:42 +0100 | [diff] [blame] | 800 | git_setup_gettext(); |
| 801 | |
Jeff King | bbc30f9 | 2011-02-24 09:30:19 -0500 | [diff] [blame] | 802 | packet_trace_identity("upload-pack"); |
Steffen Prohaska | 2fb3f6d | 2009-01-18 13:00:12 +0100 | [diff] [blame] | 803 | git_extract_argv0_path(argv[0]); |
Michael Haggerty | afc711b | 2014-02-18 12:24:55 +0100 | [diff] [blame] | 804 | check_replace_refs = 0; |
Steffen Prohaska | 2fb3f6d | 2009-01-18 13:00:12 +0100 | [diff] [blame] | 805 | |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 806 | for (i = 1; i < argc; i++) { |
| 807 | char *arg = argv[i]; |
| 808 | |
| 809 | if (arg[0] != '-') |
| 810 | break; |
Shawn O. Pearce | 42526b4 | 2009-10-30 17:47:33 -0700 | [diff] [blame] | 811 | if (!strcmp(arg, "--advertise-refs")) { |
| 812 | advertise_refs = 1; |
| 813 | continue; |
| 814 | } |
| 815 | if (!strcmp(arg, "--stateless-rpc")) { |
| 816 | stateless_rpc = 1; |
| 817 | continue; |
| 818 | } |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 819 | if (!strcmp(arg, "--strict")) { |
| 820 | strict = 1; |
| 821 | continue; |
| 822 | } |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 823 | if (starts_with(arg, "--timeout=")) { |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 824 | timeout = atoi(arg+10); |
Johannes Sixt | 9462e3f | 2009-06-16 20:41:16 +0200 | [diff] [blame] | 825 | daemon_mode = 1; |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 826 | continue; |
| 827 | } |
| 828 | if (!strcmp(arg, "--")) { |
| 829 | i++; |
| 830 | break; |
| 831 | } |
| 832 | } |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 833 | |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 834 | if (i != argc-1) |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 835 | usage(upload_pack_usage); |
Johannes Sixt | 04b3305 | 2008-02-12 12:28:01 +0100 | [diff] [blame] | 836 | |
Johannes Sixt | e1464ca | 2008-07-21 21:19:52 +0200 | [diff] [blame] | 837 | setup_path(); |
Johannes Sixt | 04b3305 | 2008-02-12 12:28:01 +0100 | [diff] [blame] | 838 | |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 839 | dir = argv[i]; |
Linus Torvalds | 113b947 | 2005-07-08 16:22:22 -0700 | [diff] [blame] | 840 | |
Andreas Ericsson | 8d63013 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 841 | if (!enter_repo(dir, strict)) |
Jeff King | 05ac6b3 | 2009-03-04 03:32:29 -0500 | [diff] [blame] | 842 | die("'%s' does not appear to be a git repository", dir); |
Nguyễn Thái Ngọc Duy | ad49136 | 2013-12-05 20:02:32 +0700 | [diff] [blame] | 843 | |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 844 | git_config(upload_pack_config, NULL); |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 845 | upload_pack(); |
| 846 | return 0; |
| 847 | } |