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