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