]>
Commit | Line | Data |
---|---|---|
b888d61c DB |
1 | /* |
2 | * "git fetch" | |
3 | */ | |
4 | #include "cache.h" | |
b2141fc1 | 5 | #include "config.h" |
e724197f | 6 | #include "repository.h" |
b888d61c | 7 | #include "refs.h" |
ec0cb496 | 8 | #include "refspec.h" |
cbd53a21 | 9 | #include "object-store.h" |
b7e2d8bc | 10 | #include "oidset.h" |
b888d61c DB |
11 | #include "commit.h" |
12 | #include "builtin.h" | |
c455c87c | 13 | #include "string-list.h" |
b888d61c DB |
14 | #include "remote.h" |
15 | #include "transport.h" | |
4191c356 | 16 | #include "run-command.h" |
83201998 | 17 | #include "parse-options.h" |
4a16d072 | 18 | #include "sigchain.h" |
027771fc | 19 | #include "submodule-config.h" |
7dce19d3 | 20 | #include "submodule.h" |
f96400cb | 21 | #include "connected.h" |
dbbcd44f | 22 | #include "strvec.h" |
6bc91f23 | 23 | #include "utf8.h" |
3836d88a | 24 | #include "packfile.h" |
acb0c572 | 25 | #include "list-objects-filter-options.h" |
64043556 | 26 | #include "commit-reach.h" |
24bc1a12 | 27 | #include "branch.h" |
b14ed5ad | 28 | #include "promisor-remote.h" |
50f26bd0 | 29 | #include "commit-graph.h" |
120ad2b0 | 30 | #include "shallow.h" |
b888d61c | 31 | |
377444b4 DS |
32 | #define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000) |
33 | ||
83201998 | 34 | static const char * const builtin_fetch_usage[] = { |
719acedb NTND |
35 | N_("git fetch [<options>] [<repository> [<refspec>...]]"), |
36 | N_("git fetch [<options>] <group>"), | |
37 | N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"), | |
38 | N_("git fetch --all [<options>]"), | |
83201998 KH |
39 | NULL |
40 | }; | |
b888d61c | 41 | |
83201998 KH |
42 | enum { |
43 | TAGS_UNSET = 0, | |
44 | TAGS_DEFAULT = 1, | |
45 | TAGS_SET = 2 | |
46 | }; | |
47 | ||
737c5a9c | 48 | static int fetch_prune_config = -1; /* unspecified */ |
cdbd70c4 | 49 | static int fetch_show_forced_updates = 1; |
377444b4 | 50 | static uint64_t forced_updates_ms = 0; |
737c5a9c MS |
51 | static int prune = -1; /* unspecified */ |
52 | #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */ | |
53 | ||
97716d21 ÆAB |
54 | static int fetch_prune_tags_config = -1; /* unspecified */ |
55 | static int prune_tags = -1; /* unspecified */ | |
56 | #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */ | |
57 | ||
24bc1a12 | 58 | static int all, append, dry_run, force, keep, multiple, update_head_ok; |
887952b8 | 59 | static int write_fetch_head = 1; |
24bc1a12 | 60 | static int verbosity, deepen_relative, set_upstream; |
8c69832d | 61 | static int progress = -1; |
c3d6b703 | 62 | static int enable_auto_gc = 1; |
508ea882 | 63 | static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen; |
d54dea77 JS |
64 | static int max_jobs = -1, submodule_fetch_jobs_config = -1; |
65 | static int fetch_parallel_config = 1; | |
c7b190da | 66 | static int atomic_fetch; |
c915f11e | 67 | static enum transport_family family; |
4191c356 | 68 | static const char *depth; |
508ea882 | 69 | static const char *deepen_since; |
83201998 | 70 | static const char *upload_pack; |
a45a2600 | 71 | static struct string_list deepen_not = STRING_LIST_INIT_NODUP; |
2d324efa | 72 | static struct strbuf default_rla = STRBUF_INIT; |
af234459 | 73 | static struct transport *gtransport; |
b26ed430 | 74 | static struct transport *gsecondary; |
7dce19d3 | 75 | static const char *submodule_prefix = ""; |
8c69832d | 76 | static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT; |
e8906a90 | 77 | static int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND; |
4b3b33a7 | 78 | static int shown_url = 0; |
e4cffacc | 79 | static struct refspec refmap = REFSPEC_INIT_FETCH; |
acb0c572 | 80 | static struct list_objects_filter_options filter_options; |
5e3548ef | 81 | static struct string_list server_options = STRING_LIST_INIT_DUP; |
3390e42a | 82 | static struct string_list negotiation_tip = STRING_LIST_INIT_NODUP; |
c14e6e79 | 83 | static int fetch_write_commit_graph = -1; |
2b713c27 | 84 | static int stdin_refspecs = 0; |
e4022ed2 | 85 | |
737c5a9c MS |
86 | static int git_fetch_config(const char *k, const char *v, void *cb) |
87 | { | |
88 | if (!strcmp(k, "fetch.prune")) { | |
89 | fetch_prune_config = git_config_bool(k, v); | |
90 | return 0; | |
91 | } | |
58f4203e | 92 | |
97716d21 ÆAB |
93 | if (!strcmp(k, "fetch.prunetags")) { |
94 | fetch_prune_tags_config = git_config_bool(k, v); | |
95 | return 0; | |
96 | } | |
97 | ||
cdbd70c4 DS |
98 | if (!strcmp(k, "fetch.showforcedupdates")) { |
99 | fetch_show_forced_updates = git_config_bool(k, v); | |
100 | return 0; | |
101 | } | |
102 | ||
58f4203e SB |
103 | if (!strcmp(k, "submodule.recurse")) { |
104 | int r = git_config_bool(k, v) ? | |
105 | RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF; | |
106 | recurse_submodules = r; | |
107 | } | |
108 | ||
f20e7c1e | 109 | if (!strcmp(k, "submodule.fetchjobs")) { |
d54dea77 | 110 | submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v); |
f20e7c1e | 111 | return 0; |
8fa29159 BW |
112 | } else if (!strcmp(k, "fetch.recursesubmodules")) { |
113 | recurse_submodules = parse_fetch_recurse_submodules_arg(k, v); | |
114 | return 0; | |
f20e7c1e BW |
115 | } |
116 | ||
d54dea77 JS |
117 | if (!strcmp(k, "fetch.parallel")) { |
118 | fetch_parallel_config = git_config_int(k, v); | |
119 | if (fetch_parallel_config < 0) | |
120 | die(_("fetch.parallel cannot be negative")); | |
121 | return 0; | |
122 | } | |
123 | ||
72549dfd | 124 | return git_default_config(k, v, cb); |
737c5a9c MS |
125 | } |
126 | ||
c5558f80 JH |
127 | static int parse_refmap_arg(const struct option *opt, const char *arg, int unset) |
128 | { | |
517fe807 JK |
129 | BUG_ON_OPT_NEG(unset); |
130 | ||
c5558f80 JH |
131 | /* |
132 | * "git fetch --refmap='' origin foo" | |
133 | * can be used to tell the command not to store anywhere | |
134 | */ | |
e4cffacc BW |
135 | refspec_append(&refmap, arg); |
136 | ||
c5558f80 JH |
137 | return 0; |
138 | } | |
139 | ||
83201998 | 140 | static struct option builtin_fetch_options[] = { |
7f87aff2 | 141 | OPT__VERBOSITY(&verbosity), |
d5d09d47 SB |
142 | OPT_BOOL(0, "all", &all, |
143 | N_("fetch from all remotes")), | |
24bc1a12 CB |
144 | OPT_BOOL(0, "set-upstream", &set_upstream, |
145 | N_("set upstream for git pull/fetch")), | |
d5d09d47 SB |
146 | OPT_BOOL('a', "append", &append, |
147 | N_("append to .git/FETCH_HEAD instead of overwriting")), | |
c7b190da PS |
148 | OPT_BOOL(0, "atomic", &atomic_fetch, |
149 | N_("use atomic transaction to update references")), | |
719acedb NTND |
150 | OPT_STRING(0, "upload-pack", &upload_pack, N_("path"), |
151 | N_("path to upload pack on remote end")), | |
8cd4b7c1 | 152 | OPT__FORCE(&force, N_("force overwrite of local reference"), 0), |
d5d09d47 SB |
153 | OPT_BOOL('m', "multiple", &multiple, |
154 | N_("fetch from multiple remotes")), | |
83201998 | 155 | OPT_SET_INT('t', "tags", &tags, |
719acedb | 156 | N_("fetch all tags and associated objects"), TAGS_SET), |
e7951290 | 157 | OPT_SET_INT('n', NULL, &tags, |
719acedb | 158 | N_("do not fetch all tags (--no-tags)"), TAGS_UNSET), |
d54dea77 | 159 | OPT_INTEGER('j', "jobs", &max_jobs, |
62104ba1 | 160 | N_("number of submodules fetched in parallel")), |
d5d09d47 SB |
161 | OPT_BOOL('p', "prune", &prune, |
162 | N_("prune remote-tracking branches no longer on remote")), | |
97716d21 ÆAB |
163 | OPT_BOOL('P', "prune-tags", &prune_tags, |
164 | N_("prune local tags no longer on remote and clobber changed tags")), | |
203c8533 | 165 | OPT_CALLBACK_F(0, "recurse-submodules", &recurse_submodules, N_("on-demand"), |
719acedb | 166 | N_("control recursive fetching of submodules"), |
203c8533 | 167 | PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules), |
d5d09d47 SB |
168 | OPT_BOOL(0, "dry-run", &dry_run, |
169 | N_("dry run")), | |
887952b8 JH |
170 | OPT_BOOL(0, "write-fetch-head", &write_fetch_head, |
171 | N_("write fetched references to the FETCH_HEAD file")), | |
d5d09d47 SB |
172 | OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")), |
173 | OPT_BOOL('u', "update-head-ok", &update_head_ok, | |
719acedb NTND |
174 | N_("allow updating of HEAD ref")), |
175 | OPT_BOOL(0, "progress", &progress, N_("force progress reporting")), | |
176 | OPT_STRING(0, "depth", &depth, N_("depth"), | |
177 | N_("deepen history of shallow clone")), | |
508ea882 NTND |
178 | OPT_STRING(0, "shallow-since", &deepen_since, N_("time"), |
179 | N_("deepen history of shallow repository based on time")), | |
a45a2600 | 180 | OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"), |
6d873865 | 181 | N_("deepen history of shallow clone, excluding rev")), |
cccf74e2 NTND |
182 | OPT_INTEGER(0, "deepen", &deepen_relative, |
183 | N_("deepen history of shallow clone")), | |
3e4a67b4 NTND |
184 | OPT_SET_INT_F(0, "unshallow", &unshallow, |
185 | N_("convert to a complete repository"), | |
186 | 1, PARSE_OPT_NONEG), | |
719acedb NTND |
187 | { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"), |
188 | N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN }, | |
203c8533 | 189 | OPT_CALLBACK_F(0, "recurse-submodules-default", |
8c69832d SB |
190 | &recurse_submodules_default, N_("on-demand"), |
191 | N_("default for recursive fetching of submodules " | |
192 | "(lower priority than config files)"), | |
203c8533 | 193 | PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules), |
48d25cae NTND |
194 | OPT_BOOL(0, "update-shallow", &update_shallow, |
195 | N_("accept refs that update .git/shallow")), | |
203c8533 DL |
196 | OPT_CALLBACK_F(0, "refmap", NULL, N_("refmap"), |
197 | N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg), | |
5e3548ef | 198 | OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")), |
c915f11e EW |
199 | OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"), |
200 | TRANSPORT_FAMILY_IPV4), | |
201 | OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"), | |
202 | TRANSPORT_FAMILY_IPV6), | |
3390e42a JT |
203 | OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"), |
204 | N_("report that we have only objects reachable from this object")), | |
acb0c572 | 205 | OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options), |
a95ce124 DS |
206 | OPT_BOOL(0, "auto-maintenance", &enable_auto_gc, |
207 | N_("run 'maintenance --auto' after fetching")), | |
c3d6b703 | 208 | OPT_BOOL(0, "auto-gc", &enable_auto_gc, |
a95ce124 | 209 | N_("run 'maintenance --auto' after fetching")), |
cdbd70c4 DS |
210 | OPT_BOOL(0, "show-forced-updates", &fetch_show_forced_updates, |
211 | N_("check for forced-updates on all updated branches")), | |
c14e6e79 JS |
212 | OPT_BOOL(0, "write-commit-graph", &fetch_write_commit_graph, |
213 | N_("write the commit-graph after fetching")), | |
2b713c27 JT |
214 | OPT_BOOL(0, "stdin", &stdin_refspecs, |
215 | N_("accept refspecs from stdin")), | |
83201998 KH |
216 | OPT_END() |
217 | }; | |
218 | ||
e4022ed2 SP |
219 | static void unlock_pack(void) |
220 | { | |
af234459 JH |
221 | if (gtransport) |
222 | transport_unlock_pack(gtransport); | |
b26ed430 JH |
223 | if (gsecondary) |
224 | transport_unlock_pack(gsecondary); | |
e4022ed2 SP |
225 | } |
226 | ||
227 | static void unlock_pack_on_signal(int signo) | |
228 | { | |
229 | unlock_pack(); | |
4a16d072 | 230 | sigchain_pop(signo); |
e4022ed2 SP |
231 | raise(signo); |
232 | } | |
b888d61c | 233 | |
85682c19 | 234 | static void add_merge_config(struct ref **head, |
4577370e | 235 | const struct ref *remote_refs, |
85682c19 SP |
236 | struct branch *branch, |
237 | struct ref ***tail) | |
b888d61c | 238 | { |
85682c19 | 239 | int i; |
b888d61c | 240 | |
85682c19 SP |
241 | for (i = 0; i < branch->merge_nr; i++) { |
242 | struct ref *rm, **old_tail = *tail; | |
0ad4a5ff | 243 | struct refspec_item refspec; |
85682c19 SP |
244 | |
245 | for (rm = *head; rm; rm = rm->next) { | |
246 | if (branch_merge_matches(branch, i, rm->name)) { | |
900f2814 | 247 | rm->fetch_head_status = FETCH_HEAD_MERGE; |
85682c19 SP |
248 | break; |
249 | } | |
b888d61c | 250 | } |
85682c19 SP |
251 | if (rm) |
252 | continue; | |
253 | ||
9ad7c5ae | 254 | /* |
8b3f3f84 | 255 | * Not fetched to a remote-tracking branch? We need to fetch |
85682c19 | 256 | * it anyway to allow this branch's "branch.$name.merge" |
05207a28 | 257 | * to be honored by 'git pull', but we do not have to |
9ad7c5ae JH |
258 | * fail if branch.$name.merge is misconfigured to point |
259 | * at a nonexisting branch. If we were indeed called by | |
05207a28 | 260 | * 'git pull', it will notice the misconfiguration because |
9ad7c5ae JH |
261 | * there is no entry in the resulting FETCH_HEAD marked |
262 | * for merging. | |
85682c19 | 263 | */ |
8da61a2a | 264 | memset(&refspec, 0, sizeof(refspec)); |
85682c19 | 265 | refspec.src = branch->merge[i]->src; |
9ad7c5ae | 266 | get_fetch_map(remote_refs, &refspec, tail, 1); |
85682c19 | 267 | for (rm = *old_tail; rm; rm = rm->next) |
900f2814 | 268 | rm->fetch_head_status = FETCH_HEAD_MERGE; |
b888d61c DB |
269 | } |
270 | } | |
271 | ||
b7e2d8bc | 272 | static void create_fetch_oidset(struct ref **head, struct oidset *out) |
a0fbb5a3 MH |
273 | { |
274 | struct ref *rm = *head; | |
275 | while (rm) { | |
b7e2d8bc | 276 | oidset_insert(out, &rm->old_oid); |
a0fbb5a3 MH |
277 | rm = rm->next; |
278 | } | |
a0fbb5a3 MH |
279 | } |
280 | ||
e198b3a7 | 281 | struct refname_hash_entry { |
e2b5038d | 282 | struct hashmap_entry ent; |
e198b3a7 | 283 | struct object_id oid; |
f80d9223 | 284 | int ignore; |
e198b3a7 JH |
285 | char refname[FLEX_ARRAY]; |
286 | }; | |
287 | ||
288 | static int refname_hash_entry_cmp(const void *hashmap_cmp_fn_data, | |
939af16e EW |
289 | const struct hashmap_entry *eptr, |
290 | const struct hashmap_entry *entry_or_key, | |
e198b3a7 JH |
291 | const void *keydata) |
292 | { | |
939af16e | 293 | const struct refname_hash_entry *e1, *e2; |
e198b3a7 | 294 | |
939af16e EW |
295 | e1 = container_of(eptr, const struct refname_hash_entry, ent); |
296 | e2 = container_of(entry_or_key, const struct refname_hash_entry, ent); | |
e198b3a7 JH |
297 | return strcmp(e1->refname, keydata ? keydata : e2->refname); |
298 | } | |
299 | ||
300 | static struct refname_hash_entry *refname_hash_add(struct hashmap *map, | |
301 | const char *refname, | |
302 | const struct object_id *oid) | |
303 | { | |
304 | struct refname_hash_entry *ent; | |
305 | size_t len = strlen(refname); | |
306 | ||
307 | FLEX_ALLOC_MEM(ent, refname, refname, len); | |
d22245a2 | 308 | hashmap_entry_init(&ent->ent, strhash(refname)); |
e198b3a7 | 309 | oidcpy(&ent->oid, oid); |
b94e5c1d | 310 | hashmap_add(map, &ent->ent); |
e198b3a7 JH |
311 | return ent; |
312 | } | |
313 | ||
314 | static int add_one_refname(const char *refname, | |
315 | const struct object_id *oid, | |
316 | int flag, void *cbdata) | |
317 | { | |
318 | struct hashmap *refname_map = cbdata; | |
319 | ||
320 | (void) refname_hash_add(refname_map, refname, oid); | |
321 | return 0; | |
322 | } | |
323 | ||
324 | static void refname_hash_init(struct hashmap *map) | |
325 | { | |
326 | hashmap_init(map, refname_hash_entry_cmp, NULL, 0); | |
327 | } | |
328 | ||
329 | static int refname_hash_exists(struct hashmap *map, const char *refname) | |
330 | { | |
331 | return !!hashmap_get_from_hash(map, strhash(refname), refname); | |
332 | } | |
333 | ||
a8363b57 FC |
334 | static void clear_item(struct refname_hash_entry *item) |
335 | { | |
f80d9223 | 336 | item->ignore = 1; |
a8363b57 FC |
337 | } |
338 | ||
6d1700d5 BW |
339 | static void find_non_local_tags(const struct ref *refs, |
340 | struct ref **head, | |
341 | struct ref ***tail) | |
a0fbb5a3 | 342 | { |
e198b3a7 JH |
343 | struct hashmap existing_refs; |
344 | struct hashmap remote_refs; | |
b7e2d8bc | 345 | struct oidset fetch_oids = OIDSET_INIT; |
e198b3a7 JH |
346 | struct string_list remote_refs_list = STRING_LIST_INIT_NODUP; |
347 | struct string_list_item *remote_ref_item; | |
a0fbb5a3 | 348 | const struct ref *ref; |
e198b3a7 | 349 | struct refname_hash_entry *item = NULL; |
3e96c668 | 350 | const int quick_flags = OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT; |
e198b3a7 JH |
351 | |
352 | refname_hash_init(&existing_refs); | |
353 | refname_hash_init(&remote_refs); | |
b7e2d8bc | 354 | create_fetch_oidset(head, &fetch_oids); |
a0fbb5a3 | 355 | |
e198b3a7 | 356 | for_each_ref(add_one_refname, &existing_refs); |
6d1700d5 | 357 | for (ref = refs; ref; ref = ref->next) { |
ad704485 | 358 | if (!starts_with(ref->name, "refs/tags/")) |
a0fbb5a3 MH |
359 | continue; |
360 | ||
361 | /* | |
362 | * The peeled ref always follows the matching base | |
363 | * ref, so if we see a peeled ref that we don't want | |
364 | * to fetch then we can mark the ref entry in the list | |
365 | * as one to ignore by setting util to NULL. | |
366 | */ | |
ad704485 | 367 | if (ends_with(ref->name, "^{}")) { |
5827a035 | 368 | if (item && |
3e96c668 | 369 | !has_object_file_with_flags(&ref->old_oid, quick_flags) && |
b7e2d8bc | 370 | !oidset_contains(&fetch_oids, &ref->old_oid) && |
3e96c668 | 371 | !has_object_file_with_flags(&item->oid, quick_flags) && |
b7e2d8bc | 372 | !oidset_contains(&fetch_oids, &item->oid)) |
a8363b57 | 373 | clear_item(item); |
a0fbb5a3 MH |
374 | item = NULL; |
375 | continue; | |
376 | } | |
377 | ||
378 | /* | |
379 | * If item is non-NULL here, then we previously saw a | |
380 | * ref not followed by a peeled reference, so we need | |
381 | * to check if it is a lightweight tag that we want to | |
382 | * fetch. | |
383 | */ | |
5827a035 | 384 | if (item && |
3e96c668 | 385 | !has_object_file_with_flags(&item->oid, quick_flags) && |
b7e2d8bc | 386 | !oidset_contains(&fetch_oids, &item->oid)) |
a8363b57 | 387 | clear_item(item); |
a0fbb5a3 MH |
388 | |
389 | item = NULL; | |
390 | ||
391 | /* skip duplicates and refs that we already have */ | |
e198b3a7 JH |
392 | if (refname_hash_exists(&remote_refs, ref->name) || |
393 | refname_hash_exists(&existing_refs, ref->name)) | |
a0fbb5a3 MH |
394 | continue; |
395 | ||
e198b3a7 JH |
396 | item = refname_hash_add(&remote_refs, ref->name, &ref->old_oid); |
397 | string_list_insert(&remote_refs_list, ref->name); | |
a0fbb5a3 | 398 | } |
6da1a258 | 399 | hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent); |
a0fbb5a3 MH |
400 | |
401 | /* | |
402 | * We may have a final lightweight tag that needs to be | |
403 | * checked to see if it needs fetching. | |
404 | */ | |
5827a035 | 405 | if (item && |
3e96c668 | 406 | !has_object_file_with_flags(&item->oid, quick_flags) && |
b7e2d8bc | 407 | !oidset_contains(&fetch_oids, &item->oid)) |
a8363b57 | 408 | clear_item(item); |
a0fbb5a3 MH |
409 | |
410 | /* | |
e198b3a7 | 411 | * For all the tags in the remote_refs_list, |
a0fbb5a3 MH |
412 | * add them to the list of refs to be fetched |
413 | */ | |
e198b3a7 JH |
414 | for_each_string_list_item(remote_ref_item, &remote_refs_list) { |
415 | const char *refname = remote_ref_item->string; | |
9528b80b | 416 | struct ref *rm; |
f23a4651 | 417 | unsigned int hash = strhash(refname); |
e198b3a7 | 418 | |
f23a4651 EW |
419 | item = hashmap_get_entry_from_hash(&remote_refs, hash, refname, |
420 | struct refname_hash_entry, ent); | |
e198b3a7 JH |
421 | if (!item) |
422 | BUG("unseen remote ref?"); | |
423 | ||
a0fbb5a3 | 424 | /* Unless we have already decided to ignore this item... */ |
f80d9223 | 425 | if (item->ignore) |
9528b80b FC |
426 | continue; |
427 | ||
428 | rm = alloc_ref(item->refname); | |
429 | rm->peer_ref = alloc_ref(item->refname); | |
430 | oidcpy(&rm->old_oid, &item->oid); | |
431 | **tail = rm; | |
432 | *tail = &rm->next; | |
a0fbb5a3 | 433 | } |
6da1a258 | 434 | hashmap_clear_and_free(&remote_refs, struct refname_hash_entry, ent); |
e198b3a7 | 435 | string_list_clear(&remote_refs_list, 0); |
b7e2d8bc | 436 | oidset_clear(&fetch_oids); |
a0fbb5a3 | 437 | } |
767f176a | 438 | |
6d1700d5 BW |
439 | static struct ref *get_ref_map(struct remote *remote, |
440 | const struct ref *remote_refs, | |
65d96c8b | 441 | struct refspec *rs, |
f137a45e | 442 | int tags, int *autotags) |
b888d61c DB |
443 | { |
444 | int i; | |
445 | struct ref *rm; | |
446 | struct ref *ref_map = NULL; | |
447 | struct ref **tail = &ref_map; | |
448 | ||
c5a84e92 MH |
449 | /* opportunistically-updated references: */ |
450 | struct ref *orefs = NULL, **oref_tail = &orefs; | |
b888d61c | 451 | |
e198b3a7 | 452 | struct hashmap existing_refs; |
abcb7eeb | 453 | int existing_refs_populated = 0; |
f2690487 | 454 | |
65d96c8b | 455 | if (rs->nr) { |
c5558f80 | 456 | struct refspec *fetch_refspec; |
c5558f80 | 457 | |
65d96c8b BW |
458 | for (i = 0; i < rs->nr; i++) { |
459 | get_fetch_map(remote_refs, &rs->items[i], &tail, 0); | |
460 | if (rs->items[i].dst && rs->items[i].dst[0]) | |
b888d61c DB |
461 | *autotags = 1; |
462 | } | |
0281c930 | 463 | /* Merge everything on the command line (but not --tags) */ |
b888d61c | 464 | for (rm = ref_map; rm; rm = rm->next) |
900f2814 | 465 | rm->fetch_head_status = FETCH_HEAD_MERGE; |
f2690487 JK |
466 | |
467 | /* | |
0281c930 MH |
468 | * For any refs that we happen to be fetching via |
469 | * command-line arguments, the destination ref might | |
470 | * have been missing or have been different than the | |
471 | * remote-tracking ref that would be derived from the | |
472 | * configured refspec. In these cases, we want to | |
473 | * take the opportunity to update their configured | |
474 | * remote-tracking reference. However, we do not want | |
475 | * to mention these entries in FETCH_HEAD at all, as | |
476 | * they would simply be duplicates of existing | |
477 | * entries, so we set them FETCH_HEAD_IGNORE below. | |
478 | * | |
479 | * We compute these entries now, based only on the | |
480 | * refspecs specified on the command line. But we add | |
481 | * them to the list following the refspecs resulting | |
482 | * from the tags option so that one of the latter, | |
483 | * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed | |
484 | * by ref_remove_duplicates() in favor of one of these | |
485 | * opportunistic entries with FETCH_HEAD_IGNORE. | |
f2690487 | 486 | */ |
65d96c8b BW |
487 | if (refmap.nr) |
488 | fetch_refspec = &refmap; | |
489 | else | |
6d1700d5 | 490 | fetch_refspec = &remote->fetch; |
c5558f80 | 491 | |
65d96c8b BW |
492 | for (i = 0; i < fetch_refspec->nr; i++) |
493 | get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1); | |
e4cffacc | 494 | } else if (refmap.nr) { |
c5558f80 | 495 | die("--refmap option is only meaningful with command-line refspec(s)."); |
b888d61c DB |
496 | } else { |
497 | /* Use the defaults */ | |
85682c19 SP |
498 | struct branch *branch = branch_get(NULL); |
499 | int has_merge = branch_has_merge_config(branch); | |
3ee1757b | 500 | if (remote && |
e5349abf | 501 | (remote->fetch.nr || |
f31dbdc7 | 502 | /* Note: has_merge implies non-NULL branch->remote_name */ |
3ee1757b | 503 | (has_merge && !strcmp(branch->remote_name, remote->name)))) { |
e5349abf BW |
504 | for (i = 0; i < remote->fetch.nr; i++) { |
505 | get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0); | |
506 | if (remote->fetch.items[i].dst && | |
507 | remote->fetch.items[i].dst[0]) | |
b888d61c | 508 | *autotags = 1; |
85682c19 | 509 | if (!i && !has_merge && ref_map && |
e5349abf | 510 | !remote->fetch.items[0].pattern) |
900f2814 | 511 | ref_map->fetch_head_status = FETCH_HEAD_MERGE; |
b888d61c | 512 | } |
da0204df JS |
513 | /* |
514 | * if the remote we're fetching from is the same | |
515 | * as given in branch.<name>.remote, we add the | |
516 | * ref given in branch.<name>.merge, too. | |
f31dbdc7 BC |
517 | * |
518 | * Note: has_merge implies non-NULL branch->remote_name | |
da0204df | 519 | */ |
9ad7c5ae JH |
520 | if (has_merge && |
521 | !strcmp(branch->remote_name, remote->name)) | |
85682c19 | 522 | add_merge_config(&ref_map, remote_refs, branch, &tail); |
b888d61c DB |
523 | } else { |
524 | ref_map = get_remote_ref(remote_refs, "HEAD"); | |
9ad7c5ae | 525 | if (!ref_map) |
bd4a51fc | 526 | die(_("Couldn't find remote ref HEAD")); |
900f2814 | 527 | ref_map->fetch_head_status = FETCH_HEAD_MERGE; |
5aaf7f2a | 528 | tail = &ref_map->next; |
b888d61c DB |
529 | } |
530 | } | |
0281c930 | 531 | |
c5a84e92 MH |
532 | if (tags == TAGS_SET) |
533 | /* also fetch all tags */ | |
534 | get_fetch_map(remote_refs, tag_refspec, &tail, 0); | |
535 | else if (tags == TAGS_DEFAULT && *autotags) | |
6d1700d5 | 536 | find_non_local_tags(remote_refs, &ref_map, &tail); |
0281c930 | 537 | |
c5a84e92 MH |
538 | /* Now append any refs to be updated opportunistically: */ |
539 | *tail = orefs; | |
540 | for (rm = orefs; rm; rm = rm->next) { | |
541 | rm->fetch_head_status = FETCH_HEAD_IGNORE; | |
542 | tail = &rm->next; | |
543 | } | |
544 | ||
c0192df6 JK |
545 | /* |
546 | * apply negative refspecs first, before we remove duplicates. This is | |
547 | * necessary as negative refspecs might remove an otherwise conflicting | |
548 | * duplicate. | |
549 | */ | |
550 | if (rs->nr) | |
551 | ref_map = apply_negative_refspecs(ref_map, rs); | |
552 | else | |
553 | ref_map = apply_negative_refspecs(ref_map, &remote->fetch); | |
554 | ||
14b8ced3 BW |
555 | ref_map = ref_remove_duplicates(ref_map); |
556 | ||
14b8ced3 BW |
557 | for (rm = ref_map; rm; rm = rm->next) { |
558 | if (rm->peer_ref) { | |
e198b3a7 JH |
559 | const char *refname = rm->peer_ref->name; |
560 | struct refname_hash_entry *peer_item; | |
f23a4651 | 561 | unsigned int hash = strhash(refname); |
e198b3a7 | 562 | |
abcb7eeb JT |
563 | if (!existing_refs_populated) { |
564 | refname_hash_init(&existing_refs); | |
565 | for_each_ref(add_one_refname, &existing_refs); | |
566 | existing_refs_populated = 1; | |
567 | } | |
568 | ||
f23a4651 EW |
569 | peer_item = hashmap_get_entry_from_hash(&existing_refs, |
570 | hash, refname, | |
571 | struct refname_hash_entry, ent); | |
14b8ced3 | 572 | if (peer_item) { |
e198b3a7 | 573 | struct object_id *old_oid = &peer_item->oid; |
14b8ced3 BW |
574 | oidcpy(&rm->peer_ref->old_oid, old_oid); |
575 | } | |
576 | } | |
577 | } | |
abcb7eeb | 578 | if (existing_refs_populated) |
6da1a258 | 579 | hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent); |
14b8ced3 BW |
580 | |
581 | return ref_map; | |
b888d61c DB |
582 | } |
583 | ||
fa250759 JK |
584 | #define STORE_REF_ERROR_OTHER 1 |
585 | #define STORE_REF_ERROR_DF_CONFLICT 2 | |
586 | ||
b888d61c DB |
587 | static int s_update_ref(const char *action, |
588 | struct ref *ref, | |
d4c8db8f | 589 | struct ref_transaction *transaction, |
b888d61c DB |
590 | int check_old) |
591 | { | |
1412f762 | 592 | char *msg; |
b888d61c | 593 | char *rla = getenv("GIT_REFLOG_ACTION"); |
d4c8db8f | 594 | struct ref_transaction *our_transaction = NULL; |
cd94f765 | 595 | struct strbuf err = STRBUF_INIT; |
c45889f1 | 596 | int ret; |
b888d61c | 597 | |
28a15401 JS |
598 | if (dry_run) |
599 | return 0; | |
b888d61c | 600 | if (!rla) |
2d324efa | 601 | rla = default_rla.buf; |
1412f762 | 602 | msg = xstrfmt("%s: %s", rla, action); |
cd94f765 | 603 | |
d4c8db8f PS |
604 | /* |
605 | * If no transaction was passed to us, we manage the transaction | |
606 | * ourselves. Otherwise, we trust the caller to handle the transaction | |
607 | * lifecycle. | |
608 | */ | |
c45889f1 | 609 | if (!transaction) { |
d4c8db8f PS |
610 | transaction = our_transaction = ref_transaction_begin(&err); |
611 | if (!transaction) { | |
612 | ret = STORE_REF_ERROR_OTHER; | |
613 | goto out; | |
614 | } | |
c45889f1 PS |
615 | } |
616 | ||
617 | ret = ref_transaction_update(transaction, ref->name, &ref->new_oid, | |
618 | check_old ? &ref->old_oid : NULL, | |
619 | 0, msg, &err); | |
cd94f765 | 620 | if (ret) { |
c45889f1 PS |
621 | ret = STORE_REF_ERROR_OTHER; |
622 | goto out; | |
cd94f765 RS |
623 | } |
624 | ||
d4c8db8f PS |
625 | if (our_transaction) { |
626 | switch (ref_transaction_commit(our_transaction, &err)) { | |
627 | case 0: | |
628 | break; | |
629 | case TRANSACTION_NAME_CONFLICT: | |
630 | ret = STORE_REF_ERROR_DF_CONFLICT; | |
631 | goto out; | |
632 | default: | |
633 | ret = STORE_REF_ERROR_OTHER; | |
634 | goto out; | |
635 | } | |
c45889f1 PS |
636 | } |
637 | ||
638 | out: | |
d4c8db8f | 639 | ref_transaction_free(our_transaction); |
c45889f1 PS |
640 | if (ret) |
641 | error("%s", err.buf); | |
cd94f765 | 642 | strbuf_release(&err); |
1412f762 | 643 | free(msg); |
c45889f1 | 644 | return ret; |
b888d61c DB |
645 | } |
646 | ||
6bc91f23 | 647 | static int refcol_width = 10; |
bc437d10 | 648 | static int compact_format; |
6bc91f23 NTND |
649 | |
650 | static void adjust_refcol_width(const struct ref *ref) | |
651 | { | |
652 | int max, rlen, llen, len; | |
653 | ||
654 | /* uptodate lines are only shown on high verbosity level */ | |
4a7e27e9 | 655 | if (!verbosity && oideq(&ref->peer_ref->old_oid, &ref->old_oid)) |
6bc91f23 NTND |
656 | return; |
657 | ||
658 | max = term_columns(); | |
659 | rlen = utf8_strwidth(prettify_refname(ref->name)); | |
bc437d10 | 660 | |
6bc91f23 NTND |
661 | llen = utf8_strwidth(prettify_refname(ref->peer_ref->name)); |
662 | ||
663 | /* | |
664 | * rough estimation to see if the output line is too long and | |
665 | * should not be counted (we can't do precise calculation | |
666 | * anyway because we don't know if the error explanation part | |
667 | * will be printed in update_local_ref) | |
668 | */ | |
bc437d10 NTND |
669 | if (compact_format) { |
670 | llen = 0; | |
671 | max = max * 2 / 3; | |
672 | } | |
6bc91f23 NTND |
673 | len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen; |
674 | if (len >= max) | |
675 | return; | |
676 | ||
bc437d10 NTND |
677 | /* |
678 | * Not precise calculation for compact mode because '*' can | |
679 | * appear on the left hand side of '->' and shrink the column | |
680 | * back. | |
681 | */ | |
6bc91f23 NTND |
682 | if (refcol_width < rlen) |
683 | refcol_width = rlen; | |
684 | } | |
685 | ||
686 | static void prepare_format_display(struct ref *ref_map) | |
687 | { | |
688 | struct ref *rm; | |
bc437d10 NTND |
689 | const char *format = "full"; |
690 | ||
f1de981e | 691 | git_config_get_string_tmp("fetch.output", &format); |
bc437d10 NTND |
692 | if (!strcasecmp(format, "full")) |
693 | compact_format = 0; | |
694 | else if (!strcasecmp(format, "compact")) | |
695 | compact_format = 1; | |
696 | else | |
697 | die(_("configuration fetch.output contains invalid value %s"), | |
698 | format); | |
6bc91f23 NTND |
699 | |
700 | for (rm = ref_map; rm; rm = rm->next) { | |
701 | if (rm->status == REF_STATUS_REJECT_SHALLOW || | |
702 | !rm->peer_ref || | |
703 | !strcmp(rm->name, "HEAD")) | |
704 | continue; | |
705 | ||
706 | adjust_refcol_width(rm); | |
707 | } | |
708 | } | |
165f3902 | 709 | |
bc437d10 NTND |
710 | static void print_remote_to_local(struct strbuf *display, |
711 | const char *remote, const char *local) | |
712 | { | |
713 | strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local); | |
714 | } | |
715 | ||
716 | static int find_and_replace(struct strbuf *haystack, | |
717 | const char *needle, | |
718 | const char *placeholder) | |
719 | { | |
dc40b24d | 720 | const char *p = NULL; |
bc437d10 NTND |
721 | int plen, nlen; |
722 | ||
dc40b24d NTND |
723 | nlen = strlen(needle); |
724 | if (ends_with(haystack->buf, needle)) | |
725 | p = haystack->buf + haystack->len - nlen; | |
726 | else | |
727 | p = strstr(haystack->buf, needle); | |
bc437d10 NTND |
728 | if (!p) |
729 | return 0; | |
730 | ||
731 | if (p > haystack->buf && p[-1] != '/') | |
732 | return 0; | |
733 | ||
734 | plen = strlen(p); | |
bc437d10 NTND |
735 | if (plen > nlen && p[nlen] != '/') |
736 | return 0; | |
737 | ||
738 | strbuf_splice(haystack, p - haystack->buf, nlen, | |
739 | placeholder, strlen(placeholder)); | |
740 | return 1; | |
741 | } | |
742 | ||
743 | static void print_compact(struct strbuf *display, | |
744 | const char *remote, const char *local) | |
745 | { | |
746 | struct strbuf r = STRBUF_INIT; | |
747 | struct strbuf l = STRBUF_INIT; | |
748 | ||
749 | if (!strcmp(remote, local)) { | |
750 | strbuf_addf(display, "%-*s -> *", refcol_width, remote); | |
751 | return; | |
752 | } | |
753 | ||
754 | strbuf_addstr(&r, remote); | |
755 | strbuf_addstr(&l, local); | |
756 | ||
757 | if (!find_and_replace(&r, local, "*")) | |
758 | find_and_replace(&l, remote, "*"); | |
759 | print_remote_to_local(display, r.buf, l.buf); | |
760 | ||
761 | strbuf_release(&r); | |
762 | strbuf_release(&l); | |
763 | } | |
764 | ||
d0b39a03 NTND |
765 | static void format_display(struct strbuf *display, char code, |
766 | const char *summary, const char *error, | |
901f3d40 JH |
767 | const char *remote, const char *local, |
768 | int summary_width) | |
d0b39a03 | 769 | { |
901f3d40 JH |
770 | int width = (summary_width + strlen(summary) - gettext_width(summary)); |
771 | ||
772 | strbuf_addf(display, "%c %-*s ", code, width, summary); | |
bc437d10 NTND |
773 | if (!compact_format) |
774 | print_remote_to_local(display, remote, local); | |
775 | else | |
776 | print_compact(display, remote, local); | |
d0b39a03 NTND |
777 | if (error) |
778 | strbuf_addf(display, " (%s)", error); | |
779 | } | |
165f3902 | 780 | |
b888d61c | 781 | static int update_local_ref(struct ref *ref, |
d4c8db8f | 782 | struct ref_transaction *transaction, |
165f3902 | 783 | const char *remote, |
6da618d5 | 784 | const struct ref *remote_ref, |
901f3d40 JH |
785 | struct strbuf *display, |
786 | int summary_width) | |
b888d61c | 787 | { |
b888d61c DB |
788 | struct commit *current = NULL, *updated; |
789 | enum object_type type; | |
790 | struct branch *current_branch = branch_get(NULL); | |
4577e483 | 791 | const char *pretty_ref = prettify_refname(ref->name); |
377444b4 | 792 | int fast_forward = 0; |
b888d61c | 793 | |
0df8e965 | 794 | type = oid_object_info(the_repository, &ref->new_oid, NULL); |
b888d61c | 795 | if (type < 0) |
f4e54d02 | 796 | die(_("object %s not found"), oid_to_hex(&ref->new_oid)); |
b888d61c | 797 | |
4a7e27e9 | 798 | if (oideq(&ref->old_oid, &ref->new_oid)) { |
7f87aff2 | 799 | if (verbosity > 0) |
d0b39a03 | 800 | format_display(display, '=', _("[up to date]"), NULL, |
901f3d40 | 801 | remote, pretty_ref, summary_width); |
b888d61c DB |
802 | return 0; |
803 | } | |
804 | ||
b3abdd9d SP |
805 | if (current_branch && |
806 | !strcmp(ref->name, current_branch->name) && | |
b888d61c | 807 | !(update_head_ok || is_bare_repository()) && |
f4e54d02 | 808 | !is_null_oid(&ref->old_oid)) { |
b888d61c DB |
809 | /* |
810 | * If this is the head, and it's not okay to update | |
811 | * the head, and the old value of the head isn't empty... | |
812 | */ | |
d0b39a03 NTND |
813 | format_display(display, '!', _("[rejected]"), |
814 | _("can't fetch in current branch"), | |
901f3d40 | 815 | remote, pretty_ref, summary_width); |
b888d61c DB |
816 | return 1; |
817 | } | |
818 | ||
f4e54d02 | 819 | if (!is_null_oid(&ref->old_oid) && |
59556548 | 820 | starts_with(ref->name, "refs/tags/")) { |
0bc8d71b ÆAB |
821 | if (force || ref->force) { |
822 | int r; | |
d4c8db8f | 823 | r = s_update_ref("updating tag", ref, transaction, 0); |
0bc8d71b ÆAB |
824 | format_display(display, r ? '!' : 't', _("[tag update]"), |
825 | r ? _("unable to update local ref") : NULL, | |
826 | remote, pretty_ref, summary_width); | |
827 | return r; | |
828 | } else { | |
829 | format_display(display, '!', _("[rejected]"), _("would clobber existing tag"), | |
830 | remote, pretty_ref, summary_width); | |
831 | return 1; | |
832 | } | |
b888d61c DB |
833 | } |
834 | ||
21e1ee8f SB |
835 | current = lookup_commit_reference_gently(the_repository, |
836 | &ref->old_oid, 1); | |
837 | updated = lookup_commit_reference_gently(the_repository, | |
838 | &ref->new_oid, 1); | |
b888d61c | 839 | if (!current || !updated) { |
165f3902 NP |
840 | const char *msg; |
841 | const char *what; | |
6315472e | 842 | int r; |
0997adaa MB |
843 | /* |
844 | * Nicely describe the new ref we're fetching. | |
845 | * Base this on the remote's ref name, as it's | |
846 | * more likely to follow a standard layout. | |
847 | */ | |
848 | const char *name = remote_ref ? remote_ref->name : ""; | |
59556548 | 849 | if (starts_with(name, "refs/tags/")) { |
b888d61c | 850 | msg = "storing tag"; |
f7b3742a | 851 | what = _("[new tag]"); |
59556548 | 852 | } else if (starts_with(name, "refs/heads/")) { |
b888d61c | 853 | msg = "storing head"; |
f7b3742a | 854 | what = _("[new branch]"); |
0997adaa MB |
855 | } else { |
856 | msg = "storing ref"; | |
857 | what = _("[new ref]"); | |
165f3902 NP |
858 | } |
859 | ||
d4c8db8f | 860 | r = s_update_ref(msg, ref, transaction, 0); |
d0b39a03 NTND |
861 | format_display(display, r ? '!' : '*', what, |
862 | r ? _("unable to update local ref") : NULL, | |
901f3d40 | 863 | remote, pretty_ref, summary_width); |
6315472e | 864 | return r; |
b888d61c DB |
865 | } |
866 | ||
377444b4 DS |
867 | if (fetch_show_forced_updates) { |
868 | uint64_t t_before = getnanotime(); | |
869 | fast_forward = in_merge_bases(current, updated); | |
870 | forced_updates_ms += (getnanotime() - t_before) / 1000000; | |
871 | } else { | |
872 | fast_forward = 1; | |
873 | } | |
874 | ||
875 | if (fast_forward) { | |
bd22d4ff | 876 | struct strbuf quickref = STRBUF_INIT; |
6315472e | 877 | int r; |
cdbd70c4 | 878 | |
30e677e0 | 879 | strbuf_add_unique_abbrev(&quickref, ¤t->object.oid, DEFAULT_ABBREV); |
bd22d4ff | 880 | strbuf_addstr(&quickref, ".."); |
30e677e0 | 881 | strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV); |
d4c8db8f | 882 | r = s_update_ref("fast-forward", ref, transaction, 1); |
d0b39a03 NTND |
883 | format_display(display, r ? '!' : ' ', quickref.buf, |
884 | r ? _("unable to update local ref") : NULL, | |
901f3d40 | 885 | remote, pretty_ref, summary_width); |
bd22d4ff | 886 | strbuf_release(&quickref); |
6315472e | 887 | return r; |
165f3902 | 888 | } else if (force || ref->force) { |
bd22d4ff | 889 | struct strbuf quickref = STRBUF_INIT; |
6315472e | 890 | int r; |
30e677e0 | 891 | strbuf_add_unique_abbrev(&quickref, ¤t->object.oid, DEFAULT_ABBREV); |
bd22d4ff | 892 | strbuf_addstr(&quickref, "..."); |
30e677e0 | 893 | strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV); |
d4c8db8f | 894 | r = s_update_ref("forced-update", ref, transaction, 1); |
d0b39a03 NTND |
895 | format_display(display, r ? '!' : '+', quickref.buf, |
896 | r ? _("unable to update local ref") : _("forced update"), | |
901f3d40 | 897 | remote, pretty_ref, summary_width); |
bd22d4ff | 898 | strbuf_release(&quickref); |
6315472e | 899 | return r; |
165f3902 | 900 | } else { |
d0b39a03 | 901 | format_display(display, '!', _("[rejected]"), _("non-fast-forward"), |
901f3d40 | 902 | remote, pretty_ref, summary_width); |
b888d61c DB |
903 | return 1; |
904 | } | |
b888d61c DB |
905 | } |
906 | ||
6ccac9ee | 907 | static int iterate_ref_map(void *cb_data, struct object_id *oid) |
6b67e0dc | 908 | { |
f0e278b1 JH |
909 | struct ref **rm = cb_data; |
910 | struct ref *ref = *rm; | |
6b67e0dc | 911 | |
4820a33b NTND |
912 | while (ref && ref->status == REF_STATUS_REJECT_SHALLOW) |
913 | ref = ref->next; | |
f0e278b1 JH |
914 | if (!ref) |
915 | return -1; /* end of the list */ | |
916 | *rm = ref->next; | |
6ccac9ee | 917 | oidcpy(oid, &ref->old_oid); |
f0e278b1 | 918 | return 0; |
6b67e0dc JH |
919 | } |
920 | ||
58a646a3 PS |
921 | struct fetch_head { |
922 | FILE *fp; | |
929d0445 | 923 | struct strbuf buf; |
58a646a3 PS |
924 | }; |
925 | ||
926 | static int open_fetch_head(struct fetch_head *fetch_head) | |
927 | { | |
928 | const char *filename = git_path_fetch_head(the_repository); | |
929 | ||
930 | if (write_fetch_head) { | |
931 | fetch_head->fp = fopen(filename, "a"); | |
932 | if (!fetch_head->fp) | |
933 | return error_errno(_("cannot open %s"), filename); | |
929d0445 | 934 | strbuf_init(&fetch_head->buf, 0); |
58a646a3 PS |
935 | } else { |
936 | fetch_head->fp = NULL; | |
937 | } | |
938 | ||
939 | return 0; | |
940 | } | |
941 | ||
942 | static void append_fetch_head(struct fetch_head *fetch_head, | |
943 | const struct object_id *old_oid, | |
944 | enum fetch_head_status fetch_head_status, | |
945 | const char *note, | |
946 | const char *url, size_t url_len) | |
947 | { | |
948 | char old_oid_hex[GIT_MAX_HEXSZ + 1]; | |
949 | const char *merge_status_marker; | |
950 | size_t i; | |
951 | ||
952 | if (!fetch_head->fp) | |
953 | return; | |
954 | ||
955 | switch (fetch_head_status) { | |
956 | case FETCH_HEAD_NOT_FOR_MERGE: | |
957 | merge_status_marker = "not-for-merge"; | |
958 | break; | |
959 | case FETCH_HEAD_MERGE: | |
960 | merge_status_marker = ""; | |
961 | break; | |
962 | default: | |
963 | /* do not write anything to FETCH_HEAD */ | |
964 | return; | |
965 | } | |
966 | ||
929d0445 PS |
967 | strbuf_addf(&fetch_head->buf, "%s\t%s\t%s", |
968 | oid_to_hex_r(old_oid_hex, old_oid), merge_status_marker, note); | |
58a646a3 PS |
969 | for (i = 0; i < url_len; ++i) |
970 | if ('\n' == url[i]) | |
929d0445 | 971 | strbuf_addstr(&fetch_head->buf, "\\n"); |
58a646a3 | 972 | else |
929d0445 PS |
973 | strbuf_addch(&fetch_head->buf, url[i]); |
974 | strbuf_addch(&fetch_head->buf, '\n'); | |
975 | ||
c7b190da PS |
976 | /* |
977 | * When using an atomic fetch, we do not want to update FETCH_HEAD if | |
978 | * any of the reference updates fails. We thus have to write all | |
979 | * updates to a buffer first and only commit it as soon as all | |
980 | * references have been successfully updated. | |
981 | */ | |
982 | if (!atomic_fetch) { | |
983 | strbuf_write(&fetch_head->buf, fetch_head->fp); | |
984 | strbuf_reset(&fetch_head->buf); | |
985 | } | |
58a646a3 PS |
986 | } |
987 | ||
988 | static void commit_fetch_head(struct fetch_head *fetch_head) | |
989 | { | |
c7b190da PS |
990 | if (!fetch_head->fp || !atomic_fetch) |
991 | return; | |
992 | strbuf_write(&fetch_head->buf, fetch_head->fp); | |
58a646a3 PS |
993 | } |
994 | ||
995 | static void close_fetch_head(struct fetch_head *fetch_head) | |
996 | { | |
997 | if (!fetch_head->fp) | |
998 | return; | |
999 | ||
1000 | fclose(fetch_head->fp); | |
929d0445 | 1001 | strbuf_release(&fetch_head->buf); |
58a646a3 PS |
1002 | } |
1003 | ||
182f59da JNA |
1004 | static const char warn_show_forced_updates[] = |
1005 | N_("Fetch normally indicates which branches had a forced update,\n" | |
1006 | "but that check has been disabled. To re-enable, use '--show-forced-updates'\n" | |
1007 | "flag or run 'git config fetch.showForcedUpdates true'."); | |
1008 | static const char warn_time_show_forced_updates[] = | |
1009 | N_("It took %.2f seconds to check forced updates. You can use\n" | |
1010 | "'--no-show-forced-updates' or run 'git config fetch.showForcedUpdates false'\n" | |
1011 | " to avoid this check.\n"); | |
1012 | ||
47abd85b | 1013 | static int store_updated_refs(const char *raw_url, const char *remote_name, |
cf1e7c07 | 1014 | int connectivity_checked, struct ref *ref_map) |
b888d61c | 1015 | { |
58a646a3 | 1016 | struct fetch_head fetch_head; |
b888d61c | 1017 | struct commit *commit; |
4b3b33a7 | 1018 | int url_len, i, rc = 0; |
c7b190da PS |
1019 | struct strbuf note = STRBUF_INIT, err = STRBUF_INIT; |
1020 | struct ref_transaction *transaction = NULL; | |
b888d61c DB |
1021 | const char *what, *kind; |
1022 | struct ref *rm; | |
dcf69262 | 1023 | char *url; |
900f2814 | 1024 | int want_status; |
11fd66de | 1025 | int summary_width = transport_summary_width(ref_map); |
b888d61c | 1026 | |
58a646a3 PS |
1027 | rc = open_fetch_head(&fetch_head); |
1028 | if (rc) | |
1029 | return -1; | |
47abd85b | 1030 | |
fb0cc87e DB |
1031 | if (raw_url) |
1032 | url = transport_anonymize_url(raw_url); | |
1033 | else | |
1034 | url = xstrdup("foreign"); | |
6b67e0dc | 1035 | |
cf1e7c07 | 1036 | if (!connectivity_checked) { |
2df1aa23 JT |
1037 | struct check_connected_options opt = CHECK_CONNECTED_INIT; |
1038 | ||
cf1e7c07 | 1039 | rm = ref_map; |
2df1aa23 | 1040 | if (check_connected(iterate_ref_map, &rm, &opt)) { |
cf1e7c07 JT |
1041 | rc = error(_("%s did not send all necessary objects\n"), url); |
1042 | goto abort; | |
1043 | } | |
9516a598 | 1044 | } |
6b67e0dc | 1045 | |
c7b190da PS |
1046 | if (atomic_fetch) { |
1047 | transaction = ref_transaction_begin(&err); | |
1048 | if (!transaction) { | |
1049 | error("%s", err.buf); | |
1050 | goto abort; | |
1051 | } | |
1052 | } | |
1053 | ||
6bc91f23 NTND |
1054 | prepare_format_display(ref_map); |
1055 | ||
96890f4c | 1056 | /* |
900f2814 JK |
1057 | * We do a pass for each fetch_head_status type in their enum order, so |
1058 | * merged entries are written before not-for-merge. That lets readers | |
1059 | * use FETCH_HEAD as a refname to refer to the ref to be merged. | |
96890f4c | 1060 | */ |
900f2814 JK |
1061 | for (want_status = FETCH_HEAD_MERGE; |
1062 | want_status <= FETCH_HEAD_IGNORE; | |
1063 | want_status++) { | |
96890f4c JH |
1064 | for (rm = ref_map; rm; rm = rm->next) { |
1065 | struct ref *ref = NULL; | |
1066 | ||
4820a33b NTND |
1067 | if (rm->status == REF_STATUS_REJECT_SHALLOW) { |
1068 | if (want_status == FETCH_HEAD_MERGE) | |
1069 | warning(_("reject %s because shallow roots are not allowed to be updated"), | |
1070 | rm->peer_ref ? rm->peer_ref->name : rm->name); | |
1071 | continue; | |
1072 | } | |
1073 | ||
21e1ee8f SB |
1074 | commit = lookup_commit_reference_gently(the_repository, |
1075 | &rm->old_oid, | |
bc83266a | 1076 | 1); |
96890f4c | 1077 | if (!commit) |
900f2814 | 1078 | rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE; |
96890f4c | 1079 | |
900f2814 | 1080 | if (rm->fetch_head_status != want_status) |
96890f4c JH |
1081 | continue; |
1082 | ||
1083 | if (rm->peer_ref) { | |
6f687c21 | 1084 | ref = alloc_ref(rm->peer_ref->name); |
f4e54d02 | 1085 | oidcpy(&ref->old_oid, &rm->peer_ref->old_oid); |
1086 | oidcpy(&ref->new_oid, &rm->old_oid); | |
96890f4c JH |
1087 | ref->force = rm->peer_ref->force; |
1088 | } | |
b888d61c | 1089 | |
7ea0c2f4 OS |
1090 | if (recurse_submodules != RECURSE_SUBMODULES_OFF && |
1091 | (!rm->peer_ref || !oideq(&ref->old_oid, &ref->new_oid))) { | |
be76c212 | 1092 | check_for_new_submodule_commits(&rm->old_oid); |
7ea0c2f4 | 1093 | } |
b888d61c | 1094 | |
96890f4c JH |
1095 | if (!strcmp(rm->name, "HEAD")) { |
1096 | kind = ""; | |
1097 | what = ""; | |
1098 | } | |
a6293f5d | 1099 | else if (skip_prefix(rm->name, "refs/heads/", &what)) |
96890f4c | 1100 | kind = "branch"; |
a6293f5d | 1101 | else if (skip_prefix(rm->name, "refs/tags/", &what)) |
96890f4c | 1102 | kind = "tag"; |
a6293f5d | 1103 | else if (skip_prefix(rm->name, "refs/remotes/", &what)) |
96890f4c | 1104 | kind = "remote-tracking branch"; |
96890f4c JH |
1105 | else { |
1106 | kind = ""; | |
1107 | what = rm->name; | |
1108 | } | |
b888d61c | 1109 | |
96890f4c JH |
1110 | url_len = strlen(url); |
1111 | for (i = url_len - 1; url[i] == '/' && 0 <= i; i--) | |
1112 | ; | |
1113 | url_len = i + 1; | |
1114 | if (4 < i && !strncmp(".git", url + i - 3, 4)) | |
1115 | url_len = i - 3; | |
1116 | ||
1117 | strbuf_reset(¬e); | |
1118 | if (*what) { | |
1119 | if (*kind) | |
1120 | strbuf_addf(¬e, "%s ", kind); | |
1121 | strbuf_addf(¬e, "'%s' of ", what); | |
1122 | } | |
58a646a3 PS |
1123 | |
1124 | append_fetch_head(&fetch_head, &rm->old_oid, | |
1125 | rm->fetch_head_status, | |
1126 | note.buf, url, url_len); | |
96890f4c JH |
1127 | |
1128 | strbuf_reset(¬e); | |
1129 | if (ref) { | |
c7b190da | 1130 | rc |= update_local_ref(ref, transaction, what, |
d4c8db8f | 1131 | rm, ¬e, summary_width); |
96890f4c | 1132 | free(ref); |
db3c293e JT |
1133 | } else if (write_fetch_head || dry_run) { |
1134 | /* | |
1135 | * Display fetches written to FETCH_HEAD (or | |
1136 | * would be written to FETCH_HEAD, if --dry-run | |
1137 | * is set). | |
1138 | */ | |
d0b39a03 NTND |
1139 | format_display(¬e, '*', |
1140 | *kind ? kind : "branch", NULL, | |
1141 | *what ? what : "HEAD", | |
901f3d40 | 1142 | "FETCH_HEAD", summary_width); |
db3c293e | 1143 | } |
96890f4c JH |
1144 | if (note.len) { |
1145 | if (verbosity >= 0 && !shown_url) { | |
1146 | fprintf(stderr, _("From %.*s\n"), | |
1147 | url_len, url); | |
1148 | shown_url = 1; | |
1149 | } | |
1150 | if (verbosity >= 0) | |
1151 | fprintf(stderr, " %s\n", note.buf); | |
165f3902 NP |
1152 | } |
1153 | } | |
b888d61c | 1154 | } |
9516a598 | 1155 | |
c7b190da PS |
1156 | if (!rc && transaction) { |
1157 | rc = ref_transaction_commit(transaction, &err); | |
1158 | if (rc) { | |
1159 | error("%s", err.buf); | |
1160 | goto abort; | |
1161 | } | |
1162 | } | |
1163 | ||
58a646a3 PS |
1164 | if (!rc) |
1165 | commit_fetch_head(&fetch_head); | |
1166 | ||
fa250759 | 1167 | if (rc & STORE_REF_ERROR_DF_CONFLICT) |
bd4a51fc | 1168 | error(_("some local refs could not be updated; try running\n" |
f3cb169b | 1169 | " 'git remote prune %s' to remove any old, conflicting " |
bd4a51fc | 1170 | "branches"), remote_name); |
9516a598 | 1171 | |
377444b4 DS |
1172 | if (advice_fetch_show_forced_updates) { |
1173 | if (!fetch_show_forced_updates) { | |
182f59da | 1174 | warning(_(warn_show_forced_updates)); |
377444b4 | 1175 | } else if (forced_updates_ms > FORCED_UPDATES_DELAY_WARNING_IN_MS) { |
182f59da | 1176 | warning(_(warn_time_show_forced_updates), |
377444b4 | 1177 | forced_updates_ms / 1000.0); |
377444b4 DS |
1178 | } |
1179 | } | |
1180 | ||
9516a598 | 1181 | abort: |
5914f2d0 | 1182 | strbuf_release(¬e); |
c7b190da PS |
1183 | strbuf_release(&err); |
1184 | ref_transaction_free(transaction); | |
9516a598 | 1185 | free(url); |
58a646a3 | 1186 | close_fetch_head(&fetch_head); |
efb98b44 | 1187 | return rc; |
b888d61c DB |
1188 | } |
1189 | ||
4191c356 SP |
1190 | /* |
1191 | * We would want to bypass the object transfer altogether if | |
d9eb0205 | 1192 | * everything we are going to fetch already exists and is connected |
4191c356 | 1193 | * locally. |
4191c356 | 1194 | */ |
35f9e3e5 | 1195 | static int check_exist_and_connected(struct ref *ref_map) |
4191c356 | 1196 | { |
f0e278b1 | 1197 | struct ref *rm = ref_map; |
7043c707 | 1198 | struct check_connected_options opt = CHECK_CONNECTED_INIT; |
35f9e3e5 | 1199 | struct ref *r; |
f0e278b1 | 1200 | |
4191c356 SP |
1201 | /* |
1202 | * If we are deepening a shallow clone we already have these | |
1203 | * objects reachable. Running rev-list here will return with | |
1204 | * a good (0) exit status and we'll bypass the fetch that we | |
1205 | * really need to perform. Claiming failure now will ensure | |
1206 | * we perform the network exchange to deepen our history. | |
1207 | */ | |
508ea882 | 1208 | if (deepen) |
4191c356 | 1209 | return -1; |
35f9e3e5 JT |
1210 | |
1211 | /* | |
1212 | * check_connected() allows objects to merely be promised, but | |
1213 | * we need all direct targets to exist. | |
1214 | */ | |
1215 | for (r = rm; r; r = r->next) { | |
6462d5eb JT |
1216 | if (!has_object_file_with_flags(&r->old_oid, |
1217 | OBJECT_INFO_SKIP_FETCH_OBJECT)) | |
35f9e3e5 JT |
1218 | return -1; |
1219 | } | |
1220 | ||
7043c707 JK |
1221 | opt.quiet = 1; |
1222 | return check_connected(iterate_ref_map, &rm, &opt); | |
4191c356 SP |
1223 | } |
1224 | ||
e2842b39 | 1225 | static int fetch_refs(struct transport *transport, struct ref *ref_map) |
b888d61c | 1226 | { |
35f9e3e5 | 1227 | int ret = check_exist_and_connected(ref_map); |
5fc31180 JS |
1228 | if (ret) { |
1229 | trace2_region_enter("fetch", "fetch_refs", the_repository); | |
e2842b39 | 1230 | ret = transport_fetch_refs(transport, ref_map); |
5fc31180 JS |
1231 | trace2_region_leave("fetch", "fetch_refs", the_repository); |
1232 | } | |
b888d61c | 1233 | if (!ret) |
05c44226 BW |
1234 | /* |
1235 | * Keep the new pack's ".keep" file around to allow the caller | |
1236 | * time to update refs to reference the new objects. | |
1237 | */ | |
1238 | return 0; | |
1239 | transport_unlock_pack(transport); | |
1240 | return ret; | |
1241 | } | |
1242 | ||
1243 | /* Update local refs based on the ref values fetched from a remote */ | |
1244 | static int consume_refs(struct transport *transport, struct ref *ref_map) | |
1245 | { | |
cf1e7c07 JT |
1246 | int connectivity_checked = transport->smart_options |
1247 | ? transport->smart_options->connectivity_checked : 0; | |
5fc31180 JS |
1248 | int ret; |
1249 | trace2_region_enter("fetch", "consume_refs", the_repository); | |
1250 | ret = store_updated_refs(transport->url, | |
1251 | transport->remote->name, | |
1252 | connectivity_checked, | |
1253 | ref_map); | |
1788c39c | 1254 | transport_unlock_pack(transport); |
5fc31180 | 1255 | trace2_region_leave("fetch", "consume_refs", the_repository); |
b888d61c DB |
1256 | return ret; |
1257 | } | |
1258 | ||
def11e71 BW |
1259 | static int prune_refs(struct refspec *rs, struct ref *ref_map, |
1260 | const char *raw_url) | |
f360d844 | 1261 | { |
4b3b33a7 | 1262 | int url_len, i, result = 0; |
a2ac50cb | 1263 | struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map); |
4b3b33a7 | 1264 | char *url; |
11fd66de | 1265 | int summary_width = transport_summary_width(stale_refs); |
f360d844 | 1266 | const char *dangling_msg = dry_run |
18986d53 NTND |
1267 | ? _(" (%s will become dangling)") |
1268 | : _(" (%s has become dangling)"); | |
f360d844 | 1269 | |
4b3b33a7 TM |
1270 | if (raw_url) |
1271 | url = transport_anonymize_url(raw_url); | |
1272 | else | |
1273 | url = xstrdup("foreign"); | |
1274 | ||
1275 | url_len = strlen(url); | |
1276 | for (i = url_len - 1; url[i] == '/' && 0 <= i; i--) | |
1277 | ; | |
1278 | ||
1279 | url_len = i + 1; | |
1280 | if (4 < i && !strncmp(".git", url + i - 3, 4)) | |
1281 | url_len = i - 3; | |
1282 | ||
a087b432 MH |
1283 | if (!dry_run) { |
1284 | struct string_list refnames = STRING_LIST_INIT_NODUP; | |
1285 | ||
1286 | for (ref = stale_refs; ref; ref = ref->next) | |
1287 | string_list_append(&refnames, ref->name); | |
1288 | ||
64da4199 | 1289 | result = delete_refs("fetch: prune", &refnames, 0); |
a087b432 MH |
1290 | string_list_clear(&refnames, 0); |
1291 | } | |
1292 | ||
1293 | if (verbosity >= 0) { | |
1294 | for (ref = stale_refs; ref; ref = ref->next) { | |
d0b39a03 | 1295 | struct strbuf sb = STRBUF_INIT; |
a087b432 MH |
1296 | if (!shown_url) { |
1297 | fprintf(stderr, _("From %.*s\n"), url_len, url); | |
1298 | shown_url = 1; | |
1299 | } | |
2cb040ba | 1300 | format_display(&sb, '-', _("[deleted]"), NULL, |
901f3d40 JH |
1301 | _("(none)"), prettify_refname(ref->name), |
1302 | summary_width); | |
d0b39a03 NTND |
1303 | fprintf(stderr, " %s\n",sb.buf); |
1304 | strbuf_release(&sb); | |
f360d844 JS |
1305 | warn_dangling_symref(stderr, dangling_msg, ref->name); |
1306 | } | |
1307 | } | |
a087b432 | 1308 | |
4b3b33a7 | 1309 | free(url); |
f360d844 JS |
1310 | free_refs(stale_refs); |
1311 | return result; | |
1312 | } | |
1313 | ||
8ee5d731 JS |
1314 | static void check_not_current_branch(struct ref *ref_map) |
1315 | { | |
1316 | struct branch *current_branch = branch_get(NULL); | |
1317 | ||
1318 | if (is_bare_repository() || !current_branch) | |
1319 | return; | |
1320 | ||
1321 | for (; ref_map; ref_map = ref_map->next) | |
1322 | if (ref_map->peer_ref && !strcmp(current_branch->refname, | |
1323 | ref_map->peer_ref->name)) | |
bd4a51fc ÆAB |
1324 | die(_("Refusing to fetch into current branch %s " |
1325 | "of non-bare repository"), current_branch->refname); | |
8ee5d731 JS |
1326 | } |
1327 | ||
e6cc5104 JH |
1328 | static int truncate_fetch_head(void) |
1329 | { | |
102de880 | 1330 | const char *filename = git_path_fetch_head(the_repository); |
ea56518d | 1331 | FILE *fp = fopen_for_writing(filename); |
e6cc5104 JH |
1332 | |
1333 | if (!fp) | |
6da31d7f | 1334 | return error_errno(_("cannot open %s"), filename); |
e6cc5104 JH |
1335 | fclose(fp); |
1336 | return 0; | |
1337 | } | |
1338 | ||
db5723c6 JH |
1339 | static void set_option(struct transport *transport, const char *name, const char *value) |
1340 | { | |
1341 | int r = transport_set_option(transport, name, value); | |
1342 | if (r < 0) | |
1343 | die(_("Option \"%s\" value \"%s\" is not valid for %s"), | |
1344 | name, value, transport->url); | |
1345 | if (r > 0) | |
1346 | warning(_("Option \"%s\" is ignored for %s\n"), | |
1347 | name, transport->url); | |
1348 | } | |
1349 | ||
3390e42a JT |
1350 | |
1351 | static int add_oid(const char *refname, const struct object_id *oid, int flags, | |
1352 | void *cb_data) | |
1353 | { | |
1354 | struct oid_array *oids = cb_data; | |
1355 | ||
1356 | oid_array_append(oids, oid); | |
1357 | return 0; | |
1358 | } | |
1359 | ||
1360 | static void add_negotiation_tips(struct git_transport_options *smart_options) | |
1361 | { | |
1362 | struct oid_array *oids = xcalloc(1, sizeof(*oids)); | |
1363 | int i; | |
1364 | ||
1365 | for (i = 0; i < negotiation_tip.nr; i++) { | |
1366 | const char *s = negotiation_tip.items[i].string; | |
1367 | int old_nr; | |
1368 | if (!has_glob_specials(s)) { | |
1369 | struct object_id oid; | |
1370 | if (get_oid(s, &oid)) | |
1371 | die("%s is not a valid object", s); | |
1372 | oid_array_append(oids, &oid); | |
1373 | continue; | |
1374 | } | |
1375 | old_nr = oids->nr; | |
1376 | for_each_glob_ref(add_oid, s, oids); | |
1377 | if (old_nr == oids->nr) | |
1378 | warning("Ignoring --negotiation-tip=%s because it does not match any refs", | |
1379 | s); | |
1380 | } | |
1381 | smart_options->negotiation_tips = oids; | |
1382 | } | |
1383 | ||
508ea882 | 1384 | static struct transport *prepare_transport(struct remote *remote, int deepen) |
db5723c6 JH |
1385 | { |
1386 | struct transport *transport; | |
87c2d9d3 | 1387 | |
db5723c6 JH |
1388 | transport = transport_get(remote, NULL); |
1389 | transport_set_verbosity(transport, verbosity, progress); | |
c915f11e | 1390 | transport->family = family; |
db5723c6 JH |
1391 | if (upload_pack) |
1392 | set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack); | |
1393 | if (keep) | |
1394 | set_option(transport, TRANS_OPT_KEEP, "yes"); | |
1395 | if (depth) | |
1396 | set_option(transport, TRANS_OPT_DEPTH, depth); | |
508ea882 NTND |
1397 | if (deepen && deepen_since) |
1398 | set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since); | |
a45a2600 NTND |
1399 | if (deepen && deepen_not.nr) |
1400 | set_option(transport, TRANS_OPT_DEEPEN_NOT, | |
1401 | (const char *)&deepen_not); | |
cccf74e2 NTND |
1402 | if (deepen_relative) |
1403 | set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes"); | |
48d25cae NTND |
1404 | if (update_shallow) |
1405 | set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes"); | |
acb0c572 | 1406 | if (filter_options.choice) { |
cf9ceb5a MD |
1407 | const char *spec = |
1408 | expand_list_objects_filter_spec(&filter_options); | |
1409 | set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER, spec); | |
acb0c572 JH |
1410 | set_option(transport, TRANS_OPT_FROM_PROMISOR, "1"); |
1411 | } | |
3390e42a JT |
1412 | if (negotiation_tip.nr) { |
1413 | if (transport->smart_options) | |
1414 | add_negotiation_tips(transport->smart_options); | |
1415 | else | |
1416 | warning("Ignoring --negotiation-tip because the protocol does not support it."); | |
1417 | } | |
db5723c6 JH |
1418 | return transport; |
1419 | } | |
1420 | ||
069d5032 JH |
1421 | static void backfill_tags(struct transport *transport, struct ref *ref_map) |
1422 | { | |
508ea882 NTND |
1423 | int cannot_reuse; |
1424 | ||
1425 | /* | |
1426 | * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it | |
1427 | * when remote helper is used (setting it to an empty string | |
1428 | * is not unsetting). We could extend the remote helper | |
1429 | * protocol for that, but for now, just force a new connection | |
a45a2600 | 1430 | * without deepen-since. Similar story for deepen-not. |
508ea882 | 1431 | */ |
a45a2600 NTND |
1432 | cannot_reuse = transport->cannot_reuse || |
1433 | deepen_since || deepen_not.nr; | |
508ea882 NTND |
1434 | if (cannot_reuse) { |
1435 | gsecondary = prepare_transport(transport->remote, 0); | |
b26ed430 JH |
1436 | transport = gsecondary; |
1437 | } | |
1438 | ||
069d5032 JH |
1439 | transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL); |
1440 | transport_set_option(transport, TRANS_OPT_DEPTH, "0"); | |
cccf74e2 | 1441 | transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL); |
e2842b39 | 1442 | if (!fetch_refs(transport, ref_map)) |
05c44226 | 1443 | consume_refs(transport, ref_map); |
b26ed430 JH |
1444 | |
1445 | if (gsecondary) { | |
1446 | transport_disconnect(gsecondary); | |
1447 | gsecondary = NULL; | |
1448 | } | |
069d5032 JH |
1449 | } |
1450 | ||
b888d61c | 1451 | static int do_fetch(struct transport *transport, |
65a1301f | 1452 | struct refspec *rs) |
b888d61c | 1453 | { |
7f98428d | 1454 | struct ref *ref_map; |
b888d61c | 1455 | int autotags = (transport->remote->fetch_tags == 1); |
5b87d8d3 | 1456 | int retcode = 0; |
6d1700d5 | 1457 | const struct ref *remote_refs; |
39835409 JT |
1458 | struct transport_ls_refs_options transport_ls_refs_options = |
1459 | TRANSPORT_LS_REFS_OPTIONS_INIT; | |
e70a3030 | 1460 | int must_list_refs = 1; |
b1a01e1c | 1461 | |
ed368546 DJ |
1462 | if (tags == TAGS_DEFAULT) { |
1463 | if (transport->remote->fetch_tags == 2) | |
1464 | tags = TAGS_SET; | |
1465 | if (transport->remote->fetch_tags == -1) | |
1466 | tags = TAGS_UNSET; | |
1467 | } | |
b888d61c | 1468 | |
b888d61c | 1469 | /* if not appending, truncate FETCH_HEAD */ |
887952b8 | 1470 | if (!append && write_fetch_head) { |
5b87d8d3 MH |
1471 | retcode = truncate_fetch_head(); |
1472 | if (retcode) | |
1473 | goto cleanup; | |
d6617c7c | 1474 | } |
b888d61c | 1475 | |
e70a3030 JT |
1476 | if (rs->nr) { |
1477 | int i; | |
1478 | ||
39835409 | 1479 | refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes); |
e70a3030 JT |
1480 | |
1481 | /* | |
1482 | * We can avoid listing refs if all of them are exact | |
1483 | * OIDs | |
1484 | */ | |
1485 | must_list_refs = 0; | |
1486 | for (i = 0; i < rs->nr; i++) { | |
1487 | if (!rs->items[i].exact_sha1) { | |
1488 | must_list_refs = 1; | |
1489 | break; | |
1490 | } | |
1491 | } | |
1492 | } else if (transport->remote && transport->remote->fetch.nr) | |
39835409 JT |
1493 | refspec_ref_prefixes(&transport->remote->fetch, |
1494 | &transport_ls_refs_options.ref_prefixes); | |
b888d61c | 1495 | |
e70a3030 JT |
1496 | if (tags == TAGS_SET || tags == TAGS_DEFAULT) { |
1497 | must_list_refs = 1; | |
39835409 JT |
1498 | if (transport_ls_refs_options.ref_prefixes.nr) |
1499 | strvec_push(&transport_ls_refs_options.ref_prefixes, | |
1500 | "refs/tags/"); | |
b888d61c DB |
1501 | } |
1502 | ||
5fc31180 JS |
1503 | if (must_list_refs) { |
1504 | trace2_region_enter("fetch", "remote_refs", the_repository); | |
39835409 JT |
1505 | remote_refs = transport_get_remote_refs(transport, |
1506 | &transport_ls_refs_options); | |
5fc31180 JS |
1507 | trace2_region_leave("fetch", "remote_refs", the_repository); |
1508 | } else | |
e70a3030 JT |
1509 | remote_refs = NULL; |
1510 | ||
39835409 | 1511 | strvec_clear(&transport_ls_refs_options.ref_prefixes); |
6d1700d5 BW |
1512 | |
1513 | ref_map = get_ref_map(transport->remote, remote_refs, rs, | |
1514 | tags, &autotags); | |
8ee5d731 JS |
1515 | if (!update_head_ok) |
1516 | check_not_current_branch(ref_map); | |
b888d61c | 1517 | |
41fa7d2e SP |
1518 | if (tags == TAGS_DEFAULT && autotags) |
1519 | transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1"); | |
ed43de6e | 1520 | if (prune) { |
0838bf47 MH |
1521 | /* |
1522 | * We only prune based on refspecs specified | |
1523 | * explicitly (via command line or configuration); we | |
1524 | * don't care whether --tags was specified. | |
1525 | */ | |
65a1301f | 1526 | if (rs->nr) { |
def11e71 | 1527 | prune_refs(rs, ref_map, transport->url); |
e8c1e6c7 | 1528 | } else { |
def11e71 | 1529 | prune_refs(&transport->remote->fetch, |
4b3b33a7 TM |
1530 | ref_map, |
1531 | transport->url); | |
e8c1e6c7 | 1532 | } |
ed43de6e | 1533 | } |
e2842b39 | 1534 | if (fetch_refs(transport, ref_map) || consume_refs(transport, ref_map)) { |
10a6cc88 TM |
1535 | free_refs(ref_map); |
1536 | retcode = 1; | |
1537 | goto cleanup; | |
1538 | } | |
24bc1a12 CB |
1539 | |
1540 | if (set_upstream) { | |
1541 | struct branch *branch = branch_get("HEAD"); | |
1542 | struct ref *rm; | |
1543 | struct ref *source_ref = NULL; | |
1544 | ||
1545 | /* | |
1546 | * We're setting the upstream configuration for the | |
15beaaa3 | 1547 | * current branch. The relevant upstream is the |
24bc1a12 CB |
1548 | * fetched branch that is meant to be merged with the |
1549 | * current one, i.e. the one fetched to FETCH_HEAD. | |
1550 | * | |
1551 | * When there are several such branches, consider the | |
1552 | * request ambiguous and err on the safe side by doing | |
1553 | * nothing and just emit a warning. | |
1554 | */ | |
1555 | for (rm = ref_map; rm; rm = rm->next) { | |
1556 | if (!rm->peer_ref) { | |
1557 | if (source_ref) { | |
391c7e40 | 1558 | warning(_("multiple branches detected, incompatible with --set-upstream")); |
24bc1a12 CB |
1559 | goto skip; |
1560 | } else { | |
1561 | source_ref = rm; | |
1562 | } | |
1563 | } | |
1564 | } | |
1565 | if (source_ref) { | |
1566 | if (!strcmp(source_ref->name, "HEAD") || | |
1567 | starts_with(source_ref->name, "refs/heads/")) | |
1568 | install_branch_config(0, | |
1569 | branch->name, | |
1570 | transport->remote->name, | |
1571 | source_ref->name); | |
1572 | else if (starts_with(source_ref->name, "refs/remotes/")) | |
1573 | warning(_("not setting upstream for a remote remote-tracking branch")); | |
1574 | else if (starts_with(source_ref->name, "refs/tags/")) | |
1575 | warning(_("not setting upstream for a remote tag")); | |
1576 | else | |
1577 | warning(_("unknown branch type")); | |
1578 | } else { | |
1579 | warning(_("no source branch found.\n" | |
1580 | "you need to specify exactly one branch with the --set-upstream option.")); | |
1581 | } | |
1582 | } | |
1583 | skip: | |
7f98428d | 1584 | free_refs(ref_map); |
b888d61c DB |
1585 | |
1586 | /* if neither --no-tags nor --tags was specified, do automated tag | |
1587 | * following ... */ | |
83201998 | 1588 | if (tags == TAGS_DEFAULT && autotags) { |
c50b2b47 SP |
1589 | struct ref **tail = &ref_map; |
1590 | ref_map = NULL; | |
6d1700d5 | 1591 | find_non_local_tags(remote_refs, &ref_map, &tail); |
069d5032 JH |
1592 | if (ref_map) |
1593 | backfill_tags(transport, ref_map); | |
b888d61c DB |
1594 | free_refs(ref_map); |
1595 | } | |
1596 | ||
5b87d8d3 | 1597 | cleanup: |
5b87d8d3 | 1598 | return retcode; |
b888d61c DB |
1599 | } |
1600 | ||
9c4a036b BG |
1601 | static int get_one_remote_for_fetch(struct remote *remote, void *priv) |
1602 | { | |
1603 | struct string_list *list = priv; | |
7cc91a2f | 1604 | if (!remote->skip_default_update) |
1d2f80fa | 1605 | string_list_append(list, remote->name); |
9c4a036b BG |
1606 | return 0; |
1607 | } | |
1608 | ||
1609 | struct remote_group_data { | |
1610 | const char *name; | |
1611 | struct string_list *list; | |
1612 | }; | |
1613 | ||
1614 | static int get_remote_group(const char *key, const char *value, void *priv) | |
1615 | { | |
1616 | struct remote_group_data *g = priv; | |
1617 | ||
bc598c32 | 1618 | if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) { |
9c4a036b | 1619 | /* split list by white space */ |
9c4a036b | 1620 | while (*value) { |
5f65499f MH |
1621 | size_t wordlen = strcspn(value, " \t\n"); |
1622 | ||
e286542d | 1623 | if (wordlen >= 1) |
b7410f61 | 1624 | string_list_append_nodup(g->list, |
e286542d MH |
1625 | xstrndup(value, wordlen)); |
1626 | value += wordlen + (value[wordlen] != '\0'); | |
9c4a036b BG |
1627 | } |
1628 | } | |
1629 | ||
1630 | return 0; | |
1631 | } | |
1632 | ||
1633 | static int add_remote_or_group(const char *name, struct string_list *list) | |
1634 | { | |
1635 | int prev_nr = list->nr; | |
66dbfd55 GV |
1636 | struct remote_group_data g; |
1637 | g.name = name; g.list = list; | |
9c4a036b BG |
1638 | |
1639 | git_config(get_remote_group, &g); | |
1640 | if (list->nr == prev_nr) { | |
674468b3 | 1641 | struct remote *remote = remote_get(name); |
e459b073 | 1642 | if (!remote_is_configured(remote, 0)) |
9c4a036b | 1643 | return 0; |
1d2f80fa | 1644 | string_list_append(list, remote->name); |
9c4a036b BG |
1645 | } |
1646 | return 1; | |
1647 | } | |
1648 | ||
22f9b7f3 | 1649 | static void add_options_to_argv(struct strvec *argv) |
9c4a036b | 1650 | { |
28a15401 | 1651 | if (dry_run) |
22f9b7f3 | 1652 | strvec_push(argv, "--dry-run"); |
90765fa3 | 1653 | if (prune != -1) |
22f9b7f3 | 1654 | strvec_push(argv, prune ? "--prune" : "--no-prune"); |
97716d21 | 1655 | if (prune_tags != -1) |
22f9b7f3 | 1656 | strvec_push(argv, prune_tags ? "--prune-tags" : "--no-prune-tags"); |
bba5322a | 1657 | if (update_head_ok) |
22f9b7f3 | 1658 | strvec_push(argv, "--update-head-ok"); |
bba5322a | 1659 | if (force) |
22f9b7f3 | 1660 | strvec_push(argv, "--force"); |
bba5322a | 1661 | if (keep) |
22f9b7f3 | 1662 | strvec_push(argv, "--keep"); |
be254a0e | 1663 | if (recurse_submodules == RECURSE_SUBMODULES_ON) |
22f9b7f3 | 1664 | strvec_push(argv, "--recurse-submodules"); |
8f0700dd | 1665 | else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) |
22f9b7f3 | 1666 | strvec_push(argv, "--recurse-submodules=on-demand"); |
85566460 | 1667 | if (tags == TAGS_SET) |
22f9b7f3 | 1668 | strvec_push(argv, "--tags"); |
85566460 | 1669 | else if (tags == TAGS_UNSET) |
22f9b7f3 | 1670 | strvec_push(argv, "--no-tags"); |
9c4a036b | 1671 | if (verbosity >= 2) |
22f9b7f3 | 1672 | strvec_push(argv, "-v"); |
9c4a036b | 1673 | if (verbosity >= 1) |
22f9b7f3 | 1674 | strvec_push(argv, "-v"); |
9c4a036b | 1675 | else if (verbosity < 0) |
22f9b7f3 | 1676 | strvec_push(argv, "-q"); |
4e735c13 | 1677 | if (family == TRANSPORT_FAMILY_IPV4) |
6854689e | 1678 | strvec_push(argv, "--ipv4"); |
4e735c13 | 1679 | else if (family == TRANSPORT_FAMILY_IPV6) |
6854689e | 1680 | strvec_push(argv, "--ipv6"); |
7dce19d3 JL |
1681 | } |
1682 | ||
d54dea77 JS |
1683 | /* Fetch multiple remotes in parallel */ |
1684 | ||
1685 | struct parallel_fetch_state { | |
1686 | const char **argv; | |
1687 | struct string_list *remotes; | |
1688 | int next, result; | |
1689 | }; | |
1690 | ||
1691 | static int fetch_next_remote(struct child_process *cp, struct strbuf *out, | |
1692 | void *cb, void **task_cb) | |
1693 | { | |
1694 | struct parallel_fetch_state *state = cb; | |
1695 | char *remote; | |
1696 | ||
1697 | if (state->next < 0 || state->next >= state->remotes->nr) | |
1698 | return 0; | |
1699 | ||
1700 | remote = state->remotes->items[state->next++].string; | |
1701 | *task_cb = remote; | |
1702 | ||
22f9b7f3 JK |
1703 | strvec_pushv(&cp->args, state->argv); |
1704 | strvec_push(&cp->args, remote); | |
d54dea77 JS |
1705 | cp->git_cmd = 1; |
1706 | ||
1707 | if (verbosity >= 0) | |
1708 | printf(_("Fetching %s\n"), remote); | |
1709 | ||
1710 | return 1; | |
1711 | } | |
1712 | ||
1713 | static int fetch_failed_to_start(struct strbuf *out, void *cb, void *task_cb) | |
1714 | { | |
1715 | struct parallel_fetch_state *state = cb; | |
1716 | const char *remote = task_cb; | |
1717 | ||
1718 | state->result = error(_("Could not fetch %s"), remote); | |
1719 | ||
1720 | return 0; | |
1721 | } | |
1722 | ||
1723 | static int fetch_finished(int result, struct strbuf *out, | |
1724 | void *cb, void *task_cb) | |
1725 | { | |
1726 | struct parallel_fetch_state *state = cb; | |
1727 | const char *remote = task_cb; | |
1728 | ||
1729 | if (result) { | |
1730 | strbuf_addf(out, _("could not fetch '%s' (exit code: %d)\n"), | |
1731 | remote, result); | |
1732 | state->result = -1; | |
1733 | } | |
1734 | ||
1735 | return 0; | |
1736 | } | |
1737 | ||
1738 | static int fetch_multiple(struct string_list *list, int max_children) | |
7dce19d3 JL |
1739 | { |
1740 | int i, result = 0; | |
22f9b7f3 | 1741 | struct strvec argv = STRVEC_INIT; |
9c4a036b | 1742 | |
887952b8 | 1743 | if (!append && write_fetch_head) { |
e6cc5104 JH |
1744 | int errcode = truncate_fetch_head(); |
1745 | if (errcode) | |
1746 | return errcode; | |
1747 | } | |
1748 | ||
22f9b7f3 | 1749 | strvec_pushl(&argv, "fetch", "--append", "--no-auto-gc", |
f6d8942b | 1750 | "--no-write-commit-graph", NULL); |
85556d4e JK |
1751 | add_options_to_argv(&argv); |
1752 | ||
d54dea77 | 1753 | if (max_children != 1 && list->nr != 1) { |
d70a9eb6 | 1754 | struct parallel_fetch_state state = { argv.v, list, 0, 0 }; |
d54dea77 | 1755 | |
22f9b7f3 | 1756 | strvec_push(&argv, "--end-of-options"); |
d54dea77 JS |
1757 | result = run_processes_parallel_tr2(max_children, |
1758 | &fetch_next_remote, | |
1759 | &fetch_failed_to_start, | |
1760 | &fetch_finished, | |
1761 | &state, | |
1762 | "fetch", "parallel/fetch"); | |
1763 | ||
1764 | if (!result) | |
1765 | result = state.result; | |
1766 | } else | |
1767 | for (i = 0; i < list->nr; i++) { | |
1768 | const char *name = list->items[i].string; | |
22f9b7f3 | 1769 | strvec_push(&argv, name); |
d54dea77 JS |
1770 | if (verbosity >= 0) |
1771 | printf(_("Fetching %s\n"), name); | |
d70a9eb6 | 1772 | if (run_command_v_opt(argv.v, RUN_GIT_CMD)) { |
d54dea77 JS |
1773 | error(_("Could not fetch %s"), name); |
1774 | result = 1; | |
1775 | } | |
22f9b7f3 | 1776 | strvec_pop(&argv); |
9c4a036b | 1777 | } |
9c4a036b | 1778 | |
22f9b7f3 | 1779 | strvec_clear(&argv); |
d54dea77 | 1780 | return !!result; |
9c4a036b BG |
1781 | } |
1782 | ||
aa57b871 JH |
1783 | /* |
1784 | * Fetching from the promisor remote should use the given filter-spec | |
1785 | * or inherit the default filter-spec from the config. | |
1786 | */ | |
1787 | static inline void fetch_one_setup_partial(struct remote *remote) | |
1788 | { | |
1789 | /* | |
1790 | * Explicit --no-filter argument overrides everything, regardless | |
1791 | * of any prior partial clones and fetches. | |
1792 | */ | |
1793 | if (filter_options.no_filter) | |
1794 | return; | |
1795 | ||
1796 | /* | |
1797 | * If no prior partial clone/fetch and the current fetch DID NOT | |
1798 | * request a partial-fetch, do a normal fetch. | |
1799 | */ | |
b14ed5ad | 1800 | if (!has_promisor_remote() && !filter_options.choice) |
aa57b871 JH |
1801 | return; |
1802 | ||
1803 | /* | |
5e461393 CC |
1804 | * If this is a partial-fetch request, we enable partial on |
1805 | * this repo if not already enabled and remember the given | |
1806 | * filter-spec as the default for subsequent fetches to this | |
23547c40 | 1807 | * remote if there is currently no default filter-spec. |
aa57b871 | 1808 | */ |
5e461393 | 1809 | if (filter_options.choice) { |
aa57b871 JH |
1810 | partial_clone_register(remote->name, &filter_options); |
1811 | return; | |
1812 | } | |
1813 | ||
aa57b871 JH |
1814 | /* |
1815 | * Do a partial-fetch from the promisor remote using either the | |
1816 | * explicitly given filter-spec or inherit the filter-spec from | |
1817 | * the config. | |
1818 | */ | |
1819 | if (!filter_options.choice) | |
fa3d1b63 | 1820 | partial_clone_get_default_filter_spec(&filter_options, remote->name); |
aa57b871 JH |
1821 | return; |
1822 | } | |
1823 | ||
2b713c27 JT |
1824 | static int fetch_one(struct remote *remote, int argc, const char **argv, |
1825 | int prune_tags_ok, int use_stdin_refspecs) | |
b888d61c | 1826 | { |
d7c8e307 BW |
1827 | struct refspec rs = REFSPEC_INIT_FETCH; |
1828 | int i; | |
7b7f39ea | 1829 | int exit_code; |
6317972c ÆAB |
1830 | int maybe_prune_tags; |
1831 | int remote_via_config = remote_is_configured(remote, 0); | |
b888d61c | 1832 | |
fa685bdf | 1833 | if (!remote) |
bd4a51fc ÆAB |
1834 | die(_("No remote repository specified. Please, specify either a URL or a\n" |
1835 | "remote name from which new revisions should be fetched.")); | |
fa685bdf | 1836 | |
508ea882 | 1837 | gtransport = prepare_transport(remote, 1); |
737c5a9c MS |
1838 | |
1839 | if (prune < 0) { | |
1840 | /* no command line request */ | |
07118832 ÆAB |
1841 | if (0 <= remote->prune) |
1842 | prune = remote->prune; | |
737c5a9c MS |
1843 | else if (0 <= fetch_prune_config) |
1844 | prune = fetch_prune_config; | |
1845 | else | |
1846 | prune = PRUNE_BY_DEFAULT; | |
1847 | } | |
1848 | ||
97716d21 ÆAB |
1849 | if (prune_tags < 0) { |
1850 | /* no command line request */ | |
1851 | if (0 <= remote->prune_tags) | |
1852 | prune_tags = remote->prune_tags; | |
1853 | else if (0 <= fetch_prune_tags_config) | |
1854 | prune_tags = fetch_prune_tags_config; | |
1855 | else | |
1856 | prune_tags = PRUNE_TAGS_BY_DEFAULT; | |
1857 | } | |
1858 | ||
6317972c ÆAB |
1859 | maybe_prune_tags = prune_tags_ok && prune_tags; |
1860 | if (maybe_prune_tags && remote_via_config) | |
95303500 | 1861 | refspec_append(&remote->fetch, TAG_REFSPEC); |
97716d21 | 1862 | |
d7c8e307 BW |
1863 | if (maybe_prune_tags && (argc || !remote_via_config)) |
1864 | refspec_append(&rs, TAG_REFSPEC); | |
6317972c | 1865 | |
d7c8e307 BW |
1866 | for (i = 0; i < argc; i++) { |
1867 | if (!strcmp(argv[i], "tag")) { | |
d7c8e307 BW |
1868 | i++; |
1869 | if (i >= argc) | |
1870 | die(_("You need to specify a tag name.")); | |
1871 | ||
1af8b8c0 RS |
1872 | refspec_appendf(&rs, "refs/tags/%s:refs/tags/%s", |
1873 | argv[i], argv[i]); | |
d7c8e307 BW |
1874 | } else { |
1875 | refspec_append(&rs, argv[i]); | |
b888d61c | 1876 | } |
b888d61c DB |
1877 | } |
1878 | ||
2b713c27 JT |
1879 | if (use_stdin_refspecs) { |
1880 | struct strbuf line = STRBUF_INIT; | |
1881 | while (strbuf_getline_lf(&line, stdin) != EOF) | |
1882 | refspec_append(&rs, line.buf); | |
1883 | strbuf_release(&line); | |
1884 | } | |
1885 | ||
5e3548ef BW |
1886 | if (server_options.nr) |
1887 | gtransport->server_options = &server_options; | |
1888 | ||
57b235a4 | 1889 | sigchain_push_common(unlock_pack_on_signal); |
e4022ed2 | 1890 | atexit(unlock_pack); |
14358894 | 1891 | sigchain_push(SIGPIPE, SIG_IGN); |
65a1301f | 1892 | exit_code = do_fetch(gtransport, &rs); |
14358894 | 1893 | sigchain_pop(SIGPIPE); |
d7c8e307 | 1894 | refspec_clear(&rs); |
af234459 JH |
1895 | transport_disconnect(gtransport); |
1896 | gtransport = NULL; | |
7b7f39ea | 1897 | return exit_code; |
b888d61c | 1898 | } |
9c4a036b BG |
1899 | |
1900 | int cmd_fetch(int argc, const char **argv, const char *prefix) | |
1901 | { | |
1902 | int i; | |
b7410f61 | 1903 | struct string_list list = STRING_LIST_INIT_DUP; |
a1743343 | 1904 | struct remote *remote = NULL; |
9c4a036b | 1905 | int result = 0; |
c1a7902f | 1906 | int prune_tags_ok = 1; |
9c4a036b | 1907 | |
bbc30f99 JK |
1908 | packet_trace_identity("fetch"); |
1909 | ||
9c4a036b BG |
1910 | /* Record the command line for the reflog */ |
1911 | strbuf_addstr(&default_rla, "fetch"); | |
46da295a JS |
1912 | for (i = 1; i < argc; i++) { |
1913 | /* This handles non-URLs gracefully */ | |
1914 | char *anon = transport_anonymize_url(argv[i]); | |
1915 | ||
1916 | strbuf_addf(&default_rla, " %s", anon); | |
1917 | free(anon); | |
1918 | } | |
9c4a036b | 1919 | |
737c5a9c MS |
1920 | git_config(git_fetch_config, NULL); |
1921 | ||
9c4a036b BG |
1922 | argc = parse_options(argc, argv, prefix, |
1923 | builtin_fetch_options, builtin_fetch_usage, 0); | |
e5b94213 JT |
1924 | if (recurse_submodules != RECURSE_SUBMODULES_OFF) { |
1925 | int *sfjc = submodule_fetch_jobs_config == -1 | |
1926 | ? &submodule_fetch_jobs_config : NULL; | |
1927 | int *rs = recurse_submodules == RECURSE_SUBMODULES_DEFAULT | |
1928 | ? &recurse_submodules : NULL; | |
1929 | ||
1930 | fetch_config_from_gitmodules(sfjc, rs); | |
1931 | } | |
9c4a036b | 1932 | |
cccf74e2 NTND |
1933 | if (deepen_relative) { |
1934 | if (deepen_relative < 0) | |
1935 | die(_("Negative depth in --deepen is not supported")); | |
1936 | if (depth) | |
1937 | die(_("--deepen and --depth are mutually exclusive")); | |
1938 | depth = xstrfmt("%d", deepen_relative); | |
1939 | } | |
4dcb167f NTND |
1940 | if (unshallow) { |
1941 | if (depth) | |
1942 | die(_("--depth and --unshallow cannot be used together")); | |
c8813487 | 1943 | else if (!is_repository_shallow(the_repository)) |
4dcb167f | 1944 | die(_("--unshallow on a complete repository does not make sense")); |
2805bb59 JK |
1945 | else |
1946 | depth = xstrfmt("%d", INFINITE_DEPTH); | |
4dcb167f NTND |
1947 | } |
1948 | ||
5594bcad NTND |
1949 | /* no need to be strict, transport_set_option() will validate it again */ |
1950 | if (depth && atoi(depth) < 1) | |
1951 | die(_("depth %s is not a positive number"), depth); | |
a45a2600 | 1952 | if (depth || deepen_since || deepen_not.nr) |
508ea882 | 1953 | deepen = 1; |
5594bcad | 1954 | |
887952b8 JH |
1955 | /* FETCH_HEAD never gets updated in --dry-run mode */ |
1956 | if (dry_run) | |
1957 | write_fetch_head = 0; | |
1958 | ||
9c4a036b BG |
1959 | if (all) { |
1960 | if (argc == 1) | |
bd4a51fc | 1961 | die(_("fetch --all does not take a repository argument")); |
9c4a036b | 1962 | else if (argc > 1) |
bd4a51fc | 1963 | die(_("fetch --all does not make sense with refspecs")); |
9c4a036b | 1964 | (void) for_each_remote(get_one_remote_for_fetch, &list); |
9c4a036b BG |
1965 | } else if (argc == 0) { |
1966 | /* No arguments -- use default remote */ | |
1967 | remote = remote_get(NULL); | |
16679e37 BG |
1968 | } else if (multiple) { |
1969 | /* All arguments are assumed to be remotes or groups */ | |
1970 | for (i = 0; i < argc; i++) | |
1971 | if (!add_remote_or_group(argv[i], &list)) | |
bd4a51fc | 1972 | die(_("No such remote or remote group: %s"), argv[i]); |
9c4a036b BG |
1973 | } else { |
1974 | /* Single remote or group */ | |
1975 | (void) add_remote_or_group(argv[0], &list); | |
1976 | if (list.nr > 1) { | |
1977 | /* More than one remote */ | |
1978 | if (argc > 1) | |
bd4a51fc | 1979 | die(_("Fetching a group and specifying refspecs does not make sense")); |
9c4a036b BG |
1980 | } else { |
1981 | /* Zero or one remotes */ | |
1982 | remote = remote_get(argv[0]); | |
c1a7902f | 1983 | prune_tags_ok = (argc == 1); |
a1743343 JT |
1984 | argc--; |
1985 | argv++; | |
9c4a036b BG |
1986 | } |
1987 | } | |
1988 | ||
acb0c572 | 1989 | if (remote) { |
b14ed5ad | 1990 | if (filter_options.choice || has_promisor_remote()) |
aa57b871 | 1991 | fetch_one_setup_partial(remote); |
2b713c27 | 1992 | result = fetch_one(remote, argc, argv, prune_tags_ok, stdin_refspecs); |
acb0c572 | 1993 | } else { |
d54dea77 JS |
1994 | int max_children = max_jobs; |
1995 | ||
acb0c572 | 1996 | if (filter_options.choice) |
e0137875 CC |
1997 | die(_("--filter can only be used with the remote " |
1998 | "configured in extensions.partialclone")); | |
d54dea77 | 1999 | |
c7b190da PS |
2000 | if (atomic_fetch) |
2001 | die(_("--atomic can only be used when fetching " | |
2002 | "from one remote")); | |
2003 | ||
2b713c27 JT |
2004 | if (stdin_refspecs) |
2005 | die(_("--stdin can only be used when fetching " | |
2006 | "from one remote")); | |
2007 | ||
d54dea77 JS |
2008 | if (max_children < 0) |
2009 | max_children = fetch_parallel_config; | |
2010 | ||
aa57b871 | 2011 | /* TODO should this also die if we have a previous partial-clone? */ |
d54dea77 | 2012 | result = fetch_multiple(&list, max_children); |
acb0c572 | 2013 | } |
a1743343 | 2014 | |
be254a0e | 2015 | if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) { |
22f9b7f3 | 2016 | struct strvec options = STRVEC_INIT; |
d54dea77 JS |
2017 | int max_children = max_jobs; |
2018 | ||
2019 | if (max_children < 0) | |
2020 | max_children = submodule_fetch_jobs_config; | |
2021 | if (max_children < 0) | |
2022 | max_children = fetch_parallel_config; | |
85556d4e JK |
2023 | |
2024 | add_options_to_argv(&options); | |
e724197f BW |
2025 | result = fetch_populated_submodules(the_repository, |
2026 | &options, | |
7dce19d3 | 2027 | submodule_prefix, |
8f0700dd | 2028 | recurse_submodules, |
8fa29159 | 2029 | recurse_submodules_default, |
62104ba1 SB |
2030 | verbosity < 0, |
2031 | max_children); | |
22f9b7f3 | 2032 | strvec_clear(&options); |
7dce19d3 JL |
2033 | } |
2034 | ||
9c4a036b BG |
2035 | string_list_clear(&list, 0); |
2036 | ||
50f26bd0 | 2037 | prepare_repo_settings(the_repository); |
c14e6e79 JS |
2038 | if (fetch_write_commit_graph > 0 || |
2039 | (fetch_write_commit_graph < 0 && | |
2040 | the_repository->settings.fetch_write_commit_graph)) { | |
5a535094 | 2041 | int commit_graph_flags = COMMIT_GRAPH_WRITE_SPLIT; |
50f26bd0 DS |
2042 | |
2043 | if (progress) | |
5a535094 | 2044 | commit_graph_flags |= COMMIT_GRAPH_WRITE_PROGRESS; |
50f26bd0 | 2045 | |
0bd52e27 | 2046 | write_commit_graph_reachable(the_repository->objects->odb, |
50f26bd0 | 2047 | commit_graph_flags, |
63020f17 | 2048 | NULL); |
50f26bd0 DS |
2049 | } |
2050 | ||
2d511cfc | 2051 | close_object_store(the_repository->objects); |
0898c962 | 2052 | |
850b6ede | 2053 | if (enable_auto_gc) |
a95ce124 | 2054 | run_auto_maintenance(verbosity < 0); |
131b8fcb | 2055 | |
9c4a036b BG |
2056 | return result; |
2057 | } |