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