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