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