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