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