]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fetch.c
memoize common git-path "constant" files
[thirdparty/git.git] / builtin / fetch.c
CommitLineData
b888d61c
DB
1/*
2 * "git fetch"
3 */
4#include "cache.h"
5#include "refs.h"
6#include "commit.h"
7#include "builtin.h"
c455c87c 8#include "string-list.h"
b888d61c
DB
9#include "remote.h"
10#include "transport.h"
4191c356 11#include "run-command.h"
83201998 12#include "parse-options.h"
4a16d072 13#include "sigchain.h"
7dce19d3 14#include "submodule.h"
f96400cb 15#include "connected.h"
85556d4e 16#include "argv-array.h"
b888d61c 17
83201998 18static const char * const builtin_fetch_usage[] = {
719acedb
NTND
19 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
20 N_("git fetch [<options>] <group>"),
21 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
22 N_("git fetch --all [<options>]"),
83201998
KH
23 NULL
24};
b888d61c 25
83201998
KH
26enum {
27 TAGS_UNSET = 0,
28 TAGS_DEFAULT = 1,
29 TAGS_SET = 2
30};
31
737c5a9c
MS
32static int fetch_prune_config = -1; /* unspecified */
33static int prune = -1; /* unspecified */
34#define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
35
36static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity;
01fdc21f 37static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
48d25cae 38static int tags = TAGS_DEFAULT, unshallow, update_shallow;
4191c356 39static const char *depth;
83201998 40static const char *upload_pack;
2d324efa 41static struct strbuf default_rla = STRBUF_INIT;
af234459 42static struct transport *gtransport;
b26ed430 43static struct transport *gsecondary;
7dce19d3 44static const char *submodule_prefix = "";
88a21979 45static const char *recurse_submodules_default;
4b3b33a7 46static int shown_url = 0;
c5558f80
JH
47static int refmap_alloc, refmap_nr;
48static const char **refmap_array;
e4022ed2 49
8f0700dd
JL
50static int option_parse_recurse_submodules(const struct option *opt,
51 const char *arg, int unset)
52{
53 if (unset) {
54 recurse_submodules = RECURSE_SUBMODULES_OFF;
55 } else {
56 if (arg)
57 recurse_submodules = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
58 else
59 recurse_submodules = RECURSE_SUBMODULES_ON;
60 }
61 return 0;
62}
e4022ed2 63
737c5a9c
MS
64static int git_fetch_config(const char *k, const char *v, void *cb)
65{
66 if (!strcmp(k, "fetch.prune")) {
67 fetch_prune_config = git_config_bool(k, v);
68 return 0;
69 }
72549dfd 70 return git_default_config(k, v, cb);
737c5a9c
MS
71}
72
c5558f80
JH
73static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
74{
75 ALLOC_GROW(refmap_array, refmap_nr + 1, refmap_alloc);
76
77 /*
78 * "git fetch --refmap='' origin foo"
79 * can be used to tell the command not to store anywhere
80 */
81 if (*arg)
82 refmap_array[refmap_nr++] = arg;
83 return 0;
84}
85
83201998 86static struct option builtin_fetch_options[] = {
7f87aff2 87 OPT__VERBOSITY(&verbosity),
d5d09d47
SB
88 OPT_BOOL(0, "all", &all,
89 N_("fetch from all remotes")),
90 OPT_BOOL('a', "append", &append,
91 N_("append to .git/FETCH_HEAD instead of overwriting")),
719acedb
NTND
92 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
93 N_("path to upload pack on remote end")),
94 OPT__FORCE(&force, N_("force overwrite of local branch")),
d5d09d47
SB
95 OPT_BOOL('m', "multiple", &multiple,
96 N_("fetch from multiple remotes")),
83201998 97 OPT_SET_INT('t', "tags", &tags,
719acedb 98 N_("fetch all tags and associated objects"), TAGS_SET),
e7951290 99 OPT_SET_INT('n', NULL, &tags,
719acedb 100 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
d5d09d47
SB
101 OPT_BOOL('p', "prune", &prune,
102 N_("prune remote-tracking branches no longer on remote")),
719acedb
NTND
103 { OPTION_CALLBACK, 0, "recurse-submodules", NULL, N_("on-demand"),
104 N_("control recursive fetching of submodules"),
8f0700dd 105 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
d5d09d47
SB
106 OPT_BOOL(0, "dry-run", &dry_run,
107 N_("dry run")),
108 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
109 OPT_BOOL('u', "update-head-ok", &update_head_ok,
719acedb
NTND
110 N_("allow updating of HEAD ref")),
111 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
112 OPT_STRING(0, "depth", &depth, N_("depth"),
113 N_("deepen history of shallow clone")),
4dcb167f
NTND
114 { OPTION_SET_INT, 0, "unshallow", &unshallow, NULL,
115 N_("convert to a complete repository"),
116 PARSE_OPT_NONEG | PARSE_OPT_NOARG, NULL, 1 },
719acedb
NTND
117 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
118 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
88a21979
JL
119 { OPTION_STRING, 0, "recurse-submodules-default",
120 &recurse_submodules_default, NULL,
719acedb 121 N_("default mode for recursion"), PARSE_OPT_HIDDEN },
48d25cae
NTND
122 OPT_BOOL(0, "update-shallow", &update_shallow,
123 N_("accept refs that update .git/shallow")),
c5558f80
JH
124 { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"),
125 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg },
83201998
KH
126 OPT_END()
127};
128
e4022ed2
SP
129static void unlock_pack(void)
130{
af234459
JH
131 if (gtransport)
132 transport_unlock_pack(gtransport);
b26ed430
JH
133 if (gsecondary)
134 transport_unlock_pack(gsecondary);
e4022ed2
SP
135}
136
137static void unlock_pack_on_signal(int signo)
138{
139 unlock_pack();
4a16d072 140 sigchain_pop(signo);
e4022ed2
SP
141 raise(signo);
142}
b888d61c 143
85682c19 144static void add_merge_config(struct ref **head,
4577370e 145 const struct ref *remote_refs,
85682c19
SP
146 struct branch *branch,
147 struct ref ***tail)
b888d61c 148{
85682c19 149 int i;
b888d61c 150
85682c19
SP
151 for (i = 0; i < branch->merge_nr; i++) {
152 struct ref *rm, **old_tail = *tail;
153 struct refspec refspec;
154
155 for (rm = *head; rm; rm = rm->next) {
156 if (branch_merge_matches(branch, i, rm->name)) {
900f2814 157 rm->fetch_head_status = FETCH_HEAD_MERGE;
85682c19
SP
158 break;
159 }
b888d61c 160 }
85682c19
SP
161 if (rm)
162 continue;
163
9ad7c5ae 164 /*
8b3f3f84 165 * Not fetched to a remote-tracking branch? We need to fetch
85682c19 166 * it anyway to allow this branch's "branch.$name.merge"
05207a28 167 * to be honored by 'git pull', but we do not have to
9ad7c5ae
JH
168 * fail if branch.$name.merge is misconfigured to point
169 * at a nonexisting branch. If we were indeed called by
05207a28 170 * 'git pull', it will notice the misconfiguration because
9ad7c5ae
JH
171 * there is no entry in the resulting FETCH_HEAD marked
172 * for merging.
85682c19 173 */
8da61a2a 174 memset(&refspec, 0, sizeof(refspec));
85682c19 175 refspec.src = branch->merge[i]->src;
9ad7c5ae 176 get_fetch_map(remote_refs, &refspec, tail, 1);
85682c19 177 for (rm = *old_tail; rm; rm = rm->next)
900f2814 178 rm->fetch_head_status = FETCH_HEAD_MERGE;
b888d61c
DB
179 }
180}
181
0e0b7de4 182static int add_existing(const char *refname, const struct object_id *oid,
a0fbb5a3
MH
183 int flag, void *cbdata)
184{
185 struct string_list *list = (struct string_list *)cbdata;
186 struct string_list_item *item = string_list_insert(list, refname);
0e0b7de4
MH
187 struct object_id *old_oid = xmalloc(sizeof(*old_oid));
188
189 oidcpy(old_oid, oid);
190 item->util = old_oid;
a0fbb5a3
MH
191 return 0;
192}
193
194static int will_fetch(struct ref **head, const unsigned char *sha1)
195{
196 struct ref *rm = *head;
197 while (rm) {
198 if (!hashcmp(rm->old_sha1, sha1))
199 return 1;
200 rm = rm->next;
201 }
202 return 0;
203}
204
767f176a
SP
205static void find_non_local_tags(struct transport *transport,
206 struct ref **head,
a0fbb5a3
MH
207 struct ref ***tail)
208{
209 struct string_list existing_refs = STRING_LIST_INIT_DUP;
210 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
211 const struct ref *ref;
212 struct string_list_item *item = NULL;
213
0e0b7de4 214 for_each_ref(add_existing, &existing_refs);
a0fbb5a3 215 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
ad704485 216 if (!starts_with(ref->name, "refs/tags/"))
a0fbb5a3
MH
217 continue;
218
219 /*
220 * The peeled ref always follows the matching base
221 * ref, so if we see a peeled ref that we don't want
222 * to fetch then we can mark the ref entry in the list
223 * as one to ignore by setting util to NULL.
224 */
ad704485 225 if (ends_with(ref->name, "^{}")) {
a0fbb5a3
MH
226 if (item && !has_sha1_file(ref->old_sha1) &&
227 !will_fetch(head, ref->old_sha1) &&
228 !has_sha1_file(item->util) &&
229 !will_fetch(head, item->util))
230 item->util = NULL;
231 item = NULL;
232 continue;
233 }
234
235 /*
236 * If item is non-NULL here, then we previously saw a
237 * ref not followed by a peeled reference, so we need
238 * to check if it is a lightweight tag that we want to
239 * fetch.
240 */
241 if (item && !has_sha1_file(item->util) &&
242 !will_fetch(head, item->util))
243 item->util = NULL;
244
245 item = NULL;
246
247 /* skip duplicates and refs that we already have */
248 if (string_list_has_string(&remote_refs, ref->name) ||
249 string_list_has_string(&existing_refs, ref->name))
250 continue;
251
252 item = string_list_insert(&remote_refs, ref->name);
253 item->util = (void *)ref->old_sha1;
254 }
255 string_list_clear(&existing_refs, 1);
256
257 /*
258 * We may have a final lightweight tag that needs to be
259 * checked to see if it needs fetching.
260 */
261 if (item && !has_sha1_file(item->util) &&
262 !will_fetch(head, item->util))
263 item->util = NULL;
264
265 /*
266 * For all the tags in the remote_refs string list,
267 * add them to the list of refs to be fetched
268 */
269 for_each_string_list_item(item, &remote_refs) {
270 /* Unless we have already decided to ignore this item... */
271 if (item->util)
272 {
273 struct ref *rm = alloc_ref(item->string);
274 rm->peer_ref = alloc_ref(item->string);
275 hashcpy(rm->old_sha1, item->util);
276 **tail = rm;
277 *tail = &rm->next;
278 }
279 }
280
281 string_list_clear(&remote_refs, 0);
282}
767f176a 283
b888d61c 284static struct ref *get_ref_map(struct transport *transport,
f137a45e
MH
285 struct refspec *refspecs, int refspec_count,
286 int tags, int *autotags)
b888d61c
DB
287{
288 int i;
289 struct ref *rm;
290 struct ref *ref_map = NULL;
291 struct ref **tail = &ref_map;
292
c5a84e92
MH
293 /* opportunistically-updated references: */
294 struct ref *orefs = NULL, **oref_tail = &orefs;
b888d61c 295
c5a84e92 296 const struct ref *remote_refs = transport_get_remote_refs(transport);
f2690487 297
c5a84e92 298 if (refspec_count) {
c5558f80
JH
299 struct refspec *fetch_refspec;
300 int fetch_refspec_nr;
301
f137a45e
MH
302 for (i = 0; i < refspec_count; i++) {
303 get_fetch_map(remote_refs, &refspecs[i], &tail, 0);
304 if (refspecs[i].dst && refspecs[i].dst[0])
b888d61c
DB
305 *autotags = 1;
306 }
0281c930 307 /* Merge everything on the command line (but not --tags) */
b888d61c 308 for (rm = ref_map; rm; rm = rm->next)
900f2814 309 rm->fetch_head_status = FETCH_HEAD_MERGE;
f2690487
JK
310
311 /*
0281c930
MH
312 * For any refs that we happen to be fetching via
313 * command-line arguments, the destination ref might
314 * have been missing or have been different than the
315 * remote-tracking ref that would be derived from the
316 * configured refspec. In these cases, we want to
317 * take the opportunity to update their configured
318 * remote-tracking reference. However, we do not want
319 * to mention these entries in FETCH_HEAD at all, as
320 * they would simply be duplicates of existing
321 * entries, so we set them FETCH_HEAD_IGNORE below.
322 *
323 * We compute these entries now, based only on the
324 * refspecs specified on the command line. But we add
325 * them to the list following the refspecs resulting
326 * from the tags option so that one of the latter,
327 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
328 * by ref_remove_duplicates() in favor of one of these
329 * opportunistic entries with FETCH_HEAD_IGNORE.
f2690487 330 */
c5558f80
JH
331 if (refmap_array) {
332 fetch_refspec = parse_fetch_refspec(refmap_nr, refmap_array);
333 fetch_refspec_nr = refmap_nr;
334 } else {
335 fetch_refspec = transport->remote->fetch;
336 fetch_refspec_nr = transport->remote->fetch_refspec_nr;
337 }
338
339 for (i = 0; i < fetch_refspec_nr; i++)
340 get_fetch_map(ref_map, &fetch_refspec[i], &oref_tail, 1);
0281c930
MH
341
342 if (tags == TAGS_SET)
343 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
c5558f80
JH
344 } else if (refmap_array) {
345 die("--refmap option is only meaningful with command-line refspec(s).");
b888d61c
DB
346 } else {
347 /* Use the defaults */
348 struct remote *remote = transport->remote;
85682c19
SP
349 struct branch *branch = branch_get(NULL);
350 int has_merge = branch_has_merge_config(branch);
3ee1757b
BC
351 if (remote &&
352 (remote->fetch_refspec_nr ||
f31dbdc7 353 /* Note: has_merge implies non-NULL branch->remote_name */
3ee1757b 354 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
b888d61c 355 for (i = 0; i < remote->fetch_refspec_nr; i++) {
9ad7c5ae 356 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
b888d61c
DB
357 if (remote->fetch[i].dst &&
358 remote->fetch[i].dst[0])
359 *autotags = 1;
85682c19 360 if (!i && !has_merge && ref_map &&
cfb8f898 361 !remote->fetch[0].pattern)
900f2814 362 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
b888d61c 363 }
da0204df
JS
364 /*
365 * if the remote we're fetching from is the same
366 * as given in branch.<name>.remote, we add the
367 * ref given in branch.<name>.merge, too.
f31dbdc7
BC
368 *
369 * Note: has_merge implies non-NULL branch->remote_name
da0204df 370 */
9ad7c5ae
JH
371 if (has_merge &&
372 !strcmp(branch->remote_name, remote->name))
85682c19 373 add_merge_config(&ref_map, remote_refs, branch, &tail);
b888d61c
DB
374 } else {
375 ref_map = get_remote_ref(remote_refs, "HEAD");
9ad7c5ae 376 if (!ref_map)
bd4a51fc 377 die(_("Couldn't find remote ref HEAD"));
900f2814 378 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
5aaf7f2a 379 tail = &ref_map->next;
b888d61c
DB
380 }
381 }
0281c930 382
c5a84e92
MH
383 if (tags == TAGS_SET)
384 /* also fetch all tags */
385 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
386 else if (tags == TAGS_DEFAULT && *autotags)
767f176a 387 find_non_local_tags(transport, &ref_map, &tail);
0281c930 388
c5a84e92
MH
389 /* Now append any refs to be updated opportunistically: */
390 *tail = orefs;
391 for (rm = orefs; rm; rm = rm->next) {
392 rm->fetch_head_status = FETCH_HEAD_IGNORE;
393 tail = &rm->next;
394 }
395
b9afe665 396 return ref_remove_duplicates(ref_map);
b888d61c
DB
397}
398
fa250759
JK
399#define STORE_REF_ERROR_OTHER 1
400#define STORE_REF_ERROR_DF_CONFLICT 2
401
b888d61c
DB
402static int s_update_ref(const char *action,
403 struct ref *ref,
404 int check_old)
405{
406 char msg[1024];
407 char *rla = getenv("GIT_REFLOG_ACTION");
cd94f765
RS
408 struct ref_transaction *transaction;
409 struct strbuf err = STRBUF_INIT;
410 int ret, df_conflict = 0;
b888d61c 411
28a15401
JS
412 if (dry_run)
413 return 0;
b888d61c 414 if (!rla)
2d324efa 415 rla = default_rla.buf;
b888d61c 416 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
cd94f765
RS
417
418 transaction = ref_transaction_begin(&err);
419 if (!transaction ||
1d147bdf
MH
420 ref_transaction_update(transaction, ref->name,
421 ref->new_sha1,
422 check_old ? ref->old_sha1 : NULL,
423 0, msg, &err))
cd94f765
RS
424 goto fail;
425
426 ret = ref_transaction_commit(transaction, &err);
427 if (ret) {
428 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
429 goto fail;
430 }
431
432 ref_transaction_free(transaction);
433 strbuf_release(&err);
b888d61c 434 return 0;
cd94f765
RS
435fail:
436 ref_transaction_free(transaction);
437 error("%s", err.buf);
438 strbuf_release(&err);
439 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
440 : STORE_REF_ERROR_OTHER;
b888d61c
DB
441}
442
9ef4272b 443#define REFCOL_WIDTH 10
165f3902 444
b888d61c 445static int update_local_ref(struct ref *ref,
165f3902 446 const char *remote,
6da618d5 447 const struct ref *remote_ref,
5914f2d0 448 struct strbuf *display)
b888d61c 449{
b888d61c
DB
450 struct commit *current = NULL, *updated;
451 enum object_type type;
452 struct branch *current_branch = branch_get(NULL);
4577e483 453 const char *pretty_ref = prettify_refname(ref->name);
b888d61c
DB
454
455 type = sha1_object_info(ref->new_sha1, NULL);
456 if (type < 0)
bd4a51fc 457 die(_("object %s not found"), sha1_to_hex(ref->new_sha1));
b888d61c 458
b888d61c 459 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
7f87aff2 460 if (verbosity > 0)
5914f2d0 461 strbuf_addf(display, "= %-*s %-*s -> %s",
754395d3
NTND
462 TRANSPORT_SUMMARY(_("[up to date]")),
463 REFCOL_WIDTH, remote, pretty_ref);
b888d61c
DB
464 return 0;
465 }
466
b3abdd9d
SP
467 if (current_branch &&
468 !strcmp(ref->name, current_branch->name) &&
b888d61c
DB
469 !(update_head_ok || is_bare_repository()) &&
470 !is_null_sha1(ref->old_sha1)) {
471 /*
472 * If this is the head, and it's not okay to update
473 * the head, and the old value of the head isn't empty...
474 */
5914f2d0
JK
475 strbuf_addf(display,
476 _("! %-*s %-*s -> %s (can't fetch in current branch)"),
754395d3 477 TRANSPORT_SUMMARY(_("[rejected]")),
5914f2d0 478 REFCOL_WIDTH, remote, pretty_ref);
b888d61c
DB
479 return 1;
480 }
481
482 if (!is_null_sha1(ref->old_sha1) &&
59556548 483 starts_with(ref->name, "refs/tags/")) {
6315472e
JK
484 int r;
485 r = s_update_ref("updating tag", ref, 0);
5914f2d0
JK
486 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
487 r ? '!' : '-',
754395d3 488 TRANSPORT_SUMMARY(_("[tag update]")),
5914f2d0
JK
489 REFCOL_WIDTH, remote, pretty_ref,
490 r ? _(" (unable to update local ref)") : "");
6315472e 491 return r;
b888d61c
DB
492 }
493
cfa5b2b7
SP
494 current = lookup_commit_reference_gently(ref->old_sha1, 1);
495 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
b888d61c 496 if (!current || !updated) {
165f3902
NP
497 const char *msg;
498 const char *what;
6315472e 499 int r;
0997adaa
MB
500 /*
501 * Nicely describe the new ref we're fetching.
502 * Base this on the remote's ref name, as it's
503 * more likely to follow a standard layout.
504 */
505 const char *name = remote_ref ? remote_ref->name : "";
59556548 506 if (starts_with(name, "refs/tags/")) {
b888d61c 507 msg = "storing tag";
f7b3742a 508 what = _("[new tag]");
59556548 509 } else if (starts_with(name, "refs/heads/")) {
b888d61c 510 msg = "storing head";
f7b3742a 511 what = _("[new branch]");
0997adaa
MB
512 } else {
513 msg = "storing ref";
514 what = _("[new ref]");
165f3902
NP
515 }
516
a6801adc
JL
517 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
518 (recurse_submodules != RECURSE_SUBMODULES_ON))
519 check_for_new_submodule_commits(ref->new_sha1);
6315472e 520 r = s_update_ref(msg, ref, 0);
5914f2d0
JK
521 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
522 r ? '!' : '*',
754395d3 523 TRANSPORT_SUMMARY(what),
5914f2d0
JK
524 REFCOL_WIDTH, remote, pretty_ref,
525 r ? _(" (unable to update local ref)") : "");
6315472e 526 return r;
b888d61c
DB
527 }
528
a20efee9 529 if (in_merge_bases(current, updated)) {
165f3902 530 char quickref[83];
6315472e 531 int r;
165f3902
NP
532 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
533 strcat(quickref, "..");
534 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
88a21979
JL
535 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
536 (recurse_submodules != RECURSE_SUBMODULES_ON))
537 check_for_new_submodule_commits(ref->new_sha1);
a75d7b54 538 r = s_update_ref("fast-forward", ref, 1);
5914f2d0
JK
539 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
540 r ? '!' : ' ',
541 TRANSPORT_SUMMARY_WIDTH, quickref,
542 REFCOL_WIDTH, remote, pretty_ref,
543 r ? _(" (unable to update local ref)") : "");
6315472e 544 return r;
165f3902
NP
545 } else if (force || ref->force) {
546 char quickref[84];
6315472e 547 int r;
165f3902
NP
548 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
549 strcat(quickref, "...");
550 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
88a21979
JL
551 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
552 (recurse_submodules != RECURSE_SUBMODULES_ON))
553 check_for_new_submodule_commits(ref->new_sha1);
6315472e 554 r = s_update_ref("forced-update", ref, 1);
5914f2d0
JK
555 strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
556 r ? '!' : '+',
557 TRANSPORT_SUMMARY_WIDTH, quickref,
558 REFCOL_WIDTH, remote, pretty_ref,
559 r ? _("unable to update local ref") : _("forced update"));
6315472e 560 return r;
165f3902 561 } else {
5914f2d0 562 strbuf_addf(display, "! %-*s %-*s -> %s %s",
754395d3 563 TRANSPORT_SUMMARY(_("[rejected]")),
5914f2d0
JK
564 REFCOL_WIDTH, remote, pretty_ref,
565 _("(non-fast-forward)"));
b888d61c
DB
566 return 1;
567 }
b888d61c
DB
568}
569
f0e278b1 570static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
6b67e0dc 571{
f0e278b1
JH
572 struct ref **rm = cb_data;
573 struct ref *ref = *rm;
6b67e0dc 574
4820a33b
NTND
575 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
576 ref = ref->next;
f0e278b1
JH
577 if (!ref)
578 return -1; /* end of the list */
579 *rm = ref->next;
580 hashcpy(sha1, ref->old_sha1);
581 return 0;
6b67e0dc
JH
582}
583
47abd85b 584static int store_updated_refs(const char *raw_url, const char *remote_name,
f3cb169b 585 struct ref *ref_map)
b888d61c
DB
586{
587 FILE *fp;
588 struct commit *commit;
4b3b33a7 589 int url_len, i, rc = 0;
5914f2d0 590 struct strbuf note = STRBUF_INIT;
b888d61c
DB
591 const char *what, *kind;
592 struct ref *rm;
dcf69262 593 char *url;
f932729c 594 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
900f2814 595 int want_status;
b888d61c 596
d6617c7c
AGR
597 fp = fopen(filename, "a");
598 if (!fp)
bd4a51fc 599 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
47abd85b 600
fb0cc87e
DB
601 if (raw_url)
602 url = transport_anonymize_url(raw_url);
603 else
604 url = xstrdup("foreign");
6b67e0dc 605
f0e278b1 606 rm = ref_map;
3f7d11c4 607 if (check_everything_connected(iterate_ref_map, 0, &rm)) {
9516a598
TRC
608 rc = error(_("%s did not send all necessary objects\n"), url);
609 goto abort;
610 }
6b67e0dc 611
96890f4c 612 /*
900f2814
JK
613 * We do a pass for each fetch_head_status type in their enum order, so
614 * merged entries are written before not-for-merge. That lets readers
615 * use FETCH_HEAD as a refname to refer to the ref to be merged.
96890f4c 616 */
900f2814
JK
617 for (want_status = FETCH_HEAD_MERGE;
618 want_status <= FETCH_HEAD_IGNORE;
619 want_status++) {
96890f4c
JH
620 for (rm = ref_map; rm; rm = rm->next) {
621 struct ref *ref = NULL;
900f2814 622 const char *merge_status_marker = "";
96890f4c 623
4820a33b
NTND
624 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
625 if (want_status == FETCH_HEAD_MERGE)
626 warning(_("reject %s because shallow roots are not allowed to be updated"),
627 rm->peer_ref ? rm->peer_ref->name : rm->name);
628 continue;
629 }
630
96890f4c
JH
631 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
632 if (!commit)
900f2814 633 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
96890f4c 634
900f2814 635 if (rm->fetch_head_status != want_status)
96890f4c
JH
636 continue;
637
638 if (rm->peer_ref) {
639 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
640 strcpy(ref->name, rm->peer_ref->name);
641 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
642 hashcpy(ref->new_sha1, rm->old_sha1);
643 ref->force = rm->peer_ref->force;
644 }
b888d61c 645
b888d61c 646
96890f4c
JH
647 if (!strcmp(rm->name, "HEAD")) {
648 kind = "";
649 what = "";
650 }
59556548 651 else if (starts_with(rm->name, "refs/heads/")) {
96890f4c
JH
652 kind = "branch";
653 what = rm->name + 11;
654 }
59556548 655 else if (starts_with(rm->name, "refs/tags/")) {
96890f4c
JH
656 kind = "tag";
657 what = rm->name + 10;
658 }
59556548 659 else if (starts_with(rm->name, "refs/remotes/")) {
96890f4c
JH
660 kind = "remote-tracking branch";
661 what = rm->name + 13;
662 }
663 else {
664 kind = "";
665 what = rm->name;
666 }
b888d61c 667
96890f4c
JH
668 url_len = strlen(url);
669 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
670 ;
671 url_len = i + 1;
672 if (4 < i && !strncmp(".git", url + i - 3, 4))
673 url_len = i - 3;
674
675 strbuf_reset(&note);
676 if (*what) {
677 if (*kind)
678 strbuf_addf(&note, "%s ", kind);
679 strbuf_addf(&note, "'%s' of ", what);
680 }
900f2814
JK
681 switch (rm->fetch_head_status) {
682 case FETCH_HEAD_NOT_FOR_MERGE:
683 merge_status_marker = "not-for-merge";
684 /* fall-through */
685 case FETCH_HEAD_MERGE:
686 fprintf(fp, "%s\t%s\t%s",
687 sha1_to_hex(rm->old_sha1),
688 merge_status_marker,
689 note.buf);
690 for (i = 0; i < url_len; ++i)
691 if ('\n' == url[i])
692 fputs("\\n", fp);
693 else
694 fputc(url[i], fp);
695 fputc('\n', fp);
696 break;
697 default:
698 /* do not write anything to FETCH_HEAD */
699 break;
700 }
96890f4c
JH
701
702 strbuf_reset(&note);
703 if (ref) {
6da618d5 704 rc |= update_local_ref(ref, what, rm, &note);
96890f4c
JH
705 free(ref);
706 } else
707 strbuf_addf(&note, "* %-*s %-*s -> FETCH_HEAD",
708 TRANSPORT_SUMMARY_WIDTH,
709 *kind ? kind : "branch",
710 REFCOL_WIDTH,
711 *what ? what : "HEAD");
712 if (note.len) {
713 if (verbosity >= 0 && !shown_url) {
714 fprintf(stderr, _("From %.*s\n"),
715 url_len, url);
716 shown_url = 1;
717 }
718 if (verbosity >= 0)
719 fprintf(stderr, " %s\n", note.buf);
165f3902
NP
720 }
721 }
b888d61c 722 }
9516a598 723
fa250759 724 if (rc & STORE_REF_ERROR_DF_CONFLICT)
bd4a51fc 725 error(_("some local refs could not be updated; try running\n"
f3cb169b 726 " 'git remote prune %s' to remove any old, conflicting "
bd4a51fc 727 "branches"), remote_name);
9516a598
TRC
728
729 abort:
5914f2d0 730 strbuf_release(&note);
9516a598
TRC
731 free(url);
732 fclose(fp);
efb98b44 733 return rc;
b888d61c
DB
734}
735
4191c356
SP
736/*
737 * We would want to bypass the object transfer altogether if
d9eb0205 738 * everything we are going to fetch already exists and is connected
4191c356 739 * locally.
4191c356
SP
740 */
741static int quickfetch(struct ref *ref_map)
742{
f0e278b1
JH
743 struct ref *rm = ref_map;
744
4191c356
SP
745 /*
746 * If we are deepening a shallow clone we already have these
747 * objects reachable. Running rev-list here will return with
748 * a good (0) exit status and we'll bypass the fetch that we
749 * really need to perform. Claiming failure now will ensure
750 * we perform the network exchange to deepen our history.
751 */
752 if (depth)
753 return -1;
f0e278b1 754 return check_everything_connected(iterate_ref_map, 1, &rm);
4191c356
SP
755}
756
b888d61c
DB
757static int fetch_refs(struct transport *transport, struct ref *ref_map)
758{
4191c356
SP
759 int ret = quickfetch(ref_map);
760 if (ret)
761 ret = transport_fetch_refs(transport, ref_map);
b888d61c 762 if (!ret)
f3cb169b
JK
763 ret |= store_updated_refs(transport->url,
764 transport->remote->name,
765 ref_map);
1788c39c 766 transport_unlock_pack(transport);
b888d61c
DB
767 return ret;
768}
769
4b3b33a7
TM
770static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
771 const char *raw_url)
f360d844 772{
4b3b33a7 773 int url_len, i, result = 0;
ed43de6e 774 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
4b3b33a7 775 char *url;
f360d844 776 const char *dangling_msg = dry_run
18986d53
NTND
777 ? _(" (%s will become dangling)")
778 : _(" (%s has become dangling)");
f360d844 779
4b3b33a7
TM
780 if (raw_url)
781 url = transport_anonymize_url(raw_url);
782 else
783 url = xstrdup("foreign");
784
785 url_len = strlen(url);
786 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
787 ;
788
789 url_len = i + 1;
790 if (4 < i && !strncmp(".git", url + i - 3, 4))
791 url_len = i - 3;
792
a087b432
MH
793 if (!dry_run) {
794 struct string_list refnames = STRING_LIST_INIT_NODUP;
795
796 for (ref = stale_refs; ref; ref = ref->next)
797 string_list_append(&refnames, ref->name);
798
799 result = delete_refs(&refnames);
800 string_list_clear(&refnames, 0);
801 }
802
803 if (verbosity >= 0) {
804 for (ref = stale_refs; ref; ref = ref->next) {
805 if (!shown_url) {
806 fprintf(stderr, _("From %.*s\n"), url_len, url);
807 shown_url = 1;
808 }
f360d844 809 fprintf(stderr, " x %-*s %-*s -> %s\n",
754395d3 810 TRANSPORT_SUMMARY(_("[deleted]")),
502681cd 811 REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
f360d844
JS
812 warn_dangling_symref(stderr, dangling_msg, ref->name);
813 }
814 }
a087b432 815
4b3b33a7 816 free(url);
f360d844
JS
817 free_refs(stale_refs);
818 return result;
819}
820
8ee5d731
JS
821static void check_not_current_branch(struct ref *ref_map)
822{
823 struct branch *current_branch = branch_get(NULL);
824
825 if (is_bare_repository() || !current_branch)
826 return;
827
828 for (; ref_map; ref_map = ref_map->next)
829 if (ref_map->peer_ref && !strcmp(current_branch->refname,
830 ref_map->peer_ref->name))
bd4a51fc
ÆAB
831 die(_("Refusing to fetch into current branch %s "
832 "of non-bare repository"), current_branch->refname);
8ee5d731
JS
833}
834
e6cc5104
JH
835static int truncate_fetch_head(void)
836{
f932729c 837 const char *filename = git_path_fetch_head();
e6cc5104
JH
838 FILE *fp = fopen(filename, "w");
839
840 if (!fp)
bd4a51fc 841 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
e6cc5104
JH
842 fclose(fp);
843 return 0;
844}
845
db5723c6
JH
846static void set_option(struct transport *transport, const char *name, const char *value)
847{
848 int r = transport_set_option(transport, name, value);
849 if (r < 0)
850 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
851 name, value, transport->url);
852 if (r > 0)
853 warning(_("Option \"%s\" is ignored for %s\n"),
854 name, transport->url);
855}
856
0f73f8bd 857static struct transport *prepare_transport(struct remote *remote)
db5723c6
JH
858{
859 struct transport *transport;
860 transport = transport_get(remote, NULL);
861 transport_set_verbosity(transport, verbosity, progress);
862 if (upload_pack)
863 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
864 if (keep)
865 set_option(transport, TRANS_OPT_KEEP, "yes");
866 if (depth)
867 set_option(transport, TRANS_OPT_DEPTH, depth);
48d25cae
NTND
868 if (update_shallow)
869 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
db5723c6
JH
870 return transport;
871}
872
069d5032
JH
873static void backfill_tags(struct transport *transport, struct ref *ref_map)
874{
b26ed430
JH
875 if (transport->cannot_reuse) {
876 gsecondary = prepare_transport(transport->remote);
877 transport = gsecondary;
878 }
879
069d5032
JH
880 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
881 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
882 fetch_refs(transport, ref_map);
b26ed430
JH
883
884 if (gsecondary) {
885 transport_disconnect(gsecondary);
886 gsecondary = NULL;
887 }
069d5032
JH
888}
889
b888d61c
DB
890static int do_fetch(struct transport *transport,
891 struct refspec *refs, int ref_count)
892{
b87dbcc8 893 struct string_list existing_refs = STRING_LIST_INIT_DUP;
7f98428d 894 struct ref *ref_map;
b888d61c
DB
895 struct ref *rm;
896 int autotags = (transport->remote->fetch_tags == 1);
5b87d8d3 897 int retcode = 0;
b1a01e1c 898
0e0b7de4 899 for_each_ref(add_existing, &existing_refs);
b1a01e1c 900
ed368546
DJ
901 if (tags == TAGS_DEFAULT) {
902 if (transport->remote->fetch_tags == 2)
903 tags = TAGS_SET;
904 if (transport->remote->fetch_tags == -1)
905 tags = TAGS_UNSET;
906 }
b888d61c 907
824d5776 908 if (!transport->get_refs_list || !transport->fetch)
bd4a51fc 909 die(_("Don't know how to fetch from %s"), transport->url);
b888d61c
DB
910
911 /* if not appending, truncate FETCH_HEAD */
28a15401 912 if (!append && !dry_run) {
5b87d8d3
MH
913 retcode = truncate_fetch_head();
914 if (retcode)
915 goto cleanup;
d6617c7c 916 }
b888d61c
DB
917
918 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
8ee5d731
JS
919 if (!update_head_ok)
920 check_not_current_branch(ref_map);
b888d61c
DB
921
922 for (rm = ref_map; rm; rm = rm->next) {
b1a01e1c 923 if (rm->peer_ref) {
6f64a16f
MH
924 struct string_list_item *peer_item =
925 string_list_lookup(&existing_refs,
926 rm->peer_ref->name);
0e0b7de4
MH
927 if (peer_item) {
928 struct object_id *old_oid = peer_item->util;
929 hashcpy(rm->peer_ref->old_sha1, old_oid->hash);
930 }
b1a01e1c 931 }
b888d61c
DB
932 }
933
41fa7d2e
SP
934 if (tags == TAGS_DEFAULT && autotags)
935 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
ed43de6e 936 if (prune) {
0838bf47
MH
937 /*
938 * We only prune based on refspecs specified
939 * explicitly (via command line or configuration); we
940 * don't care whether --tags was specified.
941 */
c5a84e92 942 if (ref_count) {
4b3b33a7 943 prune_refs(refs, ref_count, ref_map, transport->url);
e8c1e6c7 944 } else {
0838bf47
MH
945 prune_refs(transport->remote->fetch,
946 transport->remote->fetch_refspec_nr,
4b3b33a7
TM
947 ref_map,
948 transport->url);
e8c1e6c7 949 }
ed43de6e 950 }
10a6cc88
TM
951 if (fetch_refs(transport, ref_map)) {
952 free_refs(ref_map);
953 retcode = 1;
954 goto cleanup;
955 }
7f98428d 956 free_refs(ref_map);
b888d61c
DB
957
958 /* if neither --no-tags nor --tags was specified, do automated tag
959 * following ... */
83201998 960 if (tags == TAGS_DEFAULT && autotags) {
c50b2b47
SP
961 struct ref **tail = &ref_map;
962 ref_map = NULL;
963 find_non_local_tags(transport, &ref_map, &tail);
069d5032
JH
964 if (ref_map)
965 backfill_tags(transport, ref_map);
b888d61c
DB
966 free_refs(ref_map);
967 }
968
5b87d8d3 969 cleanup:
f83918ed 970 string_list_clear(&existing_refs, 1);
5b87d8d3 971 return retcode;
b888d61c
DB
972}
973
9c4a036b
BG
974static int get_one_remote_for_fetch(struct remote *remote, void *priv)
975{
976 struct string_list *list = priv;
7cc91a2f 977 if (!remote->skip_default_update)
1d2f80fa 978 string_list_append(list, remote->name);
9c4a036b
BG
979 return 0;
980}
981
982struct remote_group_data {
983 const char *name;
984 struct string_list *list;
985};
986
987static int get_remote_group(const char *key, const char *value, void *priv)
988{
989 struct remote_group_data *g = priv;
990
59556548 991 if (starts_with(key, "remotes.") &&
9c4a036b
BG
992 !strcmp(key + 8, g->name)) {
993 /* split list by white space */
994 int space = strcspn(value, " \t\n");
995 while (*value) {
996 if (space > 1) {
1d2f80fa
JP
997 string_list_append(g->list,
998 xstrndup(value, space));
9c4a036b
BG
999 }
1000 value += space + (value[space] != '\0');
1001 space = strcspn(value, " \t\n");
1002 }
1003 }
1004
1005 return 0;
1006}
1007
1008static int add_remote_or_group(const char *name, struct string_list *list)
1009{
1010 int prev_nr = list->nr;
66dbfd55
GV
1011 struct remote_group_data g;
1012 g.name = name; g.list = list;
9c4a036b
BG
1013
1014 git_config(get_remote_group, &g);
1015 if (list->nr == prev_nr) {
1016 struct remote *remote;
1017 if (!remote_is_configured(name))
1018 return 0;
1019 remote = remote_get(name);
1d2f80fa 1020 string_list_append(list, remote->name);
9c4a036b
BG
1021 }
1022 return 1;
1023}
1024
85556d4e 1025static void add_options_to_argv(struct argv_array *argv)
9c4a036b 1026{
28a15401 1027 if (dry_run)
85556d4e 1028 argv_array_push(argv, "--dry-run");
90765fa3
MH
1029 if (prune != -1)
1030 argv_array_push(argv, prune ? "--prune" : "--no-prune");
bba5322a 1031 if (update_head_ok)
85556d4e 1032 argv_array_push(argv, "--update-head-ok");
bba5322a 1033 if (force)
85556d4e 1034 argv_array_push(argv, "--force");
bba5322a 1035 if (keep)
85556d4e 1036 argv_array_push(argv, "--keep");
be254a0e 1037 if (recurse_submodules == RECURSE_SUBMODULES_ON)
85556d4e 1038 argv_array_push(argv, "--recurse-submodules");
8f0700dd 1039 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
85556d4e 1040 argv_array_push(argv, "--recurse-submodules=on-demand");
85566460
DJ
1041 if (tags == TAGS_SET)
1042 argv_array_push(argv, "--tags");
1043 else if (tags == TAGS_UNSET)
1044 argv_array_push(argv, "--no-tags");
9c4a036b 1045 if (verbosity >= 2)
85556d4e 1046 argv_array_push(argv, "-v");
9c4a036b 1047 if (verbosity >= 1)
85556d4e 1048 argv_array_push(argv, "-v");
9c4a036b 1049 else if (verbosity < 0)
85556d4e 1050 argv_array_push(argv, "-q");
7dce19d3
JL
1051
1052}
1053
1054static int fetch_multiple(struct string_list *list)
1055{
1056 int i, result = 0;
85556d4e 1057 struct argv_array argv = ARGV_ARRAY_INIT;
9c4a036b 1058
e6cc5104
JH
1059 if (!append && !dry_run) {
1060 int errcode = truncate_fetch_head();
1061 if (errcode)
1062 return errcode;
1063 }
1064
85556d4e
JK
1065 argv_array_pushl(&argv, "fetch", "--append", NULL);
1066 add_options_to_argv(&argv);
1067
9c4a036b
BG
1068 for (i = 0; i < list->nr; i++) {
1069 const char *name = list->items[i].string;
85556d4e 1070 argv_array_push(&argv, name);
9c4a036b 1071 if (verbosity >= 0)
bd4a51fc 1072 printf(_("Fetching %s\n"), name);
85556d4e 1073 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
bd4a51fc 1074 error(_("Could not fetch %s"), name);
9c4a036b
BG
1075 result = 1;
1076 }
85556d4e 1077 argv_array_pop(&argv);
9c4a036b
BG
1078 }
1079
85556d4e 1080 argv_array_clear(&argv);
9c4a036b
BG
1081 return result;
1082}
1083
1084static int fetch_one(struct remote *remote, int argc, const char **argv)
b888d61c 1085{
b888d61c 1086 static const char **refs = NULL;
d8ead159 1087 struct refspec *refspec;
b888d61c 1088 int ref_nr = 0;
7b7f39ea 1089 int exit_code;
b888d61c 1090
fa685bdf 1091 if (!remote)
bd4a51fc
ÆAB
1092 die(_("No remote repository specified. Please, specify either a URL or a\n"
1093 "remote name from which new revisions should be fetched."));
fa685bdf 1094
db5723c6 1095 gtransport = prepare_transport(remote);
737c5a9c
MS
1096
1097 if (prune < 0) {
1098 /* no command line request */
20419de9
JH
1099 if (0 <= gtransport->remote->prune)
1100 prune = gtransport->remote->prune;
737c5a9c
MS
1101 else if (0 <= fetch_prune_config)
1102 prune = fetch_prune_config;
1103 else
1104 prune = PRUNE_BY_DEFAULT;
1105 }
1106
9c4a036b 1107 if (argc > 0) {
b888d61c 1108 int j = 0;
bf7e645c 1109 int i;
83201998 1110 refs = xcalloc(argc + 1, sizeof(const char *));
9c4a036b 1111 for (i = 0; i < argc; i++) {
b888d61c 1112 if (!strcmp(argv[i], "tag")) {
b888d61c 1113 i++;
f53423b0 1114 if (i >= argc)
bd4a51fc 1115 die(_("You need to specify a tag name."));
b2724c87
JK
1116 refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1117 argv[i], argv[i]);
b888d61c
DB
1118 } else
1119 refs[j++] = argv[i];
b888d61c
DB
1120 }
1121 refs[j] = NULL;
1122 ref_nr = j;
b888d61c
DB
1123 }
1124
57b235a4 1125 sigchain_push_common(unlock_pack_on_signal);
e4022ed2 1126 atexit(unlock_pack);
d8ead159 1127 refspec = parse_fetch_refspec(ref_nr, refs);
af234459 1128 exit_code = do_fetch(gtransport, refspec, ref_nr);
5caf1973 1129 free_refspec(ref_nr, refspec);
af234459
JH
1130 transport_disconnect(gtransport);
1131 gtransport = NULL;
7b7f39ea 1132 return exit_code;
b888d61c 1133}
9c4a036b
BG
1134
1135int cmd_fetch(int argc, const char **argv, const char *prefix)
1136{
1137 int i;
183113a5 1138 struct string_list list = STRING_LIST_INIT_NODUP;
9c4a036b
BG
1139 struct remote *remote;
1140 int result = 0;
1991006c 1141 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
9c4a036b 1142
bbc30f99
JK
1143 packet_trace_identity("fetch");
1144
9c4a036b
BG
1145 /* Record the command line for the reflog */
1146 strbuf_addstr(&default_rla, "fetch");
1147 for (i = 1; i < argc; i++)
1148 strbuf_addf(&default_rla, " %s", argv[i]);
1149
737c5a9c
MS
1150 git_config(git_fetch_config, NULL);
1151
9c4a036b
BG
1152 argc = parse_options(argc, argv, prefix,
1153 builtin_fetch_options, builtin_fetch_usage, 0);
1154
4dcb167f
NTND
1155 if (unshallow) {
1156 if (depth)
1157 die(_("--depth and --unshallow cannot be used together"));
1158 else if (!is_repository_shallow())
1159 die(_("--unshallow on a complete repository does not make sense"));
1160 else {
1161 static char inf_depth[12];
1162 sprintf(inf_depth, "%d", INFINITE_DEPTH);
1163 depth = inf_depth;
1164 }
1165 }
1166
5594bcad
NTND
1167 /* no need to be strict, transport_set_option() will validate it again */
1168 if (depth && atoi(depth) < 1)
1169 die(_("depth %s is not a positive number"), depth);
1170
18322bad
JL
1171 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1172 if (recurse_submodules_default) {
1173 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
1174 set_config_fetch_recurse_submodules(arg);
1175 }
1176 gitmodules_config();
1177 git_config(submodule_config, NULL);
1178 }
1179
9c4a036b
BG
1180 if (all) {
1181 if (argc == 1)
bd4a51fc 1182 die(_("fetch --all does not take a repository argument"));
9c4a036b 1183 else if (argc > 1)
bd4a51fc 1184 die(_("fetch --all does not make sense with refspecs"));
9c4a036b
BG
1185 (void) for_each_remote(get_one_remote_for_fetch, &list);
1186 result = fetch_multiple(&list);
1187 } else if (argc == 0) {
1188 /* No arguments -- use default remote */
1189 remote = remote_get(NULL);
1190 result = fetch_one(remote, argc, argv);
16679e37
BG
1191 } else if (multiple) {
1192 /* All arguments are assumed to be remotes or groups */
1193 for (i = 0; i < argc; i++)
1194 if (!add_remote_or_group(argv[i], &list))
bd4a51fc 1195 die(_("No such remote or remote group: %s"), argv[i]);
16679e37 1196 result = fetch_multiple(&list);
9c4a036b
BG
1197 } else {
1198 /* Single remote or group */
1199 (void) add_remote_or_group(argv[0], &list);
1200 if (list.nr > 1) {
1201 /* More than one remote */
1202 if (argc > 1)
bd4a51fc 1203 die(_("Fetching a group and specifying refspecs does not make sense"));
9c4a036b
BG
1204 result = fetch_multiple(&list);
1205 } else {
1206 /* Zero or one remotes */
1207 remote = remote_get(argv[0]);
1208 result = fetch_one(remote, argc-1, argv+1);
1209 }
1210 }
1211
be254a0e 1212 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
85556d4e
JK
1213 struct argv_array options = ARGV_ARRAY_INIT;
1214
1215 add_options_to_argv(&options);
50d89ad6 1216 result = fetch_populated_submodules(&options,
7dce19d3 1217 submodule_prefix,
8f0700dd 1218 recurse_submodules,
7dce19d3 1219 verbosity < 0);
85556d4e 1220 argv_array_clear(&options);
7dce19d3
JL
1221 }
1222
9c4a036b
BG
1223 /* All names were strdup()ed or strndup()ed */
1224 list.strdup_strings = 1;
1225 string_list_clear(&list, 0);
1226
1991006c 1227 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
6fceed3b
NTND
1228 if (verbosity < 0)
1229 argv_array_push(&argv_gc_auto, "--quiet");
1991006c
NTND
1230 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1231 argv_array_clear(&argv_gc_auto);
131b8fcb 1232
9c4a036b
BG
1233 return result;
1234}