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