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