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