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