]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fetch.c
fetch: pass summary_width down the callchain
[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"
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 453static int refcol_width = 10;
bc437d10 454static int compact_format;
6bc91f23
NTND
455
456static void adjust_refcol_width(const struct ref *ref)
457{
458 int max, rlen, llen, len;
459
460 /* uptodate lines are only shown on high verbosity level */
461 if (!verbosity && !oidcmp(&ref->peer_ref->old_oid, &ref->old_oid))
462 return;
463
464 max = term_columns();
465 rlen = utf8_strwidth(prettify_refname(ref->name));
bc437d10 466
6bc91f23
NTND
467 llen = utf8_strwidth(prettify_refname(ref->peer_ref->name));
468
469 /*
470 * rough estimation to see if the output line is too long and
471 * should not be counted (we can't do precise calculation
472 * anyway because we don't know if the error explanation part
473 * will be printed in update_local_ref)
474 */
bc437d10
NTND
475 if (compact_format) {
476 llen = 0;
477 max = max * 2 / 3;
478 }
6bc91f23
NTND
479 len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
480 if (len >= max)
481 return;
482
bc437d10
NTND
483 /*
484 * Not precise calculation for compact mode because '*' can
485 * appear on the left hand side of '->' and shrink the column
486 * back.
487 */
6bc91f23
NTND
488 if (refcol_width < rlen)
489 refcol_width = rlen;
490}
491
492static void prepare_format_display(struct ref *ref_map)
493{
494 struct ref *rm;
bc437d10
NTND
495 const char *format = "full";
496
497 git_config_get_string_const("fetch.output", &format);
498 if (!strcasecmp(format, "full"))
499 compact_format = 0;
500 else if (!strcasecmp(format, "compact"))
501 compact_format = 1;
502 else
503 die(_("configuration fetch.output contains invalid value %s"),
504 format);
6bc91f23
NTND
505
506 for (rm = ref_map; rm; rm = rm->next) {
507 if (rm->status == REF_STATUS_REJECT_SHALLOW ||
508 !rm->peer_ref ||
509 !strcmp(rm->name, "HEAD"))
510 continue;
511
512 adjust_refcol_width(rm);
513 }
514}
165f3902 515
bc437d10
NTND
516static void print_remote_to_local(struct strbuf *display,
517 const char *remote, const char *local)
518{
519 strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local);
520}
521
522static int find_and_replace(struct strbuf *haystack,
523 const char *needle,
524 const char *placeholder)
525{
526 const char *p = strstr(haystack->buf, needle);
527 int plen, nlen;
528
529 if (!p)
530 return 0;
531
532 if (p > haystack->buf && p[-1] != '/')
533 return 0;
534
535 plen = strlen(p);
536 nlen = strlen(needle);
537 if (plen > nlen && p[nlen] != '/')
538 return 0;
539
540 strbuf_splice(haystack, p - haystack->buf, nlen,
541 placeholder, strlen(placeholder));
542 return 1;
543}
544
545static void print_compact(struct strbuf *display,
546 const char *remote, const char *local)
547{
548 struct strbuf r = STRBUF_INIT;
549 struct strbuf l = STRBUF_INIT;
550
551 if (!strcmp(remote, local)) {
552 strbuf_addf(display, "%-*s -> *", refcol_width, remote);
553 return;
554 }
555
556 strbuf_addstr(&r, remote);
557 strbuf_addstr(&l, local);
558
559 if (!find_and_replace(&r, local, "*"))
560 find_and_replace(&l, remote, "*");
561 print_remote_to_local(display, r.buf, l.buf);
562
563 strbuf_release(&r);
564 strbuf_release(&l);
565}
566
d0b39a03
NTND
567static void format_display(struct strbuf *display, char code,
568 const char *summary, const char *error,
901f3d40
JH
569 const char *remote, const char *local,
570 int summary_width)
d0b39a03 571{
901f3d40
JH
572 int width = (summary_width + strlen(summary) - gettext_width(summary));
573
574 strbuf_addf(display, "%c %-*s ", code, width, summary);
bc437d10
NTND
575 if (!compact_format)
576 print_remote_to_local(display, remote, local);
577 else
578 print_compact(display, remote, local);
d0b39a03
NTND
579 if (error)
580 strbuf_addf(display, " (%s)", error);
581}
165f3902 582
b888d61c 583static int update_local_ref(struct ref *ref,
165f3902 584 const char *remote,
6da618d5 585 const struct ref *remote_ref,
901f3d40
JH
586 struct strbuf *display,
587 int summary_width)
b888d61c 588{
b888d61c
DB
589 struct commit *current = NULL, *updated;
590 enum object_type type;
591 struct branch *current_branch = branch_get(NULL);
4577e483 592 const char *pretty_ref = prettify_refname(ref->name);
b888d61c 593
f4e54d02 594 type = sha1_object_info(ref->new_oid.hash, NULL);
b888d61c 595 if (type < 0)
f4e54d02 596 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
b888d61c 597
f4e54d02 598 if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
7f87aff2 599 if (verbosity > 0)
d0b39a03 600 format_display(display, '=', _("[up to date]"), NULL,
901f3d40 601 remote, pretty_ref, summary_width);
b888d61c
DB
602 return 0;
603 }
604
b3abdd9d
SP
605 if (current_branch &&
606 !strcmp(ref->name, current_branch->name) &&
b888d61c 607 !(update_head_ok || is_bare_repository()) &&
f4e54d02 608 !is_null_oid(&ref->old_oid)) {
b888d61c
DB
609 /*
610 * If this is the head, and it's not okay to update
611 * the head, and the old value of the head isn't empty...
612 */
d0b39a03
NTND
613 format_display(display, '!', _("[rejected]"),
614 _("can't fetch in current branch"),
901f3d40 615 remote, pretty_ref, summary_width);
b888d61c
DB
616 return 1;
617 }
618
f4e54d02 619 if (!is_null_oid(&ref->old_oid) &&
59556548 620 starts_with(ref->name, "refs/tags/")) {
6315472e
JK
621 int r;
622 r = s_update_ref("updating tag", ref, 0);
2cb040ba 623 format_display(display, r ? '!' : 't', _("[tag update]"),
d0b39a03 624 r ? _("unable to update local ref") : NULL,
901f3d40 625 remote, pretty_ref, summary_width);
6315472e 626 return r;
b888d61c
DB
627 }
628
f4e54d02 629 current = lookup_commit_reference_gently(ref->old_oid.hash, 1);
630 updated = lookup_commit_reference_gently(ref->new_oid.hash, 1);
b888d61c 631 if (!current || !updated) {
165f3902
NP
632 const char *msg;
633 const char *what;
6315472e 634 int r;
0997adaa
MB
635 /*
636 * Nicely describe the new ref we're fetching.
637 * Base this on the remote's ref name, as it's
638 * more likely to follow a standard layout.
639 */
640 const char *name = remote_ref ? remote_ref->name : "";
59556548 641 if (starts_with(name, "refs/tags/")) {
b888d61c 642 msg = "storing tag";
f7b3742a 643 what = _("[new tag]");
59556548 644 } else if (starts_with(name, "refs/heads/")) {
b888d61c 645 msg = "storing head";
f7b3742a 646 what = _("[new branch]");
0997adaa
MB
647 } else {
648 msg = "storing ref";
649 what = _("[new ref]");
165f3902
NP
650 }
651
a6801adc
JL
652 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
653 (recurse_submodules != RECURSE_SUBMODULES_ON))
f4e54d02 654 check_for_new_submodule_commits(ref->new_oid.hash);
6315472e 655 r = s_update_ref(msg, ref, 0);
d0b39a03
NTND
656 format_display(display, r ? '!' : '*', what,
657 r ? _("unable to update local ref") : NULL,
901f3d40 658 remote, pretty_ref, summary_width);
6315472e 659 return r;
b888d61c
DB
660 }
661
a20efee9 662 if (in_merge_bases(current, updated)) {
bd22d4ff 663 struct strbuf quickref = STRBUF_INIT;
6315472e 664 int r;
ed1c9977 665 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
bd22d4ff 666 strbuf_addstr(&quickref, "..");
f4e54d02 667 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
88a21979
JL
668 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
669 (recurse_submodules != RECURSE_SUBMODULES_ON))
f4e54d02 670 check_for_new_submodule_commits(ref->new_oid.hash);
a75d7b54 671 r = s_update_ref("fast-forward", ref, 1);
d0b39a03
NTND
672 format_display(display, r ? '!' : ' ', quickref.buf,
673 r ? _("unable to update local ref") : NULL,
901f3d40 674 remote, pretty_ref, summary_width);
bd22d4ff 675 strbuf_release(&quickref);
6315472e 676 return r;
165f3902 677 } else if (force || ref->force) {
bd22d4ff 678 struct strbuf quickref = STRBUF_INIT;
6315472e 679 int r;
ed1c9977 680 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
bd22d4ff 681 strbuf_addstr(&quickref, "...");
f4e54d02 682 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
88a21979
JL
683 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
684 (recurse_submodules != RECURSE_SUBMODULES_ON))
f4e54d02 685 check_for_new_submodule_commits(ref->new_oid.hash);
6315472e 686 r = s_update_ref("forced-update", ref, 1);
d0b39a03
NTND
687 format_display(display, r ? '!' : '+', quickref.buf,
688 r ? _("unable to update local ref") : _("forced update"),
901f3d40 689 remote, pretty_ref, summary_width);
bd22d4ff 690 strbuf_release(&quickref);
6315472e 691 return r;
165f3902 692 } else {
d0b39a03 693 format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
901f3d40 694 remote, pretty_ref, summary_width);
b888d61c
DB
695 return 1;
696 }
b888d61c
DB
697}
698
f0e278b1 699static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
6b67e0dc 700{
f0e278b1
JH
701 struct ref **rm = cb_data;
702 struct ref *ref = *rm;
6b67e0dc 703
4820a33b
NTND
704 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
705 ref = ref->next;
f0e278b1
JH
706 if (!ref)
707 return -1; /* end of the list */
708 *rm = ref->next;
f4e54d02 709 hashcpy(sha1, ref->old_oid.hash);
f0e278b1 710 return 0;
6b67e0dc
JH
711}
712
47abd85b 713static int store_updated_refs(const char *raw_url, const char *remote_name,
f3cb169b 714 struct ref *ref_map)
b888d61c
DB
715{
716 FILE *fp;
717 struct commit *commit;
4b3b33a7 718 int url_len, i, rc = 0;
5914f2d0 719 struct strbuf note = STRBUF_INIT;
b888d61c
DB
720 const char *what, *kind;
721 struct ref *rm;
dcf69262 722 char *url;
f932729c 723 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
900f2814 724 int want_status;
901f3d40 725 int summary_width = TRANSPORT_SUMMARY_WIDTH;
b888d61c 726
d6617c7c
AGR
727 fp = fopen(filename, "a");
728 if (!fp)
6da31d7f 729 return error_errno(_("cannot open %s"), filename);
47abd85b 730
fb0cc87e
DB
731 if (raw_url)
732 url = transport_anonymize_url(raw_url);
733 else
734 url = xstrdup("foreign");
6b67e0dc 735
f0e278b1 736 rm = ref_map;
7043c707 737 if (check_connected(iterate_ref_map, &rm, NULL)) {
9516a598
TRC
738 rc = error(_("%s did not send all necessary objects\n"), url);
739 goto abort;
740 }
6b67e0dc 741
6bc91f23
NTND
742 prepare_format_display(ref_map);
743
96890f4c 744 /*
900f2814
JK
745 * We do a pass for each fetch_head_status type in their enum order, so
746 * merged entries are written before not-for-merge. That lets readers
747 * use FETCH_HEAD as a refname to refer to the ref to be merged.
96890f4c 748 */
900f2814
JK
749 for (want_status = FETCH_HEAD_MERGE;
750 want_status <= FETCH_HEAD_IGNORE;
751 want_status++) {
96890f4c
JH
752 for (rm = ref_map; rm; rm = rm->next) {
753 struct ref *ref = NULL;
900f2814 754 const char *merge_status_marker = "";
96890f4c 755
4820a33b
NTND
756 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
757 if (want_status == FETCH_HEAD_MERGE)
758 warning(_("reject %s because shallow roots are not allowed to be updated"),
759 rm->peer_ref ? rm->peer_ref->name : rm->name);
760 continue;
761 }
762
f4e54d02 763 commit = lookup_commit_reference_gently(rm->old_oid.hash, 1);
96890f4c 764 if (!commit)
900f2814 765 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
96890f4c 766
900f2814 767 if (rm->fetch_head_status != want_status)
96890f4c
JH
768 continue;
769
770 if (rm->peer_ref) {
6f687c21 771 ref = alloc_ref(rm->peer_ref->name);
f4e54d02 772 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
773 oidcpy(&ref->new_oid, &rm->old_oid);
96890f4c
JH
774 ref->force = rm->peer_ref->force;
775 }
b888d61c 776
b888d61c 777
96890f4c
JH
778 if (!strcmp(rm->name, "HEAD")) {
779 kind = "";
780 what = "";
781 }
59556548 782 else if (starts_with(rm->name, "refs/heads/")) {
96890f4c
JH
783 kind = "branch";
784 what = rm->name + 11;
785 }
59556548 786 else if (starts_with(rm->name, "refs/tags/")) {
96890f4c
JH
787 kind = "tag";
788 what = rm->name + 10;
789 }
59556548 790 else if (starts_with(rm->name, "refs/remotes/")) {
96890f4c
JH
791 kind = "remote-tracking branch";
792 what = rm->name + 13;
793 }
794 else {
795 kind = "";
796 what = rm->name;
797 }
b888d61c 798
96890f4c
JH
799 url_len = strlen(url);
800 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
801 ;
802 url_len = i + 1;
803 if (4 < i && !strncmp(".git", url + i - 3, 4))
804 url_len = i - 3;
805
806 strbuf_reset(&note);
807 if (*what) {
808 if (*kind)
809 strbuf_addf(&note, "%s ", kind);
810 strbuf_addf(&note, "'%s' of ", what);
811 }
900f2814
JK
812 switch (rm->fetch_head_status) {
813 case FETCH_HEAD_NOT_FOR_MERGE:
814 merge_status_marker = "not-for-merge";
815 /* fall-through */
816 case FETCH_HEAD_MERGE:
817 fprintf(fp, "%s\t%s\t%s",
f4e54d02 818 oid_to_hex(&rm->old_oid),
900f2814
JK
819 merge_status_marker,
820 note.buf);
821 for (i = 0; i < url_len; ++i)
822 if ('\n' == url[i])
823 fputs("\\n", fp);
824 else
825 fputc(url[i], fp);
826 fputc('\n', fp);
827 break;
828 default:
829 /* do not write anything to FETCH_HEAD */
830 break;
831 }
96890f4c
JH
832
833 strbuf_reset(&note);
834 if (ref) {
901f3d40
JH
835 rc |= update_local_ref(ref, what, rm, &note,
836 summary_width);
96890f4c
JH
837 free(ref);
838 } else
d0b39a03
NTND
839 format_display(&note, '*',
840 *kind ? kind : "branch", NULL,
841 *what ? what : "HEAD",
901f3d40 842 "FETCH_HEAD", summary_width);
96890f4c
JH
843 if (note.len) {
844 if (verbosity >= 0 && !shown_url) {
845 fprintf(stderr, _("From %.*s\n"),
846 url_len, url);
847 shown_url = 1;
848 }
849 if (verbosity >= 0)
850 fprintf(stderr, " %s\n", note.buf);
165f3902
NP
851 }
852 }
b888d61c 853 }
9516a598 854
fa250759 855 if (rc & STORE_REF_ERROR_DF_CONFLICT)
bd4a51fc 856 error(_("some local refs could not be updated; try running\n"
f3cb169b 857 " 'git remote prune %s' to remove any old, conflicting "
bd4a51fc 858 "branches"), remote_name);
9516a598
TRC
859
860 abort:
5914f2d0 861 strbuf_release(&note);
9516a598
TRC
862 free(url);
863 fclose(fp);
efb98b44 864 return rc;
b888d61c
DB
865}
866
4191c356
SP
867/*
868 * We would want to bypass the object transfer altogether if
d9eb0205 869 * everything we are going to fetch already exists and is connected
4191c356 870 * locally.
4191c356
SP
871 */
872static int quickfetch(struct ref *ref_map)
873{
f0e278b1 874 struct ref *rm = ref_map;
7043c707 875 struct check_connected_options opt = CHECK_CONNECTED_INIT;
f0e278b1 876
4191c356
SP
877 /*
878 * If we are deepening a shallow clone we already have these
879 * objects reachable. Running rev-list here will return with
880 * a good (0) exit status and we'll bypass the fetch that we
881 * really need to perform. Claiming failure now will ensure
882 * we perform the network exchange to deepen our history.
883 */
884 if (depth)
885 return -1;
7043c707
JK
886 opt.quiet = 1;
887 return check_connected(iterate_ref_map, &rm, &opt);
4191c356
SP
888}
889
b888d61c
DB
890static int fetch_refs(struct transport *transport, struct ref *ref_map)
891{
4191c356
SP
892 int ret = quickfetch(ref_map);
893 if (ret)
894 ret = transport_fetch_refs(transport, ref_map);
b888d61c 895 if (!ret)
f3cb169b
JK
896 ret |= store_updated_refs(transport->url,
897 transport->remote->name,
898 ref_map);
1788c39c 899 transport_unlock_pack(transport);
b888d61c
DB
900 return ret;
901}
902
4b3b33a7
TM
903static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
904 const char *raw_url)
f360d844 905{
4b3b33a7 906 int url_len, i, result = 0;
ed43de6e 907 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
4b3b33a7 908 char *url;
901f3d40 909 int summary_width = TRANSPORT_SUMMARY_WIDTH;
f360d844 910 const char *dangling_msg = dry_run
18986d53
NTND
911 ? _(" (%s will become dangling)")
912 : _(" (%s has become dangling)");
f360d844 913
4b3b33a7
TM
914 if (raw_url)
915 url = transport_anonymize_url(raw_url);
916 else
917 url = xstrdup("foreign");
918
919 url_len = strlen(url);
920 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
921 ;
922
923 url_len = i + 1;
924 if (4 < i && !strncmp(".git", url + i - 3, 4))
925 url_len = i - 3;
926
a087b432
MH
927 if (!dry_run) {
928 struct string_list refnames = STRING_LIST_INIT_NODUP;
929
930 for (ref = stale_refs; ref; ref = ref->next)
931 string_list_append(&refnames, ref->name);
932
c5f04ddd 933 result = delete_refs(&refnames, 0);
a087b432
MH
934 string_list_clear(&refnames, 0);
935 }
936
937 if (verbosity >= 0) {
938 for (ref = stale_refs; ref; ref = ref->next) {
d0b39a03 939 struct strbuf sb = STRBUF_INIT;
a087b432
MH
940 if (!shown_url) {
941 fprintf(stderr, _("From %.*s\n"), url_len, url);
942 shown_url = 1;
943 }
2cb040ba 944 format_display(&sb, '-', _("[deleted]"), NULL,
901f3d40
JH
945 _("(none)"), prettify_refname(ref->name),
946 summary_width);
d0b39a03
NTND
947 fprintf(stderr, " %s\n",sb.buf);
948 strbuf_release(&sb);
f360d844
JS
949 warn_dangling_symref(stderr, dangling_msg, ref->name);
950 }
951 }
a087b432 952
4b3b33a7 953 free(url);
f360d844
JS
954 free_refs(stale_refs);
955 return result;
956}
957
8ee5d731
JS
958static void check_not_current_branch(struct ref *ref_map)
959{
960 struct branch *current_branch = branch_get(NULL);
961
962 if (is_bare_repository() || !current_branch)
963 return;
964
965 for (; ref_map; ref_map = ref_map->next)
966 if (ref_map->peer_ref && !strcmp(current_branch->refname,
967 ref_map->peer_ref->name))
bd4a51fc
ÆAB
968 die(_("Refusing to fetch into current branch %s "
969 "of non-bare repository"), current_branch->refname);
8ee5d731
JS
970}
971
e6cc5104
JH
972static int truncate_fetch_head(void)
973{
f932729c 974 const char *filename = git_path_fetch_head();
ea56518d 975 FILE *fp = fopen_for_writing(filename);
e6cc5104
JH
976
977 if (!fp)
6da31d7f 978 return error_errno(_("cannot open %s"), filename);
e6cc5104
JH
979 fclose(fp);
980 return 0;
981}
982
db5723c6
JH
983static void set_option(struct transport *transport, const char *name, const char *value)
984{
985 int r = transport_set_option(transport, name, value);
986 if (r < 0)
987 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
988 name, value, transport->url);
989 if (r > 0)
990 warning(_("Option \"%s\" is ignored for %s\n"),
991 name, transport->url);
992}
993
0f73f8bd 994static struct transport *prepare_transport(struct remote *remote)
db5723c6
JH
995{
996 struct transport *transport;
997 transport = transport_get(remote, NULL);
998 transport_set_verbosity(transport, verbosity, progress);
c915f11e 999 transport->family = family;
db5723c6
JH
1000 if (upload_pack)
1001 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
1002 if (keep)
1003 set_option(transport, TRANS_OPT_KEEP, "yes");
1004 if (depth)
1005 set_option(transport, TRANS_OPT_DEPTH, depth);
48d25cae
NTND
1006 if (update_shallow)
1007 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
db5723c6
JH
1008 return transport;
1009}
1010
069d5032
JH
1011static void backfill_tags(struct transport *transport, struct ref *ref_map)
1012{
b26ed430
JH
1013 if (transport->cannot_reuse) {
1014 gsecondary = prepare_transport(transport->remote);
1015 transport = gsecondary;
1016 }
1017
069d5032
JH
1018 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
1019 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
1020 fetch_refs(transport, ref_map);
b26ed430
JH
1021
1022 if (gsecondary) {
1023 transport_disconnect(gsecondary);
1024 gsecondary = NULL;
1025 }
069d5032
JH
1026}
1027
b888d61c
DB
1028static int do_fetch(struct transport *transport,
1029 struct refspec *refs, int ref_count)
1030{
b87dbcc8 1031 struct string_list existing_refs = STRING_LIST_INIT_DUP;
7f98428d 1032 struct ref *ref_map;
b888d61c
DB
1033 struct ref *rm;
1034 int autotags = (transport->remote->fetch_tags == 1);
5b87d8d3 1035 int retcode = 0;
b1a01e1c 1036
0e0b7de4 1037 for_each_ref(add_existing, &existing_refs);
b1a01e1c 1038
ed368546
DJ
1039 if (tags == TAGS_DEFAULT) {
1040 if (transport->remote->fetch_tags == 2)
1041 tags = TAGS_SET;
1042 if (transport->remote->fetch_tags == -1)
1043 tags = TAGS_UNSET;
1044 }
b888d61c 1045
824d5776 1046 if (!transport->get_refs_list || !transport->fetch)
bd4a51fc 1047 die(_("Don't know how to fetch from %s"), transport->url);
b888d61c
DB
1048
1049 /* if not appending, truncate FETCH_HEAD */
28a15401 1050 if (!append && !dry_run) {
5b87d8d3
MH
1051 retcode = truncate_fetch_head();
1052 if (retcode)
1053 goto cleanup;
d6617c7c 1054 }
b888d61c
DB
1055
1056 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
8ee5d731
JS
1057 if (!update_head_ok)
1058 check_not_current_branch(ref_map);
b888d61c
DB
1059
1060 for (rm = ref_map; rm; rm = rm->next) {
b1a01e1c 1061 if (rm->peer_ref) {
6f64a16f
MH
1062 struct string_list_item *peer_item =
1063 string_list_lookup(&existing_refs,
1064 rm->peer_ref->name);
0e0b7de4
MH
1065 if (peer_item) {
1066 struct object_id *old_oid = peer_item->util;
f4e54d02 1067 oidcpy(&rm->peer_ref->old_oid, old_oid);
0e0b7de4 1068 }
b1a01e1c 1069 }
b888d61c
DB
1070 }
1071
41fa7d2e
SP
1072 if (tags == TAGS_DEFAULT && autotags)
1073 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
ed43de6e 1074 if (prune) {
0838bf47
MH
1075 /*
1076 * We only prune based on refspecs specified
1077 * explicitly (via command line or configuration); we
1078 * don't care whether --tags was specified.
1079 */
c5a84e92 1080 if (ref_count) {
4b3b33a7 1081 prune_refs(refs, ref_count, ref_map, transport->url);
e8c1e6c7 1082 } else {
0838bf47
MH
1083 prune_refs(transport->remote->fetch,
1084 transport->remote->fetch_refspec_nr,
4b3b33a7
TM
1085 ref_map,
1086 transport->url);
e8c1e6c7 1087 }
ed43de6e 1088 }
10a6cc88
TM
1089 if (fetch_refs(transport, ref_map)) {
1090 free_refs(ref_map);
1091 retcode = 1;
1092 goto cleanup;
1093 }
7f98428d 1094 free_refs(ref_map);
b888d61c
DB
1095
1096 /* if neither --no-tags nor --tags was specified, do automated tag
1097 * following ... */
83201998 1098 if (tags == TAGS_DEFAULT && autotags) {
c50b2b47
SP
1099 struct ref **tail = &ref_map;
1100 ref_map = NULL;
1101 find_non_local_tags(transport, &ref_map, &tail);
069d5032
JH
1102 if (ref_map)
1103 backfill_tags(transport, ref_map);
b888d61c
DB
1104 free_refs(ref_map);
1105 }
1106
5b87d8d3 1107 cleanup:
f83918ed 1108 string_list_clear(&existing_refs, 1);
5b87d8d3 1109 return retcode;
b888d61c
DB
1110}
1111
9c4a036b
BG
1112static int get_one_remote_for_fetch(struct remote *remote, void *priv)
1113{
1114 struct string_list *list = priv;
7cc91a2f 1115 if (!remote->skip_default_update)
1d2f80fa 1116 string_list_append(list, remote->name);
9c4a036b
BG
1117 return 0;
1118}
1119
1120struct remote_group_data {
1121 const char *name;
1122 struct string_list *list;
1123};
1124
1125static int get_remote_group(const char *key, const char *value, void *priv)
1126{
1127 struct remote_group_data *g = priv;
1128
bc598c32 1129 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
9c4a036b 1130 /* split list by white space */
9c4a036b 1131 while (*value) {
5f65499f
MH
1132 size_t wordlen = strcspn(value, " \t\n");
1133
e286542d 1134 if (wordlen >= 1)
b7410f61 1135 string_list_append_nodup(g->list,
e286542d
MH
1136 xstrndup(value, wordlen));
1137 value += wordlen + (value[wordlen] != '\0');
9c4a036b
BG
1138 }
1139 }
1140
1141 return 0;
1142}
1143
1144static int add_remote_or_group(const char *name, struct string_list *list)
1145{
1146 int prev_nr = list->nr;
66dbfd55
GV
1147 struct remote_group_data g;
1148 g.name = name; g.list = list;
9c4a036b
BG
1149
1150 git_config(get_remote_group, &g);
1151 if (list->nr == prev_nr) {
674468b3
TG
1152 struct remote *remote = remote_get(name);
1153 if (!remote_is_configured(remote))
9c4a036b 1154 return 0;
1d2f80fa 1155 string_list_append(list, remote->name);
9c4a036b
BG
1156 }
1157 return 1;
1158}
1159
85556d4e 1160static void add_options_to_argv(struct argv_array *argv)
9c4a036b 1161{
28a15401 1162 if (dry_run)
85556d4e 1163 argv_array_push(argv, "--dry-run");
90765fa3
MH
1164 if (prune != -1)
1165 argv_array_push(argv, prune ? "--prune" : "--no-prune");
bba5322a 1166 if (update_head_ok)
85556d4e 1167 argv_array_push(argv, "--update-head-ok");
bba5322a 1168 if (force)
85556d4e 1169 argv_array_push(argv, "--force");
bba5322a 1170 if (keep)
85556d4e 1171 argv_array_push(argv, "--keep");
be254a0e 1172 if (recurse_submodules == RECURSE_SUBMODULES_ON)
85556d4e 1173 argv_array_push(argv, "--recurse-submodules");
8f0700dd 1174 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
85556d4e 1175 argv_array_push(argv, "--recurse-submodules=on-demand");
85566460
DJ
1176 if (tags == TAGS_SET)
1177 argv_array_push(argv, "--tags");
1178 else if (tags == TAGS_UNSET)
1179 argv_array_push(argv, "--no-tags");
9c4a036b 1180 if (verbosity >= 2)
85556d4e 1181 argv_array_push(argv, "-v");
9c4a036b 1182 if (verbosity >= 1)
85556d4e 1183 argv_array_push(argv, "-v");
9c4a036b 1184 else if (verbosity < 0)
85556d4e 1185 argv_array_push(argv, "-q");
7dce19d3
JL
1186
1187}
1188
1189static int fetch_multiple(struct string_list *list)
1190{
1191 int i, result = 0;
85556d4e 1192 struct argv_array argv = ARGV_ARRAY_INIT;
9c4a036b 1193
e6cc5104
JH
1194 if (!append && !dry_run) {
1195 int errcode = truncate_fetch_head();
1196 if (errcode)
1197 return errcode;
1198 }
1199
85556d4e
JK
1200 argv_array_pushl(&argv, "fetch", "--append", NULL);
1201 add_options_to_argv(&argv);
1202
9c4a036b
BG
1203 for (i = 0; i < list->nr; i++) {
1204 const char *name = list->items[i].string;
85556d4e 1205 argv_array_push(&argv, name);
9c4a036b 1206 if (verbosity >= 0)
bd4a51fc 1207 printf(_("Fetching %s\n"), name);
85556d4e 1208 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
bd4a51fc 1209 error(_("Could not fetch %s"), name);
9c4a036b
BG
1210 result = 1;
1211 }
85556d4e 1212 argv_array_pop(&argv);
9c4a036b
BG
1213 }
1214
85556d4e 1215 argv_array_clear(&argv);
9c4a036b
BG
1216 return result;
1217}
1218
1219static int fetch_one(struct remote *remote, int argc, const char **argv)
b888d61c 1220{
b888d61c 1221 static const char **refs = NULL;
d8ead159 1222 struct refspec *refspec;
b888d61c 1223 int ref_nr = 0;
7b7f39ea 1224 int exit_code;
b888d61c 1225
fa685bdf 1226 if (!remote)
bd4a51fc
ÆAB
1227 die(_("No remote repository specified. Please, specify either a URL or a\n"
1228 "remote name from which new revisions should be fetched."));
fa685bdf 1229
db5723c6 1230 gtransport = prepare_transport(remote);
737c5a9c
MS
1231
1232 if (prune < 0) {
1233 /* no command line request */
20419de9
JH
1234 if (0 <= gtransport->remote->prune)
1235 prune = gtransport->remote->prune;
737c5a9c
MS
1236 else if (0 <= fetch_prune_config)
1237 prune = fetch_prune_config;
1238 else
1239 prune = PRUNE_BY_DEFAULT;
1240 }
1241
9c4a036b 1242 if (argc > 0) {
b888d61c 1243 int j = 0;
bf7e645c 1244 int i;
50a6c8ef 1245 refs = xcalloc(st_add(argc, 1), sizeof(const char *));
9c4a036b 1246 for (i = 0; i < argc; i++) {
b888d61c 1247 if (!strcmp(argv[i], "tag")) {
b888d61c 1248 i++;
f53423b0 1249 if (i >= argc)
bd4a51fc 1250 die(_("You need to specify a tag name."));
b2724c87
JK
1251 refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1252 argv[i], argv[i]);
b888d61c
DB
1253 } else
1254 refs[j++] = argv[i];
b888d61c
DB
1255 }
1256 refs[j] = NULL;
1257 ref_nr = j;
b888d61c
DB
1258 }
1259
57b235a4 1260 sigchain_push_common(unlock_pack_on_signal);
e4022ed2 1261 atexit(unlock_pack);
d8ead159 1262 refspec = parse_fetch_refspec(ref_nr, refs);
af234459 1263 exit_code = do_fetch(gtransport, refspec, ref_nr);
5caf1973 1264 free_refspec(ref_nr, refspec);
af234459
JH
1265 transport_disconnect(gtransport);
1266 gtransport = NULL;
7b7f39ea 1267 return exit_code;
b888d61c 1268}
9c4a036b
BG
1269
1270int cmd_fetch(int argc, const char **argv, const char *prefix)
1271{
1272 int i;
b7410f61 1273 struct string_list list = STRING_LIST_INIT_DUP;
9c4a036b
BG
1274 struct remote *remote;
1275 int result = 0;
1991006c 1276 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
9c4a036b 1277
bbc30f99
JK
1278 packet_trace_identity("fetch");
1279
9c4a036b
BG
1280 /* Record the command line for the reflog */
1281 strbuf_addstr(&default_rla, "fetch");
1282 for (i = 1; i < argc; i++)
1283 strbuf_addf(&default_rla, " %s", argv[i]);
1284
737c5a9c
MS
1285 git_config(git_fetch_config, NULL);
1286
9c4a036b
BG
1287 argc = parse_options(argc, argv, prefix,
1288 builtin_fetch_options, builtin_fetch_usage, 0);
1289
4dcb167f
NTND
1290 if (unshallow) {
1291 if (depth)
1292 die(_("--depth and --unshallow cannot be used together"));
1293 else if (!is_repository_shallow())
1294 die(_("--unshallow on a complete repository does not make sense"));
2805bb59
JK
1295 else
1296 depth = xstrfmt("%d", INFINITE_DEPTH);
4dcb167f
NTND
1297 }
1298
5594bcad
NTND
1299 /* no need to be strict, transport_set_option() will validate it again */
1300 if (depth && atoi(depth) < 1)
1301 die(_("depth %s is not a positive number"), depth);
1302
18322bad
JL
1303 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1304 if (recurse_submodules_default) {
1305 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
1306 set_config_fetch_recurse_submodules(arg);
1307 }
1308 gitmodules_config();
1309 git_config(submodule_config, NULL);
1310 }
1311
9c4a036b
BG
1312 if (all) {
1313 if (argc == 1)
bd4a51fc 1314 die(_("fetch --all does not take a repository argument"));
9c4a036b 1315 else if (argc > 1)
bd4a51fc 1316 die(_("fetch --all does not make sense with refspecs"));
9c4a036b
BG
1317 (void) for_each_remote(get_one_remote_for_fetch, &list);
1318 result = fetch_multiple(&list);
1319 } else if (argc == 0) {
1320 /* No arguments -- use default remote */
1321 remote = remote_get(NULL);
1322 result = fetch_one(remote, argc, argv);
16679e37
BG
1323 } else if (multiple) {
1324 /* All arguments are assumed to be remotes or groups */
1325 for (i = 0; i < argc; i++)
1326 if (!add_remote_or_group(argv[i], &list))
bd4a51fc 1327 die(_("No such remote or remote group: %s"), argv[i]);
16679e37 1328 result = fetch_multiple(&list);
9c4a036b
BG
1329 } else {
1330 /* Single remote or group */
1331 (void) add_remote_or_group(argv[0], &list);
1332 if (list.nr > 1) {
1333 /* More than one remote */
1334 if (argc > 1)
bd4a51fc 1335 die(_("Fetching a group and specifying refspecs does not make sense"));
9c4a036b
BG
1336 result = fetch_multiple(&list);
1337 } else {
1338 /* Zero or one remotes */
1339 remote = remote_get(argv[0]);
1340 result = fetch_one(remote, argc-1, argv+1);
1341 }
1342 }
1343
be254a0e 1344 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
85556d4e
JK
1345 struct argv_array options = ARGV_ARRAY_INIT;
1346
1347 add_options_to_argv(&options);
50d89ad6 1348 result = fetch_populated_submodules(&options,
7dce19d3 1349 submodule_prefix,
8f0700dd 1350 recurse_submodules,
62104ba1
SB
1351 verbosity < 0,
1352 max_children);
85556d4e 1353 argv_array_clear(&options);
7dce19d3
JL
1354 }
1355
9c4a036b
BG
1356 string_list_clear(&list, 0);
1357
0898c962
JS
1358 close_all_packs();
1359
1991006c 1360 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
6fceed3b
NTND
1361 if (verbosity < 0)
1362 argv_array_push(&argv_gc_auto, "--quiet");
1991006c
NTND
1363 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1364 argv_array_clear(&argv_gc_auto);
131b8fcb 1365
9c4a036b
BG
1366 return result;
1367}