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