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