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