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