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