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