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