]>
Commit | Line | Data |
---|---|---|
def88e9a | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
def88e9a LT |
3 | #include "refs.h" |
4 | #include "pkt-line.h" | |
958c24b1 | 5 | #include "sideband.h" |
f6b42a81 JH |
6 | #include "tag.h" |
7 | #include "object.h" | |
f0243f26 | 8 | #include "commit.h" |
9b8dc263 JS |
9 | #include "diff.h" |
10 | #include "revision.h" | |
11 | #include "list-objects.h" | |
10ac85c7 JH |
12 | #include "list-objects-filter.h" |
13 | #include "list-objects-filter-options.h" | |
cc41fa8d | 14 | #include "run-command.h" |
47a59185 | 15 | #include "connect.h" |
051e4005 | 16 | #include "sigchain.h" |
ff5effdf | 17 | #include "version.h" |
daebaa78 | 18 | #include "string-list.h" |
569e554b | 19 | #include "argv-array.h" |
5411b10c | 20 | #include "prio-queue.h" |
aa9bab29 | 21 | #include "protocol.h" |
10ac85c7 | 22 | #include "quote.h" |
a3d6b53e | 23 | #include "upload-pack.h" |
3145ea95 | 24 | #include "serve.h" |
def88e9a | 25 | |
208acbfb | 26 | /* Remember to update object flag allocation in object.h */ |
937a515a JH |
27 | #define THEY_HAVE (1u << 11) |
28 | #define OUR_REF (1u << 12) | |
29 | #define WANTED (1u << 13) | |
30 | #define COMMON_KNOWN (1u << 14) | |
31 | #define REACHABLE (1u << 15) | |
32 | ||
f53514bc JS |
33 | #define SHALLOW (1u << 16) |
34 | #define NOT_SHALLOW (1u << 17) | |
35 | #define CLIENT_SHALLOW (1u << 18) | |
390eb36b | 36 | #define HIDDEN_REF (1u << 19) |
f53514bc | 37 | |
dddbad72 | 38 | static timestamp_t oldest_have; |
937a515a | 39 | |
cccf74e2 | 40 | static int deepen_relative; |
3f1da57f | 41 | static int multi_ack; |
4e10cf9a | 42 | static int no_done; |
348e390b | 43 | static int use_thin_pack, use_ofs_delta, use_include_tag; |
9462e3f5 | 44 | static int no_progress, daemon_mode; |
7199c093 FM |
45 | /* Allow specifying sha1 if it is a ref tip. */ |
46 | #define ALLOW_TIP_SHA1 01 | |
68ee6289 FM |
47 | /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */ |
48 | #define ALLOW_REACHABLE_SHA1 02 | |
f8edeaa0 DT |
49 | /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */ |
50 | #define ALLOW_ANY_SHA1 07 | |
7199c093 | 51 | static unsigned int allow_unadvertised_object_request; |
f0cea83f | 52 | static int shallow_nr; |
b1e9fff7 JH |
53 | static struct object_array have_obj; |
54 | static struct object_array want_obj; | |
6523078b | 55 | static struct object_array extra_edge_obj; |
96f1e58f | 56 | static unsigned int timeout; |
115dedd7 | 57 | static int keepalive = 5; |
d47f3db7 JH |
58 | /* 0 for no sideband, |
59 | * otherwise maximum packet size (up to 65520 bytes). | |
60 | */ | |
96f1e58f | 61 | static int use_sideband; |
42526b47 | 62 | static int stateless_rpc; |
20b20a22 | 63 | static const char *pack_objects_hook; |
960deccb | 64 | |
10ac85c7 | 65 | static int filter_capability_requested; |
c7620bd0 | 66 | static int allow_filter; |
10ac85c7 JH |
67 | static struct list_objects_filter_options filter_options; |
68 | ||
960deccb PA |
69 | static void reset_timeout(void) |
70 | { | |
71 | alarm(timeout); | |
72 | } | |
fb9040cc | 73 | |
fcf0fe9e | 74 | static void send_client_data(int fd, const char *data, ssize_t sz) |
583b7ea3 | 75 | { |
4c4b7d1d LF |
76 | if (use_sideband) { |
77 | send_sideband(1, fd, data, sz, use_sideband); | |
fcf0fe9e | 78 | return; |
4c4b7d1d | 79 | } |
958c24b1 JH |
80 | if (fd == 3) |
81 | /* emergency quit */ | |
82 | fd = 2; | |
83 | if (fd == 2) { | |
93822c22 | 84 | /* XXX: are we happy to lose stuff here? */ |
958c24b1 | 85 | xwrite(fd, data, sz); |
fcf0fe9e | 86 | return; |
583b7ea3 | 87 | } |
cdf4fb8e | 88 | write_or_die(fd, data, sz); |
583b7ea3 JH |
89 | } |
90 | ||
b790e0f6 NTND |
91 | static int write_one_shallow(const struct commit_graft *graft, void *cb_data) |
92 | { | |
93 | FILE *fp = cb_data; | |
94 | if (graft->nr_parent == -1) | |
7683e2e6 | 95 | fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid)); |
b790e0f6 NTND |
96 | return 0; |
97 | } | |
98 | ||
fb9040cc LT |
99 | static void create_pack_file(void) |
100 | { | |
d3180279 | 101 | struct child_process pack_objects = CHILD_PROCESS_INIT; |
363b7817 | 102 | char data[8193], progress[128]; |
583b7ea3 JH |
103 | char abort_msg[] = "aborting due to possible repository " |
104 | "corruption on the remote side."; | |
b1c71b72 | 105 | int buffered = -1; |
1456b043 | 106 | ssize_t sz; |
65a3629e | 107 | int i; |
cdab4858 | 108 | FILE *pipe_fd; |
75bfc6c2 | 109 | |
20b20a22 JK |
110 | if (!pack_objects_hook) |
111 | pack_objects.git_cmd = 1; | |
112 | else { | |
113 | argv_array_push(&pack_objects.args, pack_objects_hook); | |
114 | argv_array_push(&pack_objects.args, "git"); | |
115 | pack_objects.use_shell = 1; | |
116 | } | |
117 | ||
cdab4858 | 118 | if (shallow_nr) { |
65a3629e MP |
119 | argv_array_push(&pack_objects.args, "--shallow-file"); |
120 | argv_array_push(&pack_objects.args, ""); | |
f0cea83f | 121 | } |
65a3629e MP |
122 | argv_array_push(&pack_objects.args, "pack-objects"); |
123 | argv_array_push(&pack_objects.args, "--revs"); | |
cdab4858 | 124 | if (use_thin_pack) |
65a3629e | 125 | argv_array_push(&pack_objects.args, "--thin"); |
75bfc6c2 | 126 | |
65a3629e | 127 | argv_array_push(&pack_objects.args, "--stdout"); |
2dacf26d | 128 | if (shallow_nr) |
65a3629e | 129 | argv_array_push(&pack_objects.args, "--shallow"); |
cc41fa8d | 130 | if (!no_progress) |
65a3629e | 131 | argv_array_push(&pack_objects.args, "--progress"); |
cc41fa8d | 132 | if (use_ofs_delta) |
65a3629e | 133 | argv_array_push(&pack_objects.args, "--delta-base-offset"); |
348e390b | 134 | if (use_include_tag) |
65a3629e | 135 | argv_array_push(&pack_objects.args, "--include-tag"); |
10ac85c7 | 136 | if (filter_options.filter_spec) { |
0b6069fe JT |
137 | if (pack_objects.use_shell) { |
138 | struct strbuf buf = STRBUF_INIT; | |
139 | sq_quote_buf(&buf, filter_options.filter_spec); | |
140 | argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf); | |
141 | strbuf_release(&buf); | |
142 | } else { | |
143 | argv_array_pushf(&pack_objects.args, "--filter=%s", | |
144 | filter_options.filter_spec); | |
145 | } | |
10ac85c7 | 146 | } |
cc41fa8d | 147 | |
b9612197 | 148 | pack_objects.in = -1; |
cc41fa8d JS |
149 | pack_objects.out = -1; |
150 | pack_objects.err = -1; | |
21edd3f1 | 151 | |
4c324c00 | 152 | if (start_command(&pack_objects)) |
7e44c935 | 153 | die("git upload-pack: unable to fork git-pack-objects"); |
b1c71b72 | 154 | |
cdab4858 NTND |
155 | pipe_fd = xfdopen(pack_objects.in, "w"); |
156 | ||
b790e0f6 NTND |
157 | if (shallow_nr) |
158 | for_each_commit_graft(write_one_shallow, pipe_fd); | |
159 | ||
cdab4858 NTND |
160 | for (i = 0; i < want_obj.nr; i++) |
161 | fprintf(pipe_fd, "%s\n", | |
f2fd0760 | 162 | oid_to_hex(&want_obj.objects[i].item->oid)); |
cdab4858 NTND |
163 | fprintf(pipe_fd, "--not\n"); |
164 | for (i = 0; i < have_obj.nr; i++) | |
165 | fprintf(pipe_fd, "%s\n", | |
f2fd0760 | 166 | oid_to_hex(&have_obj.objects[i].item->oid)); |
cdab4858 NTND |
167 | for (i = 0; i < extra_edge_obj.nr; i++) |
168 | fprintf(pipe_fd, "%s\n", | |
f2fd0760 | 169 | oid_to_hex(&extra_edge_obj.objects[i].item->oid)); |
cdab4858 NTND |
170 | fprintf(pipe_fd, "\n"); |
171 | fflush(pipe_fd); | |
172 | fclose(pipe_fd); | |
f0cea83f | 173 | |
cc41fa8d JS |
174 | /* We read from pack_objects.err to capture stderr output for |
175 | * progress bar, and pack_objects.out to capture the pack data. | |
b1c71b72 | 176 | */ |
b1c71b72 JH |
177 | |
178 | while (1) { | |
b1c71b72 | 179 | struct pollfd pfd[2]; |
363b7817 | 180 | int pe, pu, pollsize; |
05e95155 | 181 | int ret; |
b1c71b72 | 182 | |
0d516ada ML |
183 | reset_timeout(); |
184 | ||
b1c71b72 | 185 | pollsize = 0; |
363b7817 | 186 | pe = pu = -1; |
b1c71b72 | 187 | |
cc41fa8d JS |
188 | if (0 <= pack_objects.out) { |
189 | pfd[pollsize].fd = pack_objects.out; | |
b1c71b72 JH |
190 | pfd[pollsize].events = POLLIN; |
191 | pu = pollsize; | |
192 | pollsize++; | |
193 | } | |
cc41fa8d JS |
194 | if (0 <= pack_objects.err) { |
195 | pfd[pollsize].fd = pack_objects.err; | |
363b7817 JH |
196 | pfd[pollsize].events = POLLIN; |
197 | pe = pollsize; | |
198 | pollsize++; | |
199 | } | |
b1c71b72 | 200 | |
4c324c00 JS |
201 | if (!pollsize) |
202 | break; | |
203 | ||
6c71f8b0 ET |
204 | ret = poll(pfd, pollsize, |
205 | keepalive < 0 ? -1 : 1000 * keepalive); | |
206 | ||
05e95155 | 207 | if (ret < 0) { |
4c324c00 | 208 | if (errno != EINTR) { |
d2b6afa2 | 209 | error_errno("poll failed, resuming"); |
4c324c00 | 210 | sleep(1); |
b1c71b72 | 211 | } |
4c324c00 JS |
212 | continue; |
213 | } | |
6b59f51b NP |
214 | if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) { |
215 | /* Status ready; we ship that in the side-band | |
216 | * or dump to the standard error. | |
217 | */ | |
218 | sz = xread(pack_objects.err, progress, | |
219 | sizeof(progress)); | |
220 | if (0 < sz) | |
221 | send_client_data(2, progress, sz); | |
222 | else if (sz == 0) { | |
223 | close(pack_objects.err); | |
224 | pack_objects.err = -1; | |
225 | } | |
226 | else | |
227 | goto fail; | |
228 | /* give priority to status messages */ | |
229 | continue; | |
230 | } | |
4c324c00 JS |
231 | if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) { |
232 | /* Data ready; we keep the last byte to ourselves | |
233 | * in case we detect broken rev-list, so that we | |
234 | * can leave the stream corrupted. This is | |
235 | * unfortunate -- unpack-objects would happily | |
236 | * accept a valid packdata with trailing garbage, | |
237 | * so appending garbage after we pass all the | |
238 | * pack data is not good enough to signal | |
239 | * breakage to downstream. | |
240 | */ | |
241 | char *cp = data; | |
242 | ssize_t outsz = 0; | |
243 | if (0 <= buffered) { | |
244 | *cp++ = buffered; | |
245 | outsz++; | |
b1c71b72 | 246 | } |
4c324c00 JS |
247 | sz = xread(pack_objects.out, cp, |
248 | sizeof(data) - outsz); | |
249 | if (0 < sz) | |
1456b043 | 250 | ; |
4c324c00 JS |
251 | else if (sz == 0) { |
252 | close(pack_objects.out); | |
253 | pack_objects.out = -1; | |
363b7817 | 254 | } |
4c324c00 JS |
255 | else |
256 | goto fail; | |
257 | sz += outsz; | |
258 | if (1 < sz) { | |
259 | buffered = data[sz-1] & 0xFF; | |
260 | sz--; | |
b1c71b72 | 261 | } |
4c324c00 JS |
262 | else |
263 | buffered = -1; | |
fcf0fe9e | 264 | send_client_data(1, data, sz); |
4c324c00 | 265 | } |
05e95155 JK |
266 | |
267 | /* | |
268 | * We hit the keepalive timeout without saying anything; send | |
269 | * an empty message on the data sideband just to let the other | |
270 | * side know we're still working on it, but don't have any data | |
271 | * yet. | |
272 | * | |
273 | * If we don't have a sideband channel, there's no room in the | |
274 | * protocol to say anything, so those clients are just out of | |
275 | * luck. | |
276 | */ | |
277 | if (!ret && use_sideband) { | |
278 | static const char buf[] = "0005\1"; | |
279 | write_or_die(1, buf, 5); | |
280 | } | |
4c324c00 | 281 | } |
b1c71b72 | 282 | |
4c324c00 | 283 | if (finish_command(&pack_objects)) { |
7e44c935 | 284 | error("git upload-pack: git-pack-objects died with error."); |
4c324c00 JS |
285 | goto fail; |
286 | } | |
b1c71b72 | 287 | |
4c324c00 JS |
288 | /* flush the data */ |
289 | if (0 <= buffered) { | |
290 | data[0] = buffered; | |
fcf0fe9e | 291 | send_client_data(1, data, 1); |
4c324c00 | 292 | fprintf(stderr, "flushed.\n"); |
b1c71b72 | 293 | } |
4c324c00 JS |
294 | if (use_sideband) |
295 | packet_flush(1); | |
296 | return; | |
297 | ||
b1c71b72 | 298 | fail: |
583b7ea3 | 299 | send_client_data(3, abort_msg, sizeof(abort_msg)); |
7e44c935 | 300 | die("git upload-pack: %s", abort_msg); |
fb9040cc LT |
301 | } |
302 | ||
cf93982f | 303 | static int got_oid(const char *hex, struct object_id *oid) |
def88e9a | 304 | { |
b1e9fff7 | 305 | struct object *o; |
937a515a | 306 | int we_knew_they_have = 0; |
b1e9fff7 | 307 | |
cf93982f | 308 | if (get_oid_hex(hex, oid)) |
7e44c935 | 309 | die("git upload-pack: expected SHA1 object, got '%s'", hex); |
cf93982f | 310 | if (!has_object_file(oid)) |
937a515a | 311 | return -1; |
b1e9fff7 | 312 | |
c251c83d | 313 | o = parse_object(oid); |
b1e9fff7 | 314 | if (!o) |
cf93982f | 315 | die("oops (%s)", oid_to_hex(oid)); |
182a8dab | 316 | if (o->type == OBJ_COMMIT) { |
b1e9fff7 | 317 | struct commit_list *parents; |
937a515a | 318 | struct commit *commit = (struct commit *)o; |
b1e9fff7 | 319 | if (o->flags & THEY_HAVE) |
937a515a JH |
320 | we_knew_they_have = 1; |
321 | else | |
322 | o->flags |= THEY_HAVE; | |
323 | if (!oldest_have || (commit->date < oldest_have)) | |
324 | oldest_have = commit->date; | |
325 | for (parents = commit->parents; | |
b1e9fff7 JH |
326 | parents; |
327 | parents = parents->next) | |
328 | parents->item->object.flags |= THEY_HAVE; | |
fb9040cc | 329 | } |
937a515a JH |
330 | if (!we_knew_they_have) { |
331 | add_object_array(o, NULL, &have_obj); | |
332 | return 1; | |
333 | } | |
334 | return 0; | |
335 | } | |
336 | ||
337 | static int reachable(struct commit *want) | |
338 | { | |
5411b10c | 339 | struct prio_queue work = { compare_commits_by_commit_date }; |
937a515a | 340 | |
5411b10c JK |
341 | prio_queue_put(&work, want); |
342 | while (work.nr) { | |
e510ab89 | 343 | struct commit_list *list; |
5411b10c | 344 | struct commit *commit = prio_queue_get(&work); |
937a515a JH |
345 | |
346 | if (commit->object.flags & THEY_HAVE) { | |
347 | want->object.flags |= COMMON_KNOWN; | |
348 | break; | |
349 | } | |
350 | if (!commit->object.parsed) | |
c251c83d | 351 | parse_object(&commit->object.oid); |
937a515a JH |
352 | if (commit->object.flags & REACHABLE) |
353 | continue; | |
354 | commit->object.flags |= REACHABLE; | |
355 | if (commit->date < oldest_have) | |
356 | continue; | |
357 | for (list = commit->parents; list; list = list->next) { | |
358 | struct commit *parent = list->item; | |
359 | if (!(parent->object.flags & REACHABLE)) | |
5411b10c | 360 | prio_queue_put(&work, parent); |
937a515a JH |
361 | } |
362 | } | |
363 | want->object.flags |= REACHABLE; | |
364 | clear_commit_marks(want, REACHABLE); | |
5411b10c | 365 | clear_prio_queue(&work); |
937a515a JH |
366 | return (want->object.flags & COMMON_KNOWN); |
367 | } | |
368 | ||
369 | static int ok_to_give_up(void) | |
370 | { | |
371 | int i; | |
372 | ||
373 | if (!have_obj.nr) | |
374 | return 0; | |
375 | ||
376 | for (i = 0; i < want_obj.nr; i++) { | |
377 | struct object *want = want_obj.objects[i].item; | |
378 | ||
379 | if (want->flags & COMMON_KNOWN) | |
380 | continue; | |
381 | want = deref_tag(want, "a want line", 0); | |
382 | if (!want || want->type != OBJ_COMMIT) { | |
383 | /* no way to tell if this is reachable by | |
384 | * looking at the ancestry chain alone, so | |
385 | * leave a note to ourselves not to worry about | |
386 | * this object anymore. | |
387 | */ | |
388 | want_obj.objects[i].item->flags |= COMMON_KNOWN; | |
389 | continue; | |
390 | } | |
391 | if (!reachable((struct commit *)want)) | |
392 | return 0; | |
393 | } | |
fb9040cc | 394 | return 1; |
def88e9a LT |
395 | } |
396 | ||
397 | static int get_common_commits(void) | |
398 | { | |
cf93982f | 399 | struct object_id oid; |
400 | char last_hex[GIT_MAX_HEXSZ + 1]; | |
49bee717 SP |
401 | int got_common = 0; |
402 | int got_other = 0; | |
4e10cf9a | 403 | int sent_ready = 0; |
def88e9a | 404 | |
f0243f26 JS |
405 | save_commit_buffer = 0; |
406 | ||
eeefa7c9 | 407 | for (;;) { |
74543a04 | 408 | char *line = packet_read_line(0, NULL); |
8bf3b758 NTND |
409 | const char *arg; |
410 | ||
960deccb | 411 | reset_timeout(); |
def88e9a | 412 | |
74543a04 | 413 | if (!line) { |
49bee717 | 414 | if (multi_ack == 2 && got_common |
4e10cf9a JH |
415 | && !got_other && ok_to_give_up()) { |
416 | sent_ready = 1; | |
81c634e9 | 417 | packet_write_fmt(1, "ACK %s ready\n", last_hex); |
4e10cf9a | 418 | } |
b1e9fff7 | 419 | if (have_obj.nr == 0 || multi_ack) |
81c634e9 | 420 | packet_write_fmt(1, "NAK\n"); |
4e10cf9a JH |
421 | |
422 | if (no_done && sent_ready) { | |
81c634e9 | 423 | packet_write_fmt(1, "ACK %s\n", last_hex); |
4e10cf9a JH |
424 | return 0; |
425 | } | |
42526b47 SP |
426 | if (stateless_rpc) |
427 | exit(0); | |
49bee717 SP |
428 | got_common = 0; |
429 | got_other = 0; | |
def88e9a LT |
430 | continue; |
431 | } | |
8bf3b758 | 432 | if (skip_prefix(line, "have ", &arg)) { |
cf93982f | 433 | switch (got_oid(arg, &oid)) { |
937a515a | 434 | case -1: /* they have what we do not */ |
49bee717 | 435 | got_other = 1; |
78affc49 | 436 | if (multi_ack && ok_to_give_up()) { |
cf93982f | 437 | const char *hex = oid_to_hex(&oid); |
4e10cf9a JH |
438 | if (multi_ack == 2) { |
439 | sent_ready = 1; | |
81c634e9 | 440 | packet_write_fmt(1, "ACK %s ready\n", hex); |
4e10cf9a | 441 | } else |
81c634e9 | 442 | packet_write_fmt(1, "ACK %s continue\n", hex); |
78affc49 | 443 | } |
937a515a JH |
444 | break; |
445 | default: | |
49bee717 | 446 | got_common = 1; |
55dc227d | 447 | oid_to_hex_r(last_hex, &oid); |
78affc49 | 448 | if (multi_ack == 2) |
81c634e9 | 449 | packet_write_fmt(1, "ACK %s common\n", last_hex); |
78affc49 | 450 | else if (multi_ack) |
81c634e9 | 451 | packet_write_fmt(1, "ACK %s continue\n", last_hex); |
c04c4e57 | 452 | else if (have_obj.nr == 1) |
81c634e9 | 453 | packet_write_fmt(1, "ACK %s\n", last_hex); |
937a515a | 454 | break; |
af2d3aa4 | 455 | } |
def88e9a LT |
456 | continue; |
457 | } | |
458 | if (!strcmp(line, "done")) { | |
b1e9fff7 | 459 | if (have_obj.nr > 0) { |
1bd8c8f0 | 460 | if (multi_ack) |
81c634e9 | 461 | packet_write_fmt(1, "ACK %s\n", last_hex); |
1bd8c8f0 JS |
462 | return 0; |
463 | } | |
81c634e9 | 464 | packet_write_fmt(1, "NAK\n"); |
def88e9a LT |
465 | return -1; |
466 | } | |
7e44c935 | 467 | die("git upload-pack: expected SHA1 list, got '%s'", line); |
def88e9a | 468 | } |
def88e9a LT |
469 | } |
470 | ||
390eb36b JH |
471 | static int is_our_ref(struct object *o) |
472 | { | |
68ee6289 FM |
473 | int allow_hidden_ref = (allow_unadvertised_object_request & |
474 | (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)); | |
7199c093 | 475 | return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF); |
390eb36b JH |
476 | } |
477 | ||
2997178e NTND |
478 | /* |
479 | * on successful case, it's up to the caller to close cmd->out | |
480 | */ | |
481 | static int do_reachable_revlist(struct child_process *cmd, | |
079aa97e NTND |
482 | struct object_array *src, |
483 | struct object_array *reachable) | |
051e4005 JH |
484 | { |
485 | static const char *argv[] = { | |
486 | "rev-list", "--stdin", NULL, | |
487 | }; | |
051e4005 | 488 | struct object *o; |
55dc227d | 489 | char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */ |
051e4005 JH |
490 | int i; |
491 | ||
2997178e NTND |
492 | cmd->argv = argv; |
493 | cmd->git_cmd = 1; | |
494 | cmd->no_stderr = 1; | |
495 | cmd->in = -1; | |
496 | cmd->out = -1; | |
051e4005 JH |
497 | |
498 | /* | |
7fcbd37f NTND |
499 | * If the next rev-list --stdin encounters an unknown commit, |
500 | * it terminates, which will cause SIGPIPE in the write loop | |
051e4005 JH |
501 | * below. |
502 | */ | |
503 | sigchain_push(SIGPIPE, SIG_IGN); | |
504 | ||
2997178e | 505 | if (start_command(cmd)) |
7fcbd37f NTND |
506 | goto error; |
507 | ||
051e4005 | 508 | namebuf[0] = '^'; |
cf93982f | 509 | namebuf[GIT_SHA1_HEXSZ + 1] = '\n'; |
051e4005 JH |
510 | for (i = get_max_object_index(); 0 < i; ) { |
511 | o = get_indexed_object(--i); | |
2a745324 BH |
512 | if (!o) |
513 | continue; | |
079aa97e NTND |
514 | if (reachable && o->type == OBJ_COMMIT) |
515 | o->flags &= ~TMP_MARK; | |
390eb36b | 516 | if (!is_our_ref(o)) |
051e4005 | 517 | continue; |
f2fd0760 | 518 | memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ); |
cf93982f | 519 | if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0) |
051e4005 JH |
520 | goto error; |
521 | } | |
cf93982f | 522 | namebuf[GIT_SHA1_HEXSZ] = '\n'; |
3f0f6624 NTND |
523 | for (i = 0; i < src->nr; i++) { |
524 | o = src->objects[i].item; | |
079aa97e NTND |
525 | if (is_our_ref(o)) { |
526 | if (reachable) | |
527 | add_object_array(o, NULL, reachable); | |
051e4005 | 528 | continue; |
079aa97e NTND |
529 | } |
530 | if (reachable && o->type == OBJ_COMMIT) | |
531 | o->flags |= TMP_MARK; | |
f2fd0760 | 532 | memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ); |
cf93982f | 533 | if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0) |
051e4005 JH |
534 | goto error; |
535 | } | |
2997178e NTND |
536 | close(cmd->in); |
537 | cmd->in = -1; | |
538 | sigchain_pop(SIGPIPE); | |
051e4005 | 539 | |
2997178e NTND |
540 | return 0; |
541 | ||
542 | error: | |
051e4005 JH |
543 | sigchain_pop(SIGPIPE); |
544 | ||
2997178e NTND |
545 | if (cmd->in >= 0) |
546 | close(cmd->in); | |
547 | if (cmd->out >= 0) | |
548 | close(cmd->out); | |
549 | return -1; | |
550 | } | |
551 | ||
079aa97e NTND |
552 | static int get_reachable_list(struct object_array *src, |
553 | struct object_array *reachable) | |
554 | { | |
555 | struct child_process cmd = CHILD_PROCESS_INIT; | |
556 | int i; | |
557 | struct object *o; | |
55dc227d | 558 | char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */ |
559 | const unsigned hexsz = the_hash_algo->hexsz; | |
079aa97e NTND |
560 | |
561 | if (do_reachable_revlist(&cmd, src, reachable) < 0) | |
562 | return -1; | |
563 | ||
55dc227d | 564 | while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) { |
079aa97e | 565 | struct object_id sha1; |
55dc227d | 566 | const char *p; |
079aa97e | 567 | |
55dc227d | 568 | if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n') |
079aa97e NTND |
569 | break; |
570 | ||
571 | o = lookup_object(sha1.hash); | |
572 | if (o && o->type == OBJ_COMMIT) { | |
573 | o->flags &= ~TMP_MARK; | |
574 | } | |
575 | } | |
576 | for (i = get_max_object_index(); 0 < i; i--) { | |
577 | o = get_indexed_object(i - 1); | |
578 | if (o && o->type == OBJ_COMMIT && | |
579 | (o->flags & TMP_MARK)) { | |
580 | add_object_array(o, NULL, reachable); | |
581 | o->flags &= ~TMP_MARK; | |
582 | } | |
583 | } | |
584 | close(cmd.out); | |
585 | ||
586 | if (finish_command(&cmd)) | |
587 | return -1; | |
588 | ||
589 | return 0; | |
590 | } | |
591 | ||
2997178e NTND |
592 | static int has_unreachable(struct object_array *src) |
593 | { | |
594 | struct child_process cmd = CHILD_PROCESS_INIT; | |
595 | char buf[1]; | |
596 | int i; | |
597 | ||
079aa97e | 598 | if (do_reachable_revlist(&cmd, src, NULL) < 0) |
2997178e | 599 | return 1; |
051e4005 JH |
600 | |
601 | /* | |
602 | * The commits out of the rev-list are not ancestors of | |
603 | * our ref. | |
604 | */ | |
2997178e | 605 | i = read_in_full(cmd.out, buf, 1); |
051e4005 JH |
606 | if (i) |
607 | goto error; | |
608 | close(cmd.out); | |
7fcbd37f | 609 | cmd.out = -1; |
051e4005 JH |
610 | |
611 | /* | |
612 | * rev-list may have died by encountering a bad commit | |
613 | * in the history, in which case we do want to bail out | |
614 | * even when it showed no commit. | |
615 | */ | |
616 | if (finish_command(&cmd)) | |
617 | goto error; | |
618 | ||
619 | /* All the non-tip ones are ancestors of what we advertised */ | |
3f0f6624 | 620 | return 0; |
051e4005 JH |
621 | |
622 | error: | |
7fcbd37f | 623 | sigchain_pop(SIGPIPE); |
7fcbd37f NTND |
624 | if (cmd.out >= 0) |
625 | close(cmd.out); | |
3f0f6624 NTND |
626 | return 1; |
627 | } | |
7fcbd37f | 628 | |
3f0f6624 NTND |
629 | static void check_non_tip(void) |
630 | { | |
631 | int i; | |
632 | ||
633 | /* | |
634 | * In the normal in-process case without | |
635 | * uploadpack.allowReachableSHA1InWant, | |
636 | * non-tip requests can never happen. | |
637 | */ | |
638 | if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1)) | |
639 | goto error; | |
640 | if (!has_unreachable(&want_obj)) | |
641 | /* All the non-tip ones are ancestors of what we advertised */ | |
642 | return; | |
051e4005 JH |
643 | |
644 | error: | |
645 | /* Pick one of them (we know there at least is one) */ | |
646 | for (i = 0; i < want_obj.nr; i++) { | |
3f0f6624 | 647 | struct object *o = want_obj.objects[i].item; |
390eb36b | 648 | if (!is_our_ref(o)) |
051e4005 | 649 | die("git upload-pack: not our ref %s", |
f2fd0760 | 650 | oid_to_hex(&o->oid)); |
051e4005 JH |
651 | } |
652 | } | |
653 | ||
5c24cdea NTND |
654 | static void send_shallow(struct commit_list *result) |
655 | { | |
656 | while (result) { | |
657 | struct object *object = &result->item->object; | |
658 | if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) { | |
dbaa6bdc JH |
659 | packet_write_fmt(1, "shallow %s", |
660 | oid_to_hex(&object->oid)); | |
e92b848c | 661 | register_shallow(&object->oid); |
5c24cdea NTND |
662 | shallow_nr++; |
663 | } | |
664 | result = result->next; | |
665 | } | |
666 | } | |
667 | ||
873700c9 | 668 | static void send_unshallow(const struct object_array *shallows) |
e8e44de7 | 669 | { |
e8e44de7 | 670 | int i; |
873700c9 | 671 | |
e8e44de7 NTND |
672 | for (i = 0; i < shallows->nr; i++) { |
673 | struct object *object = shallows->objects[i].item; | |
674 | if (object->flags & NOT_SHALLOW) { | |
675 | struct commit_list *parents; | |
dbaa6bdc JH |
676 | packet_write_fmt(1, "unshallow %s", |
677 | oid_to_hex(&object->oid)); | |
e8e44de7 | 678 | object->flags &= ~CLIENT_SHALLOW; |
873700c9 NTND |
679 | /* |
680 | * We want to _register_ "object" as shallow, but we | |
681 | * also need to traverse object's parents to deepen a | |
682 | * shallow clone. Unregister it for now so we can | |
683 | * parse and add the parents to the want list, then | |
684 | * re-register it. | |
685 | */ | |
e92b848c | 686 | unregister_shallow(&object->oid); |
e8e44de7 NTND |
687 | object->parsed = 0; |
688 | parse_commit_or_die((struct commit *)object); | |
689 | parents = ((struct commit *)object)->parents; | |
690 | while (parents) { | |
691 | add_object_array(&parents->item->object, | |
692 | NULL, &want_obj); | |
693 | parents = parents->next; | |
694 | } | |
695 | add_object_array(object, NULL, &extra_edge_obj); | |
696 | } | |
697 | /* make sure commit traversal conforms to client */ | |
e92b848c | 698 | register_shallow(&object->oid); |
e8e44de7 | 699 | } |
873700c9 NTND |
700 | } |
701 | ||
cccf74e2 NTND |
702 | static void deepen(int depth, int deepen_relative, |
703 | struct object_array *shallows) | |
873700c9 NTND |
704 | { |
705 | if (depth == INFINITE_DEPTH && !is_repository_shallow()) { | |
706 | int i; | |
707 | ||
708 | for (i = 0; i < shallows->nr; i++) { | |
709 | struct object *object = shallows->objects[i].item; | |
710 | object->flags |= NOT_SHALLOW; | |
711 | } | |
cccf74e2 NTND |
712 | } else if (deepen_relative) { |
713 | struct object_array reachable_shallows = OBJECT_ARRAY_INIT; | |
714 | struct commit_list *result; | |
715 | ||
716 | get_reachable_list(shallows, &reachable_shallows); | |
717 | result = get_shallow_commits(&reachable_shallows, | |
718 | depth + 1, | |
719 | SHALLOW, NOT_SHALLOW); | |
720 | send_shallow(result); | |
721 | free_commit_list(result); | |
722 | object_array_clear(&reachable_shallows); | |
873700c9 NTND |
723 | } else { |
724 | struct commit_list *result; | |
725 | ||
726 | result = get_shallow_commits(&want_obj, depth, | |
727 | SHALLOW, NOT_SHALLOW); | |
728 | send_shallow(result); | |
729 | free_commit_list(result); | |
730 | } | |
731 | ||
732 | send_unshallow(shallows); | |
e8e44de7 NTND |
733 | } |
734 | ||
569e554b NTND |
735 | static void deepen_by_rev_list(int ac, const char **av, |
736 | struct object_array *shallows) | |
737 | { | |
738 | struct commit_list *result; | |
739 | ||
740 | result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW); | |
741 | send_shallow(result); | |
742 | free_commit_list(result); | |
743 | send_unshallow(shallows); | |
685fbd32 BW |
744 | } |
745 | ||
746 | /* Returns 1 if a shallow list is sent or 0 otherwise */ | |
747 | static int send_shallow_list(int depth, int deepen_rev_list, | |
748 | timestamp_t deepen_since, | |
749 | struct string_list *deepen_not, | |
750 | struct object_array *shallows) | |
751 | { | |
752 | int ret = 0; | |
753 | ||
754 | if (depth > 0 && deepen_rev_list) | |
755 | die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together"); | |
756 | if (depth > 0) { | |
757 | deepen(depth, deepen_relative, shallows); | |
758 | ret = 1; | |
759 | } else if (deepen_rev_list) { | |
760 | struct argv_array av = ARGV_ARRAY_INIT; | |
761 | int i; | |
762 | ||
763 | argv_array_push(&av, "rev-list"); | |
764 | if (deepen_since) | |
765 | argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since); | |
766 | if (deepen_not->nr) { | |
767 | argv_array_push(&av, "--not"); | |
768 | for (i = 0; i < deepen_not->nr; i++) { | |
769 | struct string_list_item *s = deepen_not->items + i; | |
770 | argv_array_push(&av, s->string); | |
771 | } | |
772 | argv_array_push(&av, "--not"); | |
773 | } | |
774 | for (i = 0; i < want_obj.nr; i++) { | |
775 | struct object *o = want_obj.objects[i].item; | |
776 | argv_array_push(&av, oid_to_hex(&o->oid)); | |
777 | } | |
778 | deepen_by_rev_list(av.argc, av.argv, shallows); | |
779 | argv_array_clear(&av); | |
780 | ret = 1; | |
781 | } else { | |
782 | if (shallows->nr > 0) { | |
783 | int i; | |
784 | for (i = 0; i < shallows->nr; i++) | |
785 | register_shallow(&shallows->objects[i].item->oid); | |
786 | } | |
787 | } | |
788 | ||
789 | shallow_nr += shallows->nr; | |
790 | return ret; | |
569e554b NTND |
791 | } |
792 | ||
ae2948f3 BW |
793 | static int process_shallow(const char *line, struct object_array *shallows) |
794 | { | |
795 | const char *arg; | |
796 | if (skip_prefix(line, "shallow ", &arg)) { | |
797 | struct object_id oid; | |
798 | struct object *object; | |
799 | if (get_oid_hex(arg, &oid)) | |
800 | die("invalid shallow line: %s", line); | |
801 | object = parse_object(&oid); | |
802 | if (!object) | |
803 | return 1; | |
804 | if (object->type != OBJ_COMMIT) | |
805 | die("invalid shallow object %s", oid_to_hex(&oid)); | |
806 | if (!(object->flags & CLIENT_SHALLOW)) { | |
807 | object->flags |= CLIENT_SHALLOW; | |
808 | add_object_array(object, NULL, shallows); | |
809 | } | |
810 | return 1; | |
811 | } | |
812 | ||
813 | return 0; | |
814 | } | |
815 | ||
816 | static int process_deepen(const char *line, int *depth) | |
817 | { | |
818 | const char *arg; | |
819 | if (skip_prefix(line, "deepen ", &arg)) { | |
820 | char *end = NULL; | |
821 | *depth = (int)strtol(arg, &end, 0); | |
822 | if (!end || *end || *depth <= 0) | |
823 | die("Invalid deepen: %s", line); | |
824 | return 1; | |
825 | } | |
826 | ||
827 | return 0; | |
828 | } | |
829 | ||
830 | static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list) | |
831 | { | |
832 | const char *arg; | |
833 | if (skip_prefix(line, "deepen-since ", &arg)) { | |
834 | char *end = NULL; | |
835 | *deepen_since = parse_timestamp(arg, &end, 0); | |
836 | if (!end || *end || !deepen_since || | |
837 | /* revisions.c's max_age -1 is special */ | |
838 | *deepen_since == -1) | |
839 | die("Invalid deepen-since: %s", line); | |
840 | *deepen_rev_list = 1; | |
841 | return 1; | |
842 | } | |
843 | return 0; | |
844 | } | |
845 | ||
846 | static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list) | |
847 | { | |
848 | const char *arg; | |
849 | if (skip_prefix(line, "deepen-not ", &arg)) { | |
850 | char *ref = NULL; | |
851 | struct object_id oid; | |
852 | if (expand_ref(arg, strlen(arg), &oid, &ref) != 1) | |
853 | die("git upload-pack: ambiguous deepen-not: %s", line); | |
854 | string_list_append(deepen_not, ref); | |
855 | free(ref); | |
856 | *deepen_rev_list = 1; | |
857 | return 1; | |
858 | } | |
859 | return 0; | |
569e554b NTND |
860 | } |
861 | ||
b1e9fff7 | 862 | static void receive_needs(void) |
fb9040cc | 863 | { |
3cd47459 | 864 | struct object_array shallows = OBJECT_ARRAY_INIT; |
269a7a83 | 865 | struct string_list deepen_not = STRING_LIST_INIT_DUP; |
74543a04 | 866 | int depth = 0; |
051e4005 | 867 | int has_non_tip = 0; |
dddbad72 | 868 | timestamp_t deepen_since = 0; |
569e554b | 869 | int deepen_rev_list = 0; |
fb9040cc | 870 | |
f0cea83f | 871 | shallow_nr = 0; |
fb9040cc | 872 | for (;;) { |
565ebbf7 | 873 | struct object *o; |
f47182c8 | 874 | const char *features; |
cf93982f | 875 | struct object_id oid_buf; |
74543a04 | 876 | char *line = packet_read_line(0, NULL); |
8bf3b758 NTND |
877 | const char *arg; |
878 | ||
960deccb | 879 | reset_timeout(); |
74543a04 | 880 | if (!line) |
ed09aef0 | 881 | break; |
e091eb93 | 882 | |
ae2948f3 | 883 | if (process_shallow(line, &shallows)) |
ed09aef0 | 884 | continue; |
ae2948f3 | 885 | if (process_deepen(line, &depth)) |
016e6ccb | 886 | continue; |
ae2948f3 | 887 | if (process_deepen_since(line, &deepen_since, &deepen_rev_list)) |
569e554b | 888 | continue; |
ae2948f3 | 889 | if (process_deepen_not(line, &deepen_not, &deepen_rev_list)) |
269a7a83 | 890 | continue; |
ae2948f3 | 891 | |
10ac85c7 JH |
892 | if (skip_prefix(line, "filter ", &arg)) { |
893 | if (!filter_capability_requested) | |
894 | die("git upload-pack: filtering capability not negotiated"); | |
895 | parse_list_objects_filter(&filter_options, arg); | |
896 | continue; | |
897 | } | |
9bfa0f9b | 898 | |
8bf3b758 | 899 | if (!skip_prefix(line, "want ", &arg) || |
55dc227d | 900 | parse_oid_hex(arg, &oid_buf, &features)) |
7e44c935 | 901 | die("git upload-pack: protocol error, " |
55dc227d | 902 | "expected to get object ID, not '%s'", line); |
f47182c8 | 903 | |
cccf74e2 NTND |
904 | if (parse_feature_request(features, "deepen-relative")) |
905 | deepen_relative = 1; | |
f47182c8 | 906 | if (parse_feature_request(features, "multi_ack_detailed")) |
78affc49 | 907 | multi_ack = 2; |
f47182c8 | 908 | else if (parse_feature_request(features, "multi_ack")) |
1bd8c8f0 | 909 | multi_ack = 1; |
f47182c8 | 910 | if (parse_feature_request(features, "no-done")) |
4e10cf9a | 911 | no_done = 1; |
f47182c8 | 912 | if (parse_feature_request(features, "thin-pack")) |
b19696c2 | 913 | use_thin_pack = 1; |
f47182c8 | 914 | if (parse_feature_request(features, "ofs-delta")) |
e4fe4b8e | 915 | use_ofs_delta = 1; |
f47182c8 | 916 | if (parse_feature_request(features, "side-band-64k")) |
d47f3db7 | 917 | use_sideband = LARGE_PACKET_MAX; |
f47182c8 | 918 | else if (parse_feature_request(features, "side-band")) |
d47f3db7 | 919 | use_sideband = DEFAULT_PACKET_MAX; |
f47182c8 | 920 | if (parse_feature_request(features, "no-progress")) |
b0e90897 | 921 | no_progress = 1; |
f47182c8 | 922 | if (parse_feature_request(features, "include-tag")) |
348e390b | 923 | use_include_tag = 1; |
c7620bd0 | 924 | if (allow_filter && parse_feature_request(features, "filter")) |
10ac85c7 | 925 | filter_capability_requested = 1; |
565ebbf7 | 926 | |
c251c83d | 927 | o = parse_object(&oid_buf); |
bdb31ead JT |
928 | if (!o) { |
929 | packet_write_fmt(1, | |
930 | "ERR upload-pack: not our ref %s", | |
cf93982f | 931 | oid_to_hex(&oid_buf)); |
9f9aa761 | 932 | die("git upload-pack: not our ref %s", |
cf93982f | 933 | oid_to_hex(&oid_buf)); |
bdb31ead | 934 | } |
565ebbf7 JH |
935 | if (!(o->flags & WANTED)) { |
936 | o->flags |= WANTED; | |
f8edeaa0 DT |
937 | if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1 |
938 | || is_our_ref(o))) | |
051e4005 | 939 | has_non_tip = 1; |
b1e9fff7 | 940 | add_object_array(o, NULL, &want_obj); |
565ebbf7 | 941 | } |
fb9040cc | 942 | } |
9462e3f5 | 943 | |
051e4005 JH |
944 | /* |
945 | * We have sent all our refs already, and the other end | |
946 | * should have chosen out of them. When we are operating | |
947 | * in the stateless RPC mode, however, their choice may | |
948 | * have been based on the set of older refs advertised | |
949 | * by another process that handled the initial request. | |
950 | */ | |
951 | if (has_non_tip) | |
952 | check_non_tip(); | |
953 | ||
9462e3f5 JS |
954 | if (!use_sideband && daemon_mode) |
955 | no_progress = 1; | |
956 | ||
569e554b | 957 | if (depth == 0 && !deepen_rev_list && shallows.nr == 0) |
f53514bc | 958 | return; |
569e554b | 959 | |
685fbd32 BW |
960 | if (send_shallow_list(depth, deepen_rev_list, deepen_since, |
961 | &deepen_not, &shallows)) | |
962 | packet_flush(1); | |
dcb572ab | 963 | object_array_clear(&shallows); |
fb9040cc LT |
964 | } |
965 | ||
daebaa78 | 966 | /* return non-zero if the ref is hidden, otherwise 0 */ |
78a766ab LF |
967 | static int mark_our_ref(const char *refname, const char *refname_full, |
968 | const struct object_id *oid) | |
cbbe50db | 969 | { |
363e98bf | 970 | struct object *o = lookup_unknown_object(oid->hash); |
daebaa78 | 971 | |
78a766ab | 972 | if (ref_is_hidden(refname, refname_full)) { |
390eb36b | 973 | o->flags |= HIDDEN_REF; |
daebaa78 | 974 | return 1; |
390eb36b | 975 | } |
3f1da57f | 976 | o->flags |= OUR_REF; |
cbbe50db JH |
977 | return 0; |
978 | } | |
979 | ||
78a766ab | 980 | static int check_ref(const char *refname_full, const struct object_id *oid, |
363e98bf | 981 | int flag, void *cb_data) |
e172755b | 982 | { |
78a766ab LF |
983 | const char *refname = strip_namespace(refname_full); |
984 | ||
985 | mark_our_ref(refname, refname_full, oid); | |
e172755b JK |
986 | return 0; |
987 | } | |
988 | ||
7171d8c1 JH |
989 | static void format_symref_info(struct strbuf *buf, struct string_list *symref) |
990 | { | |
991 | struct string_list_item *item; | |
992 | ||
993 | if (!symref->nr) | |
994 | return; | |
995 | for_each_string_list_item(item, symref) | |
996 | strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util); | |
997 | } | |
998 | ||
363e98bf MH |
999 | static int send_ref(const char *refname, const struct object_id *oid, |
1000 | int flag, void *cb_data) | |
def88e9a | 1001 | { |
ed09aef0 | 1002 | static const char *capabilities = "multi_ack thin-pack side-band" |
cccf74e2 NTND |
1003 | " side-band-64k ofs-delta shallow deepen-since deepen-not" |
1004 | " deepen-relative no-progress include-tag multi_ack_detailed"; | |
6b01ecfe | 1005 | const char *refname_nons = strip_namespace(refname); |
21758aff | 1006 | struct object_id peeled; |
b5b16990 | 1007 | |
78a766ab | 1008 | if (mark_our_ref(refname_nons, refname, oid)) |
daebaa78 | 1009 | return 0; |
cbbe50db | 1010 | |
7171d8c1 JH |
1011 | if (capabilities) { |
1012 | struct strbuf symref_info = STRBUF_INIT; | |
1013 | ||
1014 | format_symref_info(&symref_info, cb_data); | |
10ac85c7 | 1015 | packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n", |
363e98bf | 1016 | oid_to_hex(oid), refname_nons, |
cf2ad8e6 | 1017 | 0, capabilities, |
7199c093 FM |
1018 | (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ? |
1019 | " allow-tip-sha1-in-want" : "", | |
68ee6289 FM |
1020 | (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ? |
1021 | " allow-reachable-sha1-in-want" : "", | |
ff5effdf | 1022 | stateless_rpc ? " no-done" : "", |
7171d8c1 | 1023 | symref_info.buf, |
c7620bd0 | 1024 | allow_filter ? " filter" : "", |
ff5effdf | 1025 | git_user_agent_sanitized()); |
7171d8c1 JH |
1026 | strbuf_release(&symref_info); |
1027 | } else { | |
81c634e9 | 1028 | packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons); |
7171d8c1 | 1029 | } |
1f5881bb | 1030 | capabilities = NULL; |
b420d909 | 1031 | if (!peel_ref(refname, &peeled)) |
81c634e9 | 1032 | packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons); |
def88e9a LT |
1033 | return 0; |
1034 | } | |
1035 | ||
7dabd056 MH |
1036 | static int find_symref(const char *refname, const struct object_id *oid, |
1037 | int flag, void *cb_data) | |
7171d8c1 JH |
1038 | { |
1039 | const char *symref_target; | |
1040 | struct string_list_item *item; | |
7171d8c1 JH |
1041 | |
1042 | if ((flag & REF_ISSYMREF) == 0) | |
1043 | return 0; | |
744c040b | 1044 | symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag); |
7171d8c1 JH |
1045 | if (!symref_target || (flag & REF_ISSYMREF) == 0) |
1046 | die("'%s' is a symref but it is not?", refname); | |
1047 | item = string_list_append(cb_data, refname); | |
1048 | item->util = xstrdup(symref_target); | |
1049 | return 0; | |
1050 | } | |
1051 | ||
daebaa78 JH |
1052 | static int upload_pack_config(const char *var, const char *value, void *unused) |
1053 | { | |
7199c093 FM |
1054 | if (!strcmp("uploadpack.allowtipsha1inwant", var)) { |
1055 | if (git_config_bool(var, value)) | |
1056 | allow_unadvertised_object_request |= ALLOW_TIP_SHA1; | |
1057 | else | |
1058 | allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1; | |
68ee6289 FM |
1059 | } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) { |
1060 | if (git_config_bool(var, value)) | |
1061 | allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1; | |
1062 | else | |
1063 | allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1; | |
f8edeaa0 DT |
1064 | } else if (!strcmp("uploadpack.allowanysha1inwant", var)) { |
1065 | if (git_config_bool(var, value)) | |
1066 | allow_unadvertised_object_request |= ALLOW_ANY_SHA1; | |
1067 | else | |
1068 | allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1; | |
7199c093 | 1069 | } else if (!strcmp("uploadpack.keepalive", var)) { |
05e95155 JK |
1070 | keepalive = git_config_int(var, value); |
1071 | if (!keepalive) | |
1072 | keepalive = -1; | |
20b20a22 JK |
1073 | } else if (current_config_scope() != CONFIG_SCOPE_REPO) { |
1074 | if (!strcmp("uploadpack.packobjectshook", var)) | |
1075 | return git_config_string(&pack_objects_hook, var, value); | |
10ac85c7 | 1076 | } else if (!strcmp("uploadpack.allowfilter", var)) { |
c7620bd0 | 1077 | allow_filter = git_config_bool(var, value); |
05e95155 | 1078 | } |
daebaa78 JH |
1079 | return parse_hide_refs_config(var, value, "uploadpack"); |
1080 | } | |
1081 | ||
a3d6b53e | 1082 | void upload_pack(struct upload_pack_options *options) |
def88e9a | 1083 | { |
a3d6b53e | 1084 | struct string_list symref = STRING_LIST_INIT_DUP; |
960deccb | 1085 | |
a3d6b53e BW |
1086 | stateless_rpc = options->stateless_rpc; |
1087 | timeout = options->timeout; | |
1088 | daemon_mode = options->daemon_mode; | |
2fb3f6db | 1089 | |
daebaa78 | 1090 | git_config(upload_pack_config, NULL); |
960deccb | 1091 | |
a3d6b53e | 1092 | head_ref_namespaced(find_symref, &symref); |
a6080a0a | 1093 | |
a3d6b53e BW |
1094 | if (options->advertise_refs || !stateless_rpc) { |
1095 | reset_timeout(); | |
1096 | head_ref_namespaced(send_ref, &symref); | |
1097 | for_each_namespaced_ref(send_ref, &symref); | |
1098 | advertise_shallow_grafts(1); | |
1099 | packet_flush(1); | |
1100 | } else { | |
1101 | head_ref_namespaced(check_ref, NULL); | |
1102 | for_each_namespaced_ref(check_ref, NULL); | |
aa9bab29 | 1103 | } |
a3d6b53e BW |
1104 | string_list_clear(&symref, 1); |
1105 | if (options->advertise_refs) | |
1106 | return; | |
04b33055 | 1107 | |
a3d6b53e BW |
1108 | receive_needs(); |
1109 | if (want_obj.nr) { | |
1110 | get_common_commits(); | |
1111 | create_pack_file(); | |
1112 | } | |
def88e9a | 1113 | } |
04b33055 | 1114 | |
3145ea95 BW |
1115 | struct upload_pack_data { |
1116 | struct object_array wants; | |
1117 | struct oid_array haves; | |
113b9475 | 1118 | |
685fbd32 BW |
1119 | struct object_array shallows; |
1120 | struct string_list deepen_not; | |
1121 | int depth; | |
1122 | timestamp_t deepen_since; | |
1123 | int deepen_rev_list; | |
1124 | int deepen_relative; | |
ad491366 | 1125 | |
3145ea95 | 1126 | unsigned stateless_rpc : 1; |
aa9bab29 | 1127 | |
3145ea95 BW |
1128 | unsigned use_thin_pack : 1; |
1129 | unsigned use_ofs_delta : 1; | |
1130 | unsigned no_progress : 1; | |
1131 | unsigned use_include_tag : 1; | |
1132 | unsigned done : 1; | |
1133 | }; | |
1134 | ||
1135 | static void upload_pack_data_init(struct upload_pack_data *data) | |
1136 | { | |
1137 | struct object_array wants = OBJECT_ARRAY_INIT; | |
1138 | struct oid_array haves = OID_ARRAY_INIT; | |
685fbd32 BW |
1139 | struct object_array shallows = OBJECT_ARRAY_INIT; |
1140 | struct string_list deepen_not = STRING_LIST_INIT_DUP; | |
3145ea95 BW |
1141 | |
1142 | memset(data, 0, sizeof(*data)); | |
1143 | data->wants = wants; | |
1144 | data->haves = haves; | |
685fbd32 BW |
1145 | data->shallows = shallows; |
1146 | data->deepen_not = deepen_not; | |
3145ea95 BW |
1147 | } |
1148 | ||
1149 | static void upload_pack_data_clear(struct upload_pack_data *data) | |
1150 | { | |
1151 | object_array_clear(&data->wants); | |
1152 | oid_array_clear(&data->haves); | |
685fbd32 BW |
1153 | object_array_clear(&data->shallows); |
1154 | string_list_clear(&data->deepen_not, 0); | |
3145ea95 BW |
1155 | } |
1156 | ||
1157 | static int parse_want(const char *line) | |
1158 | { | |
1159 | const char *arg; | |
1160 | if (skip_prefix(line, "want ", &arg)) { | |
1161 | struct object_id oid; | |
1162 | struct object *o; | |
1163 | ||
1164 | if (get_oid_hex(arg, &oid)) | |
1165 | die("git upload-pack: protocol error, " | |
1166 | "expected to get oid, not '%s'", line); | |
1167 | ||
1168 | o = parse_object(&oid); | |
1169 | if (!o) { | |
1170 | packet_write_fmt(1, | |
1171 | "ERR upload-pack: not our ref %s", | |
1172 | oid_to_hex(&oid)); | |
1173 | die("git upload-pack: not our ref %s", | |
1174 | oid_to_hex(&oid)); | |
1175 | } | |
1176 | ||
1177 | if (!(o->flags & WANTED)) { | |
1178 | o->flags |= WANTED; | |
1179 | add_object_array(o, NULL, &want_obj); | |
1180 | } | |
1181 | ||
1182 | return 1; | |
1183 | } | |
1184 | ||
1185 | return 0; | |
1186 | } | |
1187 | ||
1188 | static int parse_have(const char *line, struct oid_array *haves) | |
1189 | { | |
1190 | const char *arg; | |
1191 | if (skip_prefix(line, "have ", &arg)) { | |
1192 | struct object_id oid; | |
1193 | ||
1194 | if (get_oid_hex(arg, &oid)) | |
1195 | die("git upload-pack: expected SHA1 object, got '%s'", arg); | |
1196 | oid_array_append(haves, &oid); | |
1197 | return 1; | |
aa9bab29 BW |
1198 | } |
1199 | ||
def88e9a LT |
1200 | return 0; |
1201 | } | |
3145ea95 BW |
1202 | |
1203 | static void process_args(struct packet_reader *request, | |
1204 | struct upload_pack_data *data) | |
1205 | { | |
1206 | while (packet_reader_read(request) != PACKET_READ_FLUSH) { | |
1207 | const char *arg = request->line; | |
ba95710a | 1208 | const char *p; |
3145ea95 BW |
1209 | |
1210 | /* process want */ | |
1211 | if (parse_want(arg)) | |
1212 | continue; | |
1213 | /* process have line */ | |
1214 | if (parse_have(arg, &data->haves)) | |
1215 | continue; | |
1216 | ||
1217 | /* process args like thin-pack */ | |
1218 | if (!strcmp(arg, "thin-pack")) { | |
1219 | use_thin_pack = 1; | |
1220 | continue; | |
1221 | } | |
1222 | if (!strcmp(arg, "ofs-delta")) { | |
1223 | use_ofs_delta = 1; | |
1224 | continue; | |
1225 | } | |
1226 | if (!strcmp(arg, "no-progress")) { | |
1227 | no_progress = 1; | |
1228 | continue; | |
1229 | } | |
1230 | if (!strcmp(arg, "include-tag")) { | |
1231 | use_include_tag = 1; | |
1232 | continue; | |
1233 | } | |
1234 | if (!strcmp(arg, "done")) { | |
1235 | data->done = 1; | |
1236 | continue; | |
1237 | } | |
1238 | ||
685fbd32 BW |
1239 | /* Shallow related arguments */ |
1240 | if (process_shallow(arg, &data->shallows)) | |
1241 | continue; | |
1242 | if (process_deepen(arg, &data->depth)) | |
1243 | continue; | |
1244 | if (process_deepen_since(arg, &data->deepen_since, | |
1245 | &data->deepen_rev_list)) | |
1246 | continue; | |
1247 | if (process_deepen_not(arg, &data->deepen_not, | |
1248 | &data->deepen_rev_list)) | |
1249 | continue; | |
1250 | if (!strcmp(arg, "deepen-relative")) { | |
1251 | data->deepen_relative = 1; | |
1252 | continue; | |
1253 | } | |
1254 | ||
ba95710a JT |
1255 | if (allow_filter && skip_prefix(arg, "filter ", &p)) { |
1256 | parse_list_objects_filter(&filter_options, p); | |
1257 | continue; | |
1258 | } | |
1259 | ||
3145ea95 | 1260 | /* ignore unknown lines maybe? */ |
7cc6ed2d | 1261 | die("unexpected line: '%s'", arg); |
3145ea95 BW |
1262 | } |
1263 | } | |
1264 | ||
1265 | static int process_haves(struct oid_array *haves, struct oid_array *common) | |
1266 | { | |
1267 | int i; | |
1268 | ||
1269 | /* Process haves */ | |
1270 | for (i = 0; i < haves->nr; i++) { | |
1271 | const struct object_id *oid = &haves->oid[i]; | |
1272 | struct object *o; | |
1273 | int we_knew_they_have = 0; | |
1274 | ||
1275 | if (!has_object_file(oid)) | |
1276 | continue; | |
1277 | ||
1278 | oid_array_append(common, oid); | |
1279 | ||
1280 | o = parse_object(oid); | |
1281 | if (!o) | |
1282 | die("oops (%s)", oid_to_hex(oid)); | |
1283 | if (o->type == OBJ_COMMIT) { | |
1284 | struct commit_list *parents; | |
1285 | struct commit *commit = (struct commit *)o; | |
1286 | if (o->flags & THEY_HAVE) | |
1287 | we_knew_they_have = 1; | |
1288 | else | |
1289 | o->flags |= THEY_HAVE; | |
1290 | if (!oldest_have || (commit->date < oldest_have)) | |
1291 | oldest_have = commit->date; | |
1292 | for (parents = commit->parents; | |
1293 | parents; | |
1294 | parents = parents->next) | |
1295 | parents->item->object.flags |= THEY_HAVE; | |
1296 | } | |
1297 | if (!we_knew_they_have) | |
1298 | add_object_array(o, NULL, &have_obj); | |
1299 | } | |
1300 | ||
1301 | return 0; | |
1302 | } | |
1303 | ||
1304 | static int send_acks(struct oid_array *acks, struct strbuf *response) | |
1305 | { | |
1306 | int i; | |
1307 | ||
1308 | packet_buf_write(response, "acknowledgments\n"); | |
1309 | ||
1310 | /* Send Acks */ | |
1311 | if (!acks->nr) | |
1312 | packet_buf_write(response, "NAK\n"); | |
1313 | ||
1314 | for (i = 0; i < acks->nr; i++) { | |
1315 | packet_buf_write(response, "ACK %s\n", | |
1316 | oid_to_hex(&acks->oid[i])); | |
1317 | } | |
1318 | ||
1319 | if (ok_to_give_up()) { | |
1320 | /* Send Ready */ | |
1321 | packet_buf_write(response, "ready\n"); | |
1322 | return 1; | |
1323 | } | |
1324 | ||
1325 | return 0; | |
1326 | } | |
1327 | ||
1328 | static int process_haves_and_send_acks(struct upload_pack_data *data) | |
1329 | { | |
1330 | struct oid_array common = OID_ARRAY_INIT; | |
1331 | struct strbuf response = STRBUF_INIT; | |
1332 | int ret = 0; | |
1333 | ||
1334 | process_haves(&data->haves, &common); | |
1335 | if (data->done) { | |
1336 | ret = 1; | |
1337 | } else if (send_acks(&common, &response)) { | |
1338 | packet_buf_delim(&response); | |
1339 | ret = 1; | |
1340 | } else { | |
1341 | /* Add Flush */ | |
1342 | packet_buf_flush(&response); | |
1343 | ret = 0; | |
1344 | } | |
1345 | ||
1346 | /* Send response */ | |
1347 | write_or_die(1, response.buf, response.len); | |
1348 | strbuf_release(&response); | |
1349 | ||
1350 | oid_array_clear(&data->haves); | |
1351 | oid_array_clear(&common); | |
1352 | return ret; | |
1353 | } | |
1354 | ||
685fbd32 BW |
1355 | static void send_shallow_info(struct upload_pack_data *data) |
1356 | { | |
1357 | /* No shallow info needs to be sent */ | |
1358 | if (!data->depth && !data->deepen_rev_list && !data->shallows.nr && | |
1359 | !is_repository_shallow()) | |
1360 | return; | |
1361 | ||
1362 | packet_write_fmt(1, "shallow-info\n"); | |
1363 | ||
1364 | if (!send_shallow_list(data->depth, data->deepen_rev_list, | |
1365 | data->deepen_since, &data->deepen_not, | |
1366 | &data->shallows) && is_repository_shallow()) | |
1367 | deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows); | |
1368 | ||
1369 | packet_delim(1); | |
1370 | } | |
1371 | ||
3145ea95 BW |
1372 | enum fetch_state { |
1373 | FETCH_PROCESS_ARGS = 0, | |
1374 | FETCH_SEND_ACKS, | |
1375 | FETCH_SEND_PACK, | |
1376 | FETCH_DONE, | |
1377 | }; | |
1378 | ||
1379 | int upload_pack_v2(struct repository *r, struct argv_array *keys, | |
1380 | struct packet_reader *request) | |
1381 | { | |
1382 | enum fetch_state state = FETCH_PROCESS_ARGS; | |
1383 | struct upload_pack_data data; | |
1384 | ||
54592687 JT |
1385 | git_config(upload_pack_config, NULL); |
1386 | ||
3145ea95 BW |
1387 | upload_pack_data_init(&data); |
1388 | use_sideband = LARGE_PACKET_MAX; | |
1389 | ||
1390 | while (state != FETCH_DONE) { | |
1391 | switch (state) { | |
1392 | case FETCH_PROCESS_ARGS: | |
1393 | process_args(request, &data); | |
1394 | ||
1395 | if (!want_obj.nr) { | |
1396 | /* | |
1397 | * Request didn't contain any 'want' lines, | |
1398 | * guess they didn't want anything. | |
1399 | */ | |
1400 | state = FETCH_DONE; | |
1401 | } else if (data.haves.nr) { | |
1402 | /* | |
1403 | * Request had 'have' lines, so lets ACK them. | |
1404 | */ | |
1405 | state = FETCH_SEND_ACKS; | |
1406 | } else { | |
1407 | /* | |
1408 | * Request had 'want's but no 'have's so we can | |
1409 | * immedietly go to construct and send a pack. | |
1410 | */ | |
1411 | state = FETCH_SEND_PACK; | |
1412 | } | |
1413 | break; | |
1414 | case FETCH_SEND_ACKS: | |
1415 | if (process_haves_and_send_acks(&data)) | |
1416 | state = FETCH_SEND_PACK; | |
1417 | else | |
1418 | state = FETCH_DONE; | |
1419 | break; | |
1420 | case FETCH_SEND_PACK: | |
685fbd32 BW |
1421 | send_shallow_info(&data); |
1422 | ||
3145ea95 BW |
1423 | packet_write_fmt(1, "packfile\n"); |
1424 | create_pack_file(); | |
1425 | state = FETCH_DONE; | |
1426 | break; | |
1427 | case FETCH_DONE: | |
1428 | continue; | |
1429 | } | |
1430 | } | |
1431 | ||
1432 | upload_pack_data_clear(&data); | |
1433 | return 0; | |
1434 | } | |
685fbd32 BW |
1435 | |
1436 | int upload_pack_advertise(struct repository *r, | |
1437 | struct strbuf *value) | |
1438 | { | |
ba95710a JT |
1439 | if (value) { |
1440 | int allow_filter_value; | |
685fbd32 | 1441 | strbuf_addstr(value, "shallow"); |
ba95710a JT |
1442 | if (!repo_config_get_bool(the_repository, |
1443 | "uploadpack.allowfilter", | |
1444 | &allow_filter_value) && | |
1445 | allow_filter_value) | |
1446 | strbuf_addstr(value, " filter"); | |
1447 | } | |
685fbd32 BW |
1448 | return 1; |
1449 | } |