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