]>
Commit | Line | Data |
---|---|---|
b888d61c DB |
1 | /* |
2 | * "git fetch" | |
3 | */ | |
bc5c5ec0 | 4 | #include "builtin.h" |
6c6ddf92 | 5 | #include "advice.h" |
b2141fc1 | 6 | #include "config.h" |
f394e093 | 7 | #include "gettext.h" |
32a8f510 | 8 | #include "environment.h" |
41771fa4 | 9 | #include "hex.h" |
e724197f | 10 | #include "repository.h" |
b888d61c | 11 | #include "refs.h" |
ec0cb496 | 12 | #include "refspec.h" |
dabab1d6 | 13 | #include "object-name.h" |
a034e910 | 14 | #include "object-store-ll.h" |
b7e2d8bc | 15 | #include "oidset.h" |
6f2d7430 | 16 | #include "oid-array.h" |
b888d61c | 17 | #include "commit.h" |
c455c87c | 18 | #include "string-list.h" |
b888d61c DB |
19 | #include "remote.h" |
20 | #include "transport.h" | |
4191c356 | 21 | #include "run-command.h" |
83201998 | 22 | #include "parse-options.h" |
4a16d072 | 23 | #include "sigchain.h" |
027771fc | 24 | #include "submodule-config.h" |
7dce19d3 | 25 | #include "submodule.h" |
f96400cb | 26 | #include "connected.h" |
dbbcd44f | 27 | #include "strvec.h" |
6bc91f23 | 28 | #include "utf8.h" |
ca4eed70 | 29 | #include "pager.h" |
c339932b | 30 | #include "path.h" |
b388633c | 31 | #include "pkt-line.h" |
acb0c572 | 32 | #include "list-objects-filter-options.h" |
64043556 | 33 | #include "commit-reach.h" |
24bc1a12 | 34 | #include "branch.h" |
b14ed5ad | 35 | #include "promisor-remote.h" |
50f26bd0 | 36 | #include "commit-graph.h" |
120ad2b0 | 37 | #include "shallow.h" |
74ea5c95 EN |
38 | #include "trace.h" |
39 | #include "trace2.h" | |
7f0cc04f | 40 | #include "bundle-uri.h" |
b888d61c | 41 | |
377444b4 DS |
42 | #define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000) |
43 | ||
83201998 | 44 | static const char * const builtin_fetch_usage[] = { |
719acedb NTND |
45 | N_("git fetch [<options>] [<repository> [<refspec>...]]"), |
46 | N_("git fetch [<options>] <group>"), | |
47 | N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"), | |
48 | N_("git fetch --all [<options>]"), | |
83201998 KH |
49 | NULL |
50 | }; | |
b888d61c | 51 | |
83201998 KH |
52 | enum { |
53 | TAGS_UNSET = 0, | |
54 | TAGS_DEFAULT = 1, | |
55 | TAGS_SET = 2 | |
56 | }; | |
57 | ||
50957937 | 58 | enum display_format { |
50957937 PS |
59 | DISPLAY_FORMAT_FULL, |
60 | DISPLAY_FORMAT_COMPACT, | |
dd781e38 | 61 | DISPLAY_FORMAT_PORCELAIN, |
50957937 PS |
62 | }; |
63 | ||
ce9636d6 | 64 | struct display_state { |
d6606e02 PS |
65 | struct strbuf buf; |
66 | ||
ce9636d6 | 67 | int refcol_width; |
50957937 | 68 | enum display_format format; |
c4ef5edb PS |
69 | |
70 | char *url; | |
71 | int url_len, shown_url; | |
ce9636d6 PS |
72 | }; |
73 | ||
377444b4 | 74 | static uint64_t forced_updates_ms = 0; |
2e03115d | 75 | static int prefetch = 0; |
737c5a9c MS |
76 | static int prune = -1; /* unspecified */ |
77 | #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */ | |
78 | ||
97716d21 ÆAB |
79 | static int prune_tags = -1; /* unspecified */ |
80 | #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */ | |
81 | ||
cdc034a0 | 82 | static int append, dry_run, force, keep, update_head_ok; |
887952b8 | 83 | static int write_fetch_head = 1; |
3c7bab06 | 84 | static int verbosity, deepen_relative, set_upstream, refetch; |
8c69832d | 85 | static int progress = -1; |
cdc034a0 | 86 | static int tags = TAGS_DEFAULT, update_shallow, deepen; |
c7b190da | 87 | static int atomic_fetch; |
c915f11e | 88 | static enum transport_family family; |
4191c356 | 89 | static const char *depth; |
508ea882 | 90 | static const char *deepen_since; |
83201998 | 91 | static const char *upload_pack; |
a45a2600 | 92 | static struct string_list deepen_not = STRING_LIST_INIT_NODUP; |
2d324efa | 93 | static struct strbuf default_rla = STRBUF_INIT; |
af234459 | 94 | static struct transport *gtransport; |
b26ed430 | 95 | static struct transport *gsecondary; |
e4cffacc | 96 | static struct refspec refmap = REFSPEC_INIT_FETCH; |
2a01bded | 97 | static struct list_objects_filter_options filter_options = LIST_OBJECTS_FILTER_INIT; |
5e3548ef | 98 | static struct string_list server_options = STRING_LIST_INIT_DUP; |
3390e42a | 99 | static struct string_list negotiation_tip = STRING_LIST_INIT_NODUP; |
e4022ed2 | 100 | |
58afbe88 PS |
101 | struct fetch_config { |
102 | enum display_format display_format; | |
39487a15 | 103 | int all; |
b779a25e | 104 | int prune; |
2b472cfe | 105 | int prune_tags; |
ba28b2ca | 106 | int show_forced_updates; |
56e8bb4f | 107 | int recurse_submodules; |
ac197cc0 | 108 | int parallel; |
f7e063f3 | 109 | int submodule_fetch_jobs; |
58afbe88 | 110 | }; |
e4022ed2 | 111 | |
a4e7e317 GC |
112 | static int git_fetch_config(const char *k, const char *v, |
113 | const struct config_context *ctx, void *cb) | |
737c5a9c | 114 | { |
58afbe88 PS |
115 | struct fetch_config *fetch_config = cb; |
116 | ||
39487a15 TB |
117 | if (!strcmp(k, "fetch.all")) { |
118 | fetch_config->all = git_config_bool(k, v); | |
119 | return 0; | |
120 | } | |
121 | ||
737c5a9c | 122 | if (!strcmp(k, "fetch.prune")) { |
b779a25e | 123 | fetch_config->prune = git_config_bool(k, v); |
737c5a9c MS |
124 | return 0; |
125 | } | |
58f4203e | 126 | |
97716d21 | 127 | if (!strcmp(k, "fetch.prunetags")) { |
2b472cfe | 128 | fetch_config->prune_tags = git_config_bool(k, v); |
97716d21 ÆAB |
129 | return 0; |
130 | } | |
131 | ||
cdbd70c4 | 132 | if (!strcmp(k, "fetch.showforcedupdates")) { |
ba28b2ca | 133 | fetch_config->show_forced_updates = git_config_bool(k, v); |
cdbd70c4 DS |
134 | return 0; |
135 | } | |
136 | ||
58f4203e SB |
137 | if (!strcmp(k, "submodule.recurse")) { |
138 | int r = git_config_bool(k, v) ? | |
139 | RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF; | |
56e8bb4f | 140 | fetch_config->recurse_submodules = r; |
58f4203e SB |
141 | } |
142 | ||
f20e7c1e | 143 | if (!strcmp(k, "submodule.fetchjobs")) { |
8868b1eb | 144 | fetch_config->submodule_fetch_jobs = parse_submodule_fetchjobs(k, v, ctx->kvi); |
f20e7c1e | 145 | return 0; |
8fa29159 | 146 | } else if (!strcmp(k, "fetch.recursesubmodules")) { |
56e8bb4f | 147 | fetch_config->recurse_submodules = parse_fetch_recurse_submodules_arg(k, v); |
8fa29159 | 148 | return 0; |
f20e7c1e BW |
149 | } |
150 | ||
d54dea77 | 151 | if (!strcmp(k, "fetch.parallel")) { |
8868b1eb | 152 | fetch_config->parallel = git_config_int(k, v, ctx->kvi); |
ac197cc0 | 153 | if (fetch_config->parallel < 0) |
d54dea77 | 154 | die(_("fetch.parallel cannot be negative")); |
ac197cc0 PS |
155 | if (!fetch_config->parallel) |
156 | fetch_config->parallel = online_cpus(); | |
d54dea77 JS |
157 | return 0; |
158 | } | |
159 | ||
58afbe88 PS |
160 | if (!strcmp(k, "fetch.output")) { |
161 | if (!v) | |
162 | return config_error_nonbool(k); | |
163 | else if (!strcasecmp(v, "full")) | |
164 | fetch_config->display_format = DISPLAY_FORMAT_FULL; | |
165 | else if (!strcasecmp(v, "compact")) | |
166 | fetch_config->display_format = DISPLAY_FORMAT_COMPACT; | |
167 | else | |
168 | die(_("invalid value for '%s': '%s'"), | |
169 | "fetch.output", v); | |
170 | } | |
171 | ||
a4e7e317 | 172 | return git_default_config(k, v, ctx, cb); |
737c5a9c MS |
173 | } |
174 | ||
c5558f80 JH |
175 | static int parse_refmap_arg(const struct option *opt, const char *arg, int unset) |
176 | { | |
517fe807 JK |
177 | BUG_ON_OPT_NEG(unset); |
178 | ||
c5558f80 JH |
179 | /* |
180 | * "git fetch --refmap='' origin foo" | |
181 | * can be used to tell the command not to store anywhere | |
182 | */ | |
66e33092 | 183 | refspec_append(opt->value, arg); |
e4cffacc | 184 | |
c5558f80 JH |
185 | return 0; |
186 | } | |
187 | ||
58d4d7f1 | 188 | static void unlock_pack(unsigned int flags) |
e4022ed2 | 189 | { |
af234459 | 190 | if (gtransport) |
58d4d7f1 | 191 | transport_unlock_pack(gtransport, flags); |
b26ed430 | 192 | if (gsecondary) |
58d4d7f1 PS |
193 | transport_unlock_pack(gsecondary, flags); |
194 | } | |
195 | ||
196 | static void unlock_pack_atexit(void) | |
197 | { | |
198 | unlock_pack(0); | |
e4022ed2 SP |
199 | } |
200 | ||
201 | static void unlock_pack_on_signal(int signo) | |
202 | { | |
58d4d7f1 | 203 | unlock_pack(TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER); |
4a16d072 | 204 | sigchain_pop(signo); |
e4022ed2 SP |
205 | raise(signo); |
206 | } | |
b888d61c | 207 | |
85682c19 | 208 | static void add_merge_config(struct ref **head, |
4577370e | 209 | const struct ref *remote_refs, |
85682c19 SP |
210 | struct branch *branch, |
211 | struct ref ***tail) | |
b888d61c | 212 | { |
85682c19 | 213 | int i; |
b888d61c | 214 | |
85682c19 SP |
215 | for (i = 0; i < branch->merge_nr; i++) { |
216 | struct ref *rm, **old_tail = *tail; | |
0ad4a5ff | 217 | struct refspec_item refspec; |
85682c19 SP |
218 | |
219 | for (rm = *head; rm; rm = rm->next) { | |
220 | if (branch_merge_matches(branch, i, rm->name)) { | |
900f2814 | 221 | rm->fetch_head_status = FETCH_HEAD_MERGE; |
85682c19 SP |
222 | break; |
223 | } | |
b888d61c | 224 | } |
85682c19 SP |
225 | if (rm) |
226 | continue; | |
227 | ||
9ad7c5ae | 228 | /* |
8b3f3f84 | 229 | * Not fetched to a remote-tracking branch? We need to fetch |
85682c19 | 230 | * it anyway to allow this branch's "branch.$name.merge" |
05207a28 | 231 | * to be honored by 'git pull', but we do not have to |
9ad7c5ae JH |
232 | * fail if branch.$name.merge is misconfigured to point |
233 | * at a nonexisting branch. If we were indeed called by | |
05207a28 | 234 | * 'git pull', it will notice the misconfiguration because |
9ad7c5ae JH |
235 | * there is no entry in the resulting FETCH_HEAD marked |
236 | * for merging. | |
85682c19 | 237 | */ |
8da61a2a | 238 | memset(&refspec, 0, sizeof(refspec)); |
85682c19 | 239 | refspec.src = branch->merge[i]->src; |
9ad7c5ae | 240 | get_fetch_map(remote_refs, &refspec, tail, 1); |
85682c19 | 241 | for (rm = *old_tail; rm; rm = rm->next) |
900f2814 | 242 | rm->fetch_head_status = FETCH_HEAD_MERGE; |
b888d61c DB |
243 | } |
244 | } | |
245 | ||
b7e2d8bc | 246 | static void create_fetch_oidset(struct ref **head, struct oidset *out) |
a0fbb5a3 MH |
247 | { |
248 | struct ref *rm = *head; | |
249 | while (rm) { | |
b7e2d8bc | 250 | oidset_insert(out, &rm->old_oid); |
a0fbb5a3 MH |
251 | rm = rm->next; |
252 | } | |
a0fbb5a3 MH |
253 | } |
254 | ||
e198b3a7 | 255 | struct refname_hash_entry { |
e2b5038d | 256 | struct hashmap_entry ent; |
e198b3a7 | 257 | struct object_id oid; |
f80d9223 | 258 | int ignore; |
e198b3a7 JH |
259 | char refname[FLEX_ARRAY]; |
260 | }; | |
261 | ||
5cf88fd8 | 262 | static int refname_hash_entry_cmp(const void *hashmap_cmp_fn_data UNUSED, |
939af16e EW |
263 | const struct hashmap_entry *eptr, |
264 | const struct hashmap_entry *entry_or_key, | |
e198b3a7 JH |
265 | const void *keydata) |
266 | { | |
939af16e | 267 | const struct refname_hash_entry *e1, *e2; |
e198b3a7 | 268 | |
939af16e EW |
269 | e1 = container_of(eptr, const struct refname_hash_entry, ent); |
270 | e2 = container_of(entry_or_key, const struct refname_hash_entry, ent); | |
e198b3a7 JH |
271 | return strcmp(e1->refname, keydata ? keydata : e2->refname); |
272 | } | |
273 | ||
274 | static struct refname_hash_entry *refname_hash_add(struct hashmap *map, | |
275 | const char *refname, | |
276 | const struct object_id *oid) | |
277 | { | |
278 | struct refname_hash_entry *ent; | |
279 | size_t len = strlen(refname); | |
280 | ||
281 | FLEX_ALLOC_MEM(ent, refname, refname, len); | |
d22245a2 | 282 | hashmap_entry_init(&ent->ent, strhash(refname)); |
e198b3a7 | 283 | oidcpy(&ent->oid, oid); |
b94e5c1d | 284 | hashmap_add(map, &ent->ent); |
e198b3a7 JH |
285 | return ent; |
286 | } | |
287 | ||
288 | static int add_one_refname(const char *refname, | |
289 | const struct object_id *oid, | |
5cf88fd8 | 290 | int flag UNUSED, void *cbdata) |
e198b3a7 JH |
291 | { |
292 | struct hashmap *refname_map = cbdata; | |
293 | ||
294 | (void) refname_hash_add(refname_map, refname, oid); | |
295 | return 0; | |
296 | } | |
297 | ||
298 | static void refname_hash_init(struct hashmap *map) | |
299 | { | |
300 | hashmap_init(map, refname_hash_entry_cmp, NULL, 0); | |
301 | } | |
302 | ||
303 | static int refname_hash_exists(struct hashmap *map, const char *refname) | |
304 | { | |
305 | return !!hashmap_get_from_hash(map, strhash(refname), refname); | |
306 | } | |
307 | ||
a8363b57 FC |
308 | static void clear_item(struct refname_hash_entry *item) |
309 | { | |
f80d9223 | 310 | item->ignore = 1; |
a8363b57 FC |
311 | } |
312 | ||
b3a80466 PS |
313 | |
314 | static void add_already_queued_tags(const char *refname, | |
ccf759cd | 315 | const struct object_id *old_oid UNUSED, |
b3a80466 PS |
316 | const struct object_id *new_oid, |
317 | void *cb_data) | |
318 | { | |
319 | struct hashmap *queued_tags = cb_data; | |
320 | if (starts_with(refname, "refs/tags/") && new_oid) | |
321 | (void) refname_hash_add(queued_tags, refname, new_oid); | |
322 | } | |
323 | ||
6d1700d5 | 324 | static void find_non_local_tags(const struct ref *refs, |
b3a80466 | 325 | struct ref_transaction *transaction, |
6d1700d5 BW |
326 | struct ref **head, |
327 | struct ref ***tail) | |
a0fbb5a3 | 328 | { |
e198b3a7 JH |
329 | struct hashmap existing_refs; |
330 | struct hashmap remote_refs; | |
b7e2d8bc | 331 | struct oidset fetch_oids = OIDSET_INIT; |
e198b3a7 JH |
332 | struct string_list remote_refs_list = STRING_LIST_INIT_NODUP; |
333 | struct string_list_item *remote_ref_item; | |
a0fbb5a3 | 334 | const struct ref *ref; |
e198b3a7 | 335 | struct refname_hash_entry *item = NULL; |
3e96c668 | 336 | const int quick_flags = OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT; |
e198b3a7 JH |
337 | |
338 | refname_hash_init(&existing_refs); | |
339 | refname_hash_init(&remote_refs); | |
b7e2d8bc | 340 | create_fetch_oidset(head, &fetch_oids); |
a0fbb5a3 | 341 | |
e198b3a7 | 342 | for_each_ref(add_one_refname, &existing_refs); |
b3a80466 PS |
343 | |
344 | /* | |
345 | * If we already have a transaction, then we need to filter out all | |
346 | * tags which have already been queued up. | |
347 | */ | |
348 | if (transaction) | |
349 | ref_transaction_for_each_queued_update(transaction, | |
350 | add_already_queued_tags, | |
351 | &existing_refs); | |
352 | ||
6d1700d5 | 353 | for (ref = refs; ref; ref = ref->next) { |
ad704485 | 354 | if (!starts_with(ref->name, "refs/tags/")) |
a0fbb5a3 MH |
355 | continue; |
356 | ||
357 | /* | |
358 | * The peeled ref always follows the matching base | |
359 | * ref, so if we see a peeled ref that we don't want | |
360 | * to fetch then we can mark the ref entry in the list | |
361 | * as one to ignore by setting util to NULL. | |
362 | */ | |
ad704485 | 363 | if (ends_with(ref->name, "^{}")) { |
5827a035 | 364 | if (item && |
bc726bd0 | 365 | !repo_has_object_file_with_flags(the_repository, &ref->old_oid, quick_flags) && |
b7e2d8bc | 366 | !oidset_contains(&fetch_oids, &ref->old_oid) && |
bc726bd0 | 367 | !repo_has_object_file_with_flags(the_repository, &item->oid, quick_flags) && |
b7e2d8bc | 368 | !oidset_contains(&fetch_oids, &item->oid)) |
a8363b57 | 369 | clear_item(item); |
a0fbb5a3 MH |
370 | item = NULL; |
371 | continue; | |
372 | } | |
373 | ||
374 | /* | |
375 | * If item is non-NULL here, then we previously saw a | |
376 | * ref not followed by a peeled reference, so we need | |
377 | * to check if it is a lightweight tag that we want to | |
378 | * fetch. | |
379 | */ | |
5827a035 | 380 | if (item && |
bc726bd0 | 381 | !repo_has_object_file_with_flags(the_repository, &item->oid, quick_flags) && |
b7e2d8bc | 382 | !oidset_contains(&fetch_oids, &item->oid)) |
a8363b57 | 383 | clear_item(item); |
a0fbb5a3 MH |
384 | |
385 | item = NULL; | |
386 | ||
387 | /* skip duplicates and refs that we already have */ | |
e198b3a7 JH |
388 | if (refname_hash_exists(&remote_refs, ref->name) || |
389 | refname_hash_exists(&existing_refs, ref->name)) | |
a0fbb5a3 MH |
390 | continue; |
391 | ||
e198b3a7 JH |
392 | item = refname_hash_add(&remote_refs, ref->name, &ref->old_oid); |
393 | string_list_insert(&remote_refs_list, ref->name); | |
a0fbb5a3 | 394 | } |
6da1a258 | 395 | hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent); |
a0fbb5a3 MH |
396 | |
397 | /* | |
398 | * We may have a final lightweight tag that needs to be | |
399 | * checked to see if it needs fetching. | |
400 | */ | |
5827a035 | 401 | if (item && |
bc726bd0 | 402 | !repo_has_object_file_with_flags(the_repository, &item->oid, quick_flags) && |
b7e2d8bc | 403 | !oidset_contains(&fetch_oids, &item->oid)) |
a8363b57 | 404 | clear_item(item); |
a0fbb5a3 MH |
405 | |
406 | /* | |
e198b3a7 | 407 | * For all the tags in the remote_refs_list, |
a0fbb5a3 MH |
408 | * add them to the list of refs to be fetched |
409 | */ | |
e198b3a7 JH |
410 | for_each_string_list_item(remote_ref_item, &remote_refs_list) { |
411 | const char *refname = remote_ref_item->string; | |
9528b80b | 412 | struct ref *rm; |
f23a4651 | 413 | unsigned int hash = strhash(refname); |
e198b3a7 | 414 | |
f23a4651 EW |
415 | item = hashmap_get_entry_from_hash(&remote_refs, hash, refname, |
416 | struct refname_hash_entry, ent); | |
e198b3a7 JH |
417 | if (!item) |
418 | BUG("unseen remote ref?"); | |
419 | ||
a0fbb5a3 | 420 | /* Unless we have already decided to ignore this item... */ |
f80d9223 | 421 | if (item->ignore) |
9528b80b FC |
422 | continue; |
423 | ||
424 | rm = alloc_ref(item->refname); | |
425 | rm->peer_ref = alloc_ref(item->refname); | |
426 | oidcpy(&rm->old_oid, &item->oid); | |
427 | **tail = rm; | |
428 | *tail = &rm->next; | |
a0fbb5a3 | 429 | } |
6da1a258 | 430 | hashmap_clear_and_free(&remote_refs, struct refname_hash_entry, ent); |
e198b3a7 | 431 | string_list_clear(&remote_refs_list, 0); |
b7e2d8bc | 432 | oidset_clear(&fetch_oids); |
a0fbb5a3 | 433 | } |
767f176a | 434 | |
2e03115d DS |
435 | static void filter_prefetch_refspec(struct refspec *rs) |
436 | { | |
437 | int i; | |
438 | ||
439 | if (!prefetch) | |
440 | return; | |
441 | ||
442 | for (i = 0; i < rs->nr; i++) { | |
443 | struct strbuf new_dst = STRBUF_INIT; | |
444 | char *old_dst; | |
445 | const char *sub = NULL; | |
446 | ||
447 | if (rs->items[i].negative) | |
448 | continue; | |
449 | if (!rs->items[i].dst || | |
450 | (rs->items[i].src && | |
992f25d7 DS |
451 | !strncmp(rs->items[i].src, |
452 | ref_namespace[NAMESPACE_TAGS].ref, | |
453 | strlen(ref_namespace[NAMESPACE_TAGS].ref)))) { | |
2e03115d DS |
454 | int j; |
455 | ||
456 | free(rs->items[i].src); | |
457 | free(rs->items[i].dst); | |
458 | ||
459 | for (j = i + 1; j < rs->nr; j++) { | |
460 | rs->items[j - 1] = rs->items[j]; | |
461 | rs->raw[j - 1] = rs->raw[j]; | |
462 | } | |
463 | rs->nr--; | |
464 | i--; | |
465 | continue; | |
466 | } | |
467 | ||
468 | old_dst = rs->items[i].dst; | |
992f25d7 | 469 | strbuf_addstr(&new_dst, ref_namespace[NAMESPACE_PREFETCH].ref); |
2e03115d DS |
470 | |
471 | /* | |
472 | * If old_dst starts with "refs/", then place | |
473 | * sub after that prefix. Otherwise, start at | |
474 | * the beginning of the string. | |
475 | */ | |
476 | if (!skip_prefix(old_dst, "refs/", &sub)) | |
477 | sub = old_dst; | |
478 | strbuf_addstr(&new_dst, sub); | |
479 | ||
480 | rs->items[i].dst = strbuf_detach(&new_dst, NULL); | |
481 | rs->items[i].force = 1; | |
482 | ||
483 | free(old_dst); | |
484 | } | |
485 | } | |
486 | ||
6d1700d5 BW |
487 | static struct ref *get_ref_map(struct remote *remote, |
488 | const struct ref *remote_refs, | |
65d96c8b | 489 | struct refspec *rs, |
f137a45e | 490 | int tags, int *autotags) |
b888d61c DB |
491 | { |
492 | int i; | |
493 | struct ref *rm; | |
494 | struct ref *ref_map = NULL; | |
495 | struct ref **tail = &ref_map; | |
496 | ||
c5a84e92 MH |
497 | /* opportunistically-updated references: */ |
498 | struct ref *orefs = NULL, **oref_tail = &orefs; | |
b888d61c | 499 | |
e198b3a7 | 500 | struct hashmap existing_refs; |
abcb7eeb | 501 | int existing_refs_populated = 0; |
f2690487 | 502 | |
2e03115d DS |
503 | filter_prefetch_refspec(rs); |
504 | if (remote) | |
505 | filter_prefetch_refspec(&remote->fetch); | |
506 | ||
65d96c8b | 507 | if (rs->nr) { |
c5558f80 | 508 | struct refspec *fetch_refspec; |
c5558f80 | 509 | |
65d96c8b BW |
510 | for (i = 0; i < rs->nr; i++) { |
511 | get_fetch_map(remote_refs, &rs->items[i], &tail, 0); | |
512 | if (rs->items[i].dst && rs->items[i].dst[0]) | |
b888d61c DB |
513 | *autotags = 1; |
514 | } | |
0281c930 | 515 | /* Merge everything on the command line (but not --tags) */ |
b888d61c | 516 | for (rm = ref_map; rm; rm = rm->next) |
900f2814 | 517 | rm->fetch_head_status = FETCH_HEAD_MERGE; |
f2690487 JK |
518 | |
519 | /* | |
0281c930 MH |
520 | * For any refs that we happen to be fetching via |
521 | * command-line arguments, the destination ref might | |
522 | * have been missing or have been different than the | |
523 | * remote-tracking ref that would be derived from the | |
524 | * configured refspec. In these cases, we want to | |
525 | * take the opportunity to update their configured | |
526 | * remote-tracking reference. However, we do not want | |
527 | * to mention these entries in FETCH_HEAD at all, as | |
528 | * they would simply be duplicates of existing | |
529 | * entries, so we set them FETCH_HEAD_IGNORE below. | |
530 | * | |
531 | * We compute these entries now, based only on the | |
532 | * refspecs specified on the command line. But we add | |
533 | * them to the list following the refspecs resulting | |
534 | * from the tags option so that one of the latter, | |
535 | * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed | |
536 | * by ref_remove_duplicates() in favor of one of these | |
537 | * opportunistic entries with FETCH_HEAD_IGNORE. | |
f2690487 | 538 | */ |
65d96c8b BW |
539 | if (refmap.nr) |
540 | fetch_refspec = &refmap; | |
541 | else | |
6d1700d5 | 542 | fetch_refspec = &remote->fetch; |
c5558f80 | 543 | |
65d96c8b BW |
544 | for (i = 0; i < fetch_refspec->nr; i++) |
545 | get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1); | |
e4cffacc | 546 | } else if (refmap.nr) { |
66996bea | 547 | die("--refmap option is only meaningful with command-line refspec(s)"); |
b888d61c DB |
548 | } else { |
549 | /* Use the defaults */ | |
85682c19 SP |
550 | struct branch *branch = branch_get(NULL); |
551 | int has_merge = branch_has_merge_config(branch); | |
3ee1757b | 552 | if (remote && |
e5349abf | 553 | (remote->fetch.nr || |
f31dbdc7 | 554 | /* Note: has_merge implies non-NULL branch->remote_name */ |
3ee1757b | 555 | (has_merge && !strcmp(branch->remote_name, remote->name)))) { |
e5349abf BW |
556 | for (i = 0; i < remote->fetch.nr; i++) { |
557 | get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0); | |
558 | if (remote->fetch.items[i].dst && | |
559 | remote->fetch.items[i].dst[0]) | |
b888d61c | 560 | *autotags = 1; |
85682c19 | 561 | if (!i && !has_merge && ref_map && |
e5349abf | 562 | !remote->fetch.items[0].pattern) |
900f2814 | 563 | ref_map->fetch_head_status = FETCH_HEAD_MERGE; |
b888d61c | 564 | } |
da0204df JS |
565 | /* |
566 | * if the remote we're fetching from is the same | |
567 | * as given in branch.<name>.remote, we add the | |
568 | * ref given in branch.<name>.merge, too. | |
f31dbdc7 BC |
569 | * |
570 | * Note: has_merge implies non-NULL branch->remote_name | |
da0204df | 571 | */ |
9ad7c5ae JH |
572 | if (has_merge && |
573 | !strcmp(branch->remote_name, remote->name)) | |
85682c19 | 574 | add_merge_config(&ref_map, remote_refs, branch, &tail); |
2e03115d | 575 | } else if (!prefetch) { |
b888d61c | 576 | ref_map = get_remote_ref(remote_refs, "HEAD"); |
9ad7c5ae | 577 | if (!ref_map) |
66996bea | 578 | die(_("couldn't find remote ref HEAD")); |
900f2814 | 579 | ref_map->fetch_head_status = FETCH_HEAD_MERGE; |
5aaf7f2a | 580 | tail = &ref_map->next; |
b888d61c DB |
581 | } |
582 | } | |
0281c930 | 583 | |
c5a84e92 MH |
584 | if (tags == TAGS_SET) |
585 | /* also fetch all tags */ | |
586 | get_fetch_map(remote_refs, tag_refspec, &tail, 0); | |
587 | else if (tags == TAGS_DEFAULT && *autotags) | |
b3a80466 | 588 | find_non_local_tags(remote_refs, NULL, &ref_map, &tail); |
0281c930 | 589 | |
c5a84e92 MH |
590 | /* Now append any refs to be updated opportunistically: */ |
591 | *tail = orefs; | |
592 | for (rm = orefs; rm; rm = rm->next) { | |
593 | rm->fetch_head_status = FETCH_HEAD_IGNORE; | |
594 | tail = &rm->next; | |
595 | } | |
596 | ||
c0192df6 JK |
597 | /* |
598 | * apply negative refspecs first, before we remove duplicates. This is | |
599 | * necessary as negative refspecs might remove an otherwise conflicting | |
600 | * duplicate. | |
601 | */ | |
602 | if (rs->nr) | |
603 | ref_map = apply_negative_refspecs(ref_map, rs); | |
604 | else | |
605 | ref_map = apply_negative_refspecs(ref_map, &remote->fetch); | |
606 | ||
14b8ced3 BW |
607 | ref_map = ref_remove_duplicates(ref_map); |
608 | ||
14b8ced3 BW |
609 | for (rm = ref_map; rm; rm = rm->next) { |
610 | if (rm->peer_ref) { | |
e198b3a7 JH |
611 | const char *refname = rm->peer_ref->name; |
612 | struct refname_hash_entry *peer_item; | |
f23a4651 | 613 | unsigned int hash = strhash(refname); |
e198b3a7 | 614 | |
abcb7eeb JT |
615 | if (!existing_refs_populated) { |
616 | refname_hash_init(&existing_refs); | |
617 | for_each_ref(add_one_refname, &existing_refs); | |
618 | existing_refs_populated = 1; | |
619 | } | |
620 | ||
f23a4651 EW |
621 | peer_item = hashmap_get_entry_from_hash(&existing_refs, |
622 | hash, refname, | |
623 | struct refname_hash_entry, ent); | |
14b8ced3 | 624 | if (peer_item) { |
e198b3a7 | 625 | struct object_id *old_oid = &peer_item->oid; |
14b8ced3 BW |
626 | oidcpy(&rm->peer_ref->old_oid, old_oid); |
627 | } | |
628 | } | |
629 | } | |
abcb7eeb | 630 | if (existing_refs_populated) |
6da1a258 | 631 | hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent); |
14b8ced3 BW |
632 | |
633 | return ref_map; | |
b888d61c DB |
634 | } |
635 | ||
fa250759 JK |
636 | #define STORE_REF_ERROR_OTHER 1 |
637 | #define STORE_REF_ERROR_DF_CONFLICT 2 | |
638 | ||
b888d61c DB |
639 | static int s_update_ref(const char *action, |
640 | struct ref *ref, | |
d4c8db8f | 641 | struct ref_transaction *transaction, |
b888d61c DB |
642 | int check_old) |
643 | { | |
1412f762 | 644 | char *msg; |
b888d61c | 645 | char *rla = getenv("GIT_REFLOG_ACTION"); |
d4c8db8f | 646 | struct ref_transaction *our_transaction = NULL; |
cd94f765 | 647 | struct strbuf err = STRBUF_INIT; |
c45889f1 | 648 | int ret; |
b888d61c | 649 | |
28a15401 JS |
650 | if (dry_run) |
651 | return 0; | |
b888d61c | 652 | if (!rla) |
2d324efa | 653 | rla = default_rla.buf; |
1412f762 | 654 | msg = xstrfmt("%s: %s", rla, action); |
cd94f765 | 655 | |
d4c8db8f PS |
656 | /* |
657 | * If no transaction was passed to us, we manage the transaction | |
658 | * ourselves. Otherwise, we trust the caller to handle the transaction | |
659 | * lifecycle. | |
660 | */ | |
c45889f1 | 661 | if (!transaction) { |
d4c8db8f PS |
662 | transaction = our_transaction = ref_transaction_begin(&err); |
663 | if (!transaction) { | |
664 | ret = STORE_REF_ERROR_OTHER; | |
665 | goto out; | |
666 | } | |
c45889f1 PS |
667 | } |
668 | ||
669 | ret = ref_transaction_update(transaction, ref->name, &ref->new_oid, | |
670 | check_old ? &ref->old_oid : NULL, | |
671 | 0, msg, &err); | |
cd94f765 | 672 | if (ret) { |
c45889f1 PS |
673 | ret = STORE_REF_ERROR_OTHER; |
674 | goto out; | |
cd94f765 RS |
675 | } |
676 | ||
d4c8db8f PS |
677 | if (our_transaction) { |
678 | switch (ref_transaction_commit(our_transaction, &err)) { | |
679 | case 0: | |
680 | break; | |
681 | case TRANSACTION_NAME_CONFLICT: | |
682 | ret = STORE_REF_ERROR_DF_CONFLICT; | |
683 | goto out; | |
684 | default: | |
685 | ret = STORE_REF_ERROR_OTHER; | |
686 | goto out; | |
687 | } | |
c45889f1 PS |
688 | } |
689 | ||
690 | out: | |
d4c8db8f | 691 | ref_transaction_free(our_transaction); |
c45889f1 PS |
692 | if (ret) |
693 | error("%s", err.buf); | |
cd94f765 | 694 | strbuf_release(&err); |
1412f762 | 695 | free(msg); |
c45889f1 | 696 | return ret; |
b888d61c DB |
697 | } |
698 | ||
9539638a | 699 | static int refcol_width(const struct ref *ref_map, int compact_format) |
6bc91f23 | 700 | { |
9539638a PS |
701 | const struct ref *ref; |
702 | int max, width = 10; | |
6bc91f23 | 703 | |
9539638a PS |
704 | max = term_columns(); |
705 | if (compact_format) | |
706 | max = max * 2 / 3; | |
6bc91f23 | 707 | |
9539638a PS |
708 | for (ref = ref_map; ref; ref = ref->next) { |
709 | int rlen, llen = 0, len; | |
bc437d10 | 710 | |
9539638a PS |
711 | if (ref->status == REF_STATUS_REJECT_SHALLOW || |
712 | !ref->peer_ref || | |
713 | !strcmp(ref->name, "HEAD")) | |
714 | continue; | |
6bc91f23 | 715 | |
9539638a PS |
716 | /* uptodate lines are only shown on high verbosity level */ |
717 | if (verbosity <= 0 && oideq(&ref->peer_ref->old_oid, &ref->old_oid)) | |
718 | continue; | |
719 | ||
720 | rlen = utf8_strwidth(prettify_refname(ref->name)); | |
721 | if (!compact_format) | |
722 | llen = utf8_strwidth(prettify_refname(ref->peer_ref->name)); | |
723 | ||
724 | /* | |
725 | * rough estimation to see if the output line is too long and | |
726 | * should not be counted (we can't do precise calculation | |
727 | * anyway because we don't know if the error explanation part | |
728 | * will be printed in update_local_ref) | |
729 | */ | |
730 | len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen; | |
731 | if (len >= max) | |
732 | continue; | |
733 | ||
734 | if (width < rlen) | |
735 | width = rlen; | |
bc437d10 | 736 | } |
6bc91f23 | 737 | |
9539638a | 738 | return width; |
6bc91f23 NTND |
739 | } |
740 | ||
c4ef5edb | 741 | static void display_state_init(struct display_state *display_state, struct ref *ref_map, |
58afbe88 | 742 | const char *raw_url, enum display_format format) |
6bc91f23 | 743 | { |
c4ef5edb | 744 | int i; |
bc437d10 | 745 | |
ce9636d6 | 746 | memset(display_state, 0, sizeof(*display_state)); |
d6606e02 | 747 | strbuf_init(&display_state->buf, 0); |
58afbe88 | 748 | display_state->format = format; |
d6606e02 | 749 | |
c4ef5edb PS |
750 | if (raw_url) |
751 | display_state->url = transport_anonymize_url(raw_url); | |
752 | else | |
753 | display_state->url = xstrdup("foreign"); | |
754 | ||
755 | display_state->url_len = strlen(display_state->url); | |
756 | for (i = display_state->url_len - 1; display_state->url[i] == '/' && 0 <= i; i--) | |
757 | ; | |
758 | display_state->url_len = i + 1; | |
759 | if (4 < i && !strncmp(".git", display_state->url + i - 3, 4)) | |
760 | display_state->url_len = i - 3; | |
bc437d10 | 761 | |
f6bb64df PS |
762 | if (verbosity < 0) |
763 | return; | |
764 | ||
50957937 PS |
765 | switch (display_state->format) { |
766 | case DISPLAY_FORMAT_FULL: | |
767 | case DISPLAY_FORMAT_COMPACT: | |
768 | display_state->refcol_width = refcol_width(ref_map, | |
769 | display_state->format == DISPLAY_FORMAT_COMPACT); | |
770 | break; | |
dd781e38 PS |
771 | case DISPLAY_FORMAT_PORCELAIN: |
772 | /* We don't need to precompute anything here. */ | |
773 | break; | |
50957937 PS |
774 | default: |
775 | BUG("unexpected display format %d", display_state->format); | |
6bc91f23 NTND |
776 | } |
777 | } | |
165f3902 | 778 | |
c4ef5edb PS |
779 | static void display_state_release(struct display_state *display_state) |
780 | { | |
d6606e02 | 781 | strbuf_release(&display_state->buf); |
c4ef5edb PS |
782 | free(display_state->url); |
783 | } | |
784 | ||
ce9636d6 | 785 | static void print_remote_to_local(struct display_state *display_state, |
bc437d10 NTND |
786 | const char *remote, const char *local) |
787 | { | |
d6606e02 PS |
788 | strbuf_addf(&display_state->buf, "%-*s -> %s", |
789 | display_state->refcol_width, remote, local); | |
bc437d10 NTND |
790 | } |
791 | ||
792 | static int find_and_replace(struct strbuf *haystack, | |
793 | const char *needle, | |
794 | const char *placeholder) | |
795 | { | |
dc40b24d | 796 | const char *p = NULL; |
bc437d10 NTND |
797 | int plen, nlen; |
798 | ||
dc40b24d NTND |
799 | nlen = strlen(needle); |
800 | if (ends_with(haystack->buf, needle)) | |
801 | p = haystack->buf + haystack->len - nlen; | |
802 | else | |
803 | p = strstr(haystack->buf, needle); | |
bc437d10 NTND |
804 | if (!p) |
805 | return 0; | |
806 | ||
807 | if (p > haystack->buf && p[-1] != '/') | |
808 | return 0; | |
809 | ||
810 | plen = strlen(p); | |
bc437d10 NTND |
811 | if (plen > nlen && p[nlen] != '/') |
812 | return 0; | |
813 | ||
814 | strbuf_splice(haystack, p - haystack->buf, nlen, | |
815 | placeholder, strlen(placeholder)); | |
816 | return 1; | |
817 | } | |
818 | ||
d6606e02 | 819 | static void print_compact(struct display_state *display_state, |
bc437d10 NTND |
820 | const char *remote, const char *local) |
821 | { | |
822 | struct strbuf r = STRBUF_INIT; | |
823 | struct strbuf l = STRBUF_INIT; | |
824 | ||
825 | if (!strcmp(remote, local)) { | |
d6606e02 | 826 | strbuf_addf(&display_state->buf, "%-*s -> *", display_state->refcol_width, remote); |
bc437d10 NTND |
827 | return; |
828 | } | |
829 | ||
830 | strbuf_addstr(&r, remote); | |
831 | strbuf_addstr(&l, local); | |
832 | ||
833 | if (!find_and_replace(&r, local, "*")) | |
834 | find_and_replace(&l, remote, "*"); | |
d6606e02 | 835 | print_remote_to_local(display_state, r.buf, l.buf); |
bc437d10 NTND |
836 | |
837 | strbuf_release(&r); | |
838 | strbuf_release(&l); | |
839 | } | |
840 | ||
d6606e02 PS |
841 | static void display_ref_update(struct display_state *display_state, char code, |
842 | const char *summary, const char *error, | |
843 | const char *remote, const char *local, | |
dd781e38 PS |
844 | const struct object_id *old_oid, |
845 | const struct object_id *new_oid, | |
d6606e02 | 846 | int summary_width) |
d0b39a03 | 847 | { |
dd781e38 | 848 | FILE *f = stderr; |
f6bb64df PS |
849 | |
850 | if (verbosity < 0) | |
851 | return; | |
852 | ||
d6606e02 PS |
853 | strbuf_reset(&display_state->buf); |
854 | ||
50957937 PS |
855 | switch (display_state->format) { |
856 | case DISPLAY_FORMAT_FULL: | |
857 | case DISPLAY_FORMAT_COMPACT: { | |
858 | int width; | |
c4ef5edb | 859 | |
50957937 PS |
860 | if (!display_state->shown_url) { |
861 | strbuf_addf(&display_state->buf, _("From %.*s\n"), | |
862 | display_state->url_len, display_state->url); | |
863 | display_state->shown_url = 1; | |
864 | } | |
901f3d40 | 865 | |
50957937 PS |
866 | width = (summary_width + strlen(summary) - gettext_width(summary)); |
867 | remote = prettify_refname(remote); | |
868 | local = prettify_refname(local); | |
869 | ||
870 | strbuf_addf(&display_state->buf, " %c %-*s ", code, width, summary); | |
871 | ||
872 | if (display_state->format != DISPLAY_FORMAT_COMPACT) | |
873 | print_remote_to_local(display_state, remote, local); | |
874 | else | |
875 | print_compact(display_state, remote, local); | |
876 | ||
877 | if (error) | |
878 | strbuf_addf(&display_state->buf, " (%s)", error); | |
879 | ||
880 | break; | |
881 | } | |
dd781e38 PS |
882 | case DISPLAY_FORMAT_PORCELAIN: |
883 | strbuf_addf(&display_state->buf, "%c %s %s %s", code, | |
884 | oid_to_hex(old_oid), oid_to_hex(new_oid), local); | |
885 | f = stdout; | |
886 | break; | |
50957937 PS |
887 | default: |
888 | BUG("unexpected display format %d", display_state->format); | |
889 | }; | |
d6606e02 PS |
890 | strbuf_addch(&display_state->buf, '\n'); |
891 | ||
dd781e38 | 892 | fputs(display_state->buf.buf, f); |
d0b39a03 | 893 | } |
165f3902 | 894 | |
b888d61c | 895 | static int update_local_ref(struct ref *ref, |
d4c8db8f | 896 | struct ref_transaction *transaction, |
ce9636d6 | 897 | struct display_state *display_state, |
1c31764d | 898 | const struct ref *remote_ref, |
ba28b2ca PS |
899 | int summary_width, |
900 | const struct fetch_config *config) | |
b888d61c | 901 | { |
b888d61c | 902 | struct commit *current = NULL, *updated; |
377444b4 | 903 | int fast_forward = 0; |
b888d61c | 904 | |
47c61004 | 905 | if (!repo_has_object_file(the_repository, &ref->new_oid)) |
f4e54d02 | 906 | die(_("object %s not found"), oid_to_hex(&ref->new_oid)); |
b888d61c | 907 | |
4a7e27e9 | 908 | if (oideq(&ref->old_oid, &ref->new_oid)) { |
7f87aff2 | 909 | if (verbosity > 0) |
d6606e02 | 910 | display_ref_update(display_state, '=', _("[up to date]"), NULL, |
dd781e38 PS |
911 | remote_ref->name, ref->name, |
912 | &ref->old_oid, &ref->new_oid, summary_width); | |
b888d61c DB |
913 | return 0; |
914 | } | |
915 | ||
8bc1f39f | 916 | if (!update_head_ok && |
12d47e3b DS |
917 | !is_null_oid(&ref->old_oid) && |
918 | branch_checked_out(ref->name)) { | |
b888d61c DB |
919 | /* |
920 | * If this is the head, and it's not okay to update | |
921 | * the head, and the old value of the head isn't empty... | |
922 | */ | |
d6606e02 PS |
923 | display_ref_update(display_state, '!', _("[rejected]"), |
924 | _("can't fetch into checked-out branch"), | |
dd781e38 PS |
925 | remote_ref->name, ref->name, |
926 | &ref->old_oid, &ref->new_oid, summary_width); | |
b888d61c DB |
927 | return 1; |
928 | } | |
929 | ||
f4e54d02 | 930 | if (!is_null_oid(&ref->old_oid) && |
59556548 | 931 | starts_with(ref->name, "refs/tags/")) { |
0bc8d71b ÆAB |
932 | if (force || ref->force) { |
933 | int r; | |
d4c8db8f | 934 | r = s_update_ref("updating tag", ref, transaction, 0); |
d6606e02 PS |
935 | display_ref_update(display_state, r ? '!' : 't', _("[tag update]"), |
936 | r ? _("unable to update local ref") : NULL, | |
dd781e38 PS |
937 | remote_ref->name, ref->name, |
938 | &ref->old_oid, &ref->new_oid, summary_width); | |
0bc8d71b ÆAB |
939 | return r; |
940 | } else { | |
d6606e02 PS |
941 | display_ref_update(display_state, '!', _("[rejected]"), |
942 | _("would clobber existing tag"), | |
dd781e38 PS |
943 | remote_ref->name, ref->name, |
944 | &ref->old_oid, &ref->new_oid, summary_width); | |
0bc8d71b ÆAB |
945 | return 1; |
946 | } | |
b888d61c DB |
947 | } |
948 | ||
21e1ee8f SB |
949 | current = lookup_commit_reference_gently(the_repository, |
950 | &ref->old_oid, 1); | |
951 | updated = lookup_commit_reference_gently(the_repository, | |
952 | &ref->new_oid, 1); | |
b888d61c | 953 | if (!current || !updated) { |
165f3902 NP |
954 | const char *msg; |
955 | const char *what; | |
6315472e | 956 | int r; |
0997adaa MB |
957 | /* |
958 | * Nicely describe the new ref we're fetching. | |
959 | * Base this on the remote's ref name, as it's | |
960 | * more likely to follow a standard layout. | |
961 | */ | |
6bc7a37e | 962 | if (starts_with(remote_ref->name, "refs/tags/")) { |
b888d61c | 963 | msg = "storing tag"; |
f7b3742a | 964 | what = _("[new tag]"); |
6bc7a37e | 965 | } else if (starts_with(remote_ref->name, "refs/heads/")) { |
b888d61c | 966 | msg = "storing head"; |
f7b3742a | 967 | what = _("[new branch]"); |
0997adaa MB |
968 | } else { |
969 | msg = "storing ref"; | |
970 | what = _("[new ref]"); | |
165f3902 NP |
971 | } |
972 | ||
d4c8db8f | 973 | r = s_update_ref(msg, ref, transaction, 0); |
d6606e02 PS |
974 | display_ref_update(display_state, r ? '!' : '*', what, |
975 | r ? _("unable to update local ref") : NULL, | |
dd781e38 PS |
976 | remote_ref->name, ref->name, |
977 | &ref->old_oid, &ref->new_oid, summary_width); | |
6315472e | 978 | return r; |
b888d61c DB |
979 | } |
980 | ||
ba28b2ca | 981 | if (config->show_forced_updates) { |
377444b4 | 982 | uint64_t t_before = getnanotime(); |
cb338c23 ÆAB |
983 | fast_forward = repo_in_merge_bases(the_repository, current, |
984 | updated); | |
377444b4 DS |
985 | forced_updates_ms += (getnanotime() - t_before) / 1000000; |
986 | } else { | |
987 | fast_forward = 1; | |
988 | } | |
989 | ||
990 | if (fast_forward) { | |
bd22d4ff | 991 | struct strbuf quickref = STRBUF_INIT; |
6315472e | 992 | int r; |
cdbd70c4 | 993 | |
30e677e0 | 994 | strbuf_add_unique_abbrev(&quickref, ¤t->object.oid, DEFAULT_ABBREV); |
bd22d4ff | 995 | strbuf_addstr(&quickref, ".."); |
30e677e0 | 996 | strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV); |
d4c8db8f | 997 | r = s_update_ref("fast-forward", ref, transaction, 1); |
d6606e02 PS |
998 | display_ref_update(display_state, r ? '!' : ' ', quickref.buf, |
999 | r ? _("unable to update local ref") : NULL, | |
dd781e38 PS |
1000 | remote_ref->name, ref->name, |
1001 | &ref->old_oid, &ref->new_oid, summary_width); | |
bd22d4ff | 1002 | strbuf_release(&quickref); |
6315472e | 1003 | return r; |
165f3902 | 1004 | } else if (force || ref->force) { |
bd22d4ff | 1005 | struct strbuf quickref = STRBUF_INIT; |
6315472e | 1006 | int r; |
30e677e0 | 1007 | strbuf_add_unique_abbrev(&quickref, ¤t->object.oid, DEFAULT_ABBREV); |
bd22d4ff | 1008 | strbuf_addstr(&quickref, "..."); |
30e677e0 | 1009 | strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV); |
d4c8db8f | 1010 | r = s_update_ref("forced-update", ref, transaction, 1); |
d6606e02 PS |
1011 | display_ref_update(display_state, r ? '!' : '+', quickref.buf, |
1012 | r ? _("unable to update local ref") : _("forced update"), | |
dd781e38 PS |
1013 | remote_ref->name, ref->name, |
1014 | &ref->old_oid, &ref->new_oid, summary_width); | |
bd22d4ff | 1015 | strbuf_release(&quickref); |
6315472e | 1016 | return r; |
165f3902 | 1017 | } else { |
d6606e02 | 1018 | display_ref_update(display_state, '!', _("[rejected]"), _("non-fast-forward"), |
dd781e38 PS |
1019 | remote_ref->name, ref->name, |
1020 | &ref->old_oid, &ref->new_oid, summary_width); | |
b888d61c DB |
1021 | return 1; |
1022 | } | |
b888d61c DB |
1023 | } |
1024 | ||
9fec7b21 | 1025 | static const struct object_id *iterate_ref_map(void *cb_data) |
6b67e0dc | 1026 | { |
f0e278b1 JH |
1027 | struct ref **rm = cb_data; |
1028 | struct ref *ref = *rm; | |
6b67e0dc | 1029 | |
4820a33b NTND |
1030 | while (ref && ref->status == REF_STATUS_REJECT_SHALLOW) |
1031 | ref = ref->next; | |
f0e278b1 | 1032 | if (!ref) |
9fec7b21 | 1033 | return NULL; |
f0e278b1 | 1034 | *rm = ref->next; |
9fec7b21 | 1035 | return &ref->old_oid; |
6b67e0dc JH |
1036 | } |
1037 | ||
58a646a3 PS |
1038 | struct fetch_head { |
1039 | FILE *fp; | |
929d0445 | 1040 | struct strbuf buf; |
58a646a3 PS |
1041 | }; |
1042 | ||
1043 | static int open_fetch_head(struct fetch_head *fetch_head) | |
1044 | { | |
1045 | const char *filename = git_path_fetch_head(the_repository); | |
1046 | ||
1047 | if (write_fetch_head) { | |
1048 | fetch_head->fp = fopen(filename, "a"); | |
1049 | if (!fetch_head->fp) | |
c4904377 | 1050 | return error_errno(_("cannot open '%s'"), filename); |
929d0445 | 1051 | strbuf_init(&fetch_head->buf, 0); |
58a646a3 PS |
1052 | } else { |
1053 | fetch_head->fp = NULL; | |
1054 | } | |
1055 | ||
1056 | return 0; | |
1057 | } | |
1058 | ||
1059 | static void append_fetch_head(struct fetch_head *fetch_head, | |
1060 | const struct object_id *old_oid, | |
1061 | enum fetch_head_status fetch_head_status, | |
1062 | const char *note, | |
1063 | const char *url, size_t url_len) | |
1064 | { | |
1065 | char old_oid_hex[GIT_MAX_HEXSZ + 1]; | |
1066 | const char *merge_status_marker; | |
1067 | size_t i; | |
1068 | ||
1069 | if (!fetch_head->fp) | |
1070 | return; | |
1071 | ||
1072 | switch (fetch_head_status) { | |
1073 | case FETCH_HEAD_NOT_FOR_MERGE: | |
1074 | merge_status_marker = "not-for-merge"; | |
1075 | break; | |
1076 | case FETCH_HEAD_MERGE: | |
1077 | merge_status_marker = ""; | |
1078 | break; | |
1079 | default: | |
1080 | /* do not write anything to FETCH_HEAD */ | |
1081 | return; | |
1082 | } | |
1083 | ||
929d0445 PS |
1084 | strbuf_addf(&fetch_head->buf, "%s\t%s\t%s", |
1085 | oid_to_hex_r(old_oid_hex, old_oid), merge_status_marker, note); | |
58a646a3 PS |
1086 | for (i = 0; i < url_len; ++i) |
1087 | if ('\n' == url[i]) | |
929d0445 | 1088 | strbuf_addstr(&fetch_head->buf, "\\n"); |
58a646a3 | 1089 | else |
929d0445 PS |
1090 | strbuf_addch(&fetch_head->buf, url[i]); |
1091 | strbuf_addch(&fetch_head->buf, '\n'); | |
1092 | ||
c7b190da PS |
1093 | /* |
1094 | * When using an atomic fetch, we do not want to update FETCH_HEAD if | |
1095 | * any of the reference updates fails. We thus have to write all | |
1096 | * updates to a buffer first and only commit it as soon as all | |
1097 | * references have been successfully updated. | |
1098 | */ | |
1099 | if (!atomic_fetch) { | |
1100 | strbuf_write(&fetch_head->buf, fetch_head->fp); | |
1101 | strbuf_reset(&fetch_head->buf); | |
1102 | } | |
58a646a3 PS |
1103 | } |
1104 | ||
1105 | static void commit_fetch_head(struct fetch_head *fetch_head) | |
1106 | { | |
c7b190da PS |
1107 | if (!fetch_head->fp || !atomic_fetch) |
1108 | return; | |
1109 | strbuf_write(&fetch_head->buf, fetch_head->fp); | |
58a646a3 PS |
1110 | } |
1111 | ||
1112 | static void close_fetch_head(struct fetch_head *fetch_head) | |
1113 | { | |
1114 | if (!fetch_head->fp) | |
1115 | return; | |
1116 | ||
1117 | fclose(fetch_head->fp); | |
929d0445 | 1118 | strbuf_release(&fetch_head->buf); |
58a646a3 PS |
1119 | } |
1120 | ||
182f59da | 1121 | static const char warn_show_forced_updates[] = |
66996bea AK |
1122 | N_("fetch normally indicates which branches had a forced update,\n" |
1123 | "but that check has been disabled; to re-enable, use '--show-forced-updates'\n" | |
1124 | "flag or run 'git config fetch.showForcedUpdates true'"); | |
182f59da | 1125 | static const char warn_time_show_forced_updates[] = |
66996bea | 1126 | N_("it took %.2f seconds to check forced updates; you can use\n" |
182f59da | 1127 | "'--no-show-forced-updates' or run 'git config fetch.showForcedUpdates false'\n" |
66996bea | 1128 | "to avoid this check\n"); |
182f59da | 1129 | |
ce9636d6 | 1130 | static int store_updated_refs(struct display_state *display_state, |
c4ef5edb | 1131 | const char *remote_name, |
b3a80466 PS |
1132 | int connectivity_checked, |
1133 | struct ref_transaction *transaction, struct ref *ref_map, | |
ba28b2ca PS |
1134 | struct fetch_head *fetch_head, |
1135 | const struct fetch_config *config) | |
b888d61c | 1136 | { |
c4ef5edb | 1137 | int rc = 0; |
4f40f6cb | 1138 | struct strbuf note = STRBUF_INIT; |
b888d61c DB |
1139 | const char *what, *kind; |
1140 | struct ref *rm; | |
900f2814 | 1141 | int want_status; |
b18aaaa5 | 1142 | int summary_width = 0; |
b888d61c | 1143 | |
b18aaaa5 PS |
1144 | if (verbosity >= 0) |
1145 | summary_width = transport_summary_width(ref_map); | |
1146 | ||
cf1e7c07 | 1147 | if (!connectivity_checked) { |
2df1aa23 JT |
1148 | struct check_connected_options opt = CHECK_CONNECTED_INIT; |
1149 | ||
c6ce27ab | 1150 | opt.exclude_hidden_refs_section = "fetch"; |
cf1e7c07 | 1151 | rm = ref_map; |
2df1aa23 | 1152 | if (check_connected(iterate_ref_map, &rm, &opt)) { |
c4ef5edb PS |
1153 | rc = error(_("%s did not send all necessary objects\n"), |
1154 | display_state->url); | |
cf1e7c07 JT |
1155 | goto abort; |
1156 | } | |
9516a598 | 1157 | } |
6b67e0dc | 1158 | |
96890f4c | 1159 | /* |
900f2814 JK |
1160 | * We do a pass for each fetch_head_status type in their enum order, so |
1161 | * merged entries are written before not-for-merge. That lets readers | |
1162 | * use FETCH_HEAD as a refname to refer to the ref to be merged. | |
96890f4c | 1163 | */ |
900f2814 JK |
1164 | for (want_status = FETCH_HEAD_MERGE; |
1165 | want_status <= FETCH_HEAD_IGNORE; | |
1166 | want_status++) { | |
96890f4c JH |
1167 | for (rm = ref_map; rm; rm = rm->next) { |
1168 | struct ref *ref = NULL; | |
1169 | ||
4820a33b NTND |
1170 | if (rm->status == REF_STATUS_REJECT_SHALLOW) { |
1171 | if (want_status == FETCH_HEAD_MERGE) | |
09667e95 | 1172 | warning(_("rejected %s because shallow roots are not allowed to be updated"), |
4820a33b NTND |
1173 | rm->peer_ref ? rm->peer_ref->name : rm->name); |
1174 | continue; | |
1175 | } | |
1176 | ||
fe7df03a | 1177 | /* |
8e55634b PS |
1178 | * When writing FETCH_HEAD we need to determine whether |
1179 | * we already have the commit or not. If not, then the | |
1180 | * reference is not for merge and needs to be written | |
1181 | * to the reflog after other commits which we already | |
1182 | * have. We're not interested in this property though | |
1183 | * in case FETCH_HEAD is not to be updated, so we can | |
1184 | * skip the classification in that case. | |
fe7df03a | 1185 | */ |
8e55634b PS |
1186 | if (fetch_head->fp) { |
1187 | struct commit *commit = NULL; | |
1188 | ||
1189 | /* | |
1190 | * References in "refs/tags/" are often going to point | |
1191 | * to annotated tags, which are not part of the | |
1192 | * commit-graph. We thus only try to look up refs in | |
1193 | * the graph which are not in that namespace to not | |
1194 | * regress performance in repositories with many | |
1195 | * annotated tags. | |
1196 | */ | |
1197 | if (!starts_with(rm->name, "refs/tags/")) | |
1198 | commit = lookup_commit_in_graph(the_repository, &rm->old_oid); | |
1199 | if (!commit) { | |
1200 | commit = lookup_commit_reference_gently(the_repository, | |
1201 | &rm->old_oid, | |
1202 | 1); | |
1203 | if (!commit) | |
1204 | rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE; | |
1205 | } | |
fe7df03a | 1206 | } |
96890f4c | 1207 | |
900f2814 | 1208 | if (rm->fetch_head_status != want_status) |
96890f4c JH |
1209 | continue; |
1210 | ||
1211 | if (rm->peer_ref) { | |
6f687c21 | 1212 | ref = alloc_ref(rm->peer_ref->name); |
f4e54d02 | 1213 | oidcpy(&ref->old_oid, &rm->peer_ref->old_oid); |
1214 | oidcpy(&ref->new_oid, &rm->old_oid); | |
96890f4c JH |
1215 | ref->force = rm->peer_ref->force; |
1216 | } | |
b888d61c | 1217 | |
56e8bb4f | 1218 | if (config->recurse_submodules != RECURSE_SUBMODULES_OFF && |
7ea0c2f4 | 1219 | (!rm->peer_ref || !oideq(&ref->old_oid, &ref->new_oid))) { |
be76c212 | 1220 | check_for_new_submodule_commits(&rm->old_oid); |
7ea0c2f4 | 1221 | } |
b888d61c | 1222 | |
96890f4c JH |
1223 | if (!strcmp(rm->name, "HEAD")) { |
1224 | kind = ""; | |
1225 | what = ""; | |
1c31764d | 1226 | } else if (skip_prefix(rm->name, "refs/heads/", &what)) { |
96890f4c | 1227 | kind = "branch"; |
1c31764d | 1228 | } else if (skip_prefix(rm->name, "refs/tags/", &what)) { |
96890f4c | 1229 | kind = "tag"; |
1c31764d | 1230 | } else if (skip_prefix(rm->name, "refs/remotes/", &what)) { |
96890f4c | 1231 | kind = "remote-tracking branch"; |
1c31764d | 1232 | } else { |
96890f4c JH |
1233 | kind = ""; |
1234 | what = rm->name; | |
1235 | } | |
b888d61c | 1236 | |
96890f4c JH |
1237 | strbuf_reset(¬e); |
1238 | if (*what) { | |
1239 | if (*kind) | |
1240 | strbuf_addf(¬e, "%s ", kind); | |
1241 | strbuf_addf(¬e, "'%s' of ", what); | |
1242 | } | |
58a646a3 | 1243 | |
2983cec0 | 1244 | append_fetch_head(fetch_head, &rm->old_oid, |
58a646a3 | 1245 | rm->fetch_head_status, |
c4ef5edb PS |
1246 | note.buf, display_state->url, |
1247 | display_state->url_len); | |
96890f4c | 1248 | |
96890f4c | 1249 | if (ref) { |
1c31764d | 1250 | rc |= update_local_ref(ref, transaction, display_state, |
ba28b2ca | 1251 | rm, summary_width, config); |
96890f4c | 1252 | free(ref); |
db3c293e JT |
1253 | } else if (write_fetch_head || dry_run) { |
1254 | /* | |
1255 | * Display fetches written to FETCH_HEAD (or | |
1256 | * would be written to FETCH_HEAD, if --dry-run | |
1257 | * is set). | |
1258 | */ | |
d6606e02 PS |
1259 | display_ref_update(display_state, '*', |
1260 | *kind ? kind : "branch", NULL, | |
1c31764d | 1261 | rm->name, |
dd781e38 PS |
1262 | "FETCH_HEAD", |
1263 | &rm->new_oid, &rm->old_oid, | |
1264 | summary_width); | |
165f3902 NP |
1265 | } |
1266 | } | |
b888d61c | 1267 | } |
9516a598 | 1268 | |
fa250759 | 1269 | if (rc & STORE_REF_ERROR_DF_CONFLICT) |
bd4a51fc | 1270 | error(_("some local refs could not be updated; try running\n" |
f3cb169b | 1271 | " 'git remote prune %s' to remove any old, conflicting " |
bd4a51fc | 1272 | "branches"), remote_name); |
9516a598 | 1273 | |
ed9bff08 | 1274 | if (advice_enabled(ADVICE_FETCH_SHOW_FORCED_UPDATES)) { |
ba28b2ca | 1275 | if (!config->show_forced_updates) { |
182f59da | 1276 | warning(_(warn_show_forced_updates)); |
377444b4 | 1277 | } else if (forced_updates_ms > FORCED_UPDATES_DELAY_WARNING_IN_MS) { |
182f59da | 1278 | warning(_(warn_time_show_forced_updates), |
377444b4 | 1279 | forced_updates_ms / 1000.0); |
377444b4 DS |
1280 | } |
1281 | } | |
1282 | ||
9516a598 | 1283 | abort: |
5914f2d0 | 1284 | strbuf_release(¬e); |
efb98b44 | 1285 | return rc; |
b888d61c DB |
1286 | } |
1287 | ||
4191c356 SP |
1288 | /* |
1289 | * We would want to bypass the object transfer altogether if | |
d9eb0205 | 1290 | * everything we are going to fetch already exists and is connected |
4191c356 | 1291 | * locally. |
4191c356 | 1292 | */ |
35f9e3e5 | 1293 | static int check_exist_and_connected(struct ref *ref_map) |
4191c356 | 1294 | { |
f0e278b1 | 1295 | struct ref *rm = ref_map; |
7043c707 | 1296 | struct check_connected_options opt = CHECK_CONNECTED_INIT; |
35f9e3e5 | 1297 | struct ref *r; |
f0e278b1 | 1298 | |
4191c356 SP |
1299 | /* |
1300 | * If we are deepening a shallow clone we already have these | |
1301 | * objects reachable. Running rev-list here will return with | |
1302 | * a good (0) exit status and we'll bypass the fetch that we | |
1303 | * really need to perform. Claiming failure now will ensure | |
1304 | * we perform the network exchange to deepen our history. | |
1305 | */ | |
508ea882 | 1306 | if (deepen) |
4191c356 | 1307 | return -1; |
35f9e3e5 | 1308 | |
3c7bab06 RC |
1309 | /* |
1310 | * Similarly, if we need to refetch, we always want to perform a full | |
1311 | * fetch ignoring existing objects. | |
1312 | */ | |
1313 | if (refetch) | |
1314 | return -1; | |
1315 | ||
1316 | ||
35f9e3e5 JT |
1317 | /* |
1318 | * check_connected() allows objects to merely be promised, but | |
1319 | * we need all direct targets to exist. | |
1320 | */ | |
1321 | for (r = rm; r; r = r->next) { | |
bc726bd0 ÆAB |
1322 | if (!repo_has_object_file_with_flags(the_repository, &r->old_oid, |
1323 | OBJECT_INFO_SKIP_FETCH_OBJECT)) | |
35f9e3e5 JT |
1324 | return -1; |
1325 | } | |
1326 | ||
7043c707 | 1327 | opt.quiet = 1; |
c6ce27ab | 1328 | opt.exclude_hidden_refs_section = "fetch"; |
7043c707 | 1329 | return check_connected(iterate_ref_map, &rm, &opt); |
4191c356 SP |
1330 | } |
1331 | ||
ce9636d6 PS |
1332 | static int fetch_and_consume_refs(struct display_state *display_state, |
1333 | struct transport *transport, | |
b3a80466 | 1334 | struct ref_transaction *transaction, |
8bc1f39f | 1335 | struct ref *ref_map, |
ba28b2ca PS |
1336 | struct fetch_head *fetch_head, |
1337 | const struct fetch_config *config) | |
b888d61c | 1338 | { |
caff8b73 | 1339 | int connectivity_checked = 1; |
284b2ce8 PS |
1340 | int ret; |
1341 | ||
1342 | /* | |
1343 | * We don't need to perform a fetch in case we can already satisfy all | |
1344 | * refs. | |
1345 | */ | |
1346 | ret = check_exist_and_connected(ref_map); | |
5fc31180 JS |
1347 | if (ret) { |
1348 | trace2_region_enter("fetch", "fetch_refs", the_repository); | |
e2842b39 | 1349 | ret = transport_fetch_refs(transport, ref_map); |
5fc31180 | 1350 | trace2_region_leave("fetch", "fetch_refs", the_repository); |
284b2ce8 PS |
1351 | if (ret) |
1352 | goto out; | |
caff8b73 PS |
1353 | connectivity_checked = transport->smart_options ? |
1354 | transport->smart_options->connectivity_checked : 0; | |
5fc31180 | 1355 | } |
05c44226 | 1356 | |
5fc31180 | 1357 | trace2_region_enter("fetch", "consume_refs", the_repository); |
c4ef5edb | 1358 | ret = store_updated_refs(display_state, transport->remote->name, |
b3a80466 | 1359 | connectivity_checked, transaction, ref_map, |
ba28b2ca | 1360 | fetch_head, config); |
5fc31180 | 1361 | trace2_region_leave("fetch", "consume_refs", the_repository); |
1c7d1ab6 PS |
1362 | |
1363 | out: | |
58d4d7f1 | 1364 | transport_unlock_pack(transport, 0); |
b888d61c DB |
1365 | return ret; |
1366 | } | |
1367 | ||
ce9636d6 PS |
1368 | static int prune_refs(struct display_state *display_state, |
1369 | struct refspec *rs, | |
583bc419 | 1370 | struct ref_transaction *transaction, |
c4ef5edb | 1371 | struct ref *ref_map) |
f360d844 | 1372 | { |
c4ef5edb | 1373 | int result = 0; |
a2ac50cb | 1374 | struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map); |
583bc419 | 1375 | struct strbuf err = STRBUF_INIT; |
f360d844 | 1376 | const char *dangling_msg = dry_run |
18986d53 NTND |
1377 | ? _(" (%s will become dangling)") |
1378 | : _(" (%s has become dangling)"); | |
f360d844 | 1379 | |
a087b432 | 1380 | if (!dry_run) { |
583bc419 PS |
1381 | if (transaction) { |
1382 | for (ref = stale_refs; ref; ref = ref->next) { | |
1383 | result = ref_transaction_delete(transaction, ref->name, NULL, 0, | |
1384 | "fetch: prune", &err); | |
1385 | if (result) | |
1386 | goto cleanup; | |
1387 | } | |
1388 | } else { | |
1389 | struct string_list refnames = STRING_LIST_INIT_NODUP; | |
a087b432 | 1390 | |
583bc419 PS |
1391 | for (ref = stale_refs; ref; ref = ref->next) |
1392 | string_list_append(&refnames, ref->name); | |
a087b432 | 1393 | |
583bc419 PS |
1394 | result = delete_refs("fetch: prune", &refnames, 0); |
1395 | string_list_clear(&refnames, 0); | |
1396 | } | |
a087b432 MH |
1397 | } |
1398 | ||
1399 | if (verbosity >= 0) { | |
b18aaaa5 PS |
1400 | int summary_width = transport_summary_width(stale_refs); |
1401 | ||
a087b432 | 1402 | for (ref = stale_refs; ref; ref = ref->next) { |
d6606e02 PS |
1403 | display_ref_update(display_state, '-', _("[deleted]"), NULL, |
1404 | _("(none)"), ref->name, | |
dd781e38 | 1405 | &ref->new_oid, &ref->old_oid, |
d6606e02 | 1406 | summary_width); |
f360d844 JS |
1407 | warn_dangling_symref(stderr, dangling_msg, ref->name); |
1408 | } | |
1409 | } | |
a087b432 | 1410 | |
583bc419 PS |
1411 | cleanup: |
1412 | strbuf_release(&err); | |
f360d844 JS |
1413 | free_refs(stale_refs); |
1414 | return result; | |
1415 | } | |
1416 | ||
12d47e3b | 1417 | static void check_not_current_branch(struct ref *ref_map) |
8ee5d731 | 1418 | { |
12d47e3b | 1419 | const char *path; |
8ee5d731 | 1420 | for (; ref_map; ref_map = ref_map->next) |
8bc1f39f | 1421 | if (ref_map->peer_ref && |
f7400da8 | 1422 | starts_with(ref_map->peer_ref->name, "refs/heads/") && |
12d47e3b | 1423 | (path = branch_checked_out(ref_map->peer_ref->name))) |
8bc1f39f AK |
1424 | die(_("refusing to fetch into branch '%s' " |
1425 | "checked out at '%s'"), | |
12d47e3b | 1426 | ref_map->peer_ref->name, path); |
8ee5d731 JS |
1427 | } |
1428 | ||
e6cc5104 JH |
1429 | static int truncate_fetch_head(void) |
1430 | { | |
102de880 | 1431 | const char *filename = git_path_fetch_head(the_repository); |
ea56518d | 1432 | FILE *fp = fopen_for_writing(filename); |
e6cc5104 JH |
1433 | |
1434 | if (!fp) | |
c4904377 | 1435 | return error_errno(_("cannot open '%s'"), filename); |
e6cc5104 JH |
1436 | fclose(fp); |
1437 | return 0; | |
1438 | } | |
1439 | ||
db5723c6 JH |
1440 | static void set_option(struct transport *transport, const char *name, const char *value) |
1441 | { | |
1442 | int r = transport_set_option(transport, name, value); | |
1443 | if (r < 0) | |
66996bea | 1444 | die(_("option \"%s\" value \"%s\" is not valid for %s"), |
db5723c6 JH |
1445 | name, value, transport->url); |
1446 | if (r > 0) | |
66996bea | 1447 | warning(_("option \"%s\" is ignored for %s\n"), |
db5723c6 JH |
1448 | name, transport->url); |
1449 | } | |
1450 | ||
3390e42a | 1451 | |
5cf88fd8 | 1452 | static int add_oid(const char *refname UNUSED, |
63e14ee2 | 1453 | const struct object_id *oid, |
5cf88fd8 | 1454 | int flags UNUSED, void *cb_data) |
3390e42a JT |
1455 | { |
1456 | struct oid_array *oids = cb_data; | |
1457 | ||
1458 | oid_array_append(oids, oid); | |
1459 | return 0; | |
1460 | } | |
1461 | ||
1462 | static void add_negotiation_tips(struct git_transport_options *smart_options) | |
1463 | { | |
1464 | struct oid_array *oids = xcalloc(1, sizeof(*oids)); | |
1465 | int i; | |
1466 | ||
1467 | for (i = 0; i < negotiation_tip.nr; i++) { | |
1468 | const char *s = negotiation_tip.items[i].string; | |
1469 | int old_nr; | |
1470 | if (!has_glob_specials(s)) { | |
1471 | struct object_id oid; | |
d850b7a5 | 1472 | if (repo_get_oid(the_repository, s, &oid)) |
82823118 JT |
1473 | die(_("%s is not a valid object"), s); |
1474 | if (!has_object(the_repository, &oid, 0)) | |
1475 | die(_("the object %s does not exist"), s); | |
3390e42a JT |
1476 | oid_array_append(oids, &oid); |
1477 | continue; | |
1478 | } | |
1479 | old_nr = oids->nr; | |
1480 | for_each_glob_ref(add_oid, s, oids); | |
1481 | if (old_nr == oids->nr) | |
66996bea | 1482 | warning("ignoring --negotiation-tip=%s because it does not match any refs", |
3390e42a JT |
1483 | s); |
1484 | } | |
1485 | smart_options->negotiation_tips = oids; | |
1486 | } | |
1487 | ||
508ea882 | 1488 | static struct transport *prepare_transport(struct remote *remote, int deepen) |
db5723c6 JH |
1489 | { |
1490 | struct transport *transport; | |
87c2d9d3 | 1491 | |
db5723c6 JH |
1492 | transport = transport_get(remote, NULL); |
1493 | transport_set_verbosity(transport, verbosity, progress); | |
c915f11e | 1494 | transport->family = family; |
db5723c6 JH |
1495 | if (upload_pack) |
1496 | set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack); | |
1497 | if (keep) | |
1498 | set_option(transport, TRANS_OPT_KEEP, "yes"); | |
1499 | if (depth) | |
1500 | set_option(transport, TRANS_OPT_DEPTH, depth); | |
508ea882 NTND |
1501 | if (deepen && deepen_since) |
1502 | set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since); | |
a45a2600 NTND |
1503 | if (deepen && deepen_not.nr) |
1504 | set_option(transport, TRANS_OPT_DEEPEN_NOT, | |
1505 | (const char *)&deepen_not); | |
cccf74e2 NTND |
1506 | if (deepen_relative) |
1507 | set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes"); | |
48d25cae NTND |
1508 | if (update_shallow) |
1509 | set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes"); | |
3c7bab06 RC |
1510 | if (refetch) |
1511 | set_option(transport, TRANS_OPT_REFETCH, "yes"); | |
acb0c572 | 1512 | if (filter_options.choice) { |
cf9ceb5a MD |
1513 | const char *spec = |
1514 | expand_list_objects_filter_spec(&filter_options); | |
1515 | set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER, spec); | |
acb0c572 JH |
1516 | set_option(transport, TRANS_OPT_FROM_PROMISOR, "1"); |
1517 | } | |
3390e42a JT |
1518 | if (negotiation_tip.nr) { |
1519 | if (transport->smart_options) | |
1520 | add_negotiation_tips(transport->smart_options); | |
1521 | else | |
66996bea | 1522 | warning("ignoring --negotiation-tip because the protocol does not support it"); |
3390e42a | 1523 | } |
db5723c6 JH |
1524 | return transport; |
1525 | } | |
1526 | ||
ce9636d6 PS |
1527 | static int backfill_tags(struct display_state *display_state, |
1528 | struct transport *transport, | |
b3a80466 | 1529 | struct ref_transaction *transaction, |
62091b4c | 1530 | struct ref *ref_map, |
ba28b2ca PS |
1531 | struct fetch_head *fetch_head, |
1532 | const struct fetch_config *config) | |
069d5032 | 1533 | { |
62091b4c | 1534 | int retcode, cannot_reuse; |
508ea882 NTND |
1535 | |
1536 | /* | |
1537 | * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it | |
1538 | * when remote helper is used (setting it to an empty string | |
1539 | * is not unsetting). We could extend the remote helper | |
1540 | * protocol for that, but for now, just force a new connection | |
a45a2600 | 1541 | * without deepen-since. Similar story for deepen-not. |
508ea882 | 1542 | */ |
a45a2600 NTND |
1543 | cannot_reuse = transport->cannot_reuse || |
1544 | deepen_since || deepen_not.nr; | |
508ea882 NTND |
1545 | if (cannot_reuse) { |
1546 | gsecondary = prepare_transport(transport->remote, 0); | |
b26ed430 JH |
1547 | transport = gsecondary; |
1548 | } | |
1549 | ||
069d5032 JH |
1550 | transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL); |
1551 | transport_set_option(transport, TRANS_OPT_DEPTH, "0"); | |
cccf74e2 | 1552 | transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL); |
ba28b2ca PS |
1553 | retcode = fetch_and_consume_refs(display_state, transport, transaction, ref_map, |
1554 | fetch_head, config); | |
b26ed430 JH |
1555 | |
1556 | if (gsecondary) { | |
1557 | transport_disconnect(gsecondary); | |
1558 | gsecondary = NULL; | |
1559 | } | |
62091b4c PS |
1560 | |
1561 | return retcode; | |
069d5032 JH |
1562 | } |
1563 | ||
b888d61c | 1564 | static int do_fetch(struct transport *transport, |
58afbe88 | 1565 | struct refspec *rs, |
d1adf85b | 1566 | const struct fetch_config *config) |
b888d61c | 1567 | { |
b3a80466 | 1568 | struct ref_transaction *transaction = NULL; |
efbade06 | 1569 | struct ref *ref_map = NULL; |
c4ef5edb | 1570 | struct display_state display_state = { 0 }; |
b888d61c | 1571 | int autotags = (transport->remote->fetch_tags == 1); |
5b87d8d3 | 1572 | int retcode = 0; |
6d1700d5 | 1573 | const struct ref *remote_refs; |
39835409 JT |
1574 | struct transport_ls_refs_options transport_ls_refs_options = |
1575 | TRANSPORT_LS_REFS_OPTIONS_INIT; | |
e70a3030 | 1576 | int must_list_refs = 1; |
2983cec0 | 1577 | struct fetch_head fetch_head = { 0 }; |
b3a80466 | 1578 | struct strbuf err = STRBUF_INIT; |
b1a01e1c | 1579 | |
ed368546 DJ |
1580 | if (tags == TAGS_DEFAULT) { |
1581 | if (transport->remote->fetch_tags == 2) | |
1582 | tags = TAGS_SET; | |
1583 | if (transport->remote->fetch_tags == -1) | |
1584 | tags = TAGS_UNSET; | |
1585 | } | |
b888d61c | 1586 | |
b888d61c | 1587 | /* if not appending, truncate FETCH_HEAD */ |
887952b8 | 1588 | if (!append && write_fetch_head) { |
5b87d8d3 MH |
1589 | retcode = truncate_fetch_head(); |
1590 | if (retcode) | |
1591 | goto cleanup; | |
d6617c7c | 1592 | } |
b888d61c | 1593 | |
e70a3030 JT |
1594 | if (rs->nr) { |
1595 | int i; | |
1596 | ||
39835409 | 1597 | refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes); |
e70a3030 JT |
1598 | |
1599 | /* | |
1600 | * We can avoid listing refs if all of them are exact | |
1601 | * OIDs | |
1602 | */ | |
1603 | must_list_refs = 0; | |
1604 | for (i = 0; i < rs->nr; i++) { | |
1605 | if (!rs->items[i].exact_sha1) { | |
1606 | must_list_refs = 1; | |
1607 | break; | |
1608 | } | |
1609 | } | |
49ca2fba JK |
1610 | } else { |
1611 | struct branch *branch = branch_get(NULL); | |
1612 | ||
1613 | if (transport->remote->fetch.nr) | |
1614 | refspec_ref_prefixes(&transport->remote->fetch, | |
1615 | &transport_ls_refs_options.ref_prefixes); | |
1616 | if (branch_has_merge_config(branch) && | |
1617 | !strcmp(branch->remote_name, transport->remote->name)) { | |
1618 | int i; | |
1619 | for (i = 0; i < branch->merge_nr; i++) { | |
1620 | strvec_push(&transport_ls_refs_options.ref_prefixes, | |
1621 | branch->merge[i]->src); | |
1622 | } | |
1623 | } | |
1624 | } | |
b888d61c | 1625 | |
e70a3030 JT |
1626 | if (tags == TAGS_SET || tags == TAGS_DEFAULT) { |
1627 | must_list_refs = 1; | |
39835409 JT |
1628 | if (transport_ls_refs_options.ref_prefixes.nr) |
1629 | strvec_push(&transport_ls_refs_options.ref_prefixes, | |
1630 | "refs/tags/"); | |
b888d61c DB |
1631 | } |
1632 | ||
5fc31180 JS |
1633 | if (must_list_refs) { |
1634 | trace2_region_enter("fetch", "remote_refs", the_repository); | |
39835409 JT |
1635 | remote_refs = transport_get_remote_refs(transport, |
1636 | &transport_ls_refs_options); | |
5fc31180 JS |
1637 | trace2_region_leave("fetch", "remote_refs", the_repository); |
1638 | } else | |
e70a3030 JT |
1639 | remote_refs = NULL; |
1640 | ||
f36d4f83 | 1641 | transport_ls_refs_options_release(&transport_ls_refs_options); |
6d1700d5 BW |
1642 | |
1643 | ref_map = get_ref_map(transport->remote, remote_refs, rs, | |
1644 | tags, &autotags); | |
8ee5d731 | 1645 | if (!update_head_ok) |
12d47e3b | 1646 | check_not_current_branch(ref_map); |
b888d61c | 1647 | |
2983cec0 PS |
1648 | retcode = open_fetch_head(&fetch_head); |
1649 | if (retcode) | |
1650 | goto cleanup; | |
1651 | ||
d1adf85b PS |
1652 | display_state_init(&display_state, ref_map, transport->url, |
1653 | config->display_format); | |
ce9636d6 | 1654 | |
b3a80466 PS |
1655 | if (atomic_fetch) { |
1656 | transaction = ref_transaction_begin(&err); | |
1657 | if (!transaction) { | |
18ce4891 | 1658 | retcode = -1; |
b3a80466 PS |
1659 | goto cleanup; |
1660 | } | |
1661 | } | |
1662 | ||
41fa7d2e SP |
1663 | if (tags == TAGS_DEFAULT && autotags) |
1664 | transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1"); | |
ed43de6e | 1665 | if (prune) { |
0838bf47 MH |
1666 | /* |
1667 | * We only prune based on refspecs specified | |
1668 | * explicitly (via command line or configuration); we | |
1669 | * don't care whether --tags was specified. | |
1670 | */ | |
65a1301f | 1671 | if (rs->nr) { |
c4ef5edb | 1672 | retcode = prune_refs(&display_state, rs, transaction, ref_map); |
e8c1e6c7 | 1673 | } else { |
ce9636d6 | 1674 | retcode = prune_refs(&display_state, &transport->remote->fetch, |
c4ef5edb | 1675 | transaction, ref_map); |
e8c1e6c7 | 1676 | } |
c9e04d90 TG |
1677 | if (retcode != 0) |
1678 | retcode = 1; | |
ed43de6e | 1679 | } |
2983cec0 | 1680 | |
ba28b2ca PS |
1681 | if (fetch_and_consume_refs(&display_state, transport, transaction, ref_map, |
1682 | &fetch_head, config)) { | |
10a6cc88 TM |
1683 | retcode = 1; |
1684 | goto cleanup; | |
1685 | } | |
24bc1a12 | 1686 | |
efbade06 PS |
1687 | /* |
1688 | * If neither --no-tags nor --tags was specified, do automated tag | |
1689 | * following. | |
1690 | */ | |
1691 | if (tags == TAGS_DEFAULT && autotags) { | |
1692 | struct ref *tags_ref_map = NULL, **tail = &tags_ref_map; | |
1693 | ||
b3a80466 | 1694 | find_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail); |
62091b4c PS |
1695 | if (tags_ref_map) { |
1696 | /* | |
1697 | * If backfilling of tags fails then we want to tell | |
1698 | * the user so, but we have to continue regardless to | |
1699 | * populate upstream information of the references we | |
b3a80466 PS |
1700 | * have already fetched above. The exception though is |
1701 | * when `--atomic` is passed: in that case we'll abort | |
1702 | * the transaction and don't commit anything. | |
62091b4c | 1703 | */ |
ce9636d6 | 1704 | if (backfill_tags(&display_state, transport, transaction, tags_ref_map, |
ba28b2ca | 1705 | &fetch_head, config)) |
62091b4c PS |
1706 | retcode = 1; |
1707 | } | |
efbade06 PS |
1708 | |
1709 | free_refs(tags_ref_map); | |
1710 | } | |
1711 | ||
b3a80466 PS |
1712 | if (transaction) { |
1713 | if (retcode) | |
1714 | goto cleanup; | |
1715 | ||
1716 | retcode = ref_transaction_commit(transaction, &err); | |
1717 | if (retcode) { | |
b3a80466 PS |
1718 | ref_transaction_free(transaction); |
1719 | transaction = NULL; | |
1720 | goto cleanup; | |
1721 | } | |
1722 | } | |
1723 | ||
2983cec0 PS |
1724 | commit_fetch_head(&fetch_head); |
1725 | ||
24bc1a12 CB |
1726 | if (set_upstream) { |
1727 | struct branch *branch = branch_get("HEAD"); | |
1728 | struct ref *rm; | |
1729 | struct ref *source_ref = NULL; | |
1730 | ||
1731 | /* | |
1732 | * We're setting the upstream configuration for the | |
15beaaa3 | 1733 | * current branch. The relevant upstream is the |
24bc1a12 CB |
1734 | * fetched branch that is meant to be merged with the |
1735 | * current one, i.e. the one fetched to FETCH_HEAD. | |
1736 | * | |
1737 | * When there are several such branches, consider the | |
1738 | * request ambiguous and err on the safe side by doing | |
1739 | * nothing and just emit a warning. | |
1740 | */ | |
1741 | for (rm = ref_map; rm; rm = rm->next) { | |
1742 | if (!rm->peer_ref) { | |
1743 | if (source_ref) { | |
391c7e40 | 1744 | warning(_("multiple branches detected, incompatible with --set-upstream")); |
efbade06 | 1745 | goto cleanup; |
24bc1a12 CB |
1746 | } else { |
1747 | source_ref = rm; | |
1748 | } | |
1749 | } | |
1750 | } | |
1751 | if (source_ref) { | |
17baeaf8 ÆAB |
1752 | if (!branch) { |
1753 | const char *shortname = source_ref->name; | |
1754 | skip_prefix(shortname, "refs/heads/", &shortname); | |
1755 | ||
1756 | warning(_("could not set upstream of HEAD to '%s' from '%s' when " | |
1757 | "it does not point to any branch."), | |
1758 | shortname, transport->remote->name); | |
efbade06 | 1759 | goto cleanup; |
17baeaf8 ÆAB |
1760 | } |
1761 | ||
24bc1a12 CB |
1762 | if (!strcmp(source_ref->name, "HEAD") || |
1763 | starts_with(source_ref->name, "refs/heads/")) | |
1764 | install_branch_config(0, | |
1765 | branch->name, | |
1766 | transport->remote->name, | |
1767 | source_ref->name); | |
1768 | else if (starts_with(source_ref->name, "refs/remotes/")) | |
1769 | warning(_("not setting upstream for a remote remote-tracking branch")); | |
1770 | else if (starts_with(source_ref->name, "refs/tags/")) | |
1771 | warning(_("not setting upstream for a remote tag")); | |
1772 | else | |
1773 | warning(_("unknown branch type")); | |
1774 | } else { | |
66996bea AK |
1775 | warning(_("no source branch found;\n" |
1776 | "you need to specify exactly one branch with the --set-upstream option")); | |
24bc1a12 CB |
1777 | } |
1778 | } | |
b888d61c | 1779 | |
8bc1f39f | 1780 | cleanup: |
18ce4891 JX |
1781 | if (retcode) { |
1782 | if (err.len) { | |
1783 | error("%s", err.buf); | |
1784 | strbuf_reset(&err); | |
1785 | } | |
1786 | if (transaction && ref_transaction_abort(transaction, &err) && | |
1787 | err.len) | |
1788 | error("%s", err.buf); | |
b888d61c DB |
1789 | } |
1790 | ||
c4ef5edb | 1791 | display_state_release(&display_state); |
2983cec0 | 1792 | close_fetch_head(&fetch_head); |
b3a80466 | 1793 | strbuf_release(&err); |
efbade06 | 1794 | free_refs(ref_map); |
5b87d8d3 | 1795 | return retcode; |
b888d61c DB |
1796 | } |
1797 | ||
9c4a036b BG |
1798 | static int get_one_remote_for_fetch(struct remote *remote, void *priv) |
1799 | { | |
1800 | struct string_list *list = priv; | |
7cc91a2f | 1801 | if (!remote->skip_default_update) |
1d2f80fa | 1802 | string_list_append(list, remote->name); |
9c4a036b BG |
1803 | return 0; |
1804 | } | |
1805 | ||
1806 | struct remote_group_data { | |
1807 | const char *name; | |
1808 | struct string_list *list; | |
1809 | }; | |
1810 | ||
a4e7e317 GC |
1811 | static int get_remote_group(const char *key, const char *value, |
1812 | const struct config_context *ctx UNUSED, | |
1813 | void *priv) | |
9c4a036b BG |
1814 | { |
1815 | struct remote_group_data *g = priv; | |
1816 | ||
bc598c32 | 1817 | if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) { |
9c4a036b | 1818 | /* split list by white space */ |
9c4a036b | 1819 | while (*value) { |
5f65499f MH |
1820 | size_t wordlen = strcspn(value, " \t\n"); |
1821 | ||
e286542d | 1822 | if (wordlen >= 1) |
b7410f61 | 1823 | string_list_append_nodup(g->list, |
e286542d MH |
1824 | xstrndup(value, wordlen)); |
1825 | value += wordlen + (value[wordlen] != '\0'); | |
9c4a036b BG |
1826 | } |
1827 | } | |
1828 | ||
1829 | return 0; | |
1830 | } | |
1831 | ||
1832 | static int add_remote_or_group(const char *name, struct string_list *list) | |
1833 | { | |
1834 | int prev_nr = list->nr; | |
66dbfd55 GV |
1835 | struct remote_group_data g; |
1836 | g.name = name; g.list = list; | |
9c4a036b BG |
1837 | |
1838 | git_config(get_remote_group, &g); | |
1839 | if (list->nr == prev_nr) { | |
674468b3 | 1840 | struct remote *remote = remote_get(name); |
e459b073 | 1841 | if (!remote_is_configured(remote, 0)) |
9c4a036b | 1842 | return 0; |
1d2f80fa | 1843 | string_list_append(list, remote->name); |
9c4a036b BG |
1844 | } |
1845 | return 1; | |
1846 | } | |
1847 | ||
dd781e38 | 1848 | static void add_options_to_argv(struct strvec *argv, |
d1adf85b | 1849 | const struct fetch_config *config) |
9c4a036b | 1850 | { |
28a15401 | 1851 | if (dry_run) |
22f9b7f3 | 1852 | strvec_push(argv, "--dry-run"); |
90765fa3 | 1853 | if (prune != -1) |
22f9b7f3 | 1854 | strvec_push(argv, prune ? "--prune" : "--no-prune"); |
97716d21 | 1855 | if (prune_tags != -1) |
22f9b7f3 | 1856 | strvec_push(argv, prune_tags ? "--prune-tags" : "--no-prune-tags"); |
bba5322a | 1857 | if (update_head_ok) |
22f9b7f3 | 1858 | strvec_push(argv, "--update-head-ok"); |
bba5322a | 1859 | if (force) |
22f9b7f3 | 1860 | strvec_push(argv, "--force"); |
bba5322a | 1861 | if (keep) |
22f9b7f3 | 1862 | strvec_push(argv, "--keep"); |
56e8bb4f | 1863 | if (config->recurse_submodules == RECURSE_SUBMODULES_ON) |
22f9b7f3 | 1864 | strvec_push(argv, "--recurse-submodules"); |
56e8bb4f | 1865 | else if (config->recurse_submodules == RECURSE_SUBMODULES_OFF) |
5667141e | 1866 | strvec_push(argv, "--no-recurse-submodules"); |
56e8bb4f | 1867 | else if (config->recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) |
22f9b7f3 | 1868 | strvec_push(argv, "--recurse-submodules=on-demand"); |
85566460 | 1869 | if (tags == TAGS_SET) |
22f9b7f3 | 1870 | strvec_push(argv, "--tags"); |
85566460 | 1871 | else if (tags == TAGS_UNSET) |
22f9b7f3 | 1872 | strvec_push(argv, "--no-tags"); |
9c4a036b | 1873 | if (verbosity >= 2) |
22f9b7f3 | 1874 | strvec_push(argv, "-v"); |
9c4a036b | 1875 | if (verbosity >= 1) |
22f9b7f3 | 1876 | strvec_push(argv, "-v"); |
9c4a036b | 1877 | else if (verbosity < 0) |
22f9b7f3 | 1878 | strvec_push(argv, "-q"); |
4e735c13 | 1879 | if (family == TRANSPORT_FAMILY_IPV4) |
6854689e | 1880 | strvec_push(argv, "--ipv4"); |
4e735c13 | 1881 | else if (family == TRANSPORT_FAMILY_IPV6) |
6854689e | 1882 | strvec_push(argv, "--ipv6"); |
15184ae9 EW |
1883 | if (!write_fetch_head) |
1884 | strvec_push(argv, "--no-write-fetch-head"); | |
d1adf85b | 1885 | if (config->display_format == DISPLAY_FORMAT_PORCELAIN) |
dd781e38 | 1886 | strvec_pushf(argv, "--porcelain"); |
7dce19d3 JL |
1887 | } |
1888 | ||
d54dea77 JS |
1889 | /* Fetch multiple remotes in parallel */ |
1890 | ||
1891 | struct parallel_fetch_state { | |
1892 | const char **argv; | |
1893 | struct string_list *remotes; | |
1894 | int next, result; | |
d1adf85b | 1895 | const struct fetch_config *config; |
d54dea77 JS |
1896 | }; |
1897 | ||
a5c76b36 JK |
1898 | static int fetch_next_remote(struct child_process *cp, |
1899 | struct strbuf *out UNUSED, | |
d54dea77 JS |
1900 | void *cb, void **task_cb) |
1901 | { | |
1902 | struct parallel_fetch_state *state = cb; | |
1903 | char *remote; | |
1904 | ||
1905 | if (state->next < 0 || state->next >= state->remotes->nr) | |
1906 | return 0; | |
1907 | ||
1908 | remote = state->remotes->items[state->next++].string; | |
1909 | *task_cb = remote; | |
1910 | ||
22f9b7f3 JK |
1911 | strvec_pushv(&cp->args, state->argv); |
1912 | strvec_push(&cp->args, remote); | |
d54dea77 JS |
1913 | cp->git_cmd = 1; |
1914 | ||
d1adf85b | 1915 | if (verbosity >= 0 && state->config->display_format != DISPLAY_FORMAT_PORCELAIN) |
d54dea77 JS |
1916 | printf(_("Fetching %s\n"), remote); |
1917 | ||
1918 | return 1; | |
1919 | } | |
1920 | ||
a5c76b36 JK |
1921 | static int fetch_failed_to_start(struct strbuf *out UNUSED, |
1922 | void *cb, void *task_cb) | |
d54dea77 JS |
1923 | { |
1924 | struct parallel_fetch_state *state = cb; | |
1925 | const char *remote = task_cb; | |
1926 | ||
66996bea | 1927 | state->result = error(_("could not fetch %s"), remote); |
d54dea77 JS |
1928 | |
1929 | return 0; | |
1930 | } | |
1931 | ||
1932 | static int fetch_finished(int result, struct strbuf *out, | |
1933 | void *cb, void *task_cb) | |
1934 | { | |
1935 | struct parallel_fetch_state *state = cb; | |
1936 | const char *remote = task_cb; | |
1937 | ||
1938 | if (result) { | |
1939 | strbuf_addf(out, _("could not fetch '%s' (exit code: %d)\n"), | |
1940 | remote, result); | |
1941 | state->result = -1; | |
1942 | } | |
1943 | ||
1944 | return 0; | |
1945 | } | |
1946 | ||
dd781e38 | 1947 | static int fetch_multiple(struct string_list *list, int max_children, |
d1adf85b | 1948 | const struct fetch_config *config) |
7dce19d3 JL |
1949 | { |
1950 | int i, result = 0; | |
22f9b7f3 | 1951 | struct strvec argv = STRVEC_INIT; |
9c4a036b | 1952 | |
887952b8 | 1953 | if (!append && write_fetch_head) { |
e6cc5104 JH |
1954 | int errcode = truncate_fetch_head(); |
1955 | if (errcode) | |
1956 | return errcode; | |
1957 | } | |
1958 | ||
25bccb4b DS |
1959 | /* |
1960 | * Cancel out the fetch.bundleURI config when running subprocesses, | |
1961 | * to avoid fetching from the same bundle list multiple times. | |
1962 | */ | |
1963 | strvec_pushl(&argv, "-c", "fetch.bundleURI=", | |
1964 | "fetch", "--append", "--no-auto-gc", | |
f6d8942b | 1965 | "--no-write-commit-graph", NULL); |
d1adf85b | 1966 | add_options_to_argv(&argv, config); |
85556d4e | 1967 | |
d54dea77 | 1968 | if (max_children != 1 && list->nr != 1) { |
d1adf85b | 1969 | struct parallel_fetch_state state = { argv.v, list, 0, 0, config }; |
36d69bf7 ÆAB |
1970 | const struct run_process_parallel_opts opts = { |
1971 | .tr2_category = "fetch", | |
1972 | .tr2_label = "parallel/fetch", | |
1973 | ||
1974 | .processes = max_children, | |
1975 | ||
1976 | .get_next_task = &fetch_next_remote, | |
1977 | .start_failure = &fetch_failed_to_start, | |
1978 | .task_finished = &fetch_finished, | |
1979 | .data = &state, | |
1980 | }; | |
d54dea77 | 1981 | |
22f9b7f3 | 1982 | strvec_push(&argv, "--end-of-options"); |
7dd5762d | 1983 | |
36d69bf7 | 1984 | run_processes_parallel(&opts); |
7dd5762d | 1985 | result = state.result; |
d54dea77 JS |
1986 | } else |
1987 | for (i = 0; i < list->nr; i++) { | |
1988 | const char *name = list->items[i].string; | |
ddbb47fd RS |
1989 | struct child_process cmd = CHILD_PROCESS_INIT; |
1990 | ||
1991 | strvec_pushv(&cmd.args, argv.v); | |
1992 | strvec_push(&cmd.args, name); | |
d1adf85b | 1993 | if (verbosity >= 0 && config->display_format != DISPLAY_FORMAT_PORCELAIN) |
d54dea77 | 1994 | printf(_("Fetching %s\n"), name); |
ddbb47fd RS |
1995 | cmd.git_cmd = 1; |
1996 | if (run_command(&cmd)) { | |
66996bea | 1997 | error(_("could not fetch %s"), name); |
d54dea77 JS |
1998 | result = 1; |
1999 | } | |
9c4a036b | 2000 | } |
9c4a036b | 2001 | |
22f9b7f3 | 2002 | strvec_clear(&argv); |
d54dea77 | 2003 | return !!result; |
9c4a036b BG |
2004 | } |
2005 | ||
aa57b871 JH |
2006 | /* |
2007 | * Fetching from the promisor remote should use the given filter-spec | |
2008 | * or inherit the default filter-spec from the config. | |
2009 | */ | |
2010 | static inline void fetch_one_setup_partial(struct remote *remote) | |
2011 | { | |
2012 | /* | |
2013 | * Explicit --no-filter argument overrides everything, regardless | |
2014 | * of any prior partial clones and fetches. | |
2015 | */ | |
2016 | if (filter_options.no_filter) | |
2017 | return; | |
2018 | ||
2019 | /* | |
2020 | * If no prior partial clone/fetch and the current fetch DID NOT | |
2021 | * request a partial-fetch, do a normal fetch. | |
2022 | */ | |
a5183d76 | 2023 | if (!repo_has_promisor_remote(the_repository) && !filter_options.choice) |
aa57b871 JH |
2024 | return; |
2025 | ||
2026 | /* | |
5e461393 CC |
2027 | * If this is a partial-fetch request, we enable partial on |
2028 | * this repo if not already enabled and remember the given | |
2029 | * filter-spec as the default for subsequent fetches to this | |
23547c40 | 2030 | * remote if there is currently no default filter-spec. |
aa57b871 | 2031 | */ |
5e461393 | 2032 | if (filter_options.choice) { |
aa57b871 JH |
2033 | partial_clone_register(remote->name, &filter_options); |
2034 | return; | |
2035 | } | |
2036 | ||
aa57b871 JH |
2037 | /* |
2038 | * Do a partial-fetch from the promisor remote using either the | |
2039 | * explicitly given filter-spec or inherit the filter-spec from | |
2040 | * the config. | |
2041 | */ | |
2042 | if (!filter_options.choice) | |
fa3d1b63 | 2043 | partial_clone_get_default_filter_spec(&filter_options, remote->name); |
aa57b871 JH |
2044 | return; |
2045 | } | |
2046 | ||
2b713c27 | 2047 | static int fetch_one(struct remote *remote, int argc, const char **argv, |
58afbe88 | 2048 | int prune_tags_ok, int use_stdin_refspecs, |
d1adf85b | 2049 | const struct fetch_config *config) |
b888d61c | 2050 | { |
d7c8e307 BW |
2051 | struct refspec rs = REFSPEC_INIT_FETCH; |
2052 | int i; | |
7b7f39ea | 2053 | int exit_code; |
6317972c ÆAB |
2054 | int maybe_prune_tags; |
2055 | int remote_via_config = remote_is_configured(remote, 0); | |
b888d61c | 2056 | |
fa685bdf | 2057 | if (!remote) |
66996bea AK |
2058 | die(_("no remote repository specified; please specify either a URL or a\n" |
2059 | "remote name from which new revisions should be fetched")); | |
fa685bdf | 2060 | |
508ea882 | 2061 | gtransport = prepare_transport(remote, 1); |
737c5a9c MS |
2062 | |
2063 | if (prune < 0) { | |
2064 | /* no command line request */ | |
07118832 ÆAB |
2065 | if (0 <= remote->prune) |
2066 | prune = remote->prune; | |
b779a25e PS |
2067 | else if (0 <= config->prune) |
2068 | prune = config->prune; | |
737c5a9c MS |
2069 | else |
2070 | prune = PRUNE_BY_DEFAULT; | |
2071 | } | |
2072 | ||
97716d21 ÆAB |
2073 | if (prune_tags < 0) { |
2074 | /* no command line request */ | |
2075 | if (0 <= remote->prune_tags) | |
2076 | prune_tags = remote->prune_tags; | |
2b472cfe PS |
2077 | else if (0 <= config->prune_tags) |
2078 | prune_tags = config->prune_tags; | |
97716d21 ÆAB |
2079 | else |
2080 | prune_tags = PRUNE_TAGS_BY_DEFAULT; | |
2081 | } | |
2082 | ||
6317972c ÆAB |
2083 | maybe_prune_tags = prune_tags_ok && prune_tags; |
2084 | if (maybe_prune_tags && remote_via_config) | |
95303500 | 2085 | refspec_append(&remote->fetch, TAG_REFSPEC); |
97716d21 | 2086 | |
d7c8e307 BW |
2087 | if (maybe_prune_tags && (argc || !remote_via_config)) |
2088 | refspec_append(&rs, TAG_REFSPEC); | |
6317972c | 2089 | |
d7c8e307 BW |
2090 | for (i = 0; i < argc; i++) { |
2091 | if (!strcmp(argv[i], "tag")) { | |
d7c8e307 BW |
2092 | i++; |
2093 | if (i >= argc) | |
66996bea | 2094 | die(_("you need to specify a tag name")); |
d7c8e307 | 2095 | |
1af8b8c0 RS |
2096 | refspec_appendf(&rs, "refs/tags/%s:refs/tags/%s", |
2097 | argv[i], argv[i]); | |
d7c8e307 BW |
2098 | } else { |
2099 | refspec_append(&rs, argv[i]); | |
b888d61c | 2100 | } |
b888d61c DB |
2101 | } |
2102 | ||
2b713c27 JT |
2103 | if (use_stdin_refspecs) { |
2104 | struct strbuf line = STRBUF_INIT; | |
2105 | while (strbuf_getline_lf(&line, stdin) != EOF) | |
2106 | refspec_append(&rs, line.buf); | |
2107 | strbuf_release(&line); | |
2108 | } | |
2109 | ||
5e3548ef BW |
2110 | if (server_options.nr) |
2111 | gtransport->server_options = &server_options; | |
2112 | ||
57b235a4 | 2113 | sigchain_push_common(unlock_pack_on_signal); |
58d4d7f1 | 2114 | atexit(unlock_pack_atexit); |
14358894 | 2115 | sigchain_push(SIGPIPE, SIG_IGN); |
d1adf85b | 2116 | exit_code = do_fetch(gtransport, &rs, config); |
14358894 | 2117 | sigchain_pop(SIGPIPE); |
d7c8e307 | 2118 | refspec_clear(&rs); |
af234459 JH |
2119 | transport_disconnect(gtransport); |
2120 | gtransport = NULL; | |
7b7f39ea | 2121 | return exit_code; |
b888d61c | 2122 | } |
9c4a036b BG |
2123 | |
2124 | int cmd_fetch(int argc, const char **argv, const char *prefix) | |
2125 | { | |
58afbe88 PS |
2126 | struct fetch_config config = { |
2127 | .display_format = DISPLAY_FORMAT_FULL, | |
b779a25e | 2128 | .prune = -1, |
2b472cfe | 2129 | .prune_tags = -1, |
ba28b2ca | 2130 | .show_forced_updates = 1, |
56e8bb4f | 2131 | .recurse_submodules = RECURSE_SUBMODULES_DEFAULT, |
ac197cc0 | 2132 | .parallel = 1, |
f7e063f3 | 2133 | .submodule_fetch_jobs = -1, |
58afbe88 | 2134 | }; |
cdc034a0 | 2135 | const char *submodule_prefix = ""; |
7f0cc04f | 2136 | const char *bundle_uri; |
b7410f61 | 2137 | struct string_list list = STRING_LIST_INIT_DUP; |
a1743343 | 2138 | struct remote *remote = NULL; |
39487a15 | 2139 | int all = -1, multiple = 0; |
9c4a036b | 2140 | int result = 0; |
c1a7902f | 2141 | int prune_tags_ok = 1; |
cdc034a0 PS |
2142 | int enable_auto_gc = 1; |
2143 | int unshallow = 0; | |
2144 | int max_jobs = -1; | |
2145 | int recurse_submodules_cli = RECURSE_SUBMODULES_DEFAULT; | |
2146 | int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND; | |
2147 | int fetch_write_commit_graph = -1; | |
2148 | int stdin_refspecs = 0; | |
2149 | int negotiate_only = 0; | |
dd781e38 | 2150 | int porcelain = 0; |
cdc034a0 PS |
2151 | int i; |
2152 | ||
2153 | struct option builtin_fetch_options[] = { | |
2154 | OPT__VERBOSITY(&verbosity), | |
2155 | OPT_BOOL(0, "all", &all, | |
2156 | N_("fetch from all remotes")), | |
2157 | OPT_BOOL(0, "set-upstream", &set_upstream, | |
2158 | N_("set upstream for git pull/fetch")), | |
2159 | OPT_BOOL('a', "append", &append, | |
2160 | N_("append to .git/FETCH_HEAD instead of overwriting")), | |
2161 | OPT_BOOL(0, "atomic", &atomic_fetch, | |
2162 | N_("use atomic transaction to update references")), | |
2163 | OPT_STRING(0, "upload-pack", &upload_pack, N_("path"), | |
2164 | N_("path to upload pack on remote end")), | |
2165 | OPT__FORCE(&force, N_("force overwrite of local reference"), 0), | |
2166 | OPT_BOOL('m', "multiple", &multiple, | |
2167 | N_("fetch from multiple remotes")), | |
2168 | OPT_SET_INT('t', "tags", &tags, | |
2169 | N_("fetch all tags and associated objects"), TAGS_SET), | |
2170 | OPT_SET_INT('n', NULL, &tags, | |
2171 | N_("do not fetch all tags (--no-tags)"), TAGS_UNSET), | |
2172 | OPT_INTEGER('j', "jobs", &max_jobs, | |
2173 | N_("number of submodules fetched in parallel")), | |
2174 | OPT_BOOL(0, "prefetch", &prefetch, | |
2175 | N_("modify the refspec to place all refs within refs/prefetch/")), | |
2176 | OPT_BOOL('p', "prune", &prune, | |
2177 | N_("prune remote-tracking branches no longer on remote")), | |
2178 | OPT_BOOL('P', "prune-tags", &prune_tags, | |
2179 | N_("prune local tags no longer on remote and clobber changed tags")), | |
2180 | OPT_CALLBACK_F(0, "recurse-submodules", &recurse_submodules_cli, N_("on-demand"), | |
2181 | N_("control recursive fetching of submodules"), | |
2182 | PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules), | |
2183 | OPT_BOOL(0, "dry-run", &dry_run, | |
2184 | N_("dry run")), | |
dd781e38 | 2185 | OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")), |
cdc034a0 PS |
2186 | OPT_BOOL(0, "write-fetch-head", &write_fetch_head, |
2187 | N_("write fetched references to the FETCH_HEAD file")), | |
2188 | OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")), | |
2189 | OPT_BOOL('u', "update-head-ok", &update_head_ok, | |
2190 | N_("allow updating of HEAD ref")), | |
2191 | OPT_BOOL(0, "progress", &progress, N_("force progress reporting")), | |
2192 | OPT_STRING(0, "depth", &depth, N_("depth"), | |
2193 | N_("deepen history of shallow clone")), | |
2194 | OPT_STRING(0, "shallow-since", &deepen_since, N_("time"), | |
2195 | N_("deepen history of shallow repository based on time")), | |
2196 | OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"), | |
2197 | N_("deepen history of shallow clone, excluding rev")), | |
2198 | OPT_INTEGER(0, "deepen", &deepen_relative, | |
2199 | N_("deepen history of shallow clone")), | |
2200 | OPT_SET_INT_F(0, "unshallow", &unshallow, | |
2201 | N_("convert to a complete repository"), | |
2202 | 1, PARSE_OPT_NONEG), | |
2203 | OPT_SET_INT_F(0, "refetch", &refetch, | |
2204 | N_("re-fetch without negotiating common commits"), | |
2205 | 1, PARSE_OPT_NONEG), | |
2206 | { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"), | |
2207 | N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN }, | |
2208 | OPT_CALLBACK_F(0, "recurse-submodules-default", | |
2209 | &recurse_submodules_default, N_("on-demand"), | |
2210 | N_("default for recursive fetching of submodules " | |
2211 | "(lower priority than config files)"), | |
2212 | PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules), | |
2213 | OPT_BOOL(0, "update-shallow", &update_shallow, | |
2214 | N_("accept refs that update .git/shallow")), | |
66e33092 | 2215 | OPT_CALLBACK_F(0, "refmap", &refmap, N_("refmap"), |
cdc034a0 PS |
2216 | N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg), |
2217 | OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")), | |
ae2c912c | 2218 | OPT_IPVERSION(&family), |
cdc034a0 PS |
2219 | OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"), |
2220 | N_("report that we have only objects reachable from this object")), | |
2221 | OPT_BOOL(0, "negotiate-only", &negotiate_only, | |
2222 | N_("do not fetch a packfile; instead, print ancestors of negotiation tips")), | |
2223 | OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options), | |
2224 | OPT_BOOL(0, "auto-maintenance", &enable_auto_gc, | |
2225 | N_("run 'maintenance --auto' after fetching")), | |
2226 | OPT_BOOL(0, "auto-gc", &enable_auto_gc, | |
2227 | N_("run 'maintenance --auto' after fetching")), | |
ba28b2ca | 2228 | OPT_BOOL(0, "show-forced-updates", &config.show_forced_updates, |
cdc034a0 PS |
2229 | N_("check for forced-updates on all updated branches")), |
2230 | OPT_BOOL(0, "write-commit-graph", &fetch_write_commit_graph, | |
2231 | N_("write the commit-graph after fetching")), | |
2232 | OPT_BOOL(0, "stdin", &stdin_refspecs, | |
2233 | N_("accept refspecs from stdin")), | |
2234 | OPT_END() | |
2235 | }; | |
9c4a036b | 2236 | |
bbc30f99 JK |
2237 | packet_trace_identity("fetch"); |
2238 | ||
9c4a036b BG |
2239 | /* Record the command line for the reflog */ |
2240 | strbuf_addstr(&default_rla, "fetch"); | |
46da295a JS |
2241 | for (i = 1; i < argc; i++) { |
2242 | /* This handles non-URLs gracefully */ | |
2243 | char *anon = transport_anonymize_url(argv[i]); | |
2244 | ||
2245 | strbuf_addf(&default_rla, " %s", anon); | |
2246 | free(anon); | |
2247 | } | |
9c4a036b | 2248 | |
58afbe88 | 2249 | git_config(git_fetch_config, &config); |
059fda19 JS |
2250 | if (the_repository->gitdir) { |
2251 | prepare_repo_settings(the_repository); | |
2252 | the_repository->settings.command_requires_full_index = 0; | |
2253 | } | |
737c5a9c | 2254 | |
9c4a036b BG |
2255 | argc = parse_options(argc, argv, prefix, |
2256 | builtin_fetch_options, builtin_fetch_usage, 0); | |
386c076a GC |
2257 | |
2258 | if (recurse_submodules_cli != RECURSE_SUBMODULES_DEFAULT) | |
56e8bb4f | 2259 | config.recurse_submodules = recurse_submodules_cli; |
386c076a GC |
2260 | |
2261 | if (negotiate_only) { | |
2262 | switch (recurse_submodules_cli) { | |
2263 | case RECURSE_SUBMODULES_OFF: | |
2264 | case RECURSE_SUBMODULES_DEFAULT: | |
2265 | /* | |
2266 | * --negotiate-only should never recurse into | |
2267 | * submodules. Skip it by setting recurse_submodules to | |
2268 | * RECURSE_SUBMODULES_OFF. | |
2269 | */ | |
56e8bb4f | 2270 | config.recurse_submodules = RECURSE_SUBMODULES_OFF; |
386c076a GC |
2271 | break; |
2272 | ||
2273 | default: | |
de4eaae6 JH |
2274 | die(_("options '%s' and '%s' cannot be used together"), |
2275 | "--negotiate-only", "--recurse-submodules"); | |
386c076a GC |
2276 | } |
2277 | } | |
2278 | ||
56e8bb4f | 2279 | if (config.recurse_submodules != RECURSE_SUBMODULES_OFF) { |
f7e063f3 PS |
2280 | int *sfjc = config.submodule_fetch_jobs == -1 |
2281 | ? &config.submodule_fetch_jobs : NULL; | |
56e8bb4f PS |
2282 | int *rs = config.recurse_submodules == RECURSE_SUBMODULES_DEFAULT |
2283 | ? &config.recurse_submodules : NULL; | |
e5b94213 JT |
2284 | |
2285 | fetch_config_from_gitmodules(sfjc, rs); | |
2286 | } | |
9c4a036b | 2287 | |
dd781e38 PS |
2288 | |
2289 | if (porcelain) { | |
2290 | switch (recurse_submodules_cli) { | |
2291 | case RECURSE_SUBMODULES_OFF: | |
2292 | case RECURSE_SUBMODULES_DEFAULT: | |
2293 | /* | |
2294 | * Reference updates in submodules would be ambiguous | |
2295 | * in porcelain mode, so we reject this combination. | |
2296 | */ | |
56e8bb4f | 2297 | config.recurse_submodules = RECURSE_SUBMODULES_OFF; |
dd781e38 PS |
2298 | break; |
2299 | ||
2300 | default: | |
2301 | die(_("options '%s' and '%s' cannot be used together"), | |
2302 | "--porcelain", "--recurse-submodules"); | |
2303 | } | |
2304 | ||
2305 | config.display_format = DISPLAY_FORMAT_PORCELAIN; | |
2306 | } | |
2307 | ||
eff40457 | 2308 | if (negotiate_only && !negotiation_tip.nr) |
2826ffad | 2309 | die(_("--negotiate-only needs one or more --negotiation-tip=*")); |
eff40457 | 2310 | |
cccf74e2 NTND |
2311 | if (deepen_relative) { |
2312 | if (deepen_relative < 0) | |
66996bea | 2313 | die(_("negative depth in --deepen is not supported")); |
cccf74e2 | 2314 | if (depth) |
43ea635c | 2315 | die(_("options '%s' and '%s' cannot be used together"), "--deepen", "--depth"); |
cccf74e2 NTND |
2316 | depth = xstrfmt("%d", deepen_relative); |
2317 | } | |
4dcb167f NTND |
2318 | if (unshallow) { |
2319 | if (depth) | |
43ea635c | 2320 | die(_("options '%s' and '%s' cannot be used together"), "--depth", "--unshallow"); |
c8813487 | 2321 | else if (!is_repository_shallow(the_repository)) |
4dcb167f | 2322 | die(_("--unshallow on a complete repository does not make sense")); |
2805bb59 JK |
2323 | else |
2324 | depth = xstrfmt("%d", INFINITE_DEPTH); | |
4dcb167f NTND |
2325 | } |
2326 | ||
5594bcad NTND |
2327 | /* no need to be strict, transport_set_option() will validate it again */ |
2328 | if (depth && atoi(depth) < 1) | |
2329 | die(_("depth %s is not a positive number"), depth); | |
a45a2600 | 2330 | if (depth || deepen_since || deepen_not.nr) |
508ea882 | 2331 | deepen = 1; |
5594bcad | 2332 | |
887952b8 JH |
2333 | /* FETCH_HEAD never gets updated in --dry-run mode */ |
2334 | if (dry_run) | |
2335 | write_fetch_head = 0; | |
2336 | ||
c39952b9 MA |
2337 | if (!max_jobs) |
2338 | max_jobs = online_cpus(); | |
2339 | ||
7f0cc04f DS |
2340 | if (!git_config_get_string_tmp("fetch.bundleuri", &bundle_uri) && |
2341 | fetch_bundle_uri(the_repository, bundle_uri, NULL)) | |
2342 | warning(_("failed to fetch bundles from '%s'"), bundle_uri); | |
2343 | ||
39487a15 TB |
2344 | if (all < 0) { |
2345 | /* | |
2346 | * no --[no-]all given; | |
2347 | * only use config option if no remote was explicitly specified | |
2348 | */ | |
2349 | all = (!argc) ? config.all : 0; | |
2350 | } | |
2351 | ||
9c4a036b BG |
2352 | if (all) { |
2353 | if (argc == 1) | |
bd4a51fc | 2354 | die(_("fetch --all does not take a repository argument")); |
9c4a036b | 2355 | else if (argc > 1) |
bd4a51fc | 2356 | die(_("fetch --all does not make sense with refspecs")); |
39487a15 | 2357 | |
9c4a036b | 2358 | (void) for_each_remote(get_one_remote_for_fetch, &list); |
0353c688 JH |
2359 | |
2360 | /* do not do fetch_multiple() of one */ | |
2361 | if (list.nr == 1) | |
2362 | remote = remote_get(list.items[0].string); | |
9c4a036b BG |
2363 | } else if (argc == 0) { |
2364 | /* No arguments -- use default remote */ | |
2365 | remote = remote_get(NULL); | |
16679e37 BG |
2366 | } else if (multiple) { |
2367 | /* All arguments are assumed to be remotes or groups */ | |
2368 | for (i = 0; i < argc; i++) | |
2369 | if (!add_remote_or_group(argv[i], &list)) | |
66996bea AK |
2370 | die(_("no such remote or remote group: %s"), |
2371 | argv[i]); | |
9c4a036b BG |
2372 | } else { |
2373 | /* Single remote or group */ | |
2374 | (void) add_remote_or_group(argv[0], &list); | |
2375 | if (list.nr > 1) { | |
2376 | /* More than one remote */ | |
2377 | if (argc > 1) | |
66996bea | 2378 | die(_("fetching a group and specifying refspecs does not make sense")); |
9c4a036b BG |
2379 | } else { |
2380 | /* Zero or one remotes */ | |
2381 | remote = remote_get(argv[0]); | |
c1a7902f | 2382 | prune_tags_ok = (argc == 1); |
a1743343 JT |
2383 | argc--; |
2384 | argv++; | |
9c4a036b BG |
2385 | } |
2386 | } | |
06a668cb | 2387 | string_list_remove_duplicates(&list, 0); |
9c4a036b | 2388 | |
9c1e657a JT |
2389 | if (negotiate_only) { |
2390 | struct oidset acked_commits = OIDSET_INIT; | |
2391 | struct oidset_iter iter; | |
2392 | const struct object_id *oid; | |
2393 | ||
2394 | if (!remote) | |
2395 | die(_("must supply remote when using --negotiate-only")); | |
2396 | gtransport = prepare_transport(remote, 1); | |
2397 | if (gtransport->smart_options) { | |
2398 | gtransport->smart_options->acked_commits = &acked_commits; | |
2399 | } else { | |
66996bea | 2400 | warning(_("protocol does not support --negotiate-only, exiting")); |
bec587d4 GC |
2401 | result = 1; |
2402 | goto cleanup; | |
9c1e657a JT |
2403 | } |
2404 | if (server_options.nr) | |
2405 | gtransport->server_options = &server_options; | |
2406 | result = transport_fetch_refs(gtransport, NULL); | |
2407 | ||
2408 | oidset_iter_init(&acked_commits, &iter); | |
2409 | while ((oid = oidset_iter_next(&iter))) | |
2410 | printf("%s\n", oid_to_hex(oid)); | |
2411 | oidset_clear(&acked_commits); | |
2412 | } else if (remote) { | |
a5183d76 | 2413 | if (filter_options.choice || repo_has_promisor_remote(the_repository)) |
aa57b871 | 2414 | fetch_one_setup_partial(remote); |
58afbe88 | 2415 | result = fetch_one(remote, argc, argv, prune_tags_ok, stdin_refspecs, |
d1adf85b | 2416 | &config); |
acb0c572 | 2417 | } else { |
d54dea77 JS |
2418 | int max_children = max_jobs; |
2419 | ||
acb0c572 | 2420 | if (filter_options.choice) |
e0137875 CC |
2421 | die(_("--filter can only be used with the remote " |
2422 | "configured in extensions.partialclone")); | |
d54dea77 | 2423 | |
c7b190da PS |
2424 | if (atomic_fetch) |
2425 | die(_("--atomic can only be used when fetching " | |
2426 | "from one remote")); | |
2427 | ||
2b713c27 JT |
2428 | if (stdin_refspecs) |
2429 | die(_("--stdin can only be used when fetching " | |
2430 | "from one remote")); | |
2431 | ||
d54dea77 | 2432 | if (max_children < 0) |
ac197cc0 | 2433 | max_children = config.parallel; |
d54dea77 | 2434 | |
aa57b871 | 2435 | /* TODO should this also die if we have a previous partial-clone? */ |
d1adf85b | 2436 | result = fetch_multiple(&list, max_children, &config); |
acb0c572 | 2437 | } |
a1743343 | 2438 | |
0353c688 JH |
2439 | /* |
2440 | * This is only needed after fetch_one(), which does not fetch | |
2441 | * submodules by itself. | |
2442 | * | |
2443 | * When we fetch from multiple remotes, fetch_multiple() has | |
2444 | * already updated submodules to grab commits necessary for | |
2445 | * the fetched history from each remote, so there is no need | |
2446 | * to fetch submodules from here. | |
2447 | */ | |
56e8bb4f | 2448 | if (!result && remote && (config.recurse_submodules != RECURSE_SUBMODULES_OFF)) { |
22f9b7f3 | 2449 | struct strvec options = STRVEC_INIT; |
d54dea77 JS |
2450 | int max_children = max_jobs; |
2451 | ||
2452 | if (max_children < 0) | |
f7e063f3 | 2453 | max_children = config.submodule_fetch_jobs; |
d54dea77 | 2454 | if (max_children < 0) |
ac197cc0 | 2455 | max_children = config.parallel; |
85556d4e | 2456 | |
d1adf85b | 2457 | add_options_to_argv(&options, &config); |
b90d9f76 GC |
2458 | result = fetch_submodules(the_repository, |
2459 | &options, | |
2460 | submodule_prefix, | |
56e8bb4f | 2461 | config.recurse_submodules, |
b90d9f76 GC |
2462 | recurse_submodules_default, |
2463 | verbosity < 0, | |
2464 | max_children); | |
22f9b7f3 | 2465 | strvec_clear(&options); |
7dce19d3 JL |
2466 | } |
2467 | ||
135a12bc GC |
2468 | /* |
2469 | * Skip irrelevant tasks because we know objects were not | |
2470 | * fetched. | |
2471 | * | |
2472 | * NEEDSWORK: as a future optimization, we can return early | |
2473 | * whenever objects were not fetched e.g. if we already have all | |
2474 | * of them. | |
2475 | */ | |
2476 | if (negotiate_only) | |
2477 | goto cleanup; | |
9c4a036b | 2478 | |
50f26bd0 | 2479 | prepare_repo_settings(the_repository); |
c14e6e79 JS |
2480 | if (fetch_write_commit_graph > 0 || |
2481 | (fetch_write_commit_graph < 0 && | |
2482 | the_repository->settings.fetch_write_commit_graph)) { | |
5a535094 | 2483 | int commit_graph_flags = COMMIT_GRAPH_WRITE_SPLIT; |
50f26bd0 DS |
2484 | |
2485 | if (progress) | |
5a535094 | 2486 | commit_graph_flags |= COMMIT_GRAPH_WRITE_PROGRESS; |
50f26bd0 | 2487 | |
0bd52e27 | 2488 | write_commit_graph_reachable(the_repository->objects->odb, |
50f26bd0 | 2489 | commit_graph_flags, |
63020f17 | 2490 | NULL); |
50f26bd0 DS |
2491 | } |
2492 | ||
7390f05a RC |
2493 | if (enable_auto_gc) { |
2494 | if (refetch) { | |
2495 | /* | |
2496 | * Hint auto-maintenance strongly to encourage repacking, | |
2497 | * but respect config settings disabling it. | |
2498 | */ | |
2499 | int opt_val; | |
2500 | if (git_config_get_int("gc.autopacklimit", &opt_val)) | |
2501 | opt_val = -1; | |
2502 | if (opt_val != 0) | |
2503 | git_config_push_parameter("gc.autoPackLimit=1"); | |
2504 | ||
2505 | if (git_config_get_int("maintenance.incremental-repack.auto", &opt_val)) | |
2506 | opt_val = -1; | |
2507 | if (opt_val != 0) | |
2508 | git_config_push_parameter("maintenance.incremental-repack.auto=-1"); | |
2509 | } | |
a95ce124 | 2510 | run_auto_maintenance(verbosity < 0); |
7390f05a | 2511 | } |
131b8fcb | 2512 | |
bec587d4 GC |
2513 | cleanup: |
2514 | string_list_clear(&list, 0); | |
9c4a036b BG |
2515 | return result; |
2516 | } |