]>
Commit | Line | Data |
---|---|---|
745f7a8c | 1 | #include "cache.h" |
a49d2834 | 2 | #include "repository.h" |
b2141fc1 | 3 | #include "config.h" |
697cc8ef | 4 | #include "lockfile.h" |
745f7a8c NTND |
5 | #include "refs.h" |
6 | #include "pkt-line.h" | |
7 | #include "commit.h" | |
8 | #include "tag.h" | |
d807c4a0 | 9 | #include "exec-cmd.h" |
745f7a8c NTND |
10 | #include "pack.h" |
11 | #include "sideband.h" | |
12 | #include "fetch-pack.h" | |
13 | #include "remote.h" | |
14 | #include "run-command.h" | |
47a59185 | 15 | #include "connect.h" |
745f7a8c NTND |
16 | #include "transport.h" |
17 | #include "version.h" | |
beea4152 | 18 | #include "sha1-array.h" |
fdb69d33 | 19 | #include "oidset.h" |
0abe14f6 | 20 | #include "packfile.h" |
cbd53a21 | 21 | #include "object-store.h" |
cf1e7c07 | 22 | #include "connected.h" |
ec062838 | 23 | #include "fetch-negotiator.h" |
1362df0d | 24 | #include "fsck.h" |
745f7a8c NTND |
25 | |
26 | static int transfer_unpack_limit = -1; | |
27 | static int fetch_unpack_limit = -1; | |
28 | static int unpack_limit = 100; | |
29 | static int prefer_ofs_delta = 1; | |
30 | static int no_done; | |
508ea882 | 31 | static int deepen_since_ok; |
a45a2600 | 32 | static int deepen_not_ok; |
745f7a8c NTND |
33 | static int fetch_fsck_objects = -1; |
34 | static int transfer_fsck_objects = -1; | |
35 | static int agent_supported; | |
640d8b72 | 36 | static int server_supports_filtering; |
6035d6aa NTND |
37 | static struct lock_file shallow_lock; |
38 | static const char *alternate_shallow_file; | |
42cc7485 | 39 | static char *negotiation_algorithm; |
1362df0d | 40 | static struct strbuf fsck_msg_types = STRBUF_INIT; |
745f7a8c | 41 | |
208acbfb | 42 | /* Remember to update object flag allocation in object.h */ |
745f7a8c | 43 | #define COMPLETE (1U << 0) |
ec062838 | 44 | #define ALTERNATE (1U << 1) |
745f7a8c NTND |
45 | |
46 | /* | |
47 | * After sending this many "have"s if we do not get any new ACK , we | |
48 | * give up traversing our history. | |
49 | */ | |
50 | #define MAX_IN_VAIN 256 | |
51 | ||
d30fe89c | 52 | static int multi_ack, use_sideband; |
7199c093 FM |
53 | /* Allow specifying sha1 if it is a ref tip. */ |
54 | #define ALLOW_TIP_SHA1 01 | |
68ee6289 FM |
55 | /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */ |
56 | #define ALLOW_REACHABLE_SHA1 02 | |
7199c093 | 57 | static unsigned int allow_unadvertised_object_request; |
745f7a8c | 58 | |
0d789a5b NTND |
59 | __attribute__((format (printf, 2, 3))) |
60 | static inline void print_verbose(const struct fetch_pack_args *args, | |
61 | const char *fmt, ...) | |
62 | { | |
63 | va_list params; | |
64 | ||
65 | if (!args->verbose) | |
66 | return; | |
67 | ||
68 | va_start(params, fmt); | |
69 | vfprintf(stderr, fmt, params); | |
70 | va_end(params); | |
71 | fputc('\n', stderr); | |
72 | } | |
73 | ||
41a078c6 JK |
74 | struct alternate_object_cache { |
75 | struct object **items; | |
76 | size_t nr, alloc; | |
77 | }; | |
78 | ||
bdf4276c | 79 | static void cache_one_alternate(const struct object_id *oid, |
41a078c6 JK |
80 | void *vcache) |
81 | { | |
82 | struct alternate_object_cache *cache = vcache; | |
109cd76d | 83 | struct object *obj = parse_object(the_repository, oid); |
41a078c6 JK |
84 | |
85 | if (!obj || (obj->flags & ALTERNATE)) | |
86 | return; | |
87 | ||
88 | obj->flags |= ALTERNATE; | |
89 | ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc); | |
90 | cache->items[cache->nr++] = obj; | |
91 | } | |
92 | ||
ec062838 JT |
93 | static void for_each_cached_alternate(struct fetch_negotiator *negotiator, |
94 | void (*cb)(struct fetch_negotiator *, | |
d30fe89c | 95 | struct object *)) |
41a078c6 JK |
96 | { |
97 | static int initialized; | |
98 | static struct alternate_object_cache cache; | |
99 | size_t i; | |
100 | ||
101 | if (!initialized) { | |
102 | for_each_alternate_ref(cache_one_alternate, &cache); | |
103 | initialized = 1; | |
104 | } | |
105 | ||
106 | for (i = 0; i < cache.nr; i++) | |
ec062838 | 107 | cb(negotiator, cache.items[i]); |
745f7a8c NTND |
108 | } |
109 | ||
ec062838 | 110 | static int rev_list_insert_ref(struct fetch_negotiator *negotiator, |
d30fe89c JT |
111 | const char *refname, |
112 | const struct object_id *oid) | |
745f7a8c | 113 | { |
a74093da SB |
114 | struct object *o = deref_tag(the_repository, |
115 | parse_object(the_repository, oid), | |
109cd76d | 116 | refname, 0); |
745f7a8c NTND |
117 | |
118 | if (o && o->type == OBJ_COMMIT) | |
ec062838 | 119 | negotiator->add_tip(negotiator, (struct commit *)o); |
745f7a8c NTND |
120 | |
121 | return 0; | |
122 | } | |
123 | ||
b1b49c6e MH |
124 | static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid, |
125 | int flag, void *cb_data) | |
745f7a8c | 126 | { |
d30fe89c | 127 | return rev_list_insert_ref(cb_data, refname, oid); |
745f7a8c NTND |
128 | } |
129 | ||
130 | enum ack_type { | |
131 | NAK = 0, | |
132 | ACK, | |
133 | ACK_continue, | |
134 | ACK_common, | |
135 | ACK_ready | |
136 | }; | |
137 | ||
01f9ec64 MS |
138 | static void consume_shallow_list(struct fetch_pack_args *args, |
139 | struct packet_reader *reader) | |
745f7a8c | 140 | { |
79891cb9 | 141 | if (args->stateless_rpc && args->deepen) { |
745f7a8c NTND |
142 | /* If we sent a depth we will get back "duplicate" |
143 | * shallow and unshallow commands every time there | |
144 | * is a block of have lines exchanged. | |
145 | */ | |
01f9ec64 MS |
146 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
147 | if (starts_with(reader->line, "shallow ")) | |
745f7a8c | 148 | continue; |
01f9ec64 | 149 | if (starts_with(reader->line, "unshallow ")) |
745f7a8c | 150 | continue; |
1dd73e20 | 151 | die(_("git fetch-pack: expected shallow list")); |
745f7a8c | 152 | } |
01f9ec64 MS |
153 | if (reader->status != PACKET_READ_FLUSH) |
154 | die(_("git fetch-pack: expected a flush packet after shallow list")); | |
745f7a8c NTND |
155 | } |
156 | } | |
157 | ||
01f9ec64 MS |
158 | static enum ack_type get_ack(struct packet_reader *reader, |
159 | struct object_id *result_oid) | |
745f7a8c | 160 | { |
74543a04 | 161 | int len; |
82e56767 | 162 | const char *arg; |
745f7a8c | 163 | |
01f9ec64 | 164 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) |
bc9d4dc5 | 165 | die(_("git fetch-pack: expected ACK/NAK, got a flush packet")); |
01f9ec64 MS |
166 | len = reader->pktlen; |
167 | ||
168 | if (!strcmp(reader->line, "NAK")) | |
745f7a8c | 169 | return NAK; |
01f9ec64 | 170 | if (skip_prefix(reader->line, "ACK ", &arg)) { |
1b283377 | 171 | if (!get_oid_hex(arg, result_oid)) { |
82e56767 | 172 | arg += 40; |
01f9ec64 | 173 | len -= arg - reader->line; |
82e56767 | 174 | if (len < 1) |
030e9dd6 | 175 | return ACK; |
82e56767 | 176 | if (strstr(arg, "continue")) |
745f7a8c | 177 | return ACK_continue; |
82e56767 | 178 | if (strstr(arg, "common")) |
745f7a8c | 179 | return ACK_common; |
82e56767 | 180 | if (strstr(arg, "ready")) |
745f7a8c NTND |
181 | return ACK_ready; |
182 | return ACK; | |
183 | } | |
184 | } | |
01f9ec64 | 185 | die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line); |
745f7a8c NTND |
186 | } |
187 | ||
188 | static void send_request(struct fetch_pack_args *args, | |
189 | int fd, struct strbuf *buf) | |
190 | { | |
191 | if (args->stateless_rpc) { | |
192 | send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX); | |
193 | packet_flush(fd); | |
37c80012 JK |
194 | } else { |
195 | if (write_in_full(fd, buf->buf, buf->len) < 0) | |
196 | die_errno(_("unable to write to remote")); | |
197 | } | |
745f7a8c NTND |
198 | } |
199 | ||
ec062838 | 200 | static void insert_one_alternate_object(struct fetch_negotiator *negotiator, |
d30fe89c | 201 | struct object *obj) |
745f7a8c | 202 | { |
ec062838 | 203 | rev_list_insert_ref(negotiator, NULL, &obj->oid); |
745f7a8c NTND |
204 | } |
205 | ||
206 | #define INITIAL_FLUSH 16 | |
207 | #define PIPESAFE_FLUSH 32 | |
da470981 | 208 | #define LARGE_FLUSH 16384 |
745f7a8c | 209 | |
685fbd32 | 210 | static int next_flush(int stateless_rpc, int count) |
745f7a8c | 211 | { |
685fbd32 | 212 | if (stateless_rpc) { |
da470981 JT |
213 | if (count < LARGE_FLUSH) |
214 | count <<= 1; | |
215 | else | |
216 | count = count * 11 / 10; | |
217 | } else { | |
218 | if (count < PIPESAFE_FLUSH) | |
219 | count <<= 1; | |
220 | else | |
221 | count += PIPESAFE_FLUSH; | |
222 | } | |
745f7a8c NTND |
223 | return count; |
224 | } | |
225 | ||
3390e42a JT |
226 | static void mark_tips(struct fetch_negotiator *negotiator, |
227 | const struct oid_array *negotiation_tips) | |
228 | { | |
229 | int i; | |
230 | ||
231 | if (!negotiation_tips) { | |
232 | for_each_ref(rev_list_insert_ref_oid, negotiator); | |
233 | return; | |
234 | } | |
235 | ||
236 | for (i = 0; i < negotiation_tips->nr; i++) | |
237 | rev_list_insert_ref(negotiator, NULL, | |
238 | &negotiation_tips->oid[i]); | |
239 | return; | |
240 | } | |
241 | ||
ec062838 | 242 | static int find_common(struct fetch_negotiator *negotiator, |
d30fe89c | 243 | struct fetch_pack_args *args, |
1b283377 | 244 | int fd[2], struct object_id *result_oid, |
745f7a8c NTND |
245 | struct ref *refs) |
246 | { | |
247 | int fetching; | |
248 | int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval; | |
1b283377 | 249 | const struct object_id *oid; |
745f7a8c NTND |
250 | unsigned in_vain = 0; |
251 | int got_continue = 0; | |
252 | int got_ready = 0; | |
253 | struct strbuf req_buf = STRBUF_INIT; | |
254 | size_t state_len = 0; | |
01f9ec64 | 255 | struct packet_reader reader; |
745f7a8c NTND |
256 | |
257 | if (args->stateless_rpc && multi_ack == 1) | |
1dd73e20 | 258 | die(_("--stateless-rpc requires multi_ack_detailed")); |
745f7a8c | 259 | |
01f9ec64 | 260 | packet_reader_init(&reader, fd[0], NULL, 0, |
2d103c31 MS |
261 | PACKET_READ_CHOMP_NEWLINE | |
262 | PACKET_READ_DIE_ON_ERR_PACKET); | |
01f9ec64 | 263 | |
12f19a98 JT |
264 | if (!args->no_dependents) { |
265 | mark_tips(negotiator, args->negotiation_tips); | |
266 | for_each_cached_alternate(negotiator, insert_one_alternate_object); | |
267 | } | |
745f7a8c NTND |
268 | |
269 | fetching = 0; | |
270 | for ( ; refs ; refs = refs->next) { | |
1b283377 | 271 | struct object_id *remote = &refs->old_oid; |
745f7a8c NTND |
272 | const char *remote_hex; |
273 | struct object *o; | |
274 | ||
275 | /* | |
276 | * If that object is complete (i.e. it is an ancestor of a | |
277 | * local ref), we tell them we have it but do not have to | |
278 | * tell them about its ancestors, which they already know | |
279 | * about. | |
280 | * | |
281 | * We use lookup_object here because we are only | |
282 | * interested in the case we *know* the object is | |
283 | * reachable and we have already scanned it. | |
12f19a98 JT |
284 | * |
285 | * Do this only if args->no_dependents is false (if it is true, | |
286 | * we cannot trust the object flags). | |
745f7a8c | 287 | */ |
12f19a98 JT |
288 | if (!args->no_dependents && |
289 | ((o = lookup_object(the_repository, remote->hash)) != NULL) && | |
745f7a8c NTND |
290 | (o->flags & COMPLETE)) { |
291 | continue; | |
292 | } | |
293 | ||
1b283377 | 294 | remote_hex = oid_to_hex(remote); |
745f7a8c NTND |
295 | if (!fetching) { |
296 | struct strbuf c = STRBUF_INIT; | |
297 | if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed"); | |
298 | if (multi_ack == 1) strbuf_addstr(&c, " multi_ack"); | |
299 | if (no_done) strbuf_addstr(&c, " no-done"); | |
300 | if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k"); | |
301 | if (use_sideband == 1) strbuf_addstr(&c, " side-band"); | |
cccf74e2 | 302 | if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative"); |
745f7a8c NTND |
303 | if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack"); |
304 | if (args->no_progress) strbuf_addstr(&c, " no-progress"); | |
305 | if (args->include_tag) strbuf_addstr(&c, " include-tag"); | |
306 | if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta"); | |
508ea882 | 307 | if (deepen_since_ok) strbuf_addstr(&c, " deepen-since"); |
a45a2600 | 308 | if (deepen_not_ok) strbuf_addstr(&c, " deepen-not"); |
745f7a8c NTND |
309 | if (agent_supported) strbuf_addf(&c, " agent=%s", |
310 | git_user_agent_sanitized()); | |
640d8b72 JH |
311 | if (args->filter_options.choice) |
312 | strbuf_addstr(&c, " filter"); | |
745f7a8c NTND |
313 | packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf); |
314 | strbuf_release(&c); | |
315 | } else | |
316 | packet_buf_write(&req_buf, "want %s\n", remote_hex); | |
317 | fetching++; | |
318 | } | |
319 | ||
320 | if (!fetching) { | |
321 | strbuf_release(&req_buf); | |
322 | packet_flush(fd[1]); | |
323 | return 1; | |
324 | } | |
325 | ||
c8813487 | 326 | if (is_repository_shallow(the_repository)) |
1a30f5a2 | 327 | write_shallow_commits(&req_buf, 1, NULL); |
745f7a8c NTND |
328 | if (args->depth > 0) |
329 | packet_buf_write(&req_buf, "deepen %d", args->depth); | |
508ea882 | 330 | if (args->deepen_since) { |
dddbad72 | 331 | timestamp_t max_age = approxidate(args->deepen_since); |
cb71f8bd | 332 | packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age); |
508ea882 | 333 | } |
a45a2600 NTND |
334 | if (args->deepen_not) { |
335 | int i; | |
336 | for (i = 0; i < args->deepen_not->nr; i++) { | |
337 | struct string_list_item *s = args->deepen_not->items + i; | |
338 | packet_buf_write(&req_buf, "deepen-not %s", s->string); | |
339 | } | |
340 | } | |
87c2d9d3 JS |
341 | if (server_supports_filtering && args->filter_options.choice) { |
342 | struct strbuf expanded_filter_spec = STRBUF_INIT; | |
343 | expand_list_objects_filter_spec(&args->filter_options, | |
344 | &expanded_filter_spec); | |
640d8b72 | 345 | packet_buf_write(&req_buf, "filter %s", |
87c2d9d3 JS |
346 | expanded_filter_spec.buf); |
347 | strbuf_release(&expanded_filter_spec); | |
348 | } | |
745f7a8c NTND |
349 | packet_buf_flush(&req_buf); |
350 | state_len = req_buf.len; | |
351 | ||
79891cb9 | 352 | if (args->deepen) { |
ae021d87 | 353 | const char *arg; |
1b283377 | 354 | struct object_id oid; |
745f7a8c NTND |
355 | |
356 | send_request(args, fd[1], &req_buf); | |
01f9ec64 MS |
357 | while (packet_reader_read(&reader) == PACKET_READ_NORMAL) { |
358 | if (skip_prefix(reader.line, "shallow ", &arg)) { | |
1b283377 | 359 | if (get_oid_hex(arg, &oid)) |
01f9ec64 | 360 | die(_("invalid shallow line: %s"), reader.line); |
19143f13 | 361 | register_shallow(the_repository, &oid); |
745f7a8c NTND |
362 | continue; |
363 | } | |
01f9ec64 | 364 | if (skip_prefix(reader.line, "unshallow ", &arg)) { |
1b283377 | 365 | if (get_oid_hex(arg, &oid)) |
01f9ec64 | 366 | die(_("invalid unshallow line: %s"), reader.line); |
5abddd1e | 367 | if (!lookup_object(the_repository, oid.hash)) |
01f9ec64 | 368 | die(_("object not found: %s"), reader.line); |
745f7a8c | 369 | /* make sure that it is parsed as shallow */ |
109cd76d | 370 | if (!parse_object(the_repository, &oid)) |
01f9ec64 | 371 | die(_("error in object: %s"), reader.line); |
e92b848c | 372 | if (unregister_shallow(&oid)) |
01f9ec64 | 373 | die(_("no shallow found: %s"), reader.line); |
745f7a8c NTND |
374 | continue; |
375 | } | |
01f9ec64 | 376 | die(_("expected shallow/unshallow, got %s"), reader.line); |
745f7a8c NTND |
377 | } |
378 | } else if (!args->stateless_rpc) | |
379 | send_request(args, fd[1], &req_buf); | |
380 | ||
381 | if (!args->stateless_rpc) { | |
382 | /* If we aren't using the stateless-rpc interface | |
383 | * we don't need to retain the headers. | |
384 | */ | |
385 | strbuf_setlen(&req_buf, 0); | |
386 | state_len = 0; | |
387 | } | |
388 | ||
389 | flushes = 0; | |
390 | retval = -1; | |
88e2f9ed JT |
391 | if (args->no_dependents) |
392 | goto done; | |
ec062838 | 393 | while ((oid = negotiator->next(negotiator))) { |
1b283377 | 394 | packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid)); |
395 | print_verbose(args, "have %s", oid_to_hex(oid)); | |
745f7a8c NTND |
396 | in_vain++; |
397 | if (flush_at <= ++count) { | |
398 | int ack; | |
399 | ||
400 | packet_buf_flush(&req_buf); | |
401 | send_request(args, fd[1], &req_buf); | |
402 | strbuf_setlen(&req_buf, state_len); | |
403 | flushes++; | |
685fbd32 | 404 | flush_at = next_flush(args->stateless_rpc, count); |
745f7a8c NTND |
405 | |
406 | /* | |
407 | * We keep one window "ahead" of the other side, and | |
408 | * will wait for an ACK only on the next one | |
409 | */ | |
410 | if (!args->stateless_rpc && count == INITIAL_FLUSH) | |
411 | continue; | |
412 | ||
01f9ec64 | 413 | consume_shallow_list(args, &reader); |
745f7a8c | 414 | do { |
01f9ec64 | 415 | ack = get_ack(&reader, result_oid); |
0d789a5b | 416 | if (ack) |
1dd73e20 | 417 | print_verbose(args, _("got %s %d %s"), "ack", |
1b283377 | 418 | ack, oid_to_hex(result_oid)); |
745f7a8c NTND |
419 | switch (ack) { |
420 | case ACK: | |
421 | flushes = 0; | |
422 | multi_ack = 0; | |
423 | retval = 0; | |
424 | goto done; | |
425 | case ACK_common: | |
426 | case ACK_ready: | |
427 | case ACK_continue: { | |
428 | struct commit *commit = | |
c1f5eb49 SB |
429 | lookup_commit(the_repository, |
430 | result_oid); | |
d093bc75 | 431 | int was_common; |
3a2a1dc1 | 432 | |
745f7a8c | 433 | if (!commit) |
1b283377 | 434 | die(_("invalid commit %s"), oid_to_hex(result_oid)); |
ec062838 | 435 | was_common = negotiator->ack(negotiator, commit); |
745f7a8c NTND |
436 | if (args->stateless_rpc |
437 | && ack == ACK_common | |
d093bc75 | 438 | && !was_common) { |
745f7a8c NTND |
439 | /* We need to replay the have for this object |
440 | * on the next RPC request so the peer knows | |
441 | * it is in common with us. | |
442 | */ | |
1b283377 | 443 | const char *hex = oid_to_hex(result_oid); |
745f7a8c NTND |
444 | packet_buf_write(&req_buf, "have %s\n", hex); |
445 | state_len = req_buf.len; | |
06b3d386 JT |
446 | /* |
447 | * Reset in_vain because an ack | |
448 | * for this commit has not been | |
449 | * seen. | |
450 | */ | |
451 | in_vain = 0; | |
452 | } else if (!args->stateless_rpc | |
453 | || ack != ACK_common) | |
454 | in_vain = 0; | |
745f7a8c | 455 | retval = 0; |
745f7a8c | 456 | got_continue = 1; |
21bcf6e4 | 457 | if (ack == ACK_ready) |
745f7a8c | 458 | got_ready = 1; |
745f7a8c NTND |
459 | break; |
460 | } | |
461 | } | |
462 | } while (ack); | |
463 | flushes--; | |
464 | if (got_continue && MAX_IN_VAIN < in_vain) { | |
1dd73e20 | 465 | print_verbose(args, _("giving up")); |
745f7a8c NTND |
466 | break; /* give up */ |
467 | } | |
21bcf6e4 JT |
468 | if (got_ready) |
469 | break; | |
745f7a8c NTND |
470 | } |
471 | } | |
472 | done: | |
473 | if (!got_ready || !no_done) { | |
474 | packet_buf_write(&req_buf, "done\n"); | |
475 | send_request(args, fd[1], &req_buf); | |
476 | } | |
1dd73e20 | 477 | print_verbose(args, _("done")); |
745f7a8c NTND |
478 | if (retval != 0) { |
479 | multi_ack = 0; | |
480 | flushes++; | |
481 | } | |
482 | strbuf_release(&req_buf); | |
483 | ||
ff62eca7 | 484 | if (!got_ready || !no_done) |
01f9ec64 | 485 | consume_shallow_list(args, &reader); |
745f7a8c | 486 | while (flushes || multi_ack) { |
01f9ec64 | 487 | int ack = get_ack(&reader, result_oid); |
745f7a8c | 488 | if (ack) { |
1dd73e20 | 489 | print_verbose(args, _("got %s (%d) %s"), "ack", |
1b283377 | 490 | ack, oid_to_hex(result_oid)); |
745f7a8c NTND |
491 | if (ack == ACK) |
492 | return 0; | |
493 | multi_ack = 1; | |
494 | continue; | |
495 | } | |
496 | flushes--; | |
497 | } | |
498 | /* it is no error to fetch into a completely empty repo */ | |
499 | return count ? retval : 0; | |
500 | } | |
501 | ||
502 | static struct commit_list *complete; | |
503 | ||
1b283377 | 504 | static int mark_complete(const struct object_id *oid) |
745f7a8c | 505 | { |
109cd76d | 506 | struct object *o = parse_object(the_repository, oid); |
745f7a8c NTND |
507 | |
508 | while (o && o->type == OBJ_TAG) { | |
509 | struct tag *t = (struct tag *) o; | |
510 | if (!t->tagged) | |
511 | break; /* broken repository */ | |
512 | o->flags |= COMPLETE; | |
109cd76d | 513 | o = parse_object(the_repository, &t->tagged->oid); |
745f7a8c NTND |
514 | } |
515 | if (o && o->type == OBJ_COMMIT) { | |
516 | struct commit *commit = (struct commit *)o; | |
517 | if (!(commit->object.flags & COMPLETE)) { | |
518 | commit->object.flags |= COMPLETE; | |
16445242 | 519 | commit_list_insert(commit, &complete); |
745f7a8c NTND |
520 | } |
521 | } | |
522 | return 0; | |
523 | } | |
524 | ||
f8ee4d85 MH |
525 | static int mark_complete_oid(const char *refname, const struct object_id *oid, |
526 | int flag, void *cb_data) | |
527 | { | |
1b283377 | 528 | return mark_complete(oid); |
f8ee4d85 MH |
529 | } |
530 | ||
745f7a8c | 531 | static void mark_recent_complete_commits(struct fetch_pack_args *args, |
dddbad72 | 532 | timestamp_t cutoff) |
745f7a8c NTND |
533 | { |
534 | while (complete && cutoff <= complete->item->date) { | |
1dd73e20 | 535 | print_verbose(args, _("Marking %s as complete"), |
0d789a5b | 536 | oid_to_hex(&complete->item->object.oid)); |
745f7a8c NTND |
537 | pop_most_recent_commit(&complete, COMPLETE); |
538 | } | |
539 | } | |
540 | ||
fdb69d33 JT |
541 | static void add_refs_to_oidset(struct oidset *oids, struct ref *refs) |
542 | { | |
543 | for (; refs; refs = refs->next) | |
544 | oidset_insert(oids, &refs->old_oid); | |
545 | } | |
546 | ||
bf73282c RS |
547 | static int is_unmatched_ref(const struct ref *ref) |
548 | { | |
549 | struct object_id oid; | |
550 | const char *p; | |
551 | return ref->match_status == REF_NOT_MATCHED && | |
552 | !parse_oid_hex(ref->name, &oid, &p) && | |
553 | *p == '\0' && | |
554 | oideq(&oid, &ref->old_oid); | |
555 | } | |
556 | ||
745f7a8c | 557 | static void filter_refs(struct fetch_pack_args *args, |
f2db854d JH |
558 | struct ref **refs, |
559 | struct ref **sought, int nr_sought) | |
745f7a8c NTND |
560 | { |
561 | struct ref *newlist = NULL; | |
562 | struct ref **newtail = &newlist; | |
fdb69d33 | 563 | struct ref *unmatched = NULL; |
745f7a8c | 564 | struct ref *ref, *next; |
fdb69d33 | 565 | struct oidset tip_oids = OIDSET_INIT; |
f2db854d | 566 | int i; |
22a16465 RS |
567 | int strict = !(allow_unadvertised_object_request & |
568 | (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)); | |
745f7a8c | 569 | |
f2db854d | 570 | i = 0; |
745f7a8c NTND |
571 | for (ref = *refs; ref; ref = next) { |
572 | int keep = 0; | |
573 | next = ref->next; | |
f2db854d | 574 | |
50e19a83 | 575 | if (starts_with(ref->name, "refs/") && |
4c224081 | 576 | check_refname_format(ref->name, 0)) |
745f7a8c NTND |
577 | ; /* trash */ |
578 | else { | |
f2db854d JH |
579 | while (i < nr_sought) { |
580 | int cmp = strcmp(ref->name, sought[i]->name); | |
745f7a8c NTND |
581 | if (cmp < 0) |
582 | break; /* definitely do not have it */ | |
583 | else if (cmp == 0) { | |
584 | keep = 1; /* definitely have it */ | |
d56583de | 585 | sought[i]->match_status = REF_MATCHED; |
745f7a8c | 586 | } |
f2db854d | 587 | i++; |
745f7a8c | 588 | } |
745f7a8c | 589 | |
e9502c0a JK |
590 | if (!keep && args->fetch_all && |
591 | (!args->deepen || !starts_with(ref->name, "refs/tags/"))) | |
592 | keep = 1; | |
593 | } | |
745f7a8c NTND |
594 | |
595 | if (keep) { | |
596 | *newtail = ref; | |
597 | ref->next = NULL; | |
598 | newtail = &ref->next; | |
599 | } else { | |
fdb69d33 JT |
600 | ref->next = unmatched; |
601 | unmatched = ref; | |
745f7a8c NTND |
602 | } |
603 | } | |
604 | ||
22a16465 RS |
605 | if (strict) { |
606 | for (i = 0; i < nr_sought; i++) { | |
607 | ref = sought[i]; | |
608 | if (!is_unmatched_ref(ref)) | |
609 | continue; | |
610 | ||
611 | add_refs_to_oidset(&tip_oids, unmatched); | |
612 | add_refs_to_oidset(&tip_oids, newlist); | |
613 | break; | |
614 | } | |
615 | } | |
616 | ||
6e7b66ee | 617 | /* Append unmatched requests to the list */ |
d56583de | 618 | for (i = 0; i < nr_sought; i++) { |
d56583de | 619 | ref = sought[i]; |
bf73282c | 620 | if (!is_unmatched_ref(ref)) |
d56583de | 621 | continue; |
6e7b66ee | 622 | |
22a16465 | 623 | if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) { |
d56583de | 624 | ref->match_status = REF_MATCHED; |
c3c17bf1 JK |
625 | *newtail = copy_ref(ref); |
626 | newtail = &(*newtail)->next; | |
d56583de MM |
627 | } else { |
628 | ref->match_status = REF_UNADVERTISED_NOT_ALLOWED; | |
6e7b66ee JH |
629 | } |
630 | } | |
fdb69d33 JT |
631 | |
632 | oidset_clear(&tip_oids); | |
633 | for (ref = unmatched; ref; ref = next) { | |
634 | next = ref->next; | |
635 | free(ref); | |
636 | } | |
637 | ||
745f7a8c NTND |
638 | *refs = newlist; |
639 | } | |
640 | ||
ec062838 | 641 | static void mark_alternate_complete(struct fetch_negotiator *unused, |
d30fe89c | 642 | struct object *obj) |
745f7a8c | 643 | { |
1b283377 | 644 | mark_complete(&obj->oid); |
745f7a8c NTND |
645 | } |
646 | ||
024aa469 TI |
647 | struct loose_object_iter { |
648 | struct oidset *loose_object_set; | |
649 | struct ref *refs; | |
650 | }; | |
651 | ||
34c29034 JT |
652 | /* |
653 | * Mark recent commits available locally and reachable from a local ref as | |
654 | * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as | |
655 | * COMMON_REF (otherwise, we are not planning to participate in negotiation, and | |
656 | * thus do not need COMMON_REF marks). | |
657 | * | |
658 | * The cutoff time for recency is determined by this heuristic: it is the | |
659 | * earliest commit time of the objects in refs that are commits and that we know | |
660 | * the commit time of. | |
661 | */ | |
ec062838 | 662 | static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator, |
d30fe89c | 663 | struct fetch_pack_args *args, |
34c29034 | 664 | struct ref **refs) |
745f7a8c NTND |
665 | { |
666 | struct ref *ref; | |
a1c6d7c1 | 667 | int old_save_commit_buffer = save_commit_buffer; |
dddbad72 | 668 | timestamp_t cutoff = 0; |
745f7a8c NTND |
669 | |
670 | save_commit_buffer = 0; | |
671 | ||
672 | for (ref = *refs; ref; ref = ref->next) { | |
673 | struct object *o; | |
674 | ||
97b2fa08 JK |
675 | if (!has_object_file_with_flags(&ref->old_oid, |
676 | OBJECT_INFO_QUICK)) | |
024aa469 | 677 | continue; |
109cd76d | 678 | o = parse_object(the_repository, &ref->old_oid); |
745f7a8c NTND |
679 | if (!o) |
680 | continue; | |
681 | ||
682 | /* We already have it -- which may mean that we were | |
683 | * in sync with the other side at some time after | |
684 | * that (it is OK if we guess wrong here). | |
685 | */ | |
686 | if (o->type == OBJ_COMMIT) { | |
687 | struct commit *commit = (struct commit *)o; | |
688 | if (!cutoff || cutoff < commit->date) | |
689 | cutoff = commit->date; | |
690 | } | |
691 | } | |
692 | ||
12f19a98 JT |
693 | if (!args->deepen) { |
694 | for_each_ref(mark_complete_oid, NULL); | |
695 | for_each_cached_alternate(NULL, mark_alternate_complete); | |
696 | commit_list_sort_by_date(&complete); | |
697 | if (cutoff) | |
698 | mark_recent_complete_commits(args, cutoff); | |
699 | } | |
745f7a8c | 700 | |
12f19a98 JT |
701 | /* |
702 | * Mark all complete remote refs as common refs. | |
703 | * Don't mark them common yet; the server has to be told so first. | |
704 | */ | |
705 | for (ref = *refs; ref; ref = ref->next) { | |
706 | struct object *o = deref_tag(the_repository, | |
707 | lookup_object(the_repository, | |
708 | ref->old_oid.hash), | |
709 | NULL, 0); | |
745f7a8c | 710 | |
12f19a98 JT |
711 | if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE)) |
712 | continue; | |
745f7a8c | 713 | |
12f19a98 JT |
714 | negotiator->known_common(negotiator, |
715 | (struct commit *)o); | |
745f7a8c NTND |
716 | } |
717 | ||
34c29034 JT |
718 | save_commit_buffer = old_save_commit_buffer; |
719 | } | |
720 | ||
721 | /* | |
722 | * Returns 1 if every object pointed to by the given remote refs is available | |
723 | * locally and reachable from a local ref, and 0 otherwise. | |
724 | */ | |
725 | static int everything_local(struct fetch_pack_args *args, | |
726 | struct ref **refs) | |
727 | { | |
728 | struct ref *ref; | |
729 | int retval; | |
745f7a8c NTND |
730 | |
731 | for (retval = 1, ref = *refs; ref ; ref = ref->next) { | |
1b283377 | 732 | const struct object_id *remote = &ref->old_oid; |
745f7a8c NTND |
733 | struct object *o; |
734 | ||
5abddd1e | 735 | o = lookup_object(the_repository, remote->hash); |
745f7a8c NTND |
736 | if (!o || !(o->flags & COMPLETE)) { |
737 | retval = 0; | |
1b283377 | 738 | print_verbose(args, "want %s (%s)", oid_to_hex(remote), |
0d789a5b | 739 | ref->name); |
745f7a8c NTND |
740 | continue; |
741 | } | |
1b283377 | 742 | print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote), |
0d789a5b | 743 | ref->name); |
745f7a8c | 744 | } |
a1c6d7c1 | 745 | |
745f7a8c NTND |
746 | return retval; |
747 | } | |
748 | ||
749 | static int sideband_demux(int in, int out, void *data) | |
750 | { | |
751 | int *xd = data; | |
9ff18faf | 752 | int ret; |
745f7a8c | 753 | |
9ff18faf | 754 | ret = recv_sideband("fetch-pack", xd[0], out); |
745f7a8c NTND |
755 | close(out); |
756 | return ret; | |
757 | } | |
758 | ||
759 | static int get_pack(struct fetch_pack_args *args, | |
760 | int xd[2], char **pack_lockfile) | |
761 | { | |
762 | struct async demux; | |
745f7a8c | 763 | int do_keep = args->keep_pack; |
984a43b9 JK |
764 | const char *cmd_name; |
765 | struct pack_header header; | |
766 | int pass_header = 0; | |
d3180279 | 767 | struct child_process cmd = CHILD_PROCESS_INIT; |
c6807a40 | 768 | int ret; |
745f7a8c NTND |
769 | |
770 | memset(&demux, 0, sizeof(demux)); | |
771 | if (use_sideband) { | |
772 | /* xd[] is talking with upload-pack; subprocess reads from | |
773 | * xd[0], spits out band#2 to stderr, and feeds us band#1 | |
774 | * through demux->out. | |
775 | */ | |
776 | demux.proc = sideband_demux; | |
777 | demux.data = xd; | |
778 | demux.out = -1; | |
df857572 | 779 | demux.isolate_sigpipe = 1; |
745f7a8c | 780 | if (start_async(&demux)) |
1dd73e20 | 781 | die(_("fetch-pack: unable to fork off sideband demultiplexer")); |
745f7a8c NTND |
782 | } |
783 | else | |
784 | demux.out = xd[0]; | |
785 | ||
745f7a8c | 786 | if (!args->keep_pack && unpack_limit) { |
745f7a8c NTND |
787 | |
788 | if (read_pack_header(demux.out, &header)) | |
1dd73e20 | 789 | die(_("protocol error: bad pack header")); |
984a43b9 | 790 | pass_header = 1; |
745f7a8c NTND |
791 | if (ntohl(header.hdr_entries) < unpack_limit) |
792 | do_keep = 0; | |
793 | else | |
794 | do_keep = 1; | |
795 | } | |
796 | ||
6035d6aa | 797 | if (alternate_shallow_file) { |
984a43b9 JK |
798 | argv_array_push(&cmd.args, "--shallow-file"); |
799 | argv_array_push(&cmd.args, alternate_shallow_file); | |
6035d6aa NTND |
800 | } |
801 | ||
88e2f9ed | 802 | if (do_keep || args->from_promisor) { |
745f7a8c NTND |
803 | if (pack_lockfile) |
804 | cmd.out = -1; | |
984a43b9 JK |
805 | cmd_name = "index-pack"; |
806 | argv_array_push(&cmd.args, cmd_name); | |
807 | argv_array_push(&cmd.args, "--stdin"); | |
745f7a8c | 808 | if (!args->quiet && !args->no_progress) |
984a43b9 | 809 | argv_array_push(&cmd.args, "-v"); |
745f7a8c | 810 | if (args->use_thin_pack) |
984a43b9 | 811 | argv_array_push(&cmd.args, "--fix-thin"); |
88e2f9ed | 812 | if (do_keep && (args->lock_pack || unpack_limit)) { |
da25bdb7 | 813 | char hostname[HOST_NAME_MAX + 1]; |
5781a9a2 | 814 | if (xgethostname(hostname, sizeof(hostname))) |
984a43b9 JK |
815 | xsnprintf(hostname, sizeof(hostname), "localhost"); |
816 | argv_array_pushf(&cmd.args, | |
817 | "--keep=fetch-pack %"PRIuMAX " on %s", | |
818 | (uintmax_t)getpid(), hostname); | |
745f7a8c | 819 | } |
c6807a40 | 820 | if (args->check_self_contained_and_connected) |
984a43b9 | 821 | argv_array_push(&cmd.args, "--check-self-contained-and-connected"); |
88e2f9ed JT |
822 | if (args->from_promisor) |
823 | argv_array_push(&cmd.args, "--promisor"); | |
745f7a8c NTND |
824 | } |
825 | else { | |
984a43b9 JK |
826 | cmd_name = "unpack-objects"; |
827 | argv_array_push(&cmd.args, cmd_name); | |
745f7a8c | 828 | if (args->quiet || args->no_progress) |
984a43b9 | 829 | argv_array_push(&cmd.args, "-q"); |
c6807a40 | 830 | args->check_self_contained_and_connected = 0; |
745f7a8c | 831 | } |
984a43b9 JK |
832 | |
833 | if (pass_header) | |
834 | argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32, | |
835 | ntohl(header.hdr_version), | |
836 | ntohl(header.hdr_entries)); | |
745f7a8c NTND |
837 | if (fetch_fsck_objects >= 0 |
838 | ? fetch_fsck_objects | |
839 | : transfer_fsck_objects >= 0 | |
840 | ? transfer_fsck_objects | |
98a2ea46 JT |
841 | : 0) { |
842 | if (args->from_promisor) | |
843 | /* | |
844 | * We cannot use --strict in index-pack because it | |
845 | * checks both broken objects and links, but we only | |
846 | * want to check for broken objects. | |
847 | */ | |
848 | argv_array_push(&cmd.args, "--fsck-objects"); | |
849 | else | |
1362df0d ÆAB |
850 | argv_array_pushf(&cmd.args, "--strict%s", |
851 | fsck_msg_types.buf); | |
98a2ea46 | 852 | } |
745f7a8c NTND |
853 | |
854 | cmd.in = demux.out; | |
855 | cmd.git_cmd = 1; | |
856 | if (start_command(&cmd)) | |
1dd73e20 | 857 | die(_("fetch-pack: unable to fork off %s"), cmd_name); |
745f7a8c NTND |
858 | if (do_keep && pack_lockfile) { |
859 | *pack_lockfile = index_pack_lockfile(cmd.out); | |
860 | close(cmd.out); | |
861 | } | |
862 | ||
37cb1dd6 JL |
863 | if (!use_sideband) |
864 | /* Closed by start_command() */ | |
865 | xd[0] = -1; | |
866 | ||
c6807a40 NTND |
867 | ret = finish_command(&cmd); |
868 | if (!ret || (args->check_self_contained_and_connected && ret == 1)) | |
869 | args->self_contained_and_connected = | |
870 | args->check_self_contained_and_connected && | |
871 | ret == 0; | |
872 | else | |
1dd73e20 | 873 | die(_("%s failed"), cmd_name); |
745f7a8c | 874 | if (use_sideband && finish_async(&demux)) |
1dd73e20 | 875 | die(_("error in sideband demultiplexer")); |
745f7a8c NTND |
876 | return 0; |
877 | } | |
878 | ||
f2db854d JH |
879 | static int cmp_ref_by_name(const void *a_, const void *b_) |
880 | { | |
881 | const struct ref *a = *((const struct ref **)a_); | |
882 | const struct ref *b = *((const struct ref **)b_); | |
883 | return strcmp(a->name, b->name); | |
884 | } | |
885 | ||
745f7a8c NTND |
886 | static struct ref *do_fetch_pack(struct fetch_pack_args *args, |
887 | int fd[2], | |
888 | const struct ref *orig_ref, | |
f2db854d | 889 | struct ref **sought, int nr_sought, |
beea4152 | 890 | struct shallow_info *si, |
745f7a8c NTND |
891 | char **pack_lockfile) |
892 | { | |
893 | struct ref *ref = copy_ref_list(orig_ref); | |
1b283377 | 894 | struct object_id oid; |
745f7a8c NTND |
895 | const char *agent_feature; |
896 | int agent_len; | |
ec062838 | 897 | struct fetch_negotiator negotiator; |
42cc7485 | 898 | fetch_negotiator_init(&negotiator, negotiation_algorithm); |
745f7a8c NTND |
899 | |
900 | sort_ref_list(&ref, ref_compare_name); | |
9ed0d8d6 | 901 | QSORT(sought, nr_sought, cmp_ref_by_name); |
745f7a8c | 902 | |
c8813487 | 903 | if ((args->depth > 0 || is_repository_shallow(the_repository)) && !server_supports("shallow")) |
1dd73e20 | 904 | die(_("Server does not support shallow clients")); |
a45a2600 | 905 | if (args->depth > 0 || args->deepen_since || args->deepen_not) |
79891cb9 | 906 | args->deepen = 1; |
745f7a8c | 907 | if (server_supports("multi_ack_detailed")) { |
1dd73e20 | 908 | print_verbose(args, _("Server supports multi_ack_detailed")); |
745f7a8c NTND |
909 | multi_ack = 2; |
910 | if (server_supports("no-done")) { | |
1dd73e20 | 911 | print_verbose(args, _("Server supports no-done")); |
745f7a8c NTND |
912 | if (args->stateless_rpc) |
913 | no_done = 1; | |
914 | } | |
915 | } | |
916 | else if (server_supports("multi_ack")) { | |
1dd73e20 | 917 | print_verbose(args, _("Server supports multi_ack")); |
745f7a8c NTND |
918 | multi_ack = 1; |
919 | } | |
920 | if (server_supports("side-band-64k")) { | |
1dd73e20 | 921 | print_verbose(args, _("Server supports side-band-64k")); |
745f7a8c NTND |
922 | use_sideband = 2; |
923 | } | |
924 | else if (server_supports("side-band")) { | |
1dd73e20 | 925 | print_verbose(args, _("Server supports side-band")); |
745f7a8c NTND |
926 | use_sideband = 1; |
927 | } | |
6e7b66ee | 928 | if (server_supports("allow-tip-sha1-in-want")) { |
1dd73e20 | 929 | print_verbose(args, _("Server supports allow-tip-sha1-in-want")); |
7199c093 | 930 | allow_unadvertised_object_request |= ALLOW_TIP_SHA1; |
6e7b66ee | 931 | } |
68ee6289 | 932 | if (server_supports("allow-reachable-sha1-in-want")) { |
1dd73e20 | 933 | print_verbose(args, _("Server supports allow-reachable-sha1-in-want")); |
68ee6289 FM |
934 | allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1; |
935 | } | |
745f7a8c NTND |
936 | if (!server_supports("thin-pack")) |
937 | args->use_thin_pack = 0; | |
938 | if (!server_supports("no-progress")) | |
939 | args->no_progress = 0; | |
940 | if (!server_supports("include-tag")) | |
941 | args->include_tag = 0; | |
0d789a5b | 942 | if (server_supports("ofs-delta")) |
1dd73e20 | 943 | print_verbose(args, _("Server supports ofs-delta")); |
0d789a5b | 944 | else |
745f7a8c NTND |
945 | prefer_ofs_delta = 0; |
946 | ||
640d8b72 JH |
947 | if (server_supports("filter")) { |
948 | server_supports_filtering = 1; | |
949 | print_verbose(args, _("Server supports filter")); | |
950 | } else if (args->filter_options.choice) { | |
951 | warning("filtering not recognized by server, ignoring"); | |
952 | } | |
953 | ||
745f7a8c NTND |
954 | if ((agent_feature = server_feature_value("agent", &agent_len))) { |
955 | agent_supported = 1; | |
0d789a5b | 956 | if (agent_len) |
1dd73e20 | 957 | print_verbose(args, _("Server version is %.*s"), |
0d789a5b | 958 | agent_len, agent_feature); |
745f7a8c | 959 | } |
508ea882 NTND |
960 | if (server_supports("deepen-since")) |
961 | deepen_since_ok = 1; | |
962 | else if (args->deepen_since) | |
963 | die(_("Server does not support --shallow-since")); | |
a45a2600 NTND |
964 | if (server_supports("deepen-not")) |
965 | deepen_not_ok = 1; | |
966 | else if (args->deepen_not) | |
967 | die(_("Server does not support --shallow-exclude")); | |
cccf74e2 NTND |
968 | if (!server_supports("deepen-relative") && args->deepen_relative) |
969 | die(_("Server does not support --deepen")); | |
745f7a8c | 970 | |
12f19a98 JT |
971 | if (!args->no_dependents) { |
972 | mark_complete_and_common_ref(&negotiator, args, &ref); | |
973 | filter_refs(args, &ref, sought, nr_sought); | |
974 | if (everything_local(args, &ref)) { | |
975 | packet_flush(fd[1]); | |
976 | goto all_done; | |
977 | } | |
978 | } else { | |
979 | filter_refs(args, &ref, sought, nr_sought); | |
745f7a8c | 980 | } |
ec062838 | 981 | if (find_common(&negotiator, args, fd, &oid, ref) < 0) |
745f7a8c NTND |
982 | if (!args->keep_pack) |
983 | /* When cloning, it is not unusual to have | |
984 | * no common commit. | |
985 | */ | |
1dd73e20 | 986 | warning(_("no common commits")); |
745f7a8c NTND |
987 | |
988 | if (args->stateless_rpc) | |
989 | packet_flush(fd[1]); | |
79891cb9 | 990 | if (args->deepen) |
1a30f5a2 NTND |
991 | setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, |
992 | NULL); | |
4820a33b | 993 | else if (si->nr_ours || si->nr_theirs) |
beea4152 | 994 | alternate_shallow_file = setup_temporary_shallow(si->shallow); |
6da8bdcb NTND |
995 | else |
996 | alternate_shallow_file = NULL; | |
745f7a8c | 997 | if (get_pack(args, fd, pack_lockfile)) |
1dd73e20 | 998 | die(_("git fetch-pack: fetch failed.")); |
745f7a8c NTND |
999 | |
1000 | all_done: | |
ec062838 | 1001 | negotiator.release(&negotiator); |
745f7a8c NTND |
1002 | return ref; |
1003 | } | |
1004 | ||
f7e20501 BW |
1005 | static void add_shallow_requests(struct strbuf *req_buf, |
1006 | const struct fetch_pack_args *args) | |
1007 | { | |
00624d60 | 1008 | if (is_repository_shallow(the_repository)) |
f7e20501 BW |
1009 | write_shallow_commits(req_buf, 1, NULL); |
1010 | if (args->depth > 0) | |
1011 | packet_buf_write(req_buf, "deepen %d", args->depth); | |
1012 | if (args->deepen_since) { | |
1013 | timestamp_t max_age = approxidate(args->deepen_since); | |
1014 | packet_buf_write(req_buf, "deepen-since %"PRItime, max_age); | |
1015 | } | |
1016 | if (args->deepen_not) { | |
1017 | int i; | |
1018 | for (i = 0; i < args->deepen_not->nr; i++) { | |
1019 | struct string_list_item *s = args->deepen_not->items + i; | |
1020 | packet_buf_write(req_buf, "deepen-not %s", s->string); | |
1021 | } | |
1022 | } | |
5056cf4a JT |
1023 | if (args->deepen_relative) |
1024 | packet_buf_write(req_buf, "deepen-relative\n"); | |
f7e20501 BW |
1025 | } |
1026 | ||
12f19a98 | 1027 | static void add_wants(int no_dependents, const struct ref *wants, struct strbuf *req_buf) |
685fbd32 | 1028 | { |
73302051 BW |
1029 | int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0); |
1030 | ||
685fbd32 BW |
1031 | for ( ; wants ; wants = wants->next) { |
1032 | const struct object_id *remote = &wants->old_oid; | |
685fbd32 BW |
1033 | struct object *o; |
1034 | ||
1035 | /* | |
1036 | * If that object is complete (i.e. it is an ancestor of a | |
1037 | * local ref), we tell them we have it but do not have to | |
1038 | * tell them about its ancestors, which they already know | |
1039 | * about. | |
1040 | * | |
1041 | * We use lookup_object here because we are only | |
1042 | * interested in the case we *know* the object is | |
1043 | * reachable and we have already scanned it. | |
12f19a98 JT |
1044 | * |
1045 | * Do this only if args->no_dependents is false (if it is true, | |
1046 | * we cannot trust the object flags). | |
685fbd32 | 1047 | */ |
12f19a98 JT |
1048 | if (!no_dependents && |
1049 | ((o = lookup_object(the_repository, remote->hash)) != NULL) && | |
685fbd32 BW |
1050 | (o->flags & COMPLETE)) { |
1051 | continue; | |
1052 | } | |
1053 | ||
73302051 BW |
1054 | if (!use_ref_in_want || wants->exact_oid) |
1055 | packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote)); | |
1056 | else | |
1057 | packet_buf_write(req_buf, "want-ref %s\n", wants->name); | |
685fbd32 BW |
1058 | } |
1059 | } | |
1060 | ||
1061 | static void add_common(struct strbuf *req_buf, struct oidset *common) | |
1062 | { | |
1063 | struct oidset_iter iter; | |
1064 | const struct object_id *oid; | |
1065 | oidset_iter_init(common, &iter); | |
1066 | ||
1067 | while ((oid = oidset_iter_next(&iter))) { | |
1068 | packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid)); | |
1069 | } | |
1070 | } | |
1071 | ||
ec062838 JT |
1072 | static int add_haves(struct fetch_negotiator *negotiator, |
1073 | struct strbuf *req_buf, | |
d30fe89c | 1074 | int *haves_to_send, int *in_vain) |
685fbd32 BW |
1075 | { |
1076 | int ret = 0; | |
1077 | int haves_added = 0; | |
1078 | const struct object_id *oid; | |
1079 | ||
ec062838 | 1080 | while ((oid = negotiator->next(negotiator))) { |
685fbd32 BW |
1081 | packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid)); |
1082 | if (++haves_added >= *haves_to_send) | |
1083 | break; | |
1084 | } | |
1085 | ||
1086 | *in_vain += haves_added; | |
1087 | if (!haves_added || *in_vain >= MAX_IN_VAIN) { | |
1088 | /* Send Done */ | |
1089 | packet_buf_write(req_buf, "done\n"); | |
1090 | ret = 1; | |
1091 | } | |
1092 | ||
1093 | /* Increase haves to send on next round */ | |
1094 | *haves_to_send = next_flush(1, *haves_to_send); | |
1095 | ||
1096 | return ret; | |
1097 | } | |
1098 | ||
ec062838 | 1099 | static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out, |
d30fe89c | 1100 | const struct fetch_pack_args *args, |
685fbd32 | 1101 | const struct ref *wants, struct oidset *common, |
0bbc0bc5 JT |
1102 | int *haves_to_send, int *in_vain, |
1103 | int sideband_all) | |
685fbd32 BW |
1104 | { |
1105 | int ret = 0; | |
1106 | struct strbuf req_buf = STRBUF_INIT; | |
1107 | ||
1108 | if (server_supports_v2("fetch", 1)) | |
1109 | packet_buf_write(&req_buf, "command=fetch"); | |
1110 | if (server_supports_v2("agent", 0)) | |
1111 | packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized()); | |
5e3548ef BW |
1112 | if (args->server_options && args->server_options->nr && |
1113 | server_supports_v2("server-option", 1)) { | |
1114 | int i; | |
1115 | for (i = 0; i < args->server_options->nr; i++) | |
1116 | packet_write_fmt(fd_out, "server-option=%s", | |
1117 | args->server_options->items[i].string); | |
1118 | } | |
685fbd32 BW |
1119 | |
1120 | packet_buf_delim(&req_buf); | |
1121 | if (args->use_thin_pack) | |
1122 | packet_buf_write(&req_buf, "thin-pack"); | |
1123 | if (args->no_progress) | |
1124 | packet_buf_write(&req_buf, "no-progress"); | |
1125 | if (args->include_tag) | |
1126 | packet_buf_write(&req_buf, "include-tag"); | |
1127 | if (prefer_ofs_delta) | |
1128 | packet_buf_write(&req_buf, "ofs-delta"); | |
0bbc0bc5 JT |
1129 | if (sideband_all) |
1130 | packet_buf_write(&req_buf, "sideband-all"); | |
685fbd32 | 1131 | |
f7e20501 BW |
1132 | /* Add shallow-info and deepen request */ |
1133 | if (server_supports_feature("fetch", "shallow", 0)) | |
1134 | add_shallow_requests(&req_buf, args); | |
00624d60 | 1135 | else if (is_repository_shallow(the_repository) || args->deepen) |
f7e20501 BW |
1136 | die(_("Server does not support shallow requests")); |
1137 | ||
ba95710a JT |
1138 | /* Add filter */ |
1139 | if (server_supports_feature("fetch", "filter", 0) && | |
1140 | args->filter_options.choice) { | |
87c2d9d3 | 1141 | struct strbuf expanded_filter_spec = STRBUF_INIT; |
ba95710a | 1142 | print_verbose(args, _("Server supports filter")); |
87c2d9d3 JS |
1143 | expand_list_objects_filter_spec(&args->filter_options, |
1144 | &expanded_filter_spec); | |
ba95710a | 1145 | packet_buf_write(&req_buf, "filter %s", |
87c2d9d3 JS |
1146 | expanded_filter_spec.buf); |
1147 | strbuf_release(&expanded_filter_spec); | |
ba95710a JT |
1148 | } else if (args->filter_options.choice) { |
1149 | warning("filtering not recognized by server, ignoring"); | |
1150 | } | |
1151 | ||
685fbd32 | 1152 | /* add wants */ |
12f19a98 | 1153 | add_wants(args->no_dependents, wants, &req_buf); |
685fbd32 | 1154 | |
ba95710a JT |
1155 | if (args->no_dependents) { |
1156 | packet_buf_write(&req_buf, "done"); | |
1157 | ret = 1; | |
1158 | } else { | |
1159 | /* Add all of the common commits we've found in previous rounds */ | |
1160 | add_common(&req_buf, common); | |
685fbd32 | 1161 | |
ba95710a | 1162 | /* Add initial haves */ |
ec062838 | 1163 | ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain); |
ba95710a | 1164 | } |
685fbd32 BW |
1165 | |
1166 | /* Send request */ | |
1167 | packet_buf_flush(&req_buf); | |
37c80012 JK |
1168 | if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0) |
1169 | die_errno(_("unable to write request to remote")); | |
685fbd32 BW |
1170 | |
1171 | strbuf_release(&req_buf); | |
1172 | return ret; | |
1173 | } | |
1174 | ||
1175 | /* | |
1176 | * Processes a section header in a server's response and checks if it matches | |
1177 | * `section`. If the value of `peek` is 1, the header line will be peeked (and | |
1178 | * not consumed); if 0, the line will be consumed and the function will die if | |
1179 | * the section header doesn't match what was expected. | |
1180 | */ | |
1181 | static int process_section_header(struct packet_reader *reader, | |
1182 | const char *section, int peek) | |
1183 | { | |
1184 | int ret; | |
1185 | ||
1186 | if (packet_reader_peek(reader) != PACKET_READ_NORMAL) | |
bbb19a8b | 1187 | die(_("error reading section header '%s'"), section); |
685fbd32 BW |
1188 | |
1189 | ret = !strcmp(reader->line, section); | |
1190 | ||
1191 | if (!peek) { | |
1192 | if (!ret) | |
bbb19a8b | 1193 | die(_("expected '%s', received '%s'"), |
685fbd32 BW |
1194 | section, reader->line); |
1195 | packet_reader_read(reader); | |
1196 | } | |
1197 | ||
1198 | return ret; | |
1199 | } | |
1200 | ||
ec062838 | 1201 | static int process_acks(struct fetch_negotiator *negotiator, |
d30fe89c JT |
1202 | struct packet_reader *reader, |
1203 | struct oidset *common) | |
685fbd32 BW |
1204 | { |
1205 | /* received */ | |
1206 | int received_ready = 0; | |
1207 | int received_ack = 0; | |
1208 | ||
1209 | process_section_header(reader, "acknowledgments", 0); | |
1210 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { | |
1211 | const char *arg; | |
1212 | ||
1213 | if (!strcmp(reader->line, "NAK")) | |
1214 | continue; | |
1215 | ||
1216 | if (skip_prefix(reader->line, "ACK ", &arg)) { | |
1217 | struct object_id oid; | |
1218 | if (!get_oid_hex(arg, &oid)) { | |
1219 | struct commit *commit; | |
1220 | oidset_insert(common, &oid); | |
c1f5eb49 | 1221 | commit = lookup_commit(the_repository, &oid); |
ec062838 | 1222 | negotiator->ack(negotiator, commit); |
685fbd32 BW |
1223 | } |
1224 | continue; | |
1225 | } | |
1226 | ||
1227 | if (!strcmp(reader->line, "ready")) { | |
685fbd32 BW |
1228 | received_ready = 1; |
1229 | continue; | |
1230 | } | |
1231 | ||
bbb19a8b | 1232 | die(_("unexpected acknowledgment line: '%s'"), reader->line); |
685fbd32 BW |
1233 | } |
1234 | ||
1235 | if (reader->status != PACKET_READ_FLUSH && | |
1236 | reader->status != PACKET_READ_DELIM) | |
bbb19a8b | 1237 | die(_("error processing acks: %d"), reader->status); |
685fbd32 | 1238 | |
5400b2a2 JT |
1239 | /* |
1240 | * If an "acknowledgments" section is sent, a packfile is sent if and | |
1241 | * only if "ready" was sent in this section. The other sections | |
1242 | * ("shallow-info" and "wanted-refs") are sent only if a packfile is | |
1243 | * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH | |
1244 | * otherwise. | |
1245 | */ | |
1246 | if (received_ready && reader->status != PACKET_READ_DELIM) | |
1247 | die(_("expected packfile to be sent after 'ready'")); | |
1248 | if (!received_ready && reader->status != PACKET_READ_FLUSH) | |
1249 | die(_("expected no other sections to be sent after no 'ready'")); | |
1250 | ||
685fbd32 BW |
1251 | /* return 0 if no common, 1 if there are common, or 2 if ready */ |
1252 | return received_ready ? 2 : (received_ack ? 1 : 0); | |
1253 | } | |
1254 | ||
f7e20501 | 1255 | static void receive_shallow_info(struct fetch_pack_args *args, |
1339078f JT |
1256 | struct packet_reader *reader, |
1257 | struct oid_array *shallows, | |
1258 | struct shallow_info *si) | |
f7e20501 | 1259 | { |
1339078f | 1260 | int unshallow_received = 0; |
bd0b42ae | 1261 | |
f7e20501 BW |
1262 | process_section_header(reader, "shallow-info", 0); |
1263 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { | |
1264 | const char *arg; | |
1265 | struct object_id oid; | |
1266 | ||
1267 | if (skip_prefix(reader->line, "shallow ", &arg)) { | |
1268 | if (get_oid_hex(arg, &oid)) | |
1269 | die(_("invalid shallow line: %s"), reader->line); | |
1339078f | 1270 | oid_array_append(shallows, &oid); |
f7e20501 BW |
1271 | continue; |
1272 | } | |
1273 | if (skip_prefix(reader->line, "unshallow ", &arg)) { | |
1274 | if (get_oid_hex(arg, &oid)) | |
1275 | die(_("invalid unshallow line: %s"), reader->line); | |
5abddd1e | 1276 | if (!lookup_object(the_repository, oid.hash)) |
f7e20501 BW |
1277 | die(_("object not found: %s"), reader->line); |
1278 | /* make sure that it is parsed as shallow */ | |
109cd76d | 1279 | if (!parse_object(the_repository, &oid)) |
f7e20501 BW |
1280 | die(_("error in object: %s"), reader->line); |
1281 | if (unregister_shallow(&oid)) | |
1282 | die(_("no shallow found: %s"), reader->line); | |
1339078f | 1283 | unshallow_received = 1; |
f7e20501 BW |
1284 | continue; |
1285 | } | |
1286 | die(_("expected shallow/unshallow, got %s"), reader->line); | |
1287 | } | |
1288 | ||
1289 | if (reader->status != PACKET_READ_FLUSH && | |
1290 | reader->status != PACKET_READ_DELIM) | |
bbb19a8b | 1291 | die(_("error processing shallow info: %d"), reader->status); |
f7e20501 | 1292 | |
1339078f JT |
1293 | if (args->deepen || unshallow_received) { |
1294 | /* | |
1295 | * Treat these as shallow lines caused by our depth settings. | |
1296 | * In v0, these lines cannot cause refs to be rejected; do the | |
1297 | * same. | |
1298 | */ | |
1299 | int i; | |
1300 | ||
1301 | for (i = 0; i < shallows->nr; i++) | |
1302 | register_shallow(the_repository, &shallows->oid[i]); | |
bd0b42ae JT |
1303 | setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, |
1304 | NULL); | |
1305 | args->deepen = 1; | |
1339078f JT |
1306 | } else if (shallows->nr) { |
1307 | /* | |
1308 | * Treat these as shallow lines caused by the remote being | |
1309 | * shallow. In v0, remote refs that reach these objects are | |
1310 | * rejected (unless --update-shallow is set); do the same. | |
1311 | */ | |
1312 | prepare_shallow_info(si, shallows); | |
1313 | if (si->nr_ours || si->nr_theirs) | |
1314 | alternate_shallow_file = | |
1315 | setup_temporary_shallow(si->shallow); | |
1316 | else | |
1317 | alternate_shallow_file = NULL; | |
380ebab2 | 1318 | } else { |
1319 | alternate_shallow_file = NULL; | |
bd0b42ae | 1320 | } |
f7e20501 BW |
1321 | } |
1322 | ||
b7643009 JT |
1323 | static int cmp_name_ref(const void *name, const void *ref) |
1324 | { | |
1325 | return strcmp(name, (*(struct ref **)ref)->name); | |
1326 | } | |
1327 | ||
e2842b39 JT |
1328 | static void receive_wanted_refs(struct packet_reader *reader, |
1329 | struct ref **sought, int nr_sought) | |
73302051 BW |
1330 | { |
1331 | process_section_header(reader, "wanted-refs", 0); | |
1332 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { | |
1333 | struct object_id oid; | |
1334 | const char *end; | |
b7643009 | 1335 | struct ref **found; |
73302051 BW |
1336 | |
1337 | if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ') | |
bbb19a8b | 1338 | die(_("expected wanted-ref, got '%s'"), reader->line); |
73302051 | 1339 | |
b7643009 JT |
1340 | found = bsearch(end, sought, nr_sought, sizeof(*sought), |
1341 | cmp_name_ref); | |
1342 | if (!found) | |
bbb19a8b | 1343 | die(_("unexpected wanted-ref: '%s'"), reader->line); |
b7643009 | 1344 | oidcpy(&(*found)->old_oid, &oid); |
73302051 BW |
1345 | } |
1346 | ||
1347 | if (reader->status != PACKET_READ_DELIM) | |
bbb19a8b | 1348 | die(_("error processing wanted refs: %d"), reader->status); |
73302051 BW |
1349 | } |
1350 | ||
685fbd32 BW |
1351 | enum fetch_state { |
1352 | FETCH_CHECK_LOCAL = 0, | |
1353 | FETCH_SEND_REQUEST, | |
1354 | FETCH_PROCESS_ACKS, | |
1355 | FETCH_GET_PACK, | |
1356 | FETCH_DONE, | |
1357 | }; | |
1358 | ||
1359 | static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args, | |
1360 | int fd[2], | |
1361 | const struct ref *orig_ref, | |
1362 | struct ref **sought, int nr_sought, | |
1339078f JT |
1363 | struct oid_array *shallows, |
1364 | struct shallow_info *si, | |
685fbd32 BW |
1365 | char **pack_lockfile) |
1366 | { | |
1367 | struct ref *ref = copy_ref_list(orig_ref); | |
1368 | enum fetch_state state = FETCH_CHECK_LOCAL; | |
1369 | struct oidset common = OIDSET_INIT; | |
1370 | struct packet_reader reader; | |
1371 | int in_vain = 0; | |
1372 | int haves_to_send = INITIAL_FLUSH; | |
ec062838 | 1373 | struct fetch_negotiator negotiator; |
42cc7485 | 1374 | fetch_negotiator_init(&negotiator, negotiation_algorithm); |
685fbd32 | 1375 | packet_reader_init(&reader, fd[0], NULL, 0, |
2d103c31 MS |
1376 | PACKET_READ_CHOMP_NEWLINE | |
1377 | PACKET_READ_DIE_ON_ERR_PACKET); | |
07c3c2aa JT |
1378 | if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) && |
1379 | server_supports_feature("fetch", "sideband-all", 0)) { | |
0bbc0bc5 JT |
1380 | reader.use_sideband = 1; |
1381 | reader.me = "fetch-pack"; | |
1382 | } | |
685fbd32 BW |
1383 | |
1384 | while (state != FETCH_DONE) { | |
1385 | switch (state) { | |
1386 | case FETCH_CHECK_LOCAL: | |
1387 | sort_ref_list(&ref, ref_compare_name); | |
1388 | QSORT(sought, nr_sought, cmp_ref_by_name); | |
1389 | ||
1390 | /* v2 supports these by default */ | |
1391 | allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1; | |
1392 | use_sideband = 2; | |
f7e20501 BW |
1393 | if (args->depth > 0 || args->deepen_since || args->deepen_not) |
1394 | args->deepen = 1; | |
685fbd32 | 1395 | |
685fbd32 | 1396 | /* Filter 'ref' by 'sought' and those that aren't local */ |
12f19a98 JT |
1397 | if (!args->no_dependents) { |
1398 | mark_complete_and_common_ref(&negotiator, args, &ref); | |
1399 | filter_refs(args, &ref, sought, nr_sought); | |
1400 | if (everything_local(args, &ref)) | |
1401 | state = FETCH_DONE; | |
1402 | else | |
1403 | state = FETCH_SEND_REQUEST; | |
1404 | ||
1405 | mark_tips(&negotiator, args->negotiation_tips); | |
1406 | for_each_cached_alternate(&negotiator, | |
1407 | insert_one_alternate_object); | |
1408 | } else { | |
1409 | filter_refs(args, &ref, sought, nr_sought); | |
685fbd32 | 1410 | state = FETCH_SEND_REQUEST; |
12f19a98 | 1411 | } |
685fbd32 BW |
1412 | break; |
1413 | case FETCH_SEND_REQUEST: | |
ec062838 JT |
1414 | if (send_fetch_request(&negotiator, fd[1], args, ref, |
1415 | &common, | |
0bbc0bc5 JT |
1416 | &haves_to_send, &in_vain, |
1417 | reader.use_sideband)) | |
685fbd32 BW |
1418 | state = FETCH_GET_PACK; |
1419 | else | |
1420 | state = FETCH_PROCESS_ACKS; | |
1421 | break; | |
1422 | case FETCH_PROCESS_ACKS: | |
1423 | /* Process ACKs/NAKs */ | |
ec062838 | 1424 | switch (process_acks(&negotiator, &reader, &common)) { |
685fbd32 BW |
1425 | case 2: |
1426 | state = FETCH_GET_PACK; | |
1427 | break; | |
1428 | case 1: | |
1429 | in_vain = 0; | |
1430 | /* fallthrough */ | |
1431 | default: | |
1432 | state = FETCH_SEND_REQUEST; | |
1433 | break; | |
1434 | } | |
1435 | break; | |
1436 | case FETCH_GET_PACK: | |
f7e20501 BW |
1437 | /* Check for shallow-info section */ |
1438 | if (process_section_header(&reader, "shallow-info", 1)) | |
1339078f | 1439 | receive_shallow_info(args, &reader, shallows, si); |
f7e20501 | 1440 | |
73302051 | 1441 | if (process_section_header(&reader, "wanted-refs", 1)) |
e2842b39 | 1442 | receive_wanted_refs(&reader, sought, nr_sought); |
73302051 | 1443 | |
685fbd32 BW |
1444 | /* get the pack */ |
1445 | process_section_header(&reader, "packfile", 0); | |
1446 | if (get_pack(args, fd, pack_lockfile)) | |
1447 | die(_("git fetch-pack: fetch failed.")); | |
1448 | ||
1449 | state = FETCH_DONE; | |
1450 | break; | |
1451 | case FETCH_DONE: | |
1452 | continue; | |
1453 | } | |
1454 | } | |
1455 | ||
ec062838 | 1456 | negotiator.release(&negotiator); |
685fbd32 BW |
1457 | oidset_clear(&common); |
1458 | return ref; | |
1459 | } | |
1460 | ||
1362df0d ÆAB |
1461 | static int fetch_pack_config_cb(const char *var, const char *value, void *cb) |
1462 | { | |
1463 | if (strcmp(var, "fetch.fsck.skiplist") == 0) { | |
1464 | const char *path; | |
1465 | ||
1466 | if (git_config_pathname(&path, var, value)) | |
1467 | return 1; | |
1468 | strbuf_addf(&fsck_msg_types, "%cskiplist=%s", | |
1469 | fsck_msg_types.len ? ',' : '=', path); | |
1470 | free((char *)path); | |
1471 | return 0; | |
1472 | } | |
1473 | ||
1474 | if (skip_prefix(var, "fetch.fsck.", &var)) { | |
1475 | if (is_valid_msg_type(var, value)) | |
1476 | strbuf_addf(&fsck_msg_types, "%c%s=%s", | |
1477 | fsck_msg_types.len ? ',' : '=', var, value); | |
1478 | else | |
1479 | warning("Skipping unknown msg id '%s'", var); | |
1480 | return 0; | |
1481 | } | |
1482 | ||
1483 | return git_default_config(var, value, cb); | |
1484 | } | |
1485 | ||
f44af51d | 1486 | static void fetch_pack_config(void) |
745f7a8c | 1487 | { |
f44af51d TA |
1488 | git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit); |
1489 | git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit); | |
1490 | git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta); | |
1491 | git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects); | |
1492 | git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects); | |
42cc7485 JT |
1493 | git_config_get_string("fetch.negotiationalgorithm", |
1494 | &negotiation_algorithm); | |
745f7a8c | 1495 | |
1362df0d | 1496 | git_config(fetch_pack_config_cb, NULL); |
745f7a8c NTND |
1497 | } |
1498 | ||
745f7a8c NTND |
1499 | static void fetch_pack_setup(void) |
1500 | { | |
1501 | static int did_setup; | |
1502 | if (did_setup) | |
1503 | return; | |
f44af51d | 1504 | fetch_pack_config(); |
745f7a8c NTND |
1505 | if (0 <= transfer_unpack_limit) |
1506 | unpack_limit = transfer_unpack_limit; | |
1507 | else if (0 <= fetch_unpack_limit) | |
1508 | unpack_limit = fetch_unpack_limit; | |
1509 | did_setup = 1; | |
1510 | } | |
1511 | ||
f2db854d JH |
1512 | static int remove_duplicates_in_refs(struct ref **ref, int nr) |
1513 | { | |
1514 | struct string_list names = STRING_LIST_INIT_NODUP; | |
1515 | int src, dst; | |
1516 | ||
1517 | for (src = dst = 0; src < nr; src++) { | |
1518 | struct string_list_item *item; | |
1519 | item = string_list_insert(&names, ref[src]->name); | |
1520 | if (item->util) | |
1521 | continue; /* already have it */ | |
1522 | item->util = ref[src]; | |
1523 | if (src != dst) | |
1524 | ref[dst] = ref[src]; | |
1525 | dst++; | |
1526 | } | |
1527 | for (src = dst; src < nr; src++) | |
1528 | ref[src] = NULL; | |
1529 | string_list_clear(&names, 0); | |
1530 | return dst; | |
1531 | } | |
1532 | ||
beea4152 | 1533 | static void update_shallow(struct fetch_pack_args *args, |
e2842b39 | 1534 | struct ref **sought, int nr_sought, |
beea4152 | 1535 | struct shallow_info *si) |
a796ccee | 1536 | { |
910650d2 | 1537 | struct oid_array ref = OID_ARRAY_INIT; |
4820a33b | 1538 | int *status; |
beea4152 NTND |
1539 | int i; |
1540 | ||
79891cb9 | 1541 | if (args->deepen && alternate_shallow_file) { |
a796ccee | 1542 | if (*alternate_shallow_file == '\0') { /* --unshallow */ |
102de880 | 1543 | unlink_or_warn(git_path_shallow(the_repository)); |
a796ccee NTND |
1544 | rollback_lock_file(&shallow_lock); |
1545 | } else | |
1546 | commit_lock_file(&shallow_lock); | |
23311f35 | 1547 | alternate_shallow_file = NULL; |
a796ccee NTND |
1548 | return; |
1549 | } | |
beea4152 NTND |
1550 | |
1551 | if (!si->shallow || !si->shallow->nr) | |
1552 | return; | |
1553 | ||
beea4152 NTND |
1554 | if (args->cloning) { |
1555 | /* | |
1556 | * remote is shallow, but this is a clone, there are | |
1557 | * no objects in repo to worry about. Accept any | |
1558 | * shallow points that exist in the pack (iow in repo | |
1559 | * after get_pack() and reprepare_packed_git()) | |
1560 | */ | |
910650d2 | 1561 | struct oid_array extra = OID_ARRAY_INIT; |
ee3051bd | 1562 | struct object_id *oid = si->shallow->oid; |
beea4152 | 1563 | for (i = 0; i < si->shallow->nr; i++) |
ee3051bd | 1564 | if (has_object_file(&oid[i])) |
910650d2 | 1565 | oid_array_append(&extra, &oid[i]); |
beea4152 NTND |
1566 | if (extra.nr) { |
1567 | setup_alternate_shallow(&shallow_lock, | |
1568 | &alternate_shallow_file, | |
1569 | &extra); | |
1570 | commit_lock_file(&shallow_lock); | |
23311f35 | 1571 | alternate_shallow_file = NULL; |
beea4152 | 1572 | } |
910650d2 | 1573 | oid_array_clear(&extra); |
beea4152 NTND |
1574 | return; |
1575 | } | |
4820a33b NTND |
1576 | |
1577 | if (!si->nr_ours && !si->nr_theirs) | |
1578 | return; | |
1579 | ||
1580 | remove_nonexistent_theirs_shallow(si); | |
4820a33b NTND |
1581 | if (!si->nr_ours && !si->nr_theirs) |
1582 | return; | |
e2842b39 JT |
1583 | for (i = 0; i < nr_sought; i++) |
1584 | oid_array_append(&ref, &sought[i]->old_oid); | |
4820a33b NTND |
1585 | si->ref = &ref; |
1586 | ||
48d25cae NTND |
1587 | if (args->update_shallow) { |
1588 | /* | |
1589 | * remote is also shallow, .git/shallow may be updated | |
1590 | * so all refs can be accepted. Make sure we only add | |
1591 | * shallow roots that are actually reachable from new | |
1592 | * refs. | |
1593 | */ | |
910650d2 | 1594 | struct oid_array extra = OID_ARRAY_INIT; |
ee3051bd | 1595 | struct object_id *oid = si->shallow->oid; |
48d25cae NTND |
1596 | assign_shallow_commits_to_refs(si, NULL, NULL); |
1597 | if (!si->nr_ours && !si->nr_theirs) { | |
910650d2 | 1598 | oid_array_clear(&ref); |
48d25cae NTND |
1599 | return; |
1600 | } | |
1601 | for (i = 0; i < si->nr_ours; i++) | |
910650d2 | 1602 | oid_array_append(&extra, &oid[si->ours[i]]); |
48d25cae | 1603 | for (i = 0; i < si->nr_theirs; i++) |
910650d2 | 1604 | oid_array_append(&extra, &oid[si->theirs[i]]); |
48d25cae NTND |
1605 | setup_alternate_shallow(&shallow_lock, |
1606 | &alternate_shallow_file, | |
1607 | &extra); | |
1608 | commit_lock_file(&shallow_lock); | |
910650d2 | 1609 | oid_array_clear(&extra); |
1610 | oid_array_clear(&ref); | |
23311f35 | 1611 | alternate_shallow_file = NULL; |
48d25cae NTND |
1612 | return; |
1613 | } | |
1614 | ||
4820a33b NTND |
1615 | /* |
1616 | * remote is also shallow, check what ref is safe to update | |
1617 | * without updating .git/shallow | |
1618 | */ | |
e2842b39 | 1619 | status = xcalloc(nr_sought, sizeof(*status)); |
4820a33b NTND |
1620 | assign_shallow_commits_to_refs(si, NULL, status); |
1621 | if (si->nr_ours || si->nr_theirs) { | |
e2842b39 | 1622 | for (i = 0; i < nr_sought; i++) |
4820a33b | 1623 | if (status[i]) |
e2842b39 | 1624 | sought[i]->status = REF_STATUS_REJECT_SHALLOW; |
4820a33b NTND |
1625 | } |
1626 | free(status); | |
910650d2 | 1627 | oid_array_clear(&ref); |
a796ccee NTND |
1628 | } |
1629 | ||
cf1e7c07 JT |
1630 | static int iterate_ref_map(void *cb_data, struct object_id *oid) |
1631 | { | |
1632 | struct ref **rm = cb_data; | |
1633 | struct ref *ref = *rm; | |
1634 | ||
1635 | if (!ref) | |
1636 | return -1; /* end of the list */ | |
1637 | *rm = ref->next; | |
1638 | oidcpy(oid, &ref->old_oid); | |
1639 | return 0; | |
1640 | } | |
1641 | ||
745f7a8c | 1642 | struct ref *fetch_pack(struct fetch_pack_args *args, |
0f804b0b | 1643 | int fd[], |
745f7a8c | 1644 | const struct ref *ref, |
f2db854d | 1645 | struct ref **sought, int nr_sought, |
910650d2 | 1646 | struct oid_array *shallow, |
685fbd32 BW |
1647 | char **pack_lockfile, |
1648 | enum protocol_version version) | |
745f7a8c | 1649 | { |
745f7a8c | 1650 | struct ref *ref_cpy; |
beea4152 | 1651 | struct shallow_info si; |
1339078f | 1652 | struct oid_array shallows_scratch = OID_ARRAY_INIT; |
745f7a8c NTND |
1653 | |
1654 | fetch_pack_setup(); | |
f2db854d JH |
1655 | if (nr_sought) |
1656 | nr_sought = remove_duplicates_in_refs(sought, nr_sought); | |
745f7a8c | 1657 | |
4c7f9567 JT |
1658 | if (args->no_dependents && !args->filter_options.choice) { |
1659 | /* | |
1660 | * The protocol does not support requesting that only the | |
1661 | * wanted objects be sent, so approximate this by setting a | |
1662 | * "blob:none" filter if no filter is already set. This works | |
1663 | * for all object types: note that wanted blobs will still be | |
1664 | * sent because they are directly specified as a "want". | |
1665 | * | |
1666 | * NEEDSWORK: Add an option in the protocol to request that | |
1667 | * only the wanted objects be sent, and implement it. | |
1668 | */ | |
1669 | parse_list_objects_filter(&args->filter_options, "blob:none"); | |
1670 | } | |
1671 | ||
01775651 | 1672 | if (version != protocol_v2 && !ref) { |
745f7a8c | 1673 | packet_flush(fd[1]); |
1dd73e20 | 1674 | die(_("no matching remote head")); |
745f7a8c | 1675 | } |
1e7d440b JT |
1676 | if (version == protocol_v2) { |
1677 | if (shallow->nr) | |
1678 | BUG("Protocol V2 does not provide shallows at this point in the fetch"); | |
1679 | memset(&si, 0, sizeof(si)); | |
685fbd32 | 1680 | ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought, |
1339078f | 1681 | &shallows_scratch, &si, |
685fbd32 | 1682 | pack_lockfile); |
1e7d440b JT |
1683 | } else { |
1684 | prepare_shallow_info(&si, shallow); | |
685fbd32 BW |
1685 | ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought, |
1686 | &si, pack_lockfile); | |
1e7d440b | 1687 | } |
a49d2834 | 1688 | reprepare_packed_git(the_repository); |
cf1e7c07 JT |
1689 | |
1690 | if (!args->cloning && args->deepen) { | |
1691 | struct check_connected_options opt = CHECK_CONNECTED_INIT; | |
1692 | struct ref *iterator = ref_cpy; | |
1693 | opt.shallow_file = alternate_shallow_file; | |
1694 | if (args->deepen) | |
1695 | opt.is_deepening_fetch = 1; | |
1696 | if (check_connected(iterate_ref_map, &iterator, &opt)) { | |
1697 | error(_("remote did not send all necessary objects")); | |
1698 | free_refs(ref_cpy); | |
1699 | ref_cpy = NULL; | |
1700 | rollback_lock_file(&shallow_lock); | |
1701 | goto cleanup; | |
1702 | } | |
1703 | args->connectivity_checked = 1; | |
1704 | } | |
1705 | ||
e2842b39 | 1706 | update_shallow(args, sought, nr_sought, &si); |
cf1e7c07 | 1707 | cleanup: |
beea4152 | 1708 | clear_shallow_info(&si); |
1339078f | 1709 | oid_array_clear(&shallows_scratch); |
745f7a8c NTND |
1710 | return ref_cpy; |
1711 | } | |
e860d96b MM |
1712 | |
1713 | int report_unmatched_refs(struct ref **sought, int nr_sought) | |
1714 | { | |
1715 | int i, ret = 0; | |
1716 | ||
1717 | for (i = 0; i < nr_sought; i++) { | |
d56583de | 1718 | if (!sought[i]) |
e860d96b | 1719 | continue; |
d56583de MM |
1720 | switch (sought[i]->match_status) { |
1721 | case REF_MATCHED: | |
1722 | continue; | |
1723 | case REF_NOT_MATCHED: | |
1724 | error(_("no such remote ref %s"), sought[i]->name); | |
1725 | break; | |
1726 | case REF_UNADVERTISED_NOT_ALLOWED: | |
1727 | error(_("Server does not allow request for unadvertised object %s"), | |
1728 | sought[i]->name); | |
1729 | break; | |
1730 | } | |
e860d96b MM |
1731 | ret = 1; |
1732 | } | |
1733 | return ret; | |
1734 | } |