]>
Commit | Line | Data |
---|---|---|
e7da9385 | 1 | #define USE_THE_REPOSITORY_VARIABLE |
41f43b82 | 2 | #define DISABLE_SIGN_COMPARE_WARNINGS |
e7da9385 | 3 | |
5579f44d | 4 | #include "git-compat-util.h" |
a49d2834 | 5 | #include "repository.h" |
b2141fc1 | 6 | #include "config.h" |
d4a4f929 | 7 | #include "date.h" |
32a8f510 | 8 | #include "environment.h" |
f394e093 | 9 | #include "gettext.h" |
41771fa4 | 10 | #include "hex.h" |
697cc8ef | 11 | #include "lockfile.h" |
745f7a8c NTND |
12 | #include "refs.h" |
13 | #include "pkt-line.h" | |
14 | #include "commit.h" | |
15 | #include "tag.h" | |
745f7a8c NTND |
16 | #include "pack.h" |
17 | #include "sideband.h" | |
18 | #include "fetch-pack.h" | |
19 | #include "remote.h" | |
20 | #include "run-command.h" | |
47a59185 | 21 | #include "connect.h" |
74ea5c95 | 22 | #include "trace2.h" |
745f7a8c | 23 | #include "version.h" |
fe299ec5 | 24 | #include "oid-array.h" |
fdb69d33 | 25 | #include "oidset.h" |
0abe14f6 | 26 | #include "packfile.h" |
68cd492a | 27 | #include "object-store.h" |
c339932b | 28 | #include "path.h" |
cf1e7c07 | 29 | #include "connected.h" |
ec062838 | 30 | #include "fetch-negotiator.h" |
1362df0d | 31 | #include "fsck.h" |
120ad2b0 | 32 | #include "shallow.h" |
9c1e657a JT |
33 | #include "commit-reach.h" |
34 | #include "commit-graph.h" | |
2a4aed42 | 35 | #include "sigchain.h" |
6fc9fec0 | 36 | #include "mergesort.h" |
745f7a8c NTND |
37 | |
38 | static int transfer_unpack_limit = -1; | |
39 | static int fetch_unpack_limit = -1; | |
40 | static int unpack_limit = 100; | |
41 | static int prefer_ofs_delta = 1; | |
42 | static int no_done; | |
508ea882 | 43 | static int deepen_since_ok; |
a45a2600 | 44 | static int deepen_not_ok; |
745f7a8c NTND |
45 | static int fetch_fsck_objects = -1; |
46 | static int transfer_fsck_objects = -1; | |
47 | static int agent_supported; | |
640d8b72 | 48 | static int server_supports_filtering; |
1e905bbc | 49 | static int advertise_sid; |
cac4b8e2 | 50 | static struct shallow_lock shallow_lock; |
6035d6aa | 51 | static const char *alternate_shallow_file; |
3745e269 | 52 | static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES; |
1362df0d | 53 | static struct strbuf fsck_msg_types = STRBUF_INIT; |
dd4b732d | 54 | static struct string_list uri_protocols = STRING_LIST_INIT_DUP; |
745f7a8c | 55 | |
208acbfb | 56 | /* Remember to update object flag allocation in object.h */ |
745f7a8c | 57 | #define COMPLETE (1U << 0) |
ec062838 | 58 | #define ALTERNATE (1U << 1) |
9c1e657a JT |
59 | #define COMMON (1U << 6) |
60 | #define REACH_SCRATCH (1U << 7) | |
745f7a8c NTND |
61 | |
62 | /* | |
63 | * After sending this many "have"s if we do not get any new ACK , we | |
64 | * give up traversing our history. | |
65 | */ | |
66 | #define MAX_IN_VAIN 256 | |
67 | ||
d30fe89c | 68 | static int multi_ack, use_sideband; |
7199c093 FM |
69 | /* Allow specifying sha1 if it is a ref tip. */ |
70 | #define ALLOW_TIP_SHA1 01 | |
68ee6289 FM |
71 | /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */ |
72 | #define ALLOW_REACHABLE_SHA1 02 | |
7199c093 | 73 | static unsigned int allow_unadvertised_object_request; |
745f7a8c | 74 | |
0d789a5b NTND |
75 | __attribute__((format (printf, 2, 3))) |
76 | static inline void print_verbose(const struct fetch_pack_args *args, | |
77 | const char *fmt, ...) | |
78 | { | |
79 | va_list params; | |
80 | ||
81 | if (!args->verbose) | |
82 | return; | |
83 | ||
84 | va_start(params, fmt); | |
85 | vfprintf(stderr, fmt, params); | |
86 | va_end(params); | |
87 | fputc('\n', stderr); | |
88 | } | |
89 | ||
41a078c6 JK |
90 | struct alternate_object_cache { |
91 | struct object **items; | |
92 | size_t nr, alloc; | |
93 | }; | |
94 | ||
bdf4276c | 95 | static void cache_one_alternate(const struct object_id *oid, |
41a078c6 JK |
96 | void *vcache) |
97 | { | |
98 | struct alternate_object_cache *cache = vcache; | |
109cd76d | 99 | struct object *obj = parse_object(the_repository, oid); |
41a078c6 JK |
100 | |
101 | if (!obj || (obj->flags & ALTERNATE)) | |
102 | return; | |
103 | ||
104 | obj->flags |= ALTERNATE; | |
105 | ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc); | |
106 | cache->items[cache->nr++] = obj; | |
107 | } | |
108 | ||
ec062838 JT |
109 | static void for_each_cached_alternate(struct fetch_negotiator *negotiator, |
110 | void (*cb)(struct fetch_negotiator *, | |
d30fe89c | 111 | struct object *)) |
41a078c6 JK |
112 | { |
113 | static int initialized; | |
114 | static struct alternate_object_cache cache; | |
115 | size_t i; | |
116 | ||
117 | if (!initialized) { | |
118 | for_each_alternate_ref(cache_one_alternate, &cache); | |
119 | initialized = 1; | |
120 | } | |
121 | ||
122 | for (i = 0; i < cache.nr; i++) | |
ec062838 | 123 | cb(negotiator, cache.items[i]); |
745f7a8c NTND |
124 | } |
125 | ||
5d4cc78f JT |
126 | static void die_in_commit_graph_only(const struct object_id *oid) |
127 | { | |
128 | die(_("You are attempting to fetch %s, which is in the commit graph file but not in the object database.\n" | |
129 | "This is probably due to repo corruption.\n" | |
130 | "If you are attempting to repair this repo corruption by refetching the missing object, use 'git fetch --refetch' with the missing object."), | |
131 | oid_to_hex(oid)); | |
132 | } | |
133 | ||
bf1feb9e | 134 | static struct commit *deref_without_lazy_fetch(const struct object_id *oid, |
5d4cc78f | 135 | int mark_tags_complete_and_check_obj_db) |
5c3b801d | 136 | { |
bf1feb9e JT |
137 | enum object_type type; |
138 | struct object_info info = { .typep = &type }; | |
62b5a35a PS |
139 | struct commit *commit; |
140 | ||
141 | commit = lookup_commit_in_graph(the_repository, oid); | |
5d4cc78f JT |
142 | if (commit) { |
143 | if (mark_tags_complete_and_check_obj_db) { | |
144 | if (!has_object(the_repository, oid, 0)) | |
145 | die_in_commit_graph_only(oid); | |
146 | } | |
62b5a35a | 147 | return commit; |
5d4cc78f | 148 | } |
5c3b801d JT |
149 | |
150 | while (1) { | |
151 | if (oid_object_info_extended(the_repository, oid, &info, | |
bf1feb9e | 152 | OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK)) |
5c3b801d | 153 | return NULL; |
bf1feb9e | 154 | if (type == OBJ_TAG) { |
5c3b801d JT |
155 | struct tag *tag = (struct tag *) |
156 | parse_object(the_repository, oid); | |
157 | ||
158 | if (!tag->tagged) | |
159 | return NULL; | |
5d4cc78f | 160 | if (mark_tags_complete_and_check_obj_db) |
5c3b801d JT |
161 | tag->object.flags |= COMPLETE; |
162 | oid = &tag->tagged->oid; | |
163 | } else { | |
164 | break; | |
165 | } | |
166 | } | |
3e5e6c6e | 167 | |
bf1feb9e | 168 | if (type == OBJ_COMMIT) { |
3e5e6c6e PS |
169 | struct commit *commit = lookup_commit(the_repository, oid); |
170 | if (!commit || repo_parse_commit(the_repository, commit)) | |
171 | return NULL; | |
172 | return commit; | |
173 | } | |
174 | ||
5c3b801d JT |
175 | return NULL; |
176 | } | |
177 | ||
ec062838 | 178 | static int rev_list_insert_ref(struct fetch_negotiator *negotiator, |
d30fe89c | 179 | const struct object_id *oid) |
745f7a8c | 180 | { |
5c3b801d | 181 | struct commit *c = deref_without_lazy_fetch(oid, 0); |
745f7a8c | 182 | |
5c3b801d JT |
183 | if (c) |
184 | negotiator->add_tip(negotiator, c); | |
745f7a8c NTND |
185 | return 0; |
186 | } | |
187 | ||
5cf88fd8 | 188 | static int rev_list_insert_ref_oid(const char *refname UNUSED, |
e8207717 | 189 | const char *referent UNUSED, |
63e14ee2 | 190 | const struct object_id *oid, |
5cf88fd8 | 191 | int flag UNUSED, |
63e14ee2 | 192 | void *cb_data) |
745f7a8c | 193 | { |
5c3b801d | 194 | return rev_list_insert_ref(cb_data, oid); |
745f7a8c NTND |
195 | } |
196 | ||
197 | enum ack_type { | |
198 | NAK = 0, | |
199 | ACK, | |
200 | ACK_continue, | |
201 | ACK_common, | |
202 | ACK_ready | |
203 | }; | |
204 | ||
01f9ec64 MS |
205 | static void consume_shallow_list(struct fetch_pack_args *args, |
206 | struct packet_reader *reader) | |
745f7a8c | 207 | { |
79891cb9 | 208 | if (args->stateless_rpc && args->deepen) { |
745f7a8c NTND |
209 | /* If we sent a depth we will get back "duplicate" |
210 | * shallow and unshallow commands every time there | |
211 | * is a block of have lines exchanged. | |
212 | */ | |
01f9ec64 MS |
213 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
214 | if (starts_with(reader->line, "shallow ")) | |
745f7a8c | 215 | continue; |
01f9ec64 | 216 | if (starts_with(reader->line, "unshallow ")) |
745f7a8c | 217 | continue; |
1dd73e20 | 218 | die(_("git fetch-pack: expected shallow list")); |
745f7a8c | 219 | } |
01f9ec64 MS |
220 | if (reader->status != PACKET_READ_FLUSH) |
221 | die(_("git fetch-pack: expected a flush packet after shallow list")); | |
745f7a8c NTND |
222 | } |
223 | } | |
224 | ||
01f9ec64 MS |
225 | static enum ack_type get_ack(struct packet_reader *reader, |
226 | struct object_id *result_oid) | |
745f7a8c | 227 | { |
74543a04 | 228 | int len; |
82e56767 | 229 | const char *arg; |
745f7a8c | 230 | |
01f9ec64 | 231 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) |
bc9d4dc5 | 232 | die(_("git fetch-pack: expected ACK/NAK, got a flush packet")); |
01f9ec64 MS |
233 | len = reader->pktlen; |
234 | ||
235 | if (!strcmp(reader->line, "NAK")) | |
745f7a8c | 236 | return NAK; |
01f9ec64 | 237 | if (skip_prefix(reader->line, "ACK ", &arg)) { |
f6af19a9 | 238 | const char *p; |
239 | if (!parse_oid_hex(arg, result_oid, &p)) { | |
240 | len -= p - reader->line; | |
82e56767 | 241 | if (len < 1) |
030e9dd6 | 242 | return ACK; |
f6af19a9 | 243 | if (strstr(p, "continue")) |
745f7a8c | 244 | return ACK_continue; |
f6af19a9 | 245 | if (strstr(p, "common")) |
745f7a8c | 246 | return ACK_common; |
f6af19a9 | 247 | if (strstr(p, "ready")) |
745f7a8c NTND |
248 | return ACK_ready; |
249 | return ACK; | |
250 | } | |
251 | } | |
01f9ec64 | 252 | die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line); |
745f7a8c NTND |
253 | } |
254 | ||
255 | static void send_request(struct fetch_pack_args *args, | |
256 | int fd, struct strbuf *buf) | |
257 | { | |
258 | if (args->stateless_rpc) { | |
259 | send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX); | |
260 | packet_flush(fd); | |
37c80012 JK |
261 | } else { |
262 | if (write_in_full(fd, buf->buf, buf->len) < 0) | |
263 | die_errno(_("unable to write to remote")); | |
264 | } | |
745f7a8c NTND |
265 | } |
266 | ||
ec062838 | 267 | static void insert_one_alternate_object(struct fetch_negotiator *negotiator, |
d30fe89c | 268 | struct object *obj) |
745f7a8c | 269 | { |
5c3b801d | 270 | rev_list_insert_ref(negotiator, &obj->oid); |
745f7a8c NTND |
271 | } |
272 | ||
273 | #define INITIAL_FLUSH 16 | |
274 | #define PIPESAFE_FLUSH 32 | |
da470981 | 275 | #define LARGE_FLUSH 16384 |
745f7a8c | 276 | |
685fbd32 | 277 | static int next_flush(int stateless_rpc, int count) |
745f7a8c | 278 | { |
685fbd32 | 279 | if (stateless_rpc) { |
da470981 JT |
280 | if (count < LARGE_FLUSH) |
281 | count <<= 1; | |
282 | else | |
283 | count = count * 11 / 10; | |
284 | } else { | |
285 | if (count < PIPESAFE_FLUSH) | |
286 | count <<= 1; | |
287 | else | |
288 | count += PIPESAFE_FLUSH; | |
289 | } | |
745f7a8c NTND |
290 | return count; |
291 | } | |
292 | ||
3390e42a JT |
293 | static void mark_tips(struct fetch_negotiator *negotiator, |
294 | const struct oid_array *negotiation_tips) | |
295 | { | |
296 | int i; | |
297 | ||
298 | if (!negotiation_tips) { | |
2e5c4758 PS |
299 | refs_for_each_rawref(get_main_ref_store(the_repository), |
300 | rev_list_insert_ref_oid, negotiator); | |
3390e42a JT |
301 | return; |
302 | } | |
303 | ||
304 | for (i = 0; i < negotiation_tips->nr; i++) | |
5c3b801d | 305 | rev_list_insert_ref(negotiator, &negotiation_tips->oid[i]); |
3390e42a JT |
306 | return; |
307 | } | |
308 | ||
1007557a JT |
309 | static void send_filter(struct fetch_pack_args *args, |
310 | struct strbuf *req_buf, | |
311 | int server_supports_filter) | |
312 | { | |
313 | if (args->filter_options.choice) { | |
314 | const char *spec = | |
315 | expand_list_objects_filter_spec(&args->filter_options); | |
316 | if (server_supports_filter) { | |
317 | print_verbose(args, _("Server supports filter")); | |
318 | packet_buf_write(req_buf, "filter %s", spec); | |
319 | trace2_data_string("fetch", the_repository, | |
320 | "filter/effective", spec); | |
321 | } else { | |
322 | warning("filtering not recognized by server, ignoring"); | |
323 | trace2_data_string("fetch", the_repository, | |
324 | "filter/unsupported", spec); | |
325 | } | |
326 | } else { | |
327 | trace2_data_string("fetch", the_repository, | |
328 | "filter/none", ""); | |
329 | } | |
330 | } | |
331 | ||
ec062838 | 332 | static int find_common(struct fetch_negotiator *negotiator, |
d30fe89c | 333 | struct fetch_pack_args *args, |
1b283377 | 334 | int fd[2], struct object_id *result_oid, |
745f7a8c NTND |
335 | struct ref *refs) |
336 | { | |
337 | int fetching; | |
338 | int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval; | |
a29263cf | 339 | int negotiation_round = 0, haves = 0; |
1b283377 | 340 | const struct object_id *oid; |
745f7a8c NTND |
341 | unsigned in_vain = 0; |
342 | int got_continue = 0; | |
343 | int got_ready = 0; | |
344 | struct strbuf req_buf = STRBUF_INIT; | |
345 | size_t state_len = 0; | |
01f9ec64 | 346 | struct packet_reader reader; |
745f7a8c NTND |
347 | |
348 | if (args->stateless_rpc && multi_ack == 1) | |
6fa00ee8 | 349 | die(_("the option '%s' requires '%s'"), "--stateless-rpc", "multi_ack_detailed"); |
745f7a8c | 350 | |
01f9ec64 | 351 | packet_reader_init(&reader, fd[0], NULL, 0, |
2d103c31 MS |
352 | PACKET_READ_CHOMP_NEWLINE | |
353 | PACKET_READ_DIE_ON_ERR_PACKET); | |
01f9ec64 | 354 | |
9dfa8dbe JT |
355 | mark_tips(negotiator, args->negotiation_tips); |
356 | for_each_cached_alternate(negotiator, insert_one_alternate_object); | |
745f7a8c NTND |
357 | |
358 | fetching = 0; | |
359 | for ( ; refs ; refs = refs->next) { | |
1b283377 | 360 | struct object_id *remote = &refs->old_oid; |
745f7a8c NTND |
361 | const char *remote_hex; |
362 | struct object *o; | |
363 | ||
4dfd0925 RC |
364 | if (!args->refetch) { |
365 | /* | |
366 | * If that object is complete (i.e. it is an ancestor of a | |
367 | * local ref), we tell them we have it but do not have to | |
368 | * tell them about its ancestors, which they already know | |
369 | * about. | |
370 | * | |
371 | * We use lookup_object here because we are only | |
372 | * interested in the case we *know* the object is | |
373 | * reachable and we have already scanned it. | |
374 | */ | |
375 | if (((o = lookup_object(the_repository, remote)) != NULL) && | |
376 | (o->flags & COMPLETE)) { | |
377 | continue; | |
378 | } | |
745f7a8c NTND |
379 | } |
380 | ||
1b283377 | 381 | remote_hex = oid_to_hex(remote); |
745f7a8c NTND |
382 | if (!fetching) { |
383 | struct strbuf c = STRBUF_INIT; | |
384 | if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed"); | |
385 | if (multi_ack == 1) strbuf_addstr(&c, " multi_ack"); | |
386 | if (no_done) strbuf_addstr(&c, " no-done"); | |
387 | if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k"); | |
388 | if (use_sideband == 1) strbuf_addstr(&c, " side-band"); | |
cccf74e2 | 389 | if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative"); |
745f7a8c NTND |
390 | if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack"); |
391 | if (args->no_progress) strbuf_addstr(&c, " no-progress"); | |
392 | if (args->include_tag) strbuf_addstr(&c, " include-tag"); | |
393 | if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta"); | |
508ea882 | 394 | if (deepen_since_ok) strbuf_addstr(&c, " deepen-since"); |
a45a2600 | 395 | if (deepen_not_ok) strbuf_addstr(&c, " deepen-not"); |
745f7a8c NTND |
396 | if (agent_supported) strbuf_addf(&c, " agent=%s", |
397 | git_user_agent_sanitized()); | |
1e905bbc JS |
398 | if (advertise_sid) |
399 | strbuf_addf(&c, " session-id=%s", trace2_session_id()); | |
640d8b72 JH |
400 | if (args->filter_options.choice) |
401 | strbuf_addstr(&c, " filter"); | |
745f7a8c NTND |
402 | packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf); |
403 | strbuf_release(&c); | |
404 | } else | |
405 | packet_buf_write(&req_buf, "want %s\n", remote_hex); | |
406 | fetching++; | |
407 | } | |
408 | ||
409 | if (!fetching) { | |
410 | strbuf_release(&req_buf); | |
411 | packet_flush(fd[1]); | |
412 | return 1; | |
413 | } | |
414 | ||
c8813487 | 415 | if (is_repository_shallow(the_repository)) |
1a30f5a2 | 416 | write_shallow_commits(&req_buf, 1, NULL); |
745f7a8c NTND |
417 | if (args->depth > 0) |
418 | packet_buf_write(&req_buf, "deepen %d", args->depth); | |
508ea882 | 419 | if (args->deepen_since) { |
dddbad72 | 420 | timestamp_t max_age = approxidate(args->deepen_since); |
cb71f8bd | 421 | packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age); |
508ea882 | 422 | } |
a45a2600 NTND |
423 | if (args->deepen_not) { |
424 | int i; | |
425 | for (i = 0; i < args->deepen_not->nr; i++) { | |
426 | struct string_list_item *s = args->deepen_not->items + i; | |
427 | packet_buf_write(&req_buf, "deepen-not %s", s->string); | |
428 | } | |
429 | } | |
1007557a | 430 | send_filter(args, &req_buf, server_supports_filtering); |
745f7a8c NTND |
431 | packet_buf_flush(&req_buf); |
432 | state_len = req_buf.len; | |
433 | ||
79891cb9 | 434 | if (args->deepen) { |
ae021d87 | 435 | const char *arg; |
1b283377 | 436 | struct object_id oid; |
745f7a8c NTND |
437 | |
438 | send_request(args, fd[1], &req_buf); | |
01f9ec64 MS |
439 | while (packet_reader_read(&reader) == PACKET_READ_NORMAL) { |
440 | if (skip_prefix(reader.line, "shallow ", &arg)) { | |
1b283377 | 441 | if (get_oid_hex(arg, &oid)) |
01f9ec64 | 442 | die(_("invalid shallow line: %s"), reader.line); |
19143f13 | 443 | register_shallow(the_repository, &oid); |
745f7a8c NTND |
444 | continue; |
445 | } | |
01f9ec64 | 446 | if (skip_prefix(reader.line, "unshallow ", &arg)) { |
1b283377 | 447 | if (get_oid_hex(arg, &oid)) |
01f9ec64 | 448 | die(_("invalid unshallow line: %s"), reader.line); |
d0229abd | 449 | if (!lookup_object(the_repository, &oid)) |
01f9ec64 | 450 | die(_("object not found: %s"), reader.line); |
745f7a8c | 451 | /* make sure that it is parsed as shallow */ |
109cd76d | 452 | if (!parse_object(the_repository, &oid)) |
01f9ec64 | 453 | die(_("error in object: %s"), reader.line); |
e92b848c | 454 | if (unregister_shallow(&oid)) |
01f9ec64 | 455 | die(_("no shallow found: %s"), reader.line); |
745f7a8c NTND |
456 | continue; |
457 | } | |
01f9ec64 | 458 | die(_("expected shallow/unshallow, got %s"), reader.line); |
745f7a8c NTND |
459 | } |
460 | } else if (!args->stateless_rpc) | |
461 | send_request(args, fd[1], &req_buf); | |
462 | ||
463 | if (!args->stateless_rpc) { | |
464 | /* If we aren't using the stateless-rpc interface | |
465 | * we don't need to retain the headers. | |
466 | */ | |
467 | strbuf_setlen(&req_buf, 0); | |
468 | state_len = 0; | |
469 | } | |
470 | ||
5fc31180 | 471 | trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository); |
745f7a8c NTND |
472 | flushes = 0; |
473 | retval = -1; | |
ec062838 | 474 | while ((oid = negotiator->next(negotiator))) { |
1b283377 | 475 | packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid)); |
476 | print_verbose(args, "have %s", oid_to_hex(oid)); | |
745f7a8c | 477 | in_vain++; |
a29263cf | 478 | haves++; |
745f7a8c NTND |
479 | if (flush_at <= ++count) { |
480 | int ack; | |
481 | ||
a29263cf JS |
482 | negotiation_round++; |
483 | trace2_region_enter_printf("negotiation_v0_v1", "round", | |
484 | the_repository, "%d", | |
485 | negotiation_round); | |
486 | trace2_data_intmax("negotiation_v0_v1", the_repository, | |
487 | "haves_added", haves); | |
488 | trace2_data_intmax("negotiation_v0_v1", the_repository, | |
489 | "in_vain", in_vain); | |
490 | haves = 0; | |
745f7a8c NTND |
491 | packet_buf_flush(&req_buf); |
492 | send_request(args, fd[1], &req_buf); | |
493 | strbuf_setlen(&req_buf, state_len); | |
494 | flushes++; | |
685fbd32 | 495 | flush_at = next_flush(args->stateless_rpc, count); |
745f7a8c NTND |
496 | |
497 | /* | |
498 | * We keep one window "ahead" of the other side, and | |
499 | * will wait for an ACK only on the next one | |
500 | */ | |
501 | if (!args->stateless_rpc && count == INITIAL_FLUSH) | |
502 | continue; | |
503 | ||
01f9ec64 | 504 | consume_shallow_list(args, &reader); |
745f7a8c | 505 | do { |
01f9ec64 | 506 | ack = get_ack(&reader, result_oid); |
0d789a5b | 507 | if (ack) |
1dd73e20 | 508 | print_verbose(args, _("got %s %d %s"), "ack", |
1b283377 | 509 | ack, oid_to_hex(result_oid)); |
745f7a8c NTND |
510 | switch (ack) { |
511 | case ACK: | |
a29263cf JS |
512 | trace2_region_leave_printf("negotiation_v0_v1", "round", |
513 | the_repository, "%d", | |
514 | negotiation_round); | |
745f7a8c NTND |
515 | flushes = 0; |
516 | multi_ack = 0; | |
517 | retval = 0; | |
518 | goto done; | |
519 | case ACK_common: | |
520 | case ACK_ready: | |
521 | case ACK_continue: { | |
522 | struct commit *commit = | |
c1f5eb49 SB |
523 | lookup_commit(the_repository, |
524 | result_oid); | |
d093bc75 | 525 | int was_common; |
3a2a1dc1 | 526 | |
745f7a8c | 527 | if (!commit) |
1b283377 | 528 | die(_("invalid commit %s"), oid_to_hex(result_oid)); |
ec062838 | 529 | was_common = negotiator->ack(negotiator, commit); |
745f7a8c NTND |
530 | if (args->stateless_rpc |
531 | && ack == ACK_common | |
d093bc75 | 532 | && !was_common) { |
745f7a8c NTND |
533 | /* We need to replay the have for this object |
534 | * on the next RPC request so the peer knows | |
535 | * it is in common with us. | |
536 | */ | |
1b283377 | 537 | const char *hex = oid_to_hex(result_oid); |
745f7a8c NTND |
538 | packet_buf_write(&req_buf, "have %s\n", hex); |
539 | state_len = req_buf.len; | |
a29263cf | 540 | haves++; |
06b3d386 JT |
541 | /* |
542 | * Reset in_vain because an ack | |
543 | * for this commit has not been | |
544 | * seen. | |
545 | */ | |
546 | in_vain = 0; | |
547 | } else if (!args->stateless_rpc | |
548 | || ack != ACK_common) | |
549 | in_vain = 0; | |
745f7a8c | 550 | retval = 0; |
745f7a8c | 551 | got_continue = 1; |
21bcf6e4 | 552 | if (ack == ACK_ready) |
745f7a8c | 553 | got_ready = 1; |
745f7a8c NTND |
554 | break; |
555 | } | |
556 | } | |
557 | } while (ack); | |
558 | flushes--; | |
a29263cf JS |
559 | trace2_region_leave_printf("negotiation_v0_v1", "round", |
560 | the_repository, "%d", | |
561 | negotiation_round); | |
745f7a8c | 562 | if (got_continue && MAX_IN_VAIN < in_vain) { |
1dd73e20 | 563 | print_verbose(args, _("giving up")); |
745f7a8c NTND |
564 | break; /* give up */ |
565 | } | |
21bcf6e4 JT |
566 | if (got_ready) |
567 | break; | |
745f7a8c NTND |
568 | } |
569 | } | |
570 | done: | |
5fc31180 | 571 | trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository); |
a29263cf JS |
572 | trace2_data_intmax("negotiation_v0_v1", the_repository, "total_rounds", |
573 | negotiation_round); | |
745f7a8c NTND |
574 | if (!got_ready || !no_done) { |
575 | packet_buf_write(&req_buf, "done\n"); | |
576 | send_request(args, fd[1], &req_buf); | |
577 | } | |
1dd73e20 | 578 | print_verbose(args, _("done")); |
745f7a8c NTND |
579 | if (retval != 0) { |
580 | multi_ack = 0; | |
581 | flushes++; | |
582 | } | |
583 | strbuf_release(&req_buf); | |
584 | ||
ff62eca7 | 585 | if (!got_ready || !no_done) |
01f9ec64 | 586 | consume_shallow_list(args, &reader); |
745f7a8c | 587 | while (flushes || multi_ack) { |
01f9ec64 | 588 | int ack = get_ack(&reader, result_oid); |
745f7a8c | 589 | if (ack) { |
1dd73e20 | 590 | print_verbose(args, _("got %s (%d) %s"), "ack", |
1b283377 | 591 | ack, oid_to_hex(result_oid)); |
745f7a8c NTND |
592 | if (ack == ACK) |
593 | return 0; | |
594 | multi_ack = 1; | |
595 | continue; | |
596 | } | |
597 | flushes--; | |
598 | } | |
599 | /* it is no error to fetch into a completely empty repo */ | |
600 | return count ? retval : 0; | |
601 | } | |
602 | ||
603 | static struct commit_list *complete; | |
604 | ||
1b283377 | 605 | static int mark_complete(const struct object_id *oid) |
745f7a8c | 606 | { |
5c3b801d JT |
607 | struct commit *commit = deref_without_lazy_fetch(oid, 1); |
608 | ||
609 | if (commit && !(commit->object.flags & COMPLETE)) { | |
610 | commit->object.flags |= COMPLETE; | |
611 | commit_list_insert(commit, &complete); | |
745f7a8c NTND |
612 | } |
613 | return 0; | |
614 | } | |
615 | ||
5cf88fd8 | 616 | static int mark_complete_oid(const char *refname UNUSED, |
e8207717 | 617 | const char *referent UNUSED, |
63e14ee2 | 618 | const struct object_id *oid, |
5cf88fd8 ÆAB |
619 | int flag UNUSED, |
620 | void *cb_data UNUSED) | |
f8ee4d85 | 621 | { |
1b283377 | 622 | return mark_complete(oid); |
f8ee4d85 MH |
623 | } |
624 | ||
745f7a8c | 625 | static void mark_recent_complete_commits(struct fetch_pack_args *args, |
dddbad72 | 626 | timestamp_t cutoff) |
745f7a8c NTND |
627 | { |
628 | while (complete && cutoff <= complete->item->date) { | |
1dd73e20 | 629 | print_verbose(args, _("Marking %s as complete"), |
0d789a5b | 630 | oid_to_hex(&complete->item->object.oid)); |
745f7a8c NTND |
631 | pop_most_recent_commit(&complete, COMPLETE); |
632 | } | |
633 | } | |
634 | ||
fdb69d33 JT |
635 | static void add_refs_to_oidset(struct oidset *oids, struct ref *refs) |
636 | { | |
637 | for (; refs; refs = refs->next) | |
638 | oidset_insert(oids, &refs->old_oid); | |
639 | } | |
640 | ||
bf73282c RS |
641 | static int is_unmatched_ref(const struct ref *ref) |
642 | { | |
643 | struct object_id oid; | |
644 | const char *p; | |
645 | return ref->match_status == REF_NOT_MATCHED && | |
646 | !parse_oid_hex(ref->name, &oid, &p) && | |
647 | *p == '\0' && | |
648 | oideq(&oid, &ref->old_oid); | |
649 | } | |
650 | ||
745f7a8c | 651 | static void filter_refs(struct fetch_pack_args *args, |
f2db854d JH |
652 | struct ref **refs, |
653 | struct ref **sought, int nr_sought) | |
745f7a8c NTND |
654 | { |
655 | struct ref *newlist = NULL; | |
656 | struct ref **newtail = &newlist; | |
fdb69d33 | 657 | struct ref *unmatched = NULL; |
745f7a8c | 658 | struct ref *ref, *next; |
fdb69d33 | 659 | struct oidset tip_oids = OIDSET_INIT; |
f2db854d | 660 | int i; |
22a16465 RS |
661 | int strict = !(allow_unadvertised_object_request & |
662 | (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)); | |
745f7a8c | 663 | |
f2db854d | 664 | i = 0; |
745f7a8c NTND |
665 | for (ref = *refs; ref; ref = next) { |
666 | int keep = 0; | |
667 | next = ref->next; | |
f2db854d | 668 | |
50e19a83 | 669 | if (starts_with(ref->name, "refs/") && |
34066f06 JK |
670 | check_refname_format(ref->name, 0)) { |
671 | /* | |
672 | * trash or a peeled value; do not even add it to | |
673 | * unmatched list | |
674 | */ | |
675 | free_one_ref(ref); | |
676 | continue; | |
677 | } else { | |
f2db854d JH |
678 | while (i < nr_sought) { |
679 | int cmp = strcmp(ref->name, sought[i]->name); | |
745f7a8c NTND |
680 | if (cmp < 0) |
681 | break; /* definitely do not have it */ | |
682 | else if (cmp == 0) { | |
683 | keep = 1; /* definitely have it */ | |
d56583de | 684 | sought[i]->match_status = REF_MATCHED; |
745f7a8c | 685 | } |
f2db854d | 686 | i++; |
745f7a8c | 687 | } |
745f7a8c | 688 | |
e9502c0a JK |
689 | if (!keep && args->fetch_all && |
690 | (!args->deepen || !starts_with(ref->name, "refs/tags/"))) | |
691 | keep = 1; | |
692 | } | |
745f7a8c NTND |
693 | |
694 | if (keep) { | |
695 | *newtail = ref; | |
696 | ref->next = NULL; | |
697 | newtail = &ref->next; | |
698 | } else { | |
fdb69d33 JT |
699 | ref->next = unmatched; |
700 | unmatched = ref; | |
745f7a8c NTND |
701 | } |
702 | } | |
703 | ||
22a16465 RS |
704 | if (strict) { |
705 | for (i = 0; i < nr_sought; i++) { | |
706 | ref = sought[i]; | |
707 | if (!is_unmatched_ref(ref)) | |
708 | continue; | |
709 | ||
710 | add_refs_to_oidset(&tip_oids, unmatched); | |
711 | add_refs_to_oidset(&tip_oids, newlist); | |
712 | break; | |
713 | } | |
714 | } | |
715 | ||
6e7b66ee | 716 | /* Append unmatched requests to the list */ |
d56583de | 717 | for (i = 0; i < nr_sought; i++) { |
d56583de | 718 | ref = sought[i]; |
bf73282c | 719 | if (!is_unmatched_ref(ref)) |
d56583de | 720 | continue; |
6e7b66ee | 721 | |
22a16465 | 722 | if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) { |
d56583de | 723 | ref->match_status = REF_MATCHED; |
c3c17bf1 JK |
724 | *newtail = copy_ref(ref); |
725 | newtail = &(*newtail)->next; | |
d56583de MM |
726 | } else { |
727 | ref->match_status = REF_UNADVERTISED_NOT_ALLOWED; | |
6e7b66ee JH |
728 | } |
729 | } | |
fdb69d33 JT |
730 | |
731 | oidset_clear(&tip_oids); | |
259eddde | 732 | free_refs(unmatched); |
fdb69d33 | 733 | |
745f7a8c NTND |
734 | *refs = newlist; |
735 | } | |
736 | ||
65daa9ba | 737 | static void mark_alternate_complete(struct fetch_negotiator *negotiator UNUSED, |
d30fe89c | 738 | struct object *obj) |
745f7a8c | 739 | { |
1b283377 | 740 | mark_complete(&obj->oid); |
745f7a8c NTND |
741 | } |
742 | ||
34c29034 JT |
743 | /* |
744 | * Mark recent commits available locally and reachable from a local ref as | |
9dfa8dbe | 745 | * COMPLETE. |
34c29034 JT |
746 | * |
747 | * The cutoff time for recency is determined by this heuristic: it is the | |
748 | * earliest commit time of the objects in refs that are commits and that we know | |
749 | * the commit time of. | |
750 | */ | |
ec062838 | 751 | static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator, |
d30fe89c | 752 | struct fetch_pack_args *args, |
34c29034 | 753 | struct ref **refs) |
745f7a8c NTND |
754 | { |
755 | struct ref *ref; | |
a1c6d7c1 | 756 | int old_save_commit_buffer = save_commit_buffer; |
dddbad72 | 757 | timestamp_t cutoff = 0; |
745f7a8c | 758 | |
4dfd0925 RC |
759 | if (args->refetch) |
760 | return; | |
761 | ||
745f7a8c NTND |
762 | save_commit_buffer = 0; |
763 | ||
9e5afdf9 | 764 | trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL); |
745f7a8c | 765 | for (ref = *refs; ref; ref = ref->next) { |
6fd1cc8f PS |
766 | struct commit *commit; |
767 | ||
768 | commit = lookup_commit_in_graph(the_repository, &ref->old_oid); | |
769 | if (!commit) { | |
770 | struct object *o; | |
745f7a8c | 771 | |
062b914c | 772 | if (!has_object(the_repository, &ref->old_oid, 0)) |
6fd1cc8f PS |
773 | continue; |
774 | o = parse_object(the_repository, &ref->old_oid); | |
775 | if (!o || o->type != OBJ_COMMIT) | |
776 | continue; | |
777 | ||
778 | commit = (struct commit *)o; | |
779 | } | |
745f7a8c | 780 | |
9e5afdf9 EC |
781 | /* |
782 | * We already have it -- which may mean that we were | |
745f7a8c NTND |
783 | * in sync with the other side at some time after |
784 | * that (it is OK if we guess wrong here). | |
785 | */ | |
6fd1cc8f PS |
786 | if (!cutoff || cutoff < commit->date) |
787 | cutoff = commit->date; | |
745f7a8c | 788 | } |
9e5afdf9 | 789 | trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL); |
745f7a8c | 790 | |
9e5afdf9 EC |
791 | /* |
792 | * This block marks all local refs as COMPLETE, and then recursively marks all | |
793 | * parents of those refs as COMPLETE. | |
794 | */ | |
795 | trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL); | |
12f19a98 | 796 | if (!args->deepen) { |
2e5c4758 PS |
797 | refs_for_each_rawref(get_main_ref_store(the_repository), |
798 | mark_complete_oid, NULL); | |
12f19a98 JT |
799 | for_each_cached_alternate(NULL, mark_alternate_complete); |
800 | commit_list_sort_by_date(&complete); | |
801 | if (cutoff) | |
802 | mark_recent_complete_commits(args, cutoff); | |
803 | } | |
9e5afdf9 | 804 | trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL); |
745f7a8c | 805 | |
12f19a98 JT |
806 | /* |
807 | * Mark all complete remote refs as common refs. | |
808 | * Don't mark them common yet; the server has to be told so first. | |
809 | */ | |
9e5afdf9 | 810 | trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL); |
12f19a98 | 811 | for (ref = *refs; ref; ref = ref->next) { |
5c3b801d | 812 | struct commit *c = deref_without_lazy_fetch(&ref->old_oid, 0); |
745f7a8c | 813 | |
5c3b801d | 814 | if (!c || !(c->object.flags & COMPLETE)) |
12f19a98 | 815 | continue; |
745f7a8c | 816 | |
5c3b801d | 817 | negotiator->known_common(negotiator, c); |
745f7a8c | 818 | } |
9e5afdf9 | 819 | trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL); |
745f7a8c | 820 | |
34c29034 JT |
821 | save_commit_buffer = old_save_commit_buffer; |
822 | } | |
823 | ||
824 | /* | |
825 | * Returns 1 if every object pointed to by the given remote refs is available | |
826 | * locally and reachable from a local ref, and 0 otherwise. | |
827 | */ | |
828 | static int everything_local(struct fetch_pack_args *args, | |
829 | struct ref **refs) | |
830 | { | |
831 | struct ref *ref; | |
832 | int retval; | |
745f7a8c NTND |
833 | |
834 | for (retval = 1, ref = *refs; ref ; ref = ref->next) { | |
1b283377 | 835 | const struct object_id *remote = &ref->old_oid; |
745f7a8c NTND |
836 | struct object *o; |
837 | ||
d0229abd | 838 | o = lookup_object(the_repository, remote); |
745f7a8c NTND |
839 | if (!o || !(o->flags & COMPLETE)) { |
840 | retval = 0; | |
1b283377 | 841 | print_verbose(args, "want %s (%s)", oid_to_hex(remote), |
0d789a5b | 842 | ref->name); |
745f7a8c NTND |
843 | continue; |
844 | } | |
1b283377 | 845 | print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote), |
0d789a5b | 846 | ref->name); |
745f7a8c | 847 | } |
a1c6d7c1 | 848 | |
745f7a8c NTND |
849 | return retval; |
850 | } | |
851 | ||
5cf88fd8 | 852 | static int sideband_demux(int in UNUSED, int out, void *data) |
745f7a8c NTND |
853 | { |
854 | int *xd = data; | |
9ff18faf | 855 | int ret; |
745f7a8c | 856 | |
9ff18faf | 857 | ret = recv_sideband("fetch-pack", xd[0], out); |
745f7a8c NTND |
858 | close(out); |
859 | return ret; | |
860 | } | |
861 | ||
9d7fa3be CC |
862 | static void create_promisor_file(const char *keep_name, |
863 | struct ref **sought, int nr_sought) | |
5374a290 JT |
864 | { |
865 | struct strbuf promisor_name = STRBUF_INIT; | |
866 | int suffix_stripped; | |
5374a290 JT |
867 | |
868 | strbuf_addstr(&promisor_name, keep_name); | |
869 | suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep"); | |
870 | if (!suffix_stripped) | |
871 | BUG("name of pack lockfile should end with .keep (was '%s')", | |
872 | keep_name); | |
873 | strbuf_addstr(&promisor_name, ".promisor"); | |
874 | ||
33add2ad | 875 | write_promisor_file(promisor_name.buf, sought, nr_sought); |
5374a290 JT |
876 | |
877 | strbuf_release(&promisor_name); | |
878 | } | |
879 | ||
5476e1ef JT |
880 | static void parse_gitmodules_oids(int fd, struct oidset *gitmodules_oids) |
881 | { | |
882 | int len = the_hash_algo->hexsz + 1; /* hash + NL */ | |
883 | ||
884 | do { | |
885 | char hex_hash[GIT_MAX_HEXSZ + 1]; | |
886 | int read_len = read_in_full(fd, hex_hash, len); | |
887 | struct object_id oid; | |
888 | const char *end; | |
889 | ||
890 | if (!read_len) | |
891 | return; | |
892 | if (read_len != len) | |
893 | die("invalid length read %d", read_len); | |
894 | if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n') | |
895 | die("invalid hash"); | |
896 | oidset_insert(gitmodules_oids, &oid); | |
897 | } while (1); | |
898 | } | |
899 | ||
1f6cf450 ÆAB |
900 | static void add_index_pack_keep_option(struct strvec *args) |
901 | { | |
902 | char hostname[HOST_NAME_MAX + 1]; | |
903 | ||
904 | if (xgethostname(hostname, sizeof(hostname))) | |
905 | xsnprintf(hostname, sizeof(hostname), "localhost"); | |
906 | strvec_pushf(args, "--keep=fetch-pack %"PRIuMAX " on %s", | |
907 | (uintmax_t)getpid(), hostname); | |
908 | } | |
909 | ||
ece9aea2 | 910 | /* |
b664e9ff JT |
911 | * If packfile URIs were provided, pass a non-NULL pointer to index_pack_args. |
912 | * The strings to pass as the --index-pack-arg arguments to http-fetch will be | |
913 | * stored there. (It must be freed by the caller.) | |
ece9aea2 | 914 | */ |
745f7a8c | 915 | static int get_pack(struct fetch_pack_args *args, |
9da69a65 | 916 | int xd[2], struct string_list *pack_lockfiles, |
b664e9ff | 917 | struct strvec *index_pack_args, |
5476e1ef JT |
918 | struct ref **sought, int nr_sought, |
919 | struct oidset *gitmodules_oids) | |
745f7a8c NTND |
920 | { |
921 | struct async demux; | |
745f7a8c | 922 | int do_keep = args->keep_pack; |
984a43b9 JK |
923 | const char *cmd_name; |
924 | struct pack_header header; | |
925 | int pass_header = 0; | |
d3180279 | 926 | struct child_process cmd = CHILD_PROCESS_INIT; |
5476e1ef | 927 | int fsck_objects = 0; |
c6807a40 | 928 | int ret; |
745f7a8c NTND |
929 | |
930 | memset(&demux, 0, sizeof(demux)); | |
931 | if (use_sideband) { | |
932 | /* xd[] is talking with upload-pack; subprocess reads from | |
933 | * xd[0], spits out band#2 to stderr, and feeds us band#1 | |
934 | * through demux->out. | |
935 | */ | |
936 | demux.proc = sideband_demux; | |
937 | demux.data = xd; | |
938 | demux.out = -1; | |
df857572 | 939 | demux.isolate_sigpipe = 1; |
745f7a8c | 940 | if (start_async(&demux)) |
1dd73e20 | 941 | die(_("fetch-pack: unable to fork off sideband demultiplexer")); |
745f7a8c NTND |
942 | } |
943 | else | |
944 | demux.out = xd[0]; | |
945 | ||
2aec3bc4 | 946 | if (!args->keep_pack && unpack_limit && !index_pack_args) { |
745f7a8c NTND |
947 | |
948 | if (read_pack_header(demux.out, &header)) | |
1dd73e20 | 949 | die(_("protocol error: bad pack header")); |
984a43b9 | 950 | pass_header = 1; |
745f7a8c NTND |
951 | if (ntohl(header.hdr_entries) < unpack_limit) |
952 | do_keep = 0; | |
953 | else | |
954 | do_keep = 1; | |
955 | } | |
956 | ||
6035d6aa | 957 | if (alternate_shallow_file) { |
ef8d7ac4 JK |
958 | strvec_push(&cmd.args, "--shallow-file"); |
959 | strvec_push(&cmd.args, alternate_shallow_file); | |
6035d6aa NTND |
960 | } |
961 | ||
d0cbc756 | 962 | fsck_objects = fetch_pack_fsck_objects(); |
5476e1ef JT |
963 | |
964 | if (do_keep || args->from_promisor || index_pack_args || fsck_objects) { | |
965 | if (pack_lockfiles || fsck_objects) | |
745f7a8c | 966 | cmd.out = -1; |
984a43b9 | 967 | cmd_name = "index-pack"; |
ef8d7ac4 JK |
968 | strvec_push(&cmd.args, cmd_name); |
969 | strvec_push(&cmd.args, "--stdin"); | |
745f7a8c | 970 | if (!args->quiet && !args->no_progress) |
ef8d7ac4 | 971 | strvec_push(&cmd.args, "-v"); |
745f7a8c | 972 | if (args->use_thin_pack) |
ef8d7ac4 | 973 | strvec_push(&cmd.args, "--fix-thin"); |
1f6cf450 ÆAB |
974 | if ((do_keep || index_pack_args) && (args->lock_pack || unpack_limit)) |
975 | add_index_pack_keep_option(&cmd.args); | |
b664e9ff | 976 | if (!index_pack_args && args->check_self_contained_and_connected) |
ef8d7ac4 | 977 | strvec_push(&cmd.args, "--check-self-contained-and-connected"); |
dd4b732d JT |
978 | else |
979 | /* | |
980 | * We cannot perform any connectivity checks because | |
981 | * not all packs have been downloaded; let the caller | |
982 | * have this responsibility. | |
983 | */ | |
984 | args->check_self_contained_and_connected = 0; | |
1b03df5f JT |
985 | |
986 | if (args->from_promisor) | |
987 | /* | |
9d7fa3be | 988 | * create_promisor_file() may be called afterwards but |
1b03df5f JT |
989 | * we still need index-pack to know that this is a |
990 | * promisor pack. For example, if transfer.fsckobjects | |
991 | * is true, index-pack needs to know that .gitmodules | |
992 | * is a promisor object (so that it won't complain if | |
993 | * it is missing). | |
994 | */ | |
ef8d7ac4 | 995 | strvec_push(&cmd.args, "--promisor"); |
745f7a8c NTND |
996 | } |
997 | else { | |
984a43b9 | 998 | cmd_name = "unpack-objects"; |
ef8d7ac4 | 999 | strvec_push(&cmd.args, cmd_name); |
745f7a8c | 1000 | if (args->quiet || args->no_progress) |
ef8d7ac4 | 1001 | strvec_push(&cmd.args, "-q"); |
c6807a40 | 1002 | args->check_self_contained_and_connected = 0; |
745f7a8c | 1003 | } |
984a43b9 JK |
1004 | |
1005 | if (pass_header) | |
ef8d7ac4 | 1006 | strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32, |
f6d8942b | 1007 | ntohl(header.hdr_version), |
984a43b9 | 1008 | ntohl(header.hdr_entries)); |
5476e1ef | 1009 | if (fsck_objects) { |
b664e9ff | 1010 | if (args->from_promisor || index_pack_args) |
98a2ea46 JT |
1011 | /* |
1012 | * We cannot use --strict in index-pack because it | |
1013 | * checks both broken objects and links, but we only | |
1014 | * want to check for broken objects. | |
1015 | */ | |
ef8d7ac4 | 1016 | strvec_push(&cmd.args, "--fsck-objects"); |
98a2ea46 | 1017 | else |
ef8d7ac4 | 1018 | strvec_pushf(&cmd.args, "--strict%s", |
f6d8942b | 1019 | fsck_msg_types.buf); |
98a2ea46 | 1020 | } |
745f7a8c | 1021 | |
b664e9ff JT |
1022 | if (index_pack_args) { |
1023 | int i; | |
1024 | ||
1025 | for (i = 0; i < cmd.args.nr; i++) | |
1026 | strvec_push(index_pack_args, cmd.args.v[i]); | |
1027 | } | |
1028 | ||
2a4aed42 JK |
1029 | sigchain_push(SIGPIPE, SIG_IGN); |
1030 | ||
745f7a8c NTND |
1031 | cmd.in = demux.out; |
1032 | cmd.git_cmd = 1; | |
1033 | if (start_command(&cmd)) | |
1dd73e20 | 1034 | die(_("fetch-pack: unable to fork off %s"), cmd_name); |
5476e1ef JT |
1035 | if (do_keep && (pack_lockfiles || fsck_objects)) { |
1036 | int is_well_formed; | |
e2f6f765 KN |
1037 | char *pack_lockfile = index_pack_lockfile(the_repository, |
1038 | cmd.out, | |
1039 | &is_well_formed); | |
5476e1ef JT |
1040 | |
1041 | if (!is_well_formed) | |
1042 | die(_("fetch-pack: invalid index-pack output")); | |
96a6621d | 1043 | if (pack_lockfiles && pack_lockfile) |
6031af38 | 1044 | string_list_append_nodup(pack_lockfiles, pack_lockfile); |
96a6621d JK |
1045 | else |
1046 | free(pack_lockfile); | |
5476e1ef | 1047 | parse_gitmodules_oids(cmd.out, gitmodules_oids); |
745f7a8c NTND |
1048 | close(cmd.out); |
1049 | } | |
1050 | ||
37cb1dd6 JL |
1051 | if (!use_sideband) |
1052 | /* Closed by start_command() */ | |
1053 | xd[0] = -1; | |
1054 | ||
c6807a40 NTND |
1055 | ret = finish_command(&cmd); |
1056 | if (!ret || (args->check_self_contained_and_connected && ret == 1)) | |
1057 | args->self_contained_and_connected = | |
1058 | args->check_self_contained_and_connected && | |
1059 | ret == 0; | |
1060 | else | |
1dd73e20 | 1061 | die(_("%s failed"), cmd_name); |
745f7a8c | 1062 | if (use_sideband && finish_async(&demux)) |
1dd73e20 | 1063 | die(_("error in sideband demultiplexer")); |
5374a290 | 1064 | |
2a4aed42 JK |
1065 | sigchain_pop(SIGPIPE); |
1066 | ||
5374a290 JT |
1067 | /* |
1068 | * Now that index-pack has succeeded, write the promisor file using the | |
1069 | * obtained .keep filename if necessary | |
1070 | */ | |
9da69a65 | 1071 | if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor) |
9d7fa3be | 1072 | create_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought); |
5374a290 | 1073 | |
745f7a8c NTND |
1074 | return 0; |
1075 | } | |
1076 | ||
6fc9fec0 RS |
1077 | static int ref_compare_name(const struct ref *a, const struct ref *b) |
1078 | { | |
1079 | return strcmp(a->name, b->name); | |
1080 | } | |
1081 | ||
1082 | DEFINE_LIST_SORT(static, sort_ref_list, struct ref, next); | |
1083 | ||
f2db854d JH |
1084 | static int cmp_ref_by_name(const void *a_, const void *b_) |
1085 | { | |
1086 | const struct ref *a = *((const struct ref **)a_); | |
1087 | const struct ref *b = *((const struct ref **)b_); | |
1088 | return strcmp(a->name, b->name); | |
1089 | } | |
1090 | ||
745f7a8c NTND |
1091 | static struct ref *do_fetch_pack(struct fetch_pack_args *args, |
1092 | int fd[2], | |
1093 | const struct ref *orig_ref, | |
f2db854d | 1094 | struct ref **sought, int nr_sought, |
beea4152 | 1095 | struct shallow_info *si, |
9da69a65 | 1096 | struct string_list *pack_lockfiles) |
745f7a8c | 1097 | { |
aaf633c2 | 1098 | struct repository *r = the_repository; |
745f7a8c | 1099 | struct ref *ref = copy_ref_list(orig_ref); |
1b283377 | 1100 | struct object_id oid; |
745f7a8c | 1101 | const char *agent_feature; |
7ce4c8f7 | 1102 | size_t agent_len; |
603960b5 JT |
1103 | struct fetch_negotiator negotiator_alloc; |
1104 | struct fetch_negotiator *negotiator; | |
1105 | ||
9dfa8dbe | 1106 | negotiator = &negotiator_alloc; |
4dfd0925 RC |
1107 | if (args->refetch) { |
1108 | fetch_negotiator_init_noop(negotiator); | |
1109 | } else { | |
1110 | fetch_negotiator_init(r, negotiator); | |
1111 | } | |
745f7a8c NTND |
1112 | |
1113 | sort_ref_list(&ref, ref_compare_name); | |
9ed0d8d6 | 1114 | QSORT(sought, nr_sought, cmp_ref_by_name); |
745f7a8c | 1115 | |
0e042971 NTND |
1116 | if ((agent_feature = server_feature_value("agent", &agent_len))) { |
1117 | agent_supported = 1; | |
1118 | if (agent_len) | |
1119 | print_verbose(args, _("Server version is %.*s"), | |
7ce4c8f7 | 1120 | (int)agent_len, agent_feature); |
0e042971 NTND |
1121 | } |
1122 | ||
1e905bbc JS |
1123 | if (!server_supports("session-id")) |
1124 | advertise_sid = 0; | |
1125 | ||
5a88583b NTND |
1126 | if (server_supports("shallow")) |
1127 | print_verbose(args, _("Server supports %s"), "shallow"); | |
aaf633c2 | 1128 | else if (args->depth > 0 || is_repository_shallow(r)) |
1dd73e20 | 1129 | die(_("Server does not support shallow clients")); |
a45a2600 | 1130 | if (args->depth > 0 || args->deepen_since || args->deepen_not) |
79891cb9 | 1131 | args->deepen = 1; |
745f7a8c | 1132 | if (server_supports("multi_ack_detailed")) { |
0778b293 | 1133 | print_verbose(args, _("Server supports %s"), "multi_ack_detailed"); |
745f7a8c NTND |
1134 | multi_ack = 2; |
1135 | if (server_supports("no-done")) { | |
0778b293 | 1136 | print_verbose(args, _("Server supports %s"), "no-done"); |
745f7a8c NTND |
1137 | if (args->stateless_rpc) |
1138 | no_done = 1; | |
1139 | } | |
1140 | } | |
1141 | else if (server_supports("multi_ack")) { | |
0778b293 | 1142 | print_verbose(args, _("Server supports %s"), "multi_ack"); |
745f7a8c NTND |
1143 | multi_ack = 1; |
1144 | } | |
1145 | if (server_supports("side-band-64k")) { | |
0778b293 | 1146 | print_verbose(args, _("Server supports %s"), "side-band-64k"); |
745f7a8c NTND |
1147 | use_sideband = 2; |
1148 | } | |
1149 | else if (server_supports("side-band")) { | |
0778b293 | 1150 | print_verbose(args, _("Server supports %s"), "side-band"); |
745f7a8c NTND |
1151 | use_sideband = 1; |
1152 | } | |
6e7b66ee | 1153 | if (server_supports("allow-tip-sha1-in-want")) { |
0778b293 | 1154 | print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want"); |
7199c093 | 1155 | allow_unadvertised_object_request |= ALLOW_TIP_SHA1; |
6e7b66ee | 1156 | } |
68ee6289 | 1157 | if (server_supports("allow-reachable-sha1-in-want")) { |
0778b293 | 1158 | print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want"); |
68ee6289 FM |
1159 | allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1; |
1160 | } | |
5a88583b NTND |
1161 | if (server_supports("thin-pack")) |
1162 | print_verbose(args, _("Server supports %s"), "thin-pack"); | |
1163 | else | |
745f7a8c | 1164 | args->use_thin_pack = 0; |
5a88583b NTND |
1165 | if (server_supports("no-progress")) |
1166 | print_verbose(args, _("Server supports %s"), "no-progress"); | |
1167 | else | |
745f7a8c | 1168 | args->no_progress = 0; |
5a88583b NTND |
1169 | if (server_supports("include-tag")) |
1170 | print_verbose(args, _("Server supports %s"), "include-tag"); | |
1171 | else | |
745f7a8c | 1172 | args->include_tag = 0; |
0d789a5b | 1173 | if (server_supports("ofs-delta")) |
0778b293 | 1174 | print_verbose(args, _("Server supports %s"), "ofs-delta"); |
0d789a5b | 1175 | else |
745f7a8c NTND |
1176 | prefer_ofs_delta = 0; |
1177 | ||
640d8b72 JH |
1178 | if (server_supports("filter")) { |
1179 | server_supports_filtering = 1; | |
0778b293 | 1180 | print_verbose(args, _("Server supports %s"), "filter"); |
640d8b72 JH |
1181 | } else if (args->filter_options.choice) { |
1182 | warning("filtering not recognized by server, ignoring"); | |
1183 | } | |
1184 | ||
5a88583b NTND |
1185 | if (server_supports("deepen-since")) { |
1186 | print_verbose(args, _("Server supports %s"), "deepen-since"); | |
508ea882 | 1187 | deepen_since_ok = 1; |
5a88583b | 1188 | } else if (args->deepen_since) |
508ea882 | 1189 | die(_("Server does not support --shallow-since")); |
5a88583b NTND |
1190 | if (server_supports("deepen-not")) { |
1191 | print_verbose(args, _("Server supports %s"), "deepen-not"); | |
a45a2600 | 1192 | deepen_not_ok = 1; |
5a88583b | 1193 | } else if (args->deepen_not) |
a45a2600 | 1194 | die(_("Server does not support --shallow-exclude")); |
5a88583b NTND |
1195 | if (server_supports("deepen-relative")) |
1196 | print_verbose(args, _("Server supports %s"), "deepen-relative"); | |
1197 | else if (args->deepen_relative) | |
cccf74e2 | 1198 | die(_("Server does not support --deepen")); |
48bf1415 | 1199 | if (!server_supports_hash(the_hash_algo->name, NULL)) |
1200 | die(_("Server does not support this repository's object format")); | |
745f7a8c | 1201 | |
9dfa8dbe JT |
1202 | mark_complete_and_common_ref(negotiator, args, &ref); |
1203 | filter_refs(args, &ref, sought, nr_sought); | |
4dfd0925 | 1204 | if (!args->refetch && everything_local(args, &ref)) { |
9dfa8dbe JT |
1205 | packet_flush(fd[1]); |
1206 | goto all_done; | |
745f7a8c | 1207 | } |
603960b5 | 1208 | if (find_common(negotiator, args, fd, &oid, ref) < 0) |
745f7a8c NTND |
1209 | if (!args->keep_pack) |
1210 | /* When cloning, it is not unusual to have | |
1211 | * no common commit. | |
1212 | */ | |
1dd73e20 | 1213 | warning(_("no common commits")); |
745f7a8c NTND |
1214 | |
1215 | if (args->stateless_rpc) | |
1216 | packet_flush(fd[1]); | |
79891cb9 | 1217 | if (args->deepen) |
1a30f5a2 NTND |
1218 | setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, |
1219 | NULL); | |
4fe788b1 LL |
1220 | else if (si->nr_ours || si->nr_theirs) { |
1221 | if (args->reject_shallow_remote) | |
1222 | die(_("source repository is shallow, reject to clone.")); | |
beea4152 | 1223 | alternate_shallow_file = setup_temporary_shallow(si->shallow); |
4fe788b1 | 1224 | } else |
6da8bdcb | 1225 | alternate_shallow_file = NULL; |
5476e1ef | 1226 | if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought, |
3745e269 | 1227 | &fsck_options.gitmodules_found)) |
1dd73e20 | 1228 | die(_("git fetch-pack: fetch failed.")); |
3745e269 ÆAB |
1229 | if (fsck_finish(&fsck_options)) |
1230 | die("fsck failed"); | |
745f7a8c NTND |
1231 | |
1232 | all_done: | |
603960b5 JT |
1233 | if (negotiator) |
1234 | negotiator->release(negotiator); | |
745f7a8c NTND |
1235 | return ref; |
1236 | } | |
1237 | ||
f7e20501 BW |
1238 | static void add_shallow_requests(struct strbuf *req_buf, |
1239 | const struct fetch_pack_args *args) | |
1240 | { | |
00624d60 | 1241 | if (is_repository_shallow(the_repository)) |
f7e20501 BW |
1242 | write_shallow_commits(req_buf, 1, NULL); |
1243 | if (args->depth > 0) | |
1244 | packet_buf_write(req_buf, "deepen %d", args->depth); | |
1245 | if (args->deepen_since) { | |
1246 | timestamp_t max_age = approxidate(args->deepen_since); | |
1247 | packet_buf_write(req_buf, "deepen-since %"PRItime, max_age); | |
1248 | } | |
1249 | if (args->deepen_not) { | |
1250 | int i; | |
1251 | for (i = 0; i < args->deepen_not->nr; i++) { | |
1252 | struct string_list_item *s = args->deepen_not->items + i; | |
1253 | packet_buf_write(req_buf, "deepen-not %s", s->string); | |
1254 | } | |
1255 | } | |
5056cf4a JT |
1256 | if (args->deepen_relative) |
1257 | packet_buf_write(req_buf, "deepen-relative\n"); | |
f7e20501 BW |
1258 | } |
1259 | ||
9dfa8dbe | 1260 | static void add_wants(const struct ref *wants, struct strbuf *req_buf) |
685fbd32 | 1261 | { |
73302051 BW |
1262 | int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0); |
1263 | ||
685fbd32 BW |
1264 | for ( ; wants ; wants = wants->next) { |
1265 | const struct object_id *remote = &wants->old_oid; | |
685fbd32 BW |
1266 | struct object *o; |
1267 | ||
1268 | /* | |
1269 | * If that object is complete (i.e. it is an ancestor of a | |
1270 | * local ref), we tell them we have it but do not have to | |
1271 | * tell them about its ancestors, which they already know | |
1272 | * about. | |
1273 | * | |
1274 | * We use lookup_object here because we are only | |
1275 | * interested in the case we *know* the object is | |
1276 | * reachable and we have already scanned it. | |
1277 | */ | |
9dfa8dbe | 1278 | if (((o = lookup_object(the_repository, remote)) != NULL) && |
685fbd32 BW |
1279 | (o->flags & COMPLETE)) { |
1280 | continue; | |
1281 | } | |
1282 | ||
73302051 BW |
1283 | if (!use_ref_in_want || wants->exact_oid) |
1284 | packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote)); | |
1285 | else | |
1286 | packet_buf_write(req_buf, "want-ref %s\n", wants->name); | |
685fbd32 BW |
1287 | } |
1288 | } | |
1289 | ||
1290 | static void add_common(struct strbuf *req_buf, struct oidset *common) | |
1291 | { | |
1292 | struct oidset_iter iter; | |
1293 | const struct object_id *oid; | |
1294 | oidset_iter_init(common, &iter); | |
1295 | ||
1296 | while ((oid = oidset_iter_next(&iter))) { | |
1297 | packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid)); | |
1298 | } | |
1299 | } | |
1300 | ||
ec062838 JT |
1301 | static int add_haves(struct fetch_negotiator *negotiator, |
1302 | struct strbuf *req_buf, | |
57c3451b | 1303 | int *haves_to_send) |
685fbd32 | 1304 | { |
685fbd32 BW |
1305 | int haves_added = 0; |
1306 | const struct object_id *oid; | |
1307 | ||
ec062838 | 1308 | while ((oid = negotiator->next(negotiator))) { |
685fbd32 BW |
1309 | packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid)); |
1310 | if (++haves_added >= *haves_to_send) | |
1311 | break; | |
1312 | } | |
1313 | ||
685fbd32 BW |
1314 | /* Increase haves to send on next round */ |
1315 | *haves_to_send = next_flush(1, *haves_to_send); | |
1316 | ||
57c3451b | 1317 | return haves_added; |
685fbd32 BW |
1318 | } |
1319 | ||
6871d0ce JT |
1320 | static void write_fetch_command_and_capabilities(struct strbuf *req_buf, |
1321 | const struct string_list *server_options) | |
685fbd32 | 1322 | { |
4b831208 | 1323 | const char *hash_name; |
685fbd32 | 1324 | |
a31cfe32 JK |
1325 | ensure_server_supports_v2("fetch"); |
1326 | packet_buf_write(req_buf, "command=fetch"); | |
1327 | if (server_supports_v2("agent")) | |
6871d0ce | 1328 | packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized()); |
a31cfe32 | 1329 | if (advertise_sid && server_supports_v2("session-id")) |
6871d0ce | 1330 | packet_buf_write(req_buf, "session-id=%s", trace2_session_id()); |
a31cfe32 | 1331 | if (server_options && server_options->nr) { |
5e3548ef | 1332 | int i; |
a31cfe32 | 1333 | ensure_server_supports_v2("server-option"); |
6871d0ce JT |
1334 | for (i = 0; i < server_options->nr; i++) |
1335 | packet_buf_write(req_buf, "server-option=%s", | |
1336 | server_options->items[i].string); | |
5e3548ef | 1337 | } |
685fbd32 | 1338 | |
4b831208 | 1339 | if (server_feature_v2("object-format", &hash_name)) { |
1340 | int hash_algo = hash_algo_by_name(hash_name); | |
1341 | if (hash_algo_by_ptr(the_hash_algo) != hash_algo) | |
1342 | die(_("mismatched algorithms: client %s; server %s"), | |
1343 | the_hash_algo->name, hash_name); | |
6871d0ce | 1344 | packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name); |
4b831208 | 1345 | } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) { |
1346 | die(_("the server does not support algorithm '%s'"), | |
1347 | the_hash_algo->name); | |
1348 | } | |
6871d0ce JT |
1349 | packet_buf_delim(req_buf); |
1350 | } | |
1351 | ||
1352 | static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out, | |
1353 | struct fetch_pack_args *args, | |
1354 | const struct ref *wants, struct oidset *common, | |
1355 | int *haves_to_send, int *in_vain, | |
1356 | int sideband_all, int seen_ack) | |
1357 | { | |
1358 | int haves_added; | |
1359 | int done_sent = 0; | |
1360 | struct strbuf req_buf = STRBUF_INIT; | |
1361 | ||
1362 | write_fetch_command_and_capabilities(&req_buf, args->server_options); | |
4b831208 | 1363 | |
685fbd32 BW |
1364 | if (args->use_thin_pack) |
1365 | packet_buf_write(&req_buf, "thin-pack"); | |
1366 | if (args->no_progress) | |
1367 | packet_buf_write(&req_buf, "no-progress"); | |
1368 | if (args->include_tag) | |
1369 | packet_buf_write(&req_buf, "include-tag"); | |
1370 | if (prefer_ofs_delta) | |
1371 | packet_buf_write(&req_buf, "ofs-delta"); | |
0bbc0bc5 JT |
1372 | if (sideband_all) |
1373 | packet_buf_write(&req_buf, "sideband-all"); | |
685fbd32 | 1374 | |
f7e20501 BW |
1375 | /* Add shallow-info and deepen request */ |
1376 | if (server_supports_feature("fetch", "shallow", 0)) | |
1377 | add_shallow_requests(&req_buf, args); | |
00624d60 | 1378 | else if (is_repository_shallow(the_repository) || args->deepen) |
f7e20501 BW |
1379 | die(_("Server does not support shallow requests")); |
1380 | ||
ba95710a | 1381 | /* Add filter */ |
1007557a JT |
1382 | send_filter(args, &req_buf, |
1383 | server_supports_feature("fetch", "filter", 0)); | |
ba95710a | 1384 | |
dd4b732d JT |
1385 | if (server_supports_feature("fetch", "packfile-uris", 0)) { |
1386 | int i; | |
1387 | struct strbuf to_send = STRBUF_INIT; | |
1388 | ||
1389 | for (i = 0; i < uri_protocols.nr; i++) { | |
1390 | const char *s = uri_protocols.items[i].string; | |
1391 | ||
1392 | if (!strcmp(s, "https") || !strcmp(s, "http")) { | |
1393 | if (to_send.len) | |
1394 | strbuf_addch(&to_send, ','); | |
1395 | strbuf_addstr(&to_send, s); | |
1396 | } | |
1397 | } | |
1398 | if (to_send.len) { | |
1399 | packet_buf_write(&req_buf, "packfile-uris %s", | |
1400 | to_send.buf); | |
1401 | strbuf_release(&to_send); | |
1402 | } | |
1403 | } | |
1404 | ||
685fbd32 | 1405 | /* add wants */ |
9dfa8dbe | 1406 | add_wants(wants, &req_buf); |
685fbd32 | 1407 | |
9dfa8dbe JT |
1408 | /* Add all of the common commits we've found in previous rounds */ |
1409 | add_common(&req_buf, common); | |
685fbd32 | 1410 | |
57c3451b JT |
1411 | haves_added = add_haves(negotiator, &req_buf, haves_to_send); |
1412 | *in_vain += haves_added; | |
a29263cf JS |
1413 | trace2_data_intmax("negotiation_v2", the_repository, "haves_added", haves_added); |
1414 | trace2_data_intmax("negotiation_v2", the_repository, "in_vain", *in_vain); | |
57c3451b JT |
1415 | if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) { |
1416 | /* Send Done */ | |
1417 | packet_buf_write(&req_buf, "done\n"); | |
1418 | done_sent = 1; | |
1419 | } | |
685fbd32 BW |
1420 | |
1421 | /* Send request */ | |
1422 | packet_buf_flush(&req_buf); | |
37c80012 JK |
1423 | if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0) |
1424 | die_errno(_("unable to write request to remote")); | |
685fbd32 BW |
1425 | |
1426 | strbuf_release(&req_buf); | |
57c3451b | 1427 | return done_sent; |
685fbd32 BW |
1428 | } |
1429 | ||
1430 | /* | |
1431 | * Processes a section header in a server's response and checks if it matches | |
1432 | * `section`. If the value of `peek` is 1, the header line will be peeked (and | |
1433 | * not consumed); if 0, the line will be consumed and the function will die if | |
1434 | * the section header doesn't match what was expected. | |
1435 | */ | |
1436 | static int process_section_header(struct packet_reader *reader, | |
1437 | const char *section, int peek) | |
1438 | { | |
7709acf7 | 1439 | int ret = 0; |
685fbd32 | 1440 | |
7709acf7 JT |
1441 | if (packet_reader_peek(reader) == PACKET_READ_NORMAL && |
1442 | !strcmp(reader->line, section)) | |
1443 | ret = 1; | |
685fbd32 BW |
1444 | |
1445 | if (!peek) { | |
7709acf7 JT |
1446 | if (!ret) { |
1447 | if (reader->line) | |
1448 | die(_("expected '%s', received '%s'"), | |
1449 | section, reader->line); | |
1450 | else | |
1451 | die(_("expected '%s'"), section); | |
1452 | } | |
685fbd32 BW |
1453 | packet_reader_read(reader); |
1454 | } | |
1455 | ||
1456 | return ret; | |
1457 | } | |
1458 | ||
81025703 JT |
1459 | static int process_ack(struct fetch_negotiator *negotiator, |
1460 | struct packet_reader *reader, | |
1461 | struct object_id *common_oid, | |
1462 | int *received_ready) | |
685fbd32 | 1463 | { |
685fbd32 BW |
1464 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
1465 | const char *arg; | |
1466 | ||
1467 | if (!strcmp(reader->line, "NAK")) | |
1468 | continue; | |
1469 | ||
1470 | if (skip_prefix(reader->line, "ACK ", &arg)) { | |
81025703 | 1471 | if (!get_oid_hex(arg, common_oid)) { |
685fbd32 | 1472 | struct commit *commit; |
81025703 | 1473 | commit = lookup_commit(the_repository, common_oid); |
603960b5 JT |
1474 | if (negotiator) |
1475 | negotiator->ack(negotiator, commit); | |
685fbd32 | 1476 | } |
81025703 | 1477 | return 1; |
685fbd32 BW |
1478 | } |
1479 | ||
1480 | if (!strcmp(reader->line, "ready")) { | |
81025703 | 1481 | *received_ready = 1; |
685fbd32 BW |
1482 | continue; |
1483 | } | |
1484 | ||
bbb19a8b | 1485 | die(_("unexpected acknowledgment line: '%s'"), reader->line); |
685fbd32 BW |
1486 | } |
1487 | ||
1488 | if (reader->status != PACKET_READ_FLUSH && | |
1489 | reader->status != PACKET_READ_DELIM) | |
bbb19a8b | 1490 | die(_("error processing acks: %d"), reader->status); |
685fbd32 | 1491 | |
5400b2a2 JT |
1492 | /* |
1493 | * If an "acknowledgments" section is sent, a packfile is sent if and | |
1494 | * only if "ready" was sent in this section. The other sections | |
1495 | * ("shallow-info" and "wanted-refs") are sent only if a packfile is | |
1496 | * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH | |
1497 | * otherwise. | |
1498 | */ | |
81025703 | 1499 | if (*received_ready && reader->status != PACKET_READ_DELIM) |
3d3c23b3 BS |
1500 | /* |
1501 | * TRANSLATORS: The parameter will be 'ready', a protocol | |
1502 | * keyword. | |
1503 | */ | |
1504 | die(_("expected packfile to be sent after '%s'"), "ready"); | |
81025703 | 1505 | if (!*received_ready && reader->status != PACKET_READ_FLUSH) |
3d3c23b3 BS |
1506 | /* |
1507 | * TRANSLATORS: The parameter will be 'ready', a protocol | |
1508 | * keyword. | |
1509 | */ | |
1510 | die(_("expected no other sections to be sent after no '%s'"), "ready"); | |
5400b2a2 | 1511 | |
81025703 | 1512 | return 0; |
685fbd32 BW |
1513 | } |
1514 | ||
f7e20501 | 1515 | static void receive_shallow_info(struct fetch_pack_args *args, |
1339078f JT |
1516 | struct packet_reader *reader, |
1517 | struct oid_array *shallows, | |
1518 | struct shallow_info *si) | |
f7e20501 | 1519 | { |
1339078f | 1520 | int unshallow_received = 0; |
bd0b42ae | 1521 | |
f7e20501 BW |
1522 | process_section_header(reader, "shallow-info", 0); |
1523 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { | |
1524 | const char *arg; | |
1525 | struct object_id oid; | |
1526 | ||
1527 | if (skip_prefix(reader->line, "shallow ", &arg)) { | |
1528 | if (get_oid_hex(arg, &oid)) | |
1529 | die(_("invalid shallow line: %s"), reader->line); | |
1339078f | 1530 | oid_array_append(shallows, &oid); |
f7e20501 BW |
1531 | continue; |
1532 | } | |
1533 | if (skip_prefix(reader->line, "unshallow ", &arg)) { | |
1534 | if (get_oid_hex(arg, &oid)) | |
1535 | die(_("invalid unshallow line: %s"), reader->line); | |
d0229abd | 1536 | if (!lookup_object(the_repository, &oid)) |
f7e20501 BW |
1537 | die(_("object not found: %s"), reader->line); |
1538 | /* make sure that it is parsed as shallow */ | |
109cd76d | 1539 | if (!parse_object(the_repository, &oid)) |
f7e20501 BW |
1540 | die(_("error in object: %s"), reader->line); |
1541 | if (unregister_shallow(&oid)) | |
1542 | die(_("no shallow found: %s"), reader->line); | |
1339078f | 1543 | unshallow_received = 1; |
f7e20501 BW |
1544 | continue; |
1545 | } | |
1546 | die(_("expected shallow/unshallow, got %s"), reader->line); | |
1547 | } | |
1548 | ||
1549 | if (reader->status != PACKET_READ_FLUSH && | |
1550 | reader->status != PACKET_READ_DELIM) | |
bbb19a8b | 1551 | die(_("error processing shallow info: %d"), reader->status); |
f7e20501 | 1552 | |
1339078f JT |
1553 | if (args->deepen || unshallow_received) { |
1554 | /* | |
1555 | * Treat these as shallow lines caused by our depth settings. | |
1556 | * In v0, these lines cannot cause refs to be rejected; do the | |
1557 | * same. | |
1558 | */ | |
1559 | int i; | |
1560 | ||
1561 | for (i = 0; i < shallows->nr; i++) | |
1562 | register_shallow(the_repository, &shallows->oid[i]); | |
bd0b42ae JT |
1563 | setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, |
1564 | NULL); | |
1565 | args->deepen = 1; | |
1339078f JT |
1566 | } else if (shallows->nr) { |
1567 | /* | |
1568 | * Treat these as shallow lines caused by the remote being | |
1569 | * shallow. In v0, remote refs that reach these objects are | |
1570 | * rejected (unless --update-shallow is set); do the same. | |
1571 | */ | |
1572 | prepare_shallow_info(si, shallows); | |
4fe788b1 LL |
1573 | if (si->nr_ours || si->nr_theirs) { |
1574 | if (args->reject_shallow_remote) | |
1575 | die(_("source repository is shallow, reject to clone.")); | |
1339078f JT |
1576 | alternate_shallow_file = |
1577 | setup_temporary_shallow(si->shallow); | |
4fe788b1 | 1578 | } else |
1339078f | 1579 | alternate_shallow_file = NULL; |
380ebab2 | 1580 | } else { |
1581 | alternate_shallow_file = NULL; | |
bd0b42ae | 1582 | } |
f7e20501 BW |
1583 | } |
1584 | ||
b7643009 JT |
1585 | static int cmp_name_ref(const void *name, const void *ref) |
1586 | { | |
1587 | return strcmp(name, (*(struct ref **)ref)->name); | |
1588 | } | |
1589 | ||
e2842b39 JT |
1590 | static void receive_wanted_refs(struct packet_reader *reader, |
1591 | struct ref **sought, int nr_sought) | |
73302051 BW |
1592 | { |
1593 | process_section_header(reader, "wanted-refs", 0); | |
1594 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { | |
1595 | struct object_id oid; | |
1596 | const char *end; | |
b7643009 | 1597 | struct ref **found; |
73302051 BW |
1598 | |
1599 | if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ') | |
bbb19a8b | 1600 | die(_("expected wanted-ref, got '%s'"), reader->line); |
73302051 | 1601 | |
b7643009 JT |
1602 | found = bsearch(end, sought, nr_sought, sizeof(*sought), |
1603 | cmp_name_ref); | |
1604 | if (!found) | |
bbb19a8b | 1605 | die(_("unexpected wanted-ref: '%s'"), reader->line); |
b7643009 | 1606 | oidcpy(&(*found)->old_oid, &oid); |
73302051 BW |
1607 | } |
1608 | ||
1609 | if (reader->status != PACKET_READ_DELIM) | |
bbb19a8b | 1610 | die(_("error processing wanted refs: %d"), reader->status); |
73302051 BW |
1611 | } |
1612 | ||
dd4b732d JT |
1613 | static void receive_packfile_uris(struct packet_reader *reader, |
1614 | struct string_list *uris) | |
1615 | { | |
1616 | process_section_header(reader, "packfile-uris", 0); | |
1617 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { | |
1618 | if (reader->pktlen < the_hash_algo->hexsz || | |
1619 | reader->line[the_hash_algo->hexsz] != ' ') | |
1a60f206 | 1620 | die("expected '<hash> <uri>', got: %s", reader->line); |
dd4b732d JT |
1621 | |
1622 | string_list_append(uris, reader->line); | |
1623 | } | |
1624 | if (reader->status != PACKET_READ_DELIM) | |
1625 | die("expected DELIM"); | |
1626 | } | |
1627 | ||
685fbd32 BW |
1628 | enum fetch_state { |
1629 | FETCH_CHECK_LOCAL = 0, | |
1630 | FETCH_SEND_REQUEST, | |
1631 | FETCH_PROCESS_ACKS, | |
1632 | FETCH_GET_PACK, | |
1633 | FETCH_DONE, | |
1634 | }; | |
1635 | ||
9c1e657a | 1636 | static void do_check_stateless_delimiter(int stateless_rpc, |
b0df0c16 DL |
1637 | struct packet_reader *reader) |
1638 | { | |
9c1e657a | 1639 | check_stateless_delimiter(stateless_rpc, reader, |
b0df0c16 DL |
1640 | _("git fetch-pack: expected response end packet")); |
1641 | } | |
1642 | ||
685fbd32 BW |
1643 | static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args, |
1644 | int fd[2], | |
1645 | const struct ref *orig_ref, | |
1646 | struct ref **sought, int nr_sought, | |
1339078f JT |
1647 | struct oid_array *shallows, |
1648 | struct shallow_info *si, | |
9da69a65 | 1649 | struct string_list *pack_lockfiles) |
685fbd32 | 1650 | { |
aaf633c2 | 1651 | struct repository *r = the_repository; |
685fbd32 BW |
1652 | struct ref *ref = copy_ref_list(orig_ref); |
1653 | enum fetch_state state = FETCH_CHECK_LOCAL; | |
1654 | struct oidset common = OIDSET_INIT; | |
1655 | struct packet_reader reader; | |
5fc31180 | 1656 | int in_vain = 0, negotiation_started = 0; |
a29263cf | 1657 | int negotiation_round = 0; |
685fbd32 | 1658 | int haves_to_send = INITIAL_FLUSH; |
603960b5 JT |
1659 | struct fetch_negotiator negotiator_alloc; |
1660 | struct fetch_negotiator *negotiator; | |
4fa3f00a | 1661 | int seen_ack = 0; |
81025703 JT |
1662 | struct object_id common_oid; |
1663 | int received_ready = 0; | |
dd4b732d JT |
1664 | struct string_list packfile_uris = STRING_LIST_INIT_DUP; |
1665 | int i; | |
b664e9ff | 1666 | struct strvec index_pack_args = STRVEC_INIT; |
603960b5 | 1667 | |
9dfa8dbe | 1668 | negotiator = &negotiator_alloc; |
4dfd0925 RC |
1669 | if (args->refetch) |
1670 | fetch_negotiator_init_noop(negotiator); | |
1671 | else | |
1672 | fetch_negotiator_init(r, negotiator); | |
603960b5 | 1673 | |
685fbd32 | 1674 | packet_reader_init(&reader, fd[0], NULL, 0, |
2d103c31 MS |
1675 | PACKET_READ_CHOMP_NEWLINE | |
1676 | PACKET_READ_DIE_ON_ERR_PACKET); | |
07c3c2aa JT |
1677 | if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) && |
1678 | server_supports_feature("fetch", "sideband-all", 0)) { | |
0bbc0bc5 JT |
1679 | reader.use_sideband = 1; |
1680 | reader.me = "fetch-pack"; | |
1681 | } | |
685fbd32 BW |
1682 | |
1683 | while (state != FETCH_DONE) { | |
1684 | switch (state) { | |
1685 | case FETCH_CHECK_LOCAL: | |
1686 | sort_ref_list(&ref, ref_compare_name); | |
1687 | QSORT(sought, nr_sought, cmp_ref_by_name); | |
1688 | ||
1689 | /* v2 supports these by default */ | |
1690 | allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1; | |
1691 | use_sideband = 2; | |
f7e20501 BW |
1692 | if (args->depth > 0 || args->deepen_since || args->deepen_not) |
1693 | args->deepen = 1; | |
685fbd32 | 1694 | |
685fbd32 | 1695 | /* Filter 'ref' by 'sought' and those that aren't local */ |
9dfa8dbe JT |
1696 | mark_complete_and_common_ref(negotiator, args, &ref); |
1697 | filter_refs(args, &ref, sought, nr_sought); | |
4dfd0925 | 1698 | if (!args->refetch && everything_local(args, &ref)) |
9dfa8dbe JT |
1699 | state = FETCH_DONE; |
1700 | else | |
685fbd32 | 1701 | state = FETCH_SEND_REQUEST; |
9dfa8dbe JT |
1702 | |
1703 | mark_tips(negotiator, args->negotiation_tips); | |
1704 | for_each_cached_alternate(negotiator, | |
1705 | insert_one_alternate_object); | |
685fbd32 BW |
1706 | break; |
1707 | case FETCH_SEND_REQUEST: | |
5fc31180 JS |
1708 | if (!negotiation_started) { |
1709 | negotiation_started = 1; | |
1710 | trace2_region_enter("fetch-pack", | |
1711 | "negotiation_v2", | |
1712 | the_repository); | |
1713 | } | |
a29263cf JS |
1714 | negotiation_round++; |
1715 | trace2_region_enter_printf("negotiation_v2", "round", | |
1716 | the_repository, "%d", | |
1717 | negotiation_round); | |
603960b5 | 1718 | if (send_fetch_request(negotiator, fd[1], args, ref, |
ec062838 | 1719 | &common, |
0bbc0bc5 | 1720 | &haves_to_send, &in_vain, |
4fa3f00a | 1721 | reader.use_sideband, |
a29263cf JS |
1722 | seen_ack)) { |
1723 | trace2_region_leave_printf("negotiation_v2", "round", | |
1724 | the_repository, "%d", | |
1725 | negotiation_round); | |
685fbd32 | 1726 | state = FETCH_GET_PACK; |
a29263cf | 1727 | } |
685fbd32 BW |
1728 | else |
1729 | state = FETCH_PROCESS_ACKS; | |
1730 | break; | |
1731 | case FETCH_PROCESS_ACKS: | |
1732 | /* Process ACKs/NAKs */ | |
81025703 JT |
1733 | process_section_header(&reader, "acknowledgments", 0); |
1734 | while (process_ack(negotiator, &reader, &common_oid, | |
1735 | &received_ready)) { | |
1736 | in_vain = 0; | |
1737 | seen_ack = 1; | |
1738 | oidset_insert(&common, &common_oid); | |
1739 | } | |
a29263cf JS |
1740 | trace2_region_leave_printf("negotiation_v2", "round", |
1741 | the_repository, "%d", | |
1742 | negotiation_round); | |
81025703 | 1743 | if (received_ready) { |
b0df0c16 DL |
1744 | /* |
1745 | * Don't check for response delimiter; get_pack() will | |
1746 | * read the rest of this response. | |
1747 | */ | |
685fbd32 | 1748 | state = FETCH_GET_PACK; |
81025703 | 1749 | } else { |
9c1e657a | 1750 | do_check_stateless_delimiter(args->stateless_rpc, &reader); |
685fbd32 | 1751 | state = FETCH_SEND_REQUEST; |
685fbd32 BW |
1752 | } |
1753 | break; | |
1754 | case FETCH_GET_PACK: | |
5fc31180 JS |
1755 | trace2_region_leave("fetch-pack", |
1756 | "negotiation_v2", | |
1757 | the_repository); | |
a29263cf JS |
1758 | trace2_data_intmax("negotiation_v2", the_repository, |
1759 | "total_rounds", negotiation_round); | |
f7e20501 BW |
1760 | /* Check for shallow-info section */ |
1761 | if (process_section_header(&reader, "shallow-info", 1)) | |
1339078f | 1762 | receive_shallow_info(args, &reader, shallows, si); |
f7e20501 | 1763 | |
73302051 | 1764 | if (process_section_header(&reader, "wanted-refs", 1)) |
e2842b39 | 1765 | receive_wanted_refs(&reader, sought, nr_sought); |
73302051 | 1766 | |
dd4b732d | 1767 | /* get the pack(s) */ |
88e9b1e3 IF |
1768 | if (git_env_bool("GIT_TRACE_REDACT", 1)) |
1769 | reader.options |= PACKET_READ_REDACT_URI_PATH; | |
dd4b732d JT |
1770 | if (process_section_header(&reader, "packfile-uris", 1)) |
1771 | receive_packfile_uris(&reader, &packfile_uris); | |
88e9b1e3 IF |
1772 | /* We don't expect more URIs. Reset to avoid expensive URI check. */ |
1773 | reader.options &= ~PACKET_READ_REDACT_URI_PATH; | |
1774 | ||
685fbd32 | 1775 | process_section_header(&reader, "packfile", 0); |
ae1a7eef JK |
1776 | |
1777 | /* | |
1778 | * this is the final request we'll make of the server; | |
1779 | * do a half-duplex shutdown to indicate that they can | |
1780 | * hang up as soon as the pack is sent. | |
1781 | */ | |
1782 | close(fd[1]); | |
1783 | fd[1] = -1; | |
1784 | ||
dd4b732d | 1785 | if (get_pack(args, fd, pack_lockfiles, |
b664e9ff | 1786 | packfile_uris.nr ? &index_pack_args : NULL, |
3745e269 | 1787 | sought, nr_sought, &fsck_options.gitmodules_found)) |
685fbd32 | 1788 | die(_("git fetch-pack: fetch failed.")); |
9c1e657a | 1789 | do_check_stateless_delimiter(args->stateless_rpc, &reader); |
685fbd32 BW |
1790 | |
1791 | state = FETCH_DONE; | |
1792 | break; | |
1793 | case FETCH_DONE: | |
1794 | continue; | |
1795 | } | |
1796 | } | |
1797 | ||
dd4b732d | 1798 | for (i = 0; i < packfile_uris.nr; i++) { |
b664e9ff | 1799 | int j; |
dd4b732d JT |
1800 | struct child_process cmd = CHILD_PROCESS_INIT; |
1801 | char packname[GIT_MAX_HEXSZ + 1]; | |
1802 | const char *uri = packfile_uris.items[i].string + | |
1803 | the_hash_algo->hexsz + 1; | |
1804 | ||
ef8d7ac4 JK |
1805 | strvec_push(&cmd.args, "http-fetch"); |
1806 | strvec_pushf(&cmd.args, "--packfile=%.*s", | |
f6d8942b JK |
1807 | (int) the_hash_algo->hexsz, |
1808 | packfile_uris.items[i].string); | |
b664e9ff JT |
1809 | for (j = 0; j < index_pack_args.nr; j++) |
1810 | strvec_pushf(&cmd.args, "--index-pack-arg=%s", | |
1811 | index_pack_args.v[j]); | |
ef8d7ac4 | 1812 | strvec_push(&cmd.args, uri); |
dd4b732d JT |
1813 | cmd.git_cmd = 1; |
1814 | cmd.no_stdin = 1; | |
1815 | cmd.out = -1; | |
1816 | if (start_command(&cmd)) | |
1817 | die("fetch-pack: unable to spawn http-fetch"); | |
1818 | ||
1819 | if (read_in_full(cmd.out, packname, 5) < 0 || | |
1820 | memcmp(packname, "keep\t", 5)) | |
1821 | die("fetch-pack: expected keep then TAB at start of http-fetch output"); | |
1822 | ||
1823 | if (read_in_full(cmd.out, packname, | |
1824 | the_hash_algo->hexsz + 1) < 0 || | |
1825 | packname[the_hash_algo->hexsz] != '\n') | |
1826 | die("fetch-pack: expected hash then LF at end of http-fetch output"); | |
1827 | ||
1828 | packname[the_hash_algo->hexsz] = '\0'; | |
1829 | ||
3745e269 | 1830 | parse_gitmodules_oids(cmd.out, &fsck_options.gitmodules_found); |
5476e1ef | 1831 | |
dd4b732d JT |
1832 | close(cmd.out); |
1833 | ||
1834 | if (finish_command(&cmd)) | |
1835 | die("fetch-pack: unable to finish http-fetch"); | |
1836 | ||
1837 | if (memcmp(packfile_uris.items[i].string, packname, | |
1838 | the_hash_algo->hexsz)) | |
1839 | die("fetch-pack: pack downloaded from %s does not match expected hash %.*s", | |
1840 | uri, (int) the_hash_algo->hexsz, | |
1841 | packfile_uris.items[i].string); | |
1842 | ||
1843 | string_list_append_nodup(pack_lockfiles, | |
1844 | xstrfmt("%s/pack/pack-%s.keep", | |
a3673f48 | 1845 | repo_get_object_directory(the_repository), |
dd4b732d JT |
1846 | packname)); |
1847 | } | |
1848 | string_list_clear(&packfile_uris, 0); | |
b664e9ff | 1849 | strvec_clear(&index_pack_args); |
dd4b732d | 1850 | |
3745e269 ÆAB |
1851 | if (fsck_finish(&fsck_options)) |
1852 | die("fsck failed"); | |
dd4b732d | 1853 | |
603960b5 JT |
1854 | if (negotiator) |
1855 | negotiator->release(negotiator); | |
dd4b732d | 1856 | |
685fbd32 BW |
1857 | oidset_clear(&common); |
1858 | return ref; | |
1859 | } | |
1860 | ||
05596e93 JT |
1861 | int fetch_pack_fsck_config(const char *var, const char *value, |
1862 | struct strbuf *msg_types) | |
1362df0d | 1863 | { |
d49cb162 JK |
1864 | const char *msg_id; |
1865 | ||
1362df0d | 1866 | if (strcmp(var, "fetch.fsck.skiplist") == 0) { |
6073b3b5 | 1867 | char *path ; |
1362df0d ÆAB |
1868 | |
1869 | if (git_config_pathname(&path, var, value)) | |
ca715807 | 1870 | return -1; |
05596e93 JT |
1871 | strbuf_addf(msg_types, "%cskiplist=%s", |
1872 | msg_types->len ? ',' : '=', path); | |
6073b3b5 | 1873 | free(path); |
1362df0d ÆAB |
1874 | return 0; |
1875 | } | |
1876 | ||
d49cb162 JK |
1877 | if (skip_prefix(var, "fetch.fsck.", &msg_id)) { |
1878 | if (!value) | |
1879 | return config_error_nonbool(var); | |
1880 | if (is_valid_msg_type(msg_id, value)) | |
05596e93 JT |
1881 | strbuf_addf(msg_types, "%c%s=%s", |
1882 | msg_types->len ? ',' : '=', msg_id, value); | |
1362df0d | 1883 | else |
d49cb162 | 1884 | warning("Skipping unknown msg id '%s'", msg_id); |
1362df0d ÆAB |
1885 | return 0; |
1886 | } | |
1887 | ||
05596e93 JT |
1888 | return 1; |
1889 | } | |
1890 | ||
1891 | static int fetch_pack_config_cb(const char *var, const char *value, | |
1892 | const struct config_context *ctx, void *cb) | |
1893 | { | |
1894 | int ret = fetch_pack_fsck_config(var, value, &fsck_msg_types); | |
1895 | if (ret > 0) | |
1896 | return git_default_config(var, value, ctx, cb); | |
1897 | ||
1898 | return ret; | |
1362df0d ÆAB |
1899 | } |
1900 | ||
f44af51d | 1901 | static void fetch_pack_config(void) |
745f7a8c | 1902 | { |
f44af51d TA |
1903 | git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit); |
1904 | git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit); | |
1905 | git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta); | |
1906 | git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects); | |
1907 | git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects); | |
1e905bbc | 1908 | git_config_get_bool("transfer.advertisesid", &advertise_sid); |
dd4b732d JT |
1909 | if (!uri_protocols.nr) { |
1910 | char *str; | |
1911 | ||
1912 | if (!git_config_get_string("fetch.uriprotocols", &str) && str) { | |
1913 | string_list_split(&uri_protocols, str, ',', -1); | |
1914 | free(str); | |
1915 | } | |
1916 | } | |
745f7a8c | 1917 | |
1362df0d | 1918 | git_config(fetch_pack_config_cb, NULL); |
745f7a8c NTND |
1919 | } |
1920 | ||
745f7a8c NTND |
1921 | static void fetch_pack_setup(void) |
1922 | { | |
1923 | static int did_setup; | |
1924 | if (did_setup) | |
1925 | return; | |
f44af51d | 1926 | fetch_pack_config(); |
f3d33f8c | 1927 | if (0 <= fetch_unpack_limit) |
745f7a8c | 1928 | unpack_limit = fetch_unpack_limit; |
f3d33f8c JH |
1929 | else if (0 <= transfer_unpack_limit) |
1930 | unpack_limit = transfer_unpack_limit; | |
745f7a8c NTND |
1931 | did_setup = 1; |
1932 | } | |
1933 | ||
f2db854d JH |
1934 | static int remove_duplicates_in_refs(struct ref **ref, int nr) |
1935 | { | |
1936 | struct string_list names = STRING_LIST_INIT_NODUP; | |
1937 | int src, dst; | |
1938 | ||
1939 | for (src = dst = 0; src < nr; src++) { | |
1940 | struct string_list_item *item; | |
1941 | item = string_list_insert(&names, ref[src]->name); | |
1942 | if (item->util) | |
1943 | continue; /* already have it */ | |
1944 | item->util = ref[src]; | |
1945 | if (src != dst) | |
1946 | ref[dst] = ref[src]; | |
1947 | dst++; | |
1948 | } | |
1949 | for (src = dst; src < nr; src++) | |
1950 | ref[src] = NULL; | |
1951 | string_list_clear(&names, 0); | |
1952 | return dst; | |
1953 | } | |
1954 | ||
beea4152 | 1955 | static void update_shallow(struct fetch_pack_args *args, |
e2842b39 | 1956 | struct ref **sought, int nr_sought, |
beea4152 | 1957 | struct shallow_info *si) |
a796ccee | 1958 | { |
910650d2 | 1959 | struct oid_array ref = OID_ARRAY_INIT; |
4820a33b | 1960 | int *status; |
beea4152 NTND |
1961 | int i; |
1962 | ||
79891cb9 | 1963 | if (args->deepen && alternate_shallow_file) { |
a796ccee | 1964 | if (*alternate_shallow_file == '\0') { /* --unshallow */ |
102de880 | 1965 | unlink_or_warn(git_path_shallow(the_repository)); |
37b9dcab | 1966 | rollback_shallow_file(the_repository, &shallow_lock); |
a796ccee | 1967 | } else |
37b9dcab | 1968 | commit_shallow_file(the_repository, &shallow_lock); |
23311f35 | 1969 | alternate_shallow_file = NULL; |
a796ccee NTND |
1970 | return; |
1971 | } | |
beea4152 NTND |
1972 | |
1973 | if (!si->shallow || !si->shallow->nr) | |
1974 | return; | |
1975 | ||
beea4152 NTND |
1976 | if (args->cloning) { |
1977 | /* | |
1978 | * remote is shallow, but this is a clone, there are | |
1979 | * no objects in repo to worry about. Accept any | |
1980 | * shallow points that exist in the pack (iow in repo | |
1981 | * after get_pack() and reprepare_packed_git()) | |
1982 | */ | |
910650d2 | 1983 | struct oid_array extra = OID_ARRAY_INIT; |
ee3051bd | 1984 | struct object_id *oid = si->shallow->oid; |
beea4152 | 1985 | for (i = 0; i < si->shallow->nr; i++) |
062b914c PS |
1986 | if (has_object(the_repository, &oid[i], |
1987 | HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR)) | |
910650d2 | 1988 | oid_array_append(&extra, &oid[i]); |
beea4152 NTND |
1989 | if (extra.nr) { |
1990 | setup_alternate_shallow(&shallow_lock, | |
1991 | &alternate_shallow_file, | |
1992 | &extra); | |
37b9dcab | 1993 | commit_shallow_file(the_repository, &shallow_lock); |
23311f35 | 1994 | alternate_shallow_file = NULL; |
beea4152 | 1995 | } |
910650d2 | 1996 | oid_array_clear(&extra); |
beea4152 NTND |
1997 | return; |
1998 | } | |
4820a33b NTND |
1999 | |
2000 | if (!si->nr_ours && !si->nr_theirs) | |
2001 | return; | |
2002 | ||
2003 | remove_nonexistent_theirs_shallow(si); | |
4820a33b NTND |
2004 | if (!si->nr_ours && !si->nr_theirs) |
2005 | return; | |
e2842b39 JT |
2006 | for (i = 0; i < nr_sought; i++) |
2007 | oid_array_append(&ref, &sought[i]->old_oid); | |
4820a33b NTND |
2008 | si->ref = &ref; |
2009 | ||
48d25cae NTND |
2010 | if (args->update_shallow) { |
2011 | /* | |
2012 | * remote is also shallow, .git/shallow may be updated | |
2013 | * so all refs can be accepted. Make sure we only add | |
2014 | * shallow roots that are actually reachable from new | |
2015 | * refs. | |
2016 | */ | |
910650d2 | 2017 | struct oid_array extra = OID_ARRAY_INIT; |
ee3051bd | 2018 | struct object_id *oid = si->shallow->oid; |
48d25cae NTND |
2019 | assign_shallow_commits_to_refs(si, NULL, NULL); |
2020 | if (!si->nr_ours && !si->nr_theirs) { | |
910650d2 | 2021 | oid_array_clear(&ref); |
48d25cae NTND |
2022 | return; |
2023 | } | |
2024 | for (i = 0; i < si->nr_ours; i++) | |
910650d2 | 2025 | oid_array_append(&extra, &oid[si->ours[i]]); |
48d25cae | 2026 | for (i = 0; i < si->nr_theirs; i++) |
910650d2 | 2027 | oid_array_append(&extra, &oid[si->theirs[i]]); |
48d25cae NTND |
2028 | setup_alternate_shallow(&shallow_lock, |
2029 | &alternate_shallow_file, | |
2030 | &extra); | |
37b9dcab | 2031 | commit_shallow_file(the_repository, &shallow_lock); |
910650d2 | 2032 | oid_array_clear(&extra); |
2033 | oid_array_clear(&ref); | |
23311f35 | 2034 | alternate_shallow_file = NULL; |
48d25cae NTND |
2035 | return; |
2036 | } | |
2037 | ||
4820a33b NTND |
2038 | /* |
2039 | * remote is also shallow, check what ref is safe to update | |
2040 | * without updating .git/shallow | |
2041 | */ | |
ca56dadb | 2042 | CALLOC_ARRAY(status, nr_sought); |
4820a33b NTND |
2043 | assign_shallow_commits_to_refs(si, NULL, status); |
2044 | if (si->nr_ours || si->nr_theirs) { | |
e2842b39 | 2045 | for (i = 0; i < nr_sought; i++) |
4820a33b | 2046 | if (status[i]) |
e2842b39 | 2047 | sought[i]->status = REF_STATUS_REJECT_SHALLOW; |
4820a33b NTND |
2048 | } |
2049 | free(status); | |
910650d2 | 2050 | oid_array_clear(&ref); |
a796ccee NTND |
2051 | } |
2052 | ||
9fec7b21 | 2053 | static const struct object_id *iterate_ref_map(void *cb_data) |
cf1e7c07 JT |
2054 | { |
2055 | struct ref **rm = cb_data; | |
2056 | struct ref *ref = *rm; | |
2057 | ||
2058 | if (!ref) | |
9fec7b21 | 2059 | return NULL; |
cf1e7c07 | 2060 | *rm = ref->next; |
9fec7b21 | 2061 | return &ref->old_oid; |
cf1e7c07 JT |
2062 | } |
2063 | ||
d0cbc756 XX |
2064 | int fetch_pack_fsck_objects(void) |
2065 | { | |
2066 | fetch_pack_setup(); | |
2067 | if (fetch_fsck_objects >= 0) | |
2068 | return fetch_fsck_objects; | |
2069 | if (transfer_fsck_objects >= 0) | |
2070 | return transfer_fsck_objects; | |
2071 | return 0; | |
2072 | } | |
2073 | ||
745f7a8c | 2074 | struct ref *fetch_pack(struct fetch_pack_args *args, |
0f804b0b | 2075 | int fd[], |
745f7a8c | 2076 | const struct ref *ref, |
f2db854d | 2077 | struct ref **sought, int nr_sought, |
910650d2 | 2078 | struct oid_array *shallow, |
9da69a65 | 2079 | struct string_list *pack_lockfiles, |
685fbd32 | 2080 | enum protocol_version version) |
745f7a8c | 2081 | { |
745f7a8c | 2082 | struct ref *ref_cpy; |
beea4152 | 2083 | struct shallow_info si; |
1339078f | 2084 | struct oid_array shallows_scratch = OID_ARRAY_INIT; |
745f7a8c NTND |
2085 | |
2086 | fetch_pack_setup(); | |
f2db854d JH |
2087 | if (nr_sought) |
2088 | nr_sought = remove_duplicates_in_refs(sought, nr_sought); | |
745f7a8c | 2089 | |
01775651 | 2090 | if (version != protocol_v2 && !ref) { |
745f7a8c | 2091 | packet_flush(fd[1]); |
1dd73e20 | 2092 | die(_("no matching remote head")); |
745f7a8c | 2093 | } |
1e7d440b JT |
2094 | if (version == protocol_v2) { |
2095 | if (shallow->nr) | |
2096 | BUG("Protocol V2 does not provide shallows at this point in the fetch"); | |
2097 | memset(&si, 0, sizeof(si)); | |
685fbd32 | 2098 | ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought, |
1339078f | 2099 | &shallows_scratch, &si, |
9da69a65 | 2100 | pack_lockfiles); |
1e7d440b JT |
2101 | } else { |
2102 | prepare_shallow_info(&si, shallow); | |
685fbd32 | 2103 | ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought, |
9da69a65 | 2104 | &si, pack_lockfiles); |
1e7d440b | 2105 | } |
a49d2834 | 2106 | reprepare_packed_git(the_repository); |
cf1e7c07 JT |
2107 | |
2108 | if (!args->cloning && args->deepen) { | |
2109 | struct check_connected_options opt = CHECK_CONNECTED_INIT; | |
2110 | struct ref *iterator = ref_cpy; | |
2111 | opt.shallow_file = alternate_shallow_file; | |
2112 | if (args->deepen) | |
2113 | opt.is_deepening_fetch = 1; | |
2114 | if (check_connected(iterate_ref_map, &iterator, &opt)) { | |
2115 | error(_("remote did not send all necessary objects")); | |
2116 | free_refs(ref_cpy); | |
2117 | ref_cpy = NULL; | |
37b9dcab | 2118 | rollback_shallow_file(the_repository, &shallow_lock); |
cf1e7c07 JT |
2119 | goto cleanup; |
2120 | } | |
2121 | args->connectivity_checked = 1; | |
2122 | } | |
2123 | ||
e2842b39 | 2124 | update_shallow(args, sought, nr_sought, &si); |
cf1e7c07 | 2125 | cleanup: |
beea4152 | 2126 | clear_shallow_info(&si); |
1339078f | 2127 | oid_array_clear(&shallows_scratch); |
745f7a8c NTND |
2128 | return ref_cpy; |
2129 | } | |
e860d96b | 2130 | |
9c1e657a JT |
2131 | static int add_to_object_array(const struct object_id *oid, void *data) |
2132 | { | |
2133 | struct object_array *a = data; | |
2134 | ||
2135 | add_object_array(lookup_object(the_repository, oid), "", a); | |
2136 | return 0; | |
2137 | } | |
2138 | ||
2139 | static void clear_common_flag(struct oidset *s) | |
2140 | { | |
2141 | struct oidset_iter iter; | |
2142 | const struct object_id *oid; | |
2143 | oidset_iter_init(s, &iter); | |
2144 | ||
2145 | while ((oid = oidset_iter_next(&iter))) { | |
2146 | struct object *obj = lookup_object(the_repository, oid); | |
2147 | obj->flags &= ~COMMON; | |
2148 | } | |
2149 | } | |
2150 | ||
2151 | void negotiate_using_fetch(const struct oid_array *negotiation_tips, | |
2152 | const struct string_list *server_options, | |
2153 | int stateless_rpc, | |
2154 | int fd[], | |
2155 | struct oidset *acked_commits) | |
2156 | { | |
2157 | struct fetch_negotiator negotiator; | |
2158 | struct packet_reader reader; | |
2159 | struct object_array nt_object_array = OBJECT_ARRAY_INIT; | |
2160 | struct strbuf req_buf = STRBUF_INIT; | |
2161 | int haves_to_send = INITIAL_FLUSH; | |
2162 | int in_vain = 0; | |
2163 | int seen_ack = 0; | |
2164 | int last_iteration = 0; | |
a29263cf | 2165 | int negotiation_round = 0; |
9c1e657a JT |
2166 | timestamp_t min_generation = GENERATION_NUMBER_INFINITY; |
2167 | ||
2168 | fetch_negotiator_init(the_repository, &negotiator); | |
2169 | mark_tips(&negotiator, negotiation_tips); | |
2170 | ||
2171 | packet_reader_init(&reader, fd[0], NULL, 0, | |
2172 | PACKET_READ_CHOMP_NEWLINE | | |
2173 | PACKET_READ_DIE_ON_ERR_PACKET); | |
2174 | ||
2175 | oid_array_for_each((struct oid_array *) negotiation_tips, | |
2176 | add_to_object_array, | |
2177 | &nt_object_array); | |
2178 | ||
a29263cf | 2179 | trace2_region_enter("fetch-pack", "negotiate_using_fetch", the_repository); |
9c1e657a JT |
2180 | while (!last_iteration) { |
2181 | int haves_added; | |
2182 | struct object_id common_oid; | |
2183 | int received_ready = 0; | |
2184 | ||
a29263cf JS |
2185 | negotiation_round++; |
2186 | ||
2187 | trace2_region_enter_printf("negotiate_using_fetch", "round", | |
2188 | the_repository, "%d", | |
2189 | negotiation_round); | |
9c1e657a JT |
2190 | strbuf_reset(&req_buf); |
2191 | write_fetch_command_and_capabilities(&req_buf, server_options); | |
2192 | ||
2193 | packet_buf_write(&req_buf, "wait-for-done"); | |
2194 | ||
2195 | haves_added = add_haves(&negotiator, &req_buf, &haves_to_send); | |
2196 | in_vain += haves_added; | |
2197 | if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN)) | |
2198 | last_iteration = 1; | |
2199 | ||
a29263cf JS |
2200 | trace2_data_intmax("negotiate_using_fetch", the_repository, |
2201 | "haves_added", haves_added); | |
2202 | trace2_data_intmax("negotiate_using_fetch", the_repository, | |
2203 | "in_vain", in_vain); | |
2204 | ||
9c1e657a JT |
2205 | /* Send request */ |
2206 | packet_buf_flush(&req_buf); | |
2207 | if (write_in_full(fd[1], req_buf.buf, req_buf.len) < 0) | |
2208 | die_errno(_("unable to write request to remote")); | |
2209 | ||
2210 | /* Process ACKs/NAKs */ | |
2211 | process_section_header(&reader, "acknowledgments", 0); | |
2212 | while (process_ack(&negotiator, &reader, &common_oid, | |
2213 | &received_ready)) { | |
2214 | struct commit *commit = lookup_commit(the_repository, | |
2215 | &common_oid); | |
2216 | if (commit) { | |
2217 | timestamp_t generation; | |
2218 | ||
2219 | parse_commit_or_die(commit); | |
2220 | commit->object.flags |= COMMON; | |
2221 | generation = commit_graph_generation(commit); | |
2222 | if (generation < min_generation) | |
2223 | min_generation = generation; | |
2224 | } | |
2225 | in_vain = 0; | |
2226 | seen_ack = 1; | |
2227 | oidset_insert(acked_commits, &common_oid); | |
2228 | } | |
2229 | if (received_ready) | |
2230 | die(_("unexpected 'ready' from remote")); | |
2231 | else | |
2232 | do_check_stateless_delimiter(stateless_rpc, &reader); | |
2233 | if (can_all_from_reach_with_flag(&nt_object_array, COMMON, | |
2234 | REACH_SCRATCH, 0, | |
2235 | min_generation)) | |
2236 | last_iteration = 1; | |
a29263cf JS |
2237 | trace2_region_leave_printf("negotiation", "round", |
2238 | the_repository, "%d", | |
2239 | negotiation_round); | |
9c1e657a | 2240 | } |
ee9895b0 | 2241 | trace2_region_leave("fetch-pack", "negotiate_using_fetch", the_repository); |
a29263cf JS |
2242 | trace2_data_intmax("negotiate_using_fetch", the_repository, |
2243 | "total_rounds", negotiation_round); | |
63494913 | 2244 | |
9c1e657a | 2245 | clear_common_flag(acked_commits); |
63494913 PS |
2246 | object_array_clear(&nt_object_array); |
2247 | negotiator.release(&negotiator); | |
9c1e657a JT |
2248 | strbuf_release(&req_buf); |
2249 | } | |
2250 | ||
e860d96b MM |
2251 | int report_unmatched_refs(struct ref **sought, int nr_sought) |
2252 | { | |
2253 | int i, ret = 0; | |
2254 | ||
2255 | for (i = 0; i < nr_sought; i++) { | |
d56583de | 2256 | if (!sought[i]) |
e860d96b | 2257 | continue; |
d56583de MM |
2258 | switch (sought[i]->match_status) { |
2259 | case REF_MATCHED: | |
2260 | continue; | |
2261 | case REF_NOT_MATCHED: | |
2262 | error(_("no such remote ref %s"), sought[i]->name); | |
2263 | break; | |
2264 | case REF_UNADVERTISED_NOT_ALLOWED: | |
2265 | error(_("Server does not allow request for unadvertised object %s"), | |
2266 | sought[i]->name); | |
2267 | break; | |
2268 | } | |
e860d96b MM |
2269 | ret = 1; |
2270 | } | |
2271 | return ret; | |
2272 | } |