blob: b24a9e09a46f075fad963f56ee0addce3c73b70e [file] [log] [blame]
Daniel Barkalowb888d612007-09-10 23:03:25 -04001/*
2 * "git fetch"
3 */
4#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07005#include "config.h"
Brandon Williamse7241972017-12-12 11:53:52 -08006#include "repository.h"
Daniel Barkalowb888d612007-09-10 23:03:25 -04007#include "refs.h"
Brandon Williamsec0cb492018-05-16 15:57:48 -07008#include "refspec.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07009#include "object-store.h"
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -070010#include "oidset.h"
Daniel Barkalowb888d612007-09-10 23:03:25 -040011#include "commit.h"
12#include "builtin.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +010013#include "string-list.h"
Daniel Barkalowb888d612007-09-10 23:03:25 -040014#include "remote.h"
15#include "transport.h"
Shawn O. Pearce4191c352007-11-11 02:29:47 -050016#include "run-command.h"
Kristian Høgsberg83201992007-12-04 02:25:47 -050017#include "parse-options.h"
Jeff King4a16d072009-01-22 01:02:35 -050018#include "sigchain.h"
Heiko Voigt027771f2015-08-17 17:22:00 -070019#include "submodule-config.h"
Jens Lehmann7dce19d2010-11-12 13:54:52 +010020#include "submodule.h"
Junio C Hamanof96400c2011-09-02 16:33:22 -070021#include "connected.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040022#include "strvec.h"
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +020023#include "utf8.h"
Jonathan Tan3836d882017-08-18 15:20:21 -070024#include "packfile.h"
Jeff Hostetleracb0c572017-12-08 15:58:44 +000025#include "list-objects-filter-options.h"
Derrick Stolee64043552018-07-20 16:33:04 +000026#include "commit-reach.h"
Corentin BOMPARD24bc1a12019-08-19 11:11:20 +020027#include "branch.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020028#include "promisor-remote.h"
Derrick Stolee50f26bd2019-09-02 19:22:02 -070029#include "commit-graph.h"
Taylor Blau120ad2b2020-04-30 13:48:50 -060030#include "shallow.h"
Daniel Barkalowb888d612007-09-10 23:03:25 -040031
Derrick Stolee377444b2019-06-18 13:25:27 -070032#define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000)
33
Kristian Høgsberg83201992007-12-04 02:25:47 -050034static const char * const builtin_fetch_usage[] = {
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070035 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
36 N_("git fetch [<options>] <group>"),
37 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
38 N_("git fetch --all [<options>]"),
Kristian Høgsberg83201992007-12-04 02:25:47 -050039 NULL
40};
Daniel Barkalowb888d612007-09-10 23:03:25 -040041
Kristian Høgsberg83201992007-12-04 02:25:47 -050042enum {
43 TAGS_UNSET = 0,
44 TAGS_DEFAULT = 1,
45 TAGS_SET = 2
46};
47
Michael Schubert737c5a92013-07-13 11:36:24 +020048static int fetch_prune_config = -1; /* unspecified */
Derrick Stoleecdbd70c2019-06-18 13:25:26 -070049static int fetch_show_forced_updates = 1;
Derrick Stolee377444b2019-06-18 13:25:27 -070050static uint64_t forced_updates_ms = 0;
Michael Schubert737c5a92013-07-13 11:36:24 +020051static int prune = -1; /* unspecified */
52#define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
53
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +000054static int fetch_prune_tags_config = -1; /* unspecified */
55static int prune_tags = -1; /* unspecified */
56#define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
57
Corentin BOMPARD24bc1a12019-08-19 11:11:20 +020058static int all, append, dry_run, force, keep, multiple, update_head_ok;
Junio C Hamano887952b2020-08-18 14:25:22 +000059static int write_fetch_head = 1;
Corentin BOMPARD24bc1a12019-08-19 11:11:20 +020060static int verbosity, deepen_relative, set_upstream;
Stefan Beller8c698322017-06-23 12:13:01 -070061static int progress = -1;
Nguyễn Thái Ngọc Duyc3d6b702019-06-19 16:46:30 +070062static int enable_auto_gc = 1;
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +070063static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
Johannes Schindelind54dea72019-10-05 11:46:40 -070064static int max_jobs = -1, submodule_fetch_jobs_config = -1;
65static int fetch_parallel_config = 1;
Eric Wongc915f112016-02-03 04:09:14 +000066static enum transport_family family;
Shawn O. Pearce4191c352007-11-11 02:29:47 -050067static const char *depth;
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +070068static const char *deepen_since;
Kristian Høgsberg83201992007-12-04 02:25:47 -050069static const char *upload_pack;
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +070070static struct string_list deepen_not = STRING_LIST_INIT_NODUP;
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -050071static struct strbuf default_rla = STRBUF_INIT;
Junio C Hamanoaf234452013-08-07 15:38:45 -070072static struct transport *gtransport;
Junio C Hamanob26ed432013-08-07 15:47:18 -070073static struct transport *gsecondary;
Jens Lehmann7dce19d2010-11-12 13:54:52 +010074static const char *submodule_prefix = "";
Stefan Beller8c698322017-06-23 12:13:01 -070075static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
Stefan Bellere8906a92017-06-27 14:31:59 -070076static int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND;
Tom Miller4b3b33a2014-01-02 20:28:51 -060077static int shown_url = 0;
Brandon Williamse4cffac2018-05-16 15:58:05 -070078static struct refspec refmap = REFSPEC_INIT_FETCH;
Jeff Hostetleracb0c572017-12-08 15:58:44 +000079static struct list_objects_filter_options filter_options;
Brandon Williams5e3548e2018-04-23 15:46:24 -070080static struct string_list server_options = STRING_LIST_INIT_DUP;
Jonathan Tan3390e422018-07-02 15:39:44 -070081static struct string_list negotiation_tip = STRING_LIST_INIT_NODUP;
Johannes Schindelinc14e6e72019-11-03 00:21:56 +000082static int fetch_write_commit_graph = -1;
Jonathan Tan2b713c22020-08-17 21:01:32 -070083static int stdin_refspecs = 0;
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -040084
Michael Schubert737c5a92013-07-13 11:36:24 +020085static int git_fetch_config(const char *k, const char *v, void *cb)
86{
87 if (!strcmp(k, "fetch.prune")) {
88 fetch_prune_config = git_config_bool(k, v);
89 return 0;
90 }
Stefan Beller58f42032017-05-31 17:30:50 -070091
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +000092 if (!strcmp(k, "fetch.prunetags")) {
93 fetch_prune_tags_config = git_config_bool(k, v);
94 return 0;
95 }
96
Derrick Stoleecdbd70c2019-06-18 13:25:26 -070097 if (!strcmp(k, "fetch.showforcedupdates")) {
98 fetch_show_forced_updates = git_config_bool(k, v);
99 return 0;
100 }
101
Stefan Beller58f42032017-05-31 17:30:50 -0700102 if (!strcmp(k, "submodule.recurse")) {
103 int r = git_config_bool(k, v) ?
104 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
105 recurse_submodules = r;
106 }
107
Brandon Williamsf20e7c12017-08-02 12:49:18 -0700108 if (!strcmp(k, "submodule.fetchjobs")) {
Johannes Schindelind54dea72019-10-05 11:46:40 -0700109 submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v);
Brandon Williamsf20e7c12017-08-02 12:49:18 -0700110 return 0;
Brandon Williams8fa29152017-08-02 12:49:19 -0700111 } else if (!strcmp(k, "fetch.recursesubmodules")) {
112 recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
113 return 0;
Brandon Williamsf20e7c12017-08-02 12:49:18 -0700114 }
115
Johannes Schindelind54dea72019-10-05 11:46:40 -0700116 if (!strcmp(k, "fetch.parallel")) {
117 fetch_parallel_config = git_config_int(k, v);
118 if (fetch_parallel_config < 0)
119 die(_("fetch.parallel cannot be negative"));
120 return 0;
121 }
122
Jeff King72549df2014-11-04 08:11:19 -0500123 return git_default_config(k, v, cb);
Michael Schubert737c5a92013-07-13 11:36:24 +0200124}
125
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700126static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
127{
Jeff King517fe802018-11-05 01:45:42 -0500128 BUG_ON_OPT_NEG(unset);
129
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700130 /*
131 * "git fetch --refmap='' origin foo"
132 * can be used to tell the command not to store anywhere
133 */
Brandon Williamse4cffac2018-05-16 15:58:05 -0700134 refspec_append(&refmap, arg);
135
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700136 return 0;
137}
138
Kristian Høgsberg83201992007-12-04 02:25:47 -0500139static struct option builtin_fetch_options[] = {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100140 OPT__VERBOSITY(&verbosity),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200141 OPT_BOOL(0, "all", &all,
142 N_("fetch from all remotes")),
Corentin BOMPARD24bc1a12019-08-19 11:11:20 +0200143 OPT_BOOL(0, "set-upstream", &set_upstream,
144 N_("set upstream for git pull/fetch")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200145 OPT_BOOL('a', "append", &append,
146 N_("append to .git/FETCH_HEAD instead of overwriting")),
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700147 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
148 N_("path to upload pack on remote end")),
Ævar Arnfjörð Bjarmason8cd4b7c2018-08-31 20:09:56 +0000149 OPT__FORCE(&force, N_("force overwrite of local reference"), 0),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200150 OPT_BOOL('m', "multiple", &multiple,
151 N_("fetch from multiple remotes")),
Kristian Høgsberg83201992007-12-04 02:25:47 -0500152 OPT_SET_INT('t', "tags", &tags,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700153 N_("fetch all tags and associated objects"), TAGS_SET),
Johannes Schindeline7951292008-03-13 08:13:15 +0100154 OPT_SET_INT('n', NULL, &tags,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700155 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
Johannes Schindelind54dea72019-10-05 11:46:40 -0700156 OPT_INTEGER('j', "jobs", &max_jobs,
Stefan Beller62104ba2015-12-15 16:04:12 -0800157 N_("number of submodules fetched in parallel")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200158 OPT_BOOL('p', "prune", &prune,
159 N_("prune remote-tracking branches no longer on remote")),
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +0000160 OPT_BOOL('P', "prune-tags", &prune_tags,
161 N_("prune local tags no longer on remote and clobber changed tags")),
Denton Liu203c8532020-04-28 04:36:28 -0400162 OPT_CALLBACK_F(0, "recurse-submodules", &recurse_submodules, N_("on-demand"),
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700163 N_("control recursive fetching of submodules"),
Denton Liu203c8532020-04-28 04:36:28 -0400164 PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200165 OPT_BOOL(0, "dry-run", &dry_run,
166 N_("dry run")),
Junio C Hamano887952b2020-08-18 14:25:22 +0000167 OPT_BOOL(0, "write-fetch-head", &write_fetch_head,
168 N_("write fetched references to the FETCH_HEAD file")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200169 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
170 OPT_BOOL('u', "update-head-ok", &update_head_ok,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700171 N_("allow updating of HEAD ref")),
172 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
173 OPT_STRING(0, "depth", &depth, N_("depth"),
174 N_("deepen history of shallow clone")),
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700175 OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
176 N_("deepen history of shallow repository based on time")),
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700177 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"),
Alex Henrie6d873862016-12-04 15:03:59 -0700178 N_("deepen history of shallow clone, excluding rev")),
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700179 OPT_INTEGER(0, "deepen", &deepen_relative,
180 N_("deepen history of shallow clone")),
Nguyễn Thái Ngọc Duy3e4a67b2018-05-20 17:42:58 +0200181 OPT_SET_INT_F(0, "unshallow", &unshallow,
182 N_("convert to a complete repository"),
183 1, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700184 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
185 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
Denton Liu203c8532020-04-28 04:36:28 -0400186 OPT_CALLBACK_F(0, "recurse-submodules-default",
Stefan Beller8c698322017-06-23 12:13:01 -0700187 &recurse_submodules_default, N_("on-demand"),
188 N_("default for recursive fetching of submodules "
189 "(lower priority than config files)"),
Denton Liu203c8532020-04-28 04:36:28 -0400190 PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules),
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +0700191 OPT_BOOL(0, "update-shallow", &update_shallow,
192 N_("accept refs that update .git/shallow")),
Denton Liu203c8532020-04-28 04:36:28 -0400193 OPT_CALLBACK_F(0, "refmap", NULL, N_("refmap"),
194 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg),
Brandon Williams5e3548e2018-04-23 15:46:24 -0700195 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
Eric Wongc915f112016-02-03 04:09:14 +0000196 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
197 TRANSPORT_FAMILY_IPV4),
198 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
199 TRANSPORT_FAMILY_IPV6),
Jonathan Tan3390e422018-07-02 15:39:44 -0700200 OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"),
201 N_("report that we have only objects reachable from this object")),
Jeff Hostetleracb0c572017-12-08 15:58:44 +0000202 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
Derrick Stoleea95ce122020-09-17 18:11:44 +0000203 OPT_BOOL(0, "auto-maintenance", &enable_auto_gc,
204 N_("run 'maintenance --auto' after fetching")),
Nguyễn Thái Ngọc Duyc3d6b702019-06-19 16:46:30 +0700205 OPT_BOOL(0, "auto-gc", &enable_auto_gc,
Derrick Stoleea95ce122020-09-17 18:11:44 +0000206 N_("run 'maintenance --auto' after fetching")),
Derrick Stoleecdbd70c2019-06-18 13:25:26 -0700207 OPT_BOOL(0, "show-forced-updates", &fetch_show_forced_updates,
208 N_("check for forced-updates on all updated branches")),
Johannes Schindelinc14e6e72019-11-03 00:21:56 +0000209 OPT_BOOL(0, "write-commit-graph", &fetch_write_commit_graph,
210 N_("write the commit-graph after fetching")),
Jonathan Tan2b713c22020-08-17 21:01:32 -0700211 OPT_BOOL(0, "stdin", &stdin_refspecs,
212 N_("accept refspecs from stdin")),
Kristian Høgsberg83201992007-12-04 02:25:47 -0500213 OPT_END()
214};
215
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400216static void unlock_pack(void)
217{
Junio C Hamanoaf234452013-08-07 15:38:45 -0700218 if (gtransport)
219 transport_unlock_pack(gtransport);
Junio C Hamanob26ed432013-08-07 15:47:18 -0700220 if (gsecondary)
221 transport_unlock_pack(gsecondary);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400222}
223
224static void unlock_pack_on_signal(int signo)
225{
226 unlock_pack();
Jeff King4a16d072009-01-22 01:02:35 -0500227 sigchain_pop(signo);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400228 raise(signo);
229}
Daniel Barkalowb888d612007-09-10 23:03:25 -0400230
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400231static void add_merge_config(struct ref **head,
Daniel Barkalow45773702007-10-29 21:05:40 -0400232 const struct ref *remote_refs,
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400233 struct branch *branch,
234 struct ref ***tail)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400235{
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400236 int i;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400237
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400238 for (i = 0; i < branch->merge_nr; i++) {
239 struct ref *rm, **old_tail = *tail;
Brandon Williams0ad4a5f2018-05-16 15:57:49 -0700240 struct refspec_item refspec;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400241
242 for (rm = *head; rm; rm = rm->next) {
243 if (branch_merge_matches(branch, i, rm->name)) {
Jeff King900f2812013-05-11 18:15:59 +0200244 rm->fetch_head_status = FETCH_HEAD_MERGE;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400245 break;
246 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400247 }
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400248 if (rm)
249 continue;
250
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700251 /*
Matthieu Moy8b3f3f82010-11-02 16:31:23 +0100252 * Not fetched to a remote-tracking branch? We need to fetch
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400253 * it anyway to allow this branch's "branch.$name.merge"
Heikki Orsila05207a22008-09-09 13:28:30 +0300254 * to be honored by 'git pull', but we do not have to
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700255 * fail if branch.$name.merge is misconfigured to point
256 * at a nonexisting branch. If we were indeed called by
Heikki Orsila05207a22008-09-09 13:28:30 +0300257 * 'git pull', it will notice the misconfiguration because
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700258 * there is no entry in the resulting FETCH_HEAD marked
259 * for merging.
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400260 */
Andreas Gruenbacher8da61a22010-03-12 23:27:33 +0100261 memset(&refspec, 0, sizeof(refspec));
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400262 refspec.src = branch->merge[i]->src;
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700263 get_fetch_map(remote_refs, &refspec, tail, 1);
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400264 for (rm = *old_tail; rm; rm = rm->next)
Jeff King900f2812013-05-11 18:15:59 +0200265 rm->fetch_head_status = FETCH_HEAD_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400266 }
267}
268
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700269static void create_fetch_oidset(struct ref **head, struct oidset *out)
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100270{
271 struct ref *rm = *head;
272 while (rm) {
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700273 oidset_insert(out, &rm->old_oid);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100274 rm = rm->next;
275 }
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100276}
277
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700278struct refname_hash_entry {
Eric Wonge2b50382019-10-06 23:30:43 +0000279 struct hashmap_entry ent;
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700280 struct object_id oid;
Felipe Contrerasf80d9222019-06-03 21:13:30 -0500281 int ignore;
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700282 char refname[FLEX_ARRAY];
283};
284
285static int refname_hash_entry_cmp(const void *hashmap_cmp_fn_data,
Eric Wong939af162019-10-06 23:30:37 +0000286 const struct hashmap_entry *eptr,
287 const struct hashmap_entry *entry_or_key,
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700288 const void *keydata)
289{
Eric Wong939af162019-10-06 23:30:37 +0000290 const struct refname_hash_entry *e1, *e2;
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700291
Eric Wong939af162019-10-06 23:30:37 +0000292 e1 = container_of(eptr, const struct refname_hash_entry, ent);
293 e2 = container_of(entry_or_key, const struct refname_hash_entry, ent);
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700294 return strcmp(e1->refname, keydata ? keydata : e2->refname);
295}
296
297static struct refname_hash_entry *refname_hash_add(struct hashmap *map,
298 const char *refname,
299 const struct object_id *oid)
300{
301 struct refname_hash_entry *ent;
302 size_t len = strlen(refname);
303
304 FLEX_ALLOC_MEM(ent, refname, refname, len);
Eric Wongd22245a2019-10-06 23:30:27 +0000305 hashmap_entry_init(&ent->ent, strhash(refname));
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700306 oidcpy(&ent->oid, oid);
Eric Wongb94e5c12019-10-06 23:30:29 +0000307 hashmap_add(map, &ent->ent);
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700308 return ent;
309}
310
311static int add_one_refname(const char *refname,
312 const struct object_id *oid,
313 int flag, void *cbdata)
314{
315 struct hashmap *refname_map = cbdata;
316
317 (void) refname_hash_add(refname_map, refname, oid);
318 return 0;
319}
320
321static void refname_hash_init(struct hashmap *map)
322{
323 hashmap_init(map, refname_hash_entry_cmp, NULL, 0);
324}
325
326static int refname_hash_exists(struct hashmap *map, const char *refname)
327{
328 return !!hashmap_get_from_hash(map, strhash(refname), refname);
329}
330
Felipe Contrerasa8363b52019-06-03 21:13:28 -0500331static void clear_item(struct refname_hash_entry *item)
332{
Felipe Contrerasf80d9222019-06-03 21:13:30 -0500333 item->ignore = 1;
Felipe Contrerasa8363b52019-06-03 21:13:28 -0500334}
335
Brandon Williams6d1700d2018-06-27 15:30:21 -0700336static void find_non_local_tags(const struct ref *refs,
337 struct ref **head,
338 struct ref ***tail)
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100339{
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700340 struct hashmap existing_refs;
341 struct hashmap remote_refs;
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700342 struct oidset fetch_oids = OIDSET_INIT;
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700343 struct string_list remote_refs_list = STRING_LIST_INIT_NODUP;
344 struct string_list_item *remote_ref_item;
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100345 const struct ref *ref;
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700346 struct refname_hash_entry *item = NULL;
Derrick Stolee3e96c662020-02-21 21:47:28 +0000347 const int quick_flags = OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT;
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100348
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700349 refname_hash_init(&existing_refs);
350 refname_hash_init(&remote_refs);
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700351 create_fetch_oidset(head, &fetch_oids);
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700352
353 for_each_ref(add_one_refname, &existing_refs);
Brandon Williams6d1700d2018-06-27 15:30:21 -0700354 for (ref = refs; ref; ref = ref->next) {
Junio C Hamanoad704482013-12-17 11:47:35 -0800355 if (!starts_with(ref->name, "refs/tags/"))
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100356 continue;
357
358 /*
359 * The peeled ref always follows the matching base
360 * ref, so if we see a peeled ref that we don't want
361 * to fetch then we can mark the ref entry in the list
362 * as one to ignore by setting util to NULL.
363 */
Junio C Hamanoad704482013-12-17 11:47:35 -0800364 if (ends_with(ref->name, "^{}")) {
Jeff King5827a032016-10-13 12:53:44 -0400365 if (item &&
Derrick Stolee3e96c662020-02-21 21:47:28 +0000366 !has_object_file_with_flags(&ref->old_oid, quick_flags) &&
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700367 !oidset_contains(&fetch_oids, &ref->old_oid) &&
Derrick Stolee3e96c662020-02-21 21:47:28 +0000368 !has_object_file_with_flags(&item->oid, quick_flags) &&
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700369 !oidset_contains(&fetch_oids, &item->oid))
Felipe Contrerasa8363b52019-06-03 21:13:28 -0500370 clear_item(item);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100371 item = NULL;
372 continue;
373 }
374
375 /*
376 * If item is non-NULL here, then we previously saw a
377 * ref not followed by a peeled reference, so we need
378 * to check if it is a lightweight tag that we want to
379 * fetch.
380 */
Jeff King5827a032016-10-13 12:53:44 -0400381 if (item &&
Derrick Stolee3e96c662020-02-21 21:47:28 +0000382 !has_object_file_with_flags(&item->oid, quick_flags) &&
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700383 !oidset_contains(&fetch_oids, &item->oid))
Felipe Contrerasa8363b52019-06-03 21:13:28 -0500384 clear_item(item);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100385
386 item = NULL;
387
388 /* skip duplicates and refs that we already have */
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700389 if (refname_hash_exists(&remote_refs, ref->name) ||
390 refname_hash_exists(&existing_refs, ref->name))
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100391 continue;
392
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700393 item = refname_hash_add(&remote_refs, ref->name, &ref->old_oid);
394 string_list_insert(&remote_refs_list, ref->name);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100395 }
Elijah Newren6da1a252020-11-02 18:55:05 +0000396 hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100397
398 /*
399 * We may have a final lightweight tag that needs to be
400 * checked to see if it needs fetching.
401 */
Jeff King5827a032016-10-13 12:53:44 -0400402 if (item &&
Derrick Stolee3e96c662020-02-21 21:47:28 +0000403 !has_object_file_with_flags(&item->oid, quick_flags) &&
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700404 !oidset_contains(&fetch_oids, &item->oid))
Felipe Contrerasa8363b52019-06-03 21:13:28 -0500405 clear_item(item);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100406
407 /*
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700408 * For all the tags in the remote_refs_list,
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100409 * add them to the list of refs to be fetched
410 */
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700411 for_each_string_list_item(remote_ref_item, &remote_refs_list) {
412 const char *refname = remote_ref_item->string;
Felipe Contreras9528b802019-06-03 21:13:29 -0500413 struct ref *rm;
Eric Wongf23a4652019-10-06 23:30:36 +0000414 unsigned int hash = strhash(refname);
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700415
Eric Wongf23a4652019-10-06 23:30:36 +0000416 item = hashmap_get_entry_from_hash(&remote_refs, hash, refname,
417 struct refname_hash_entry, ent);
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700418 if (!item)
419 BUG("unseen remote ref?");
420
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100421 /* Unless we have already decided to ignore this item... */
Felipe Contrerasf80d9222019-06-03 21:13:30 -0500422 if (item->ignore)
Felipe Contreras9528b802019-06-03 21:13:29 -0500423 continue;
424
425 rm = alloc_ref(item->refname);
426 rm->peer_ref = alloc_ref(item->refname);
427 oidcpy(&rm->old_oid, &item->oid);
428 **tail = rm;
429 *tail = &rm->next;
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100430 }
Elijah Newren6da1a252020-11-02 18:55:05 +0000431 hashmap_clear_and_free(&remote_refs, struct refname_hash_entry, ent);
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700432 string_list_clear(&remote_refs_list, 0);
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700433 oidset_clear(&fetch_oids);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100434}
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500435
Brandon Williams6d1700d2018-06-27 15:30:21 -0700436static struct ref *get_ref_map(struct remote *remote,
437 const struct ref *remote_refs,
Brandon Williams65d96c82018-05-16 15:58:08 -0700438 struct refspec *rs,
Michael Haggertyf137a452013-10-23 17:50:38 +0200439 int tags, int *autotags)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400440{
441 int i;
442 struct ref *rm;
443 struct ref *ref_map = NULL;
444 struct ref **tail = &ref_map;
445
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100446 /* opportunistically-updated references: */
447 struct ref *orefs = NULL, **oref_tail = &orefs;
448
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700449 struct hashmap existing_refs;
Jonathan Tanabcb7ee2020-08-17 21:01:34 -0700450 int existing_refs_populated = 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400451
Brandon Williams65d96c82018-05-16 15:58:08 -0700452 if (rs->nr) {
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700453 struct refspec *fetch_refspec;
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700454
Brandon Williams65d96c82018-05-16 15:58:08 -0700455 for (i = 0; i < rs->nr; i++) {
456 get_fetch_map(remote_refs, &rs->items[i], &tail, 0);
457 if (rs->items[i].dst && rs->items[i].dst[0])
Daniel Barkalowb888d612007-09-10 23:03:25 -0400458 *autotags = 1;
459 }
Michael Haggerty0281c932013-10-30 06:32:58 +0100460 /* Merge everything on the command line (but not --tags) */
Daniel Barkalowb888d612007-09-10 23:03:25 -0400461 for (rm = ref_map; rm; rm = rm->next)
Jeff King900f2812013-05-11 18:15:59 +0200462 rm->fetch_head_status = FETCH_HEAD_MERGE;
Michael Haggerty0281c932013-10-30 06:32:58 +0100463
464 /*
465 * For any refs that we happen to be fetching via
466 * command-line arguments, the destination ref might
467 * have been missing or have been different than the
468 * remote-tracking ref that would be derived from the
469 * configured refspec. In these cases, we want to
470 * take the opportunity to update their configured
471 * remote-tracking reference. However, we do not want
472 * to mention these entries in FETCH_HEAD at all, as
473 * they would simply be duplicates of existing
474 * entries, so we set them FETCH_HEAD_IGNORE below.
475 *
476 * We compute these entries now, based only on the
477 * refspecs specified on the command line. But we add
478 * them to the list following the refspecs resulting
479 * from the tags option so that one of the latter,
480 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
481 * by ref_remove_duplicates() in favor of one of these
482 * opportunistic entries with FETCH_HEAD_IGNORE.
483 */
Brandon Williams65d96c82018-05-16 15:58:08 -0700484 if (refmap.nr)
485 fetch_refspec = &refmap;
486 else
Brandon Williams6d1700d2018-06-27 15:30:21 -0700487 fetch_refspec = &remote->fetch;
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700488
Brandon Williams65d96c82018-05-16 15:58:08 -0700489 for (i = 0; i < fetch_refspec->nr; i++)
490 get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
Brandon Williamse4cffac2018-05-16 15:58:05 -0700491 } else if (refmap.nr) {
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700492 die("--refmap option is only meaningful with command-line refspec(s).");
Daniel Barkalowb888d612007-09-10 23:03:25 -0400493 } else {
494 /* Use the defaults */
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400495 struct branch *branch = branch_get(NULL);
496 int has_merge = branch_has_merge_config(branch);
Brandon Casey3ee17572010-08-25 12:52:56 -0500497 if (remote &&
Brandon Williamse5349ab2018-05-16 15:58:01 -0700498 (remote->fetch.nr ||
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500499 /* Note: has_merge implies non-NULL branch->remote_name */
Brandon Casey3ee17572010-08-25 12:52:56 -0500500 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
Brandon Williamse5349ab2018-05-16 15:58:01 -0700501 for (i = 0; i < remote->fetch.nr; i++) {
502 get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);
503 if (remote->fetch.items[i].dst &&
504 remote->fetch.items[i].dst[0])
Daniel Barkalowb888d612007-09-10 23:03:25 -0400505 *autotags = 1;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400506 if (!i && !has_merge && ref_map &&
Brandon Williamse5349ab2018-05-16 15:58:01 -0700507 !remote->fetch.items[0].pattern)
Jeff King900f2812013-05-11 18:15:59 +0200508 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400509 }
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100510 /*
511 * if the remote we're fetching from is the same
512 * as given in branch.<name>.remote, we add the
513 * ref given in branch.<name>.merge, too.
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500514 *
515 * Note: has_merge implies non-NULL branch->remote_name
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100516 */
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700517 if (has_merge &&
518 !strcmp(branch->remote_name, remote->name))
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400519 add_merge_config(&ref_map, remote_refs, branch, &tail);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400520 } else {
521 ref_map = get_remote_ref(remote_refs, "HEAD");
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700522 if (!ref_map)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000523 die(_("Couldn't find remote ref HEAD"));
Jeff King900f2812013-05-11 18:15:59 +0200524 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
Shawn O. Pearce5aaf7f22008-03-02 21:34:51 -0500525 tail = &ref_map->next;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400526 }
527 }
Michael Haggerty0281c932013-10-30 06:32:58 +0100528
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100529 if (tags == TAGS_SET)
530 /* also fetch all tags */
531 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
532 else if (tags == TAGS_DEFAULT && *autotags)
Brandon Williams6d1700d2018-06-27 15:30:21 -0700533 find_non_local_tags(remote_refs, &ref_map, &tail);
Michael Haggerty0281c932013-10-30 06:32:58 +0100534
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100535 /* Now append any refs to be updated opportunistically: */
536 *tail = orefs;
537 for (rm = orefs; rm; rm = rm->next) {
538 rm->fetch_head_status = FETCH_HEAD_IGNORE;
539 tail = &rm->next;
540 }
541
Jacob Kellerc0192df2020-09-30 14:25:29 -0700542 /*
543 * apply negative refspecs first, before we remove duplicates. This is
544 * necessary as negative refspecs might remove an otherwise conflicting
545 * duplicate.
546 */
547 if (rs->nr)
548 ref_map = apply_negative_refspecs(ref_map, rs);
549 else
550 ref_map = apply_negative_refspecs(ref_map, &remote->fetch);
551
Brandon Williams14b8ced2018-06-27 15:30:19 -0700552 ref_map = ref_remove_duplicates(ref_map);
553
Brandon Williams14b8ced2018-06-27 15:30:19 -0700554 for (rm = ref_map; rm; rm = rm->next) {
555 if (rm->peer_ref) {
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700556 const char *refname = rm->peer_ref->name;
557 struct refname_hash_entry *peer_item;
Eric Wongf23a4652019-10-06 23:30:36 +0000558 unsigned int hash = strhash(refname);
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700559
Jonathan Tanabcb7ee2020-08-17 21:01:34 -0700560 if (!existing_refs_populated) {
561 refname_hash_init(&existing_refs);
562 for_each_ref(add_one_refname, &existing_refs);
563 existing_refs_populated = 1;
564 }
565
Eric Wongf23a4652019-10-06 23:30:36 +0000566 peer_item = hashmap_get_entry_from_hash(&existing_refs,
567 hash, refname,
568 struct refname_hash_entry, ent);
Brandon Williams14b8ced2018-06-27 15:30:19 -0700569 if (peer_item) {
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700570 struct object_id *old_oid = &peer_item->oid;
Brandon Williams14b8ced2018-06-27 15:30:19 -0700571 oidcpy(&rm->peer_ref->old_oid, old_oid);
572 }
573 }
574 }
Jonathan Tanabcb7ee2020-08-17 21:01:34 -0700575 if (existing_refs_populated)
Elijah Newren6da1a252020-11-02 18:55:05 +0000576 hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);
Brandon Williams14b8ced2018-06-27 15:30:19 -0700577
578 return ref_map;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400579}
580
Jeff Kingfa250752009-05-25 06:40:54 -0400581#define STORE_REF_ERROR_OTHER 1
582#define STORE_REF_ERROR_DF_CONFLICT 2
583
Daniel Barkalowb888d612007-09-10 23:03:25 -0400584static int s_update_ref(const char *action,
585 struct ref *ref,
586 int check_old)
587{
Jeff King1412f762017-03-28 15:46:26 -0400588 char *msg;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400589 char *rla = getenv("GIT_REFLOG_ACTION");
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700590 struct ref_transaction *transaction;
591 struct strbuf err = STRBUF_INIT;
Patrick Steinhardtc45889f2021-01-12 13:27:43 +0100592 int ret;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400593
Jay Soffian28a15402009-11-10 09:19:43 +0100594 if (dry_run)
595 return 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400596 if (!rla)
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -0500597 rla = default_rla.buf;
Jeff King1412f762017-03-28 15:46:26 -0400598 msg = xstrfmt("%s: %s", rla, action);
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700599
600 transaction = ref_transaction_begin(&err);
Patrick Steinhardtc45889f2021-01-12 13:27:43 +0100601 if (!transaction) {
602 ret = STORE_REF_ERROR_OTHER;
603 goto out;
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700604 }
605
Patrick Steinhardtc45889f2021-01-12 13:27:43 +0100606 ret = ref_transaction_update(transaction, ref->name, &ref->new_oid,
607 check_old ? &ref->old_oid : NULL,
608 0, msg, &err);
609 if (ret) {
610 ret = STORE_REF_ERROR_OTHER;
611 goto out;
612 }
613
614 switch (ref_transaction_commit(transaction, &err)) {
615 case 0:
616 break;
617 case TRANSACTION_NAME_CONFLICT:
618 ret = STORE_REF_ERROR_DF_CONFLICT;
619 goto out;
620 default:
621 ret = STORE_REF_ERROR_OTHER;
622 goto out;
623 }
624
625out:
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700626 ref_transaction_free(transaction);
Patrick Steinhardtc45889f2021-01-12 13:27:43 +0100627 if (ret)
628 error("%s", err.buf);
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700629 strbuf_release(&err);
Jeff King1412f762017-03-28 15:46:26 -0400630 free(msg);
Patrick Steinhardtc45889f2021-01-12 13:27:43 +0100631 return ret;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400632}
633
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200634static int refcol_width = 10;
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200635static int compact_format;
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200636
637static void adjust_refcol_width(const struct ref *ref)
638{
639 int max, rlen, llen, len;
640
641 /* uptodate lines are only shown on high verbosity level */
Jeff King4a7e27e2018-08-28 17:22:40 -0400642 if (!verbosity && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200643 return;
644
645 max = term_columns();
646 rlen = utf8_strwidth(prettify_refname(ref->name));
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200647
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200648 llen = utf8_strwidth(prettify_refname(ref->peer_ref->name));
649
650 /*
651 * rough estimation to see if the output line is too long and
652 * should not be counted (we can't do precise calculation
653 * anyway because we don't know if the error explanation part
654 * will be printed in update_local_ref)
655 */
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200656 if (compact_format) {
657 llen = 0;
658 max = max * 2 / 3;
659 }
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200660 len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
661 if (len >= max)
662 return;
663
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200664 /*
665 * Not precise calculation for compact mode because '*' can
666 * appear on the left hand side of '->' and shrink the column
667 * back.
668 */
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200669 if (refcol_width < rlen)
670 refcol_width = rlen;
671}
672
673static void prepare_format_display(struct ref *ref_map)
674{
675 struct ref *rm;
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200676 const char *format = "full";
677
Jeff Kingf1de9812020-08-14 12:17:36 -0400678 git_config_get_string_tmp("fetch.output", &format);
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200679 if (!strcasecmp(format, "full"))
680 compact_format = 0;
681 else if (!strcasecmp(format, "compact"))
682 compact_format = 1;
683 else
684 die(_("configuration fetch.output contains invalid value %s"),
685 format);
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200686
687 for (rm = ref_map; rm; rm = rm->next) {
688 if (rm->status == REF_STATUS_REJECT_SHALLOW ||
689 !rm->peer_ref ||
690 !strcmp(rm->name, "HEAD"))
691 continue;
692
693 adjust_refcol_width(rm);
694 }
695}
Nicolas Pitre165f3902007-11-03 01:32:48 -0400696
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200697static void print_remote_to_local(struct strbuf *display,
698 const char *remote, const char *local)
699{
700 strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local);
701}
702
703static int find_and_replace(struct strbuf *haystack,
704 const char *needle,
705 const char *placeholder)
706{
Nguyễn Thái Ngọc Duydc40b242019-01-25 16:51:22 +0700707 const char *p = NULL;
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200708 int plen, nlen;
709
Nguyễn Thái Ngọc Duydc40b242019-01-25 16:51:22 +0700710 nlen = strlen(needle);
711 if (ends_with(haystack->buf, needle))
712 p = haystack->buf + haystack->len - nlen;
713 else
714 p = strstr(haystack->buf, needle);
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200715 if (!p)
716 return 0;
717
718 if (p > haystack->buf && p[-1] != '/')
719 return 0;
720
721 plen = strlen(p);
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200722 if (plen > nlen && p[nlen] != '/')
723 return 0;
724
725 strbuf_splice(haystack, p - haystack->buf, nlen,
726 placeholder, strlen(placeholder));
727 return 1;
728}
729
730static void print_compact(struct strbuf *display,
731 const char *remote, const char *local)
732{
733 struct strbuf r = STRBUF_INIT;
734 struct strbuf l = STRBUF_INIT;
735
736 if (!strcmp(remote, local)) {
737 strbuf_addf(display, "%-*s -> *", refcol_width, remote);
738 return;
739 }
740
741 strbuf_addstr(&r, remote);
742 strbuf_addstr(&l, local);
743
744 if (!find_and_replace(&r, local, "*"))
745 find_and_replace(&l, remote, "*");
746 print_remote_to_local(display, r.buf, l.buf);
747
748 strbuf_release(&r);
749 strbuf_release(&l);
750}
751
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200752static void format_display(struct strbuf *display, char code,
753 const char *summary, const char *error,
Junio C Hamano901f3d42016-10-21 15:22:55 -0700754 const char *remote, const char *local,
755 int summary_width)
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200756{
Junio C Hamano901f3d42016-10-21 15:22:55 -0700757 int width = (summary_width + strlen(summary) - gettext_width(summary));
758
759 strbuf_addf(display, "%c %-*s ", code, width, summary);
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200760 if (!compact_format)
761 print_remote_to_local(display, remote, local);
762 else
763 print_compact(display, remote, local);
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200764 if (error)
765 strbuf_addf(display, " (%s)", error);
766}
Daniel Barkalowb888d612007-09-10 23:03:25 -0400767
768static int update_local_ref(struct ref *ref,
Nicolas Pitre165f3902007-11-03 01:32:48 -0400769 const char *remote,
Marc Branchaud6da618d2012-04-16 18:08:49 -0400770 const struct ref *remote_ref,
Junio C Hamano901f3d42016-10-21 15:22:55 -0700771 struct strbuf *display,
772 int summary_width)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400773{
Daniel Barkalowb888d612007-09-10 23:03:25 -0400774 struct commit *current = NULL, *updated;
775 enum object_type type;
776 struct branch *current_branch = branch_get(NULL);
Felipe Contreras4577e482009-05-14 00:22:04 +0300777 const char *pretty_ref = prettify_refname(ref->name);
Derrick Stolee377444b2019-06-18 13:25:27 -0700778 int fast_forward = 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400779
Stefan Beller0df8e962018-04-25 11:20:59 -0700780 type = oid_object_info(the_repository, &ref->new_oid, NULL);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400781 if (type < 0)
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000782 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
Daniel Barkalowb888d612007-09-10 23:03:25 -0400783
Jeff King4a7e27e2018-08-28 17:22:40 -0400784 if (oideq(&ref->old_oid, &ref->new_oid)) {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100785 if (verbosity > 0)
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200786 format_display(display, '=', _("[up to date]"), NULL,
Junio C Hamano901f3d42016-10-21 15:22:55 -0700787 remote, pretty_ref, summary_width);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400788 return 0;
789 }
790
Shawn O. Pearceb3abdd92007-09-16 02:31:26 -0400791 if (current_branch &&
792 !strcmp(ref->name, current_branch->name) &&
Daniel Barkalowb888d612007-09-10 23:03:25 -0400793 !(update_head_ok || is_bare_repository()) &&
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000794 !is_null_oid(&ref->old_oid)) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400795 /*
796 * If this is the head, and it's not okay to update
797 * the head, and the old value of the head isn't empty...
798 */
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200799 format_display(display, '!', _("[rejected]"),
800 _("can't fetch in current branch"),
Junio C Hamano901f3d42016-10-21 15:22:55 -0700801 remote, pretty_ref, summary_width);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400802 return 1;
803 }
804
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000805 if (!is_null_oid(&ref->old_oid) &&
Christian Couder59556542013-11-30 21:55:40 +0100806 starts_with(ref->name, "refs/tags/")) {
Ævar Arnfjörð Bjarmason0bc8d712018-08-31 20:10:04 +0000807 if (force || ref->force) {
808 int r;
809 r = s_update_ref("updating tag", ref, 0);
810 format_display(display, r ? '!' : 't', _("[tag update]"),
811 r ? _("unable to update local ref") : NULL,
812 remote, pretty_ref, summary_width);
813 return r;
814 } else {
815 format_display(display, '!', _("[rejected]"), _("would clobber existing tag"),
816 remote, pretty_ref, summary_width);
817 return 1;
818 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400819 }
820
Stefan Beller21e1ee82018-06-28 18:21:57 -0700821 current = lookup_commit_reference_gently(the_repository,
822 &ref->old_oid, 1);
823 updated = lookup_commit_reference_gently(the_repository,
824 &ref->new_oid, 1);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400825 if (!current || !updated) {
Nicolas Pitre165f3902007-11-03 01:32:48 -0400826 const char *msg;
827 const char *what;
Jeff King63154722008-06-26 23:59:50 -0400828 int r;
Marc Branchaud0997ada2012-04-16 18:08:50 -0400829 /*
830 * Nicely describe the new ref we're fetching.
831 * Base this on the remote's ref name, as it's
832 * more likely to follow a standard layout.
833 */
834 const char *name = remote_ref ? remote_ref->name : "";
Christian Couder59556542013-11-30 21:55:40 +0100835 if (starts_with(name, "refs/tags/")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400836 msg = "storing tag";
Ævar Arnfjörð Bjarmasonf7b37422011-02-22 23:41:53 +0000837 what = _("[new tag]");
Christian Couder59556542013-11-30 21:55:40 +0100838 } else if (starts_with(name, "refs/heads/")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400839 msg = "storing head";
Ævar Arnfjörð Bjarmasonf7b37422011-02-22 23:41:53 +0000840 what = _("[new branch]");
Marc Branchaud0997ada2012-04-16 18:08:50 -0400841 } else {
842 msg = "storing ref";
843 what = _("[new ref]");
Nicolas Pitre165f3902007-11-03 01:32:48 -0400844 }
845
Jeff King63154722008-06-26 23:59:50 -0400846 r = s_update_ref(msg, ref, 0);
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200847 format_display(display, r ? '!' : '*', what,
848 r ? _("unable to update local ref") : NULL,
Junio C Hamano901f3d42016-10-21 15:22:55 -0700849 remote, pretty_ref, summary_width);
Jeff King63154722008-06-26 23:59:50 -0400850 return r;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400851 }
852
Derrick Stolee377444b2019-06-18 13:25:27 -0700853 if (fetch_show_forced_updates) {
854 uint64_t t_before = getnanotime();
855 fast_forward = in_merge_bases(current, updated);
856 forced_updates_ms += (getnanotime() - t_before) / 1000000;
857 } else {
858 fast_forward = 1;
859 }
860
861 if (fast_forward) {
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400862 struct strbuf quickref = STRBUF_INIT;
Jeff King63154722008-06-26 23:59:50 -0400863 int r;
Derrick Stoleecdbd70c2019-06-18 13:25:26 -0700864
brian m. carlson30e677e2018-03-12 02:27:28 +0000865 strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400866 strbuf_addstr(&quickref, "..");
brian m. carlson30e677e2018-03-12 02:27:28 +0000867 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300868 r = s_update_ref("fast-forward", ref, 1);
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200869 format_display(display, r ? '!' : ' ', quickref.buf,
870 r ? _("unable to update local ref") : NULL,
Junio C Hamano901f3d42016-10-21 15:22:55 -0700871 remote, pretty_ref, summary_width);
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400872 strbuf_release(&quickref);
Jeff King63154722008-06-26 23:59:50 -0400873 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400874 } else if (force || ref->force) {
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400875 struct strbuf quickref = STRBUF_INIT;
Jeff King63154722008-06-26 23:59:50 -0400876 int r;
brian m. carlson30e677e2018-03-12 02:27:28 +0000877 strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400878 strbuf_addstr(&quickref, "...");
brian m. carlson30e677e2018-03-12 02:27:28 +0000879 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
Jeff King63154722008-06-26 23:59:50 -0400880 r = s_update_ref("forced-update", ref, 1);
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200881 format_display(display, r ? '!' : '+', quickref.buf,
882 r ? _("unable to update local ref") : _("forced update"),
Junio C Hamano901f3d42016-10-21 15:22:55 -0700883 remote, pretty_ref, summary_width);
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400884 strbuf_release(&quickref);
Jeff King63154722008-06-26 23:59:50 -0400885 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400886 } else {
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200887 format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
Junio C Hamano901f3d42016-10-21 15:22:55 -0700888 remote, pretty_ref, summary_width);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400889 return 1;
890 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400891}
892
brian m. carlson6ccac9e2017-10-15 22:06:54 +0000893static int iterate_ref_map(void *cb_data, struct object_id *oid)
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700894{
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700895 struct ref **rm = cb_data;
896 struct ref *ref = *rm;
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700897
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +0700898 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
899 ref = ref->next;
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700900 if (!ref)
901 return -1; /* end of the list */
902 *rm = ref->next;
brian m. carlson6ccac9e2017-10-15 22:06:54 +0000903 oidcpy(oid, &ref->old_oid);
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700904 return 0;
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700905}
906
Patrick Steinhardt58a646a2021-01-12 13:27:35 +0100907struct fetch_head {
908 FILE *fp;
Patrick Steinhardt929d0442021-01-12 13:27:39 +0100909 struct strbuf buf;
Patrick Steinhardt58a646a2021-01-12 13:27:35 +0100910};
911
912static int open_fetch_head(struct fetch_head *fetch_head)
913{
914 const char *filename = git_path_fetch_head(the_repository);
915
916 if (write_fetch_head) {
917 fetch_head->fp = fopen(filename, "a");
918 if (!fetch_head->fp)
919 return error_errno(_("cannot open %s"), filename);
Patrick Steinhardt929d0442021-01-12 13:27:39 +0100920 strbuf_init(&fetch_head->buf, 0);
Patrick Steinhardt58a646a2021-01-12 13:27:35 +0100921 } else {
922 fetch_head->fp = NULL;
923 }
924
925 return 0;
926}
927
928static void append_fetch_head(struct fetch_head *fetch_head,
929 const struct object_id *old_oid,
930 enum fetch_head_status fetch_head_status,
931 const char *note,
932 const char *url, size_t url_len)
933{
934 char old_oid_hex[GIT_MAX_HEXSZ + 1];
935 const char *merge_status_marker;
936 size_t i;
937
938 if (!fetch_head->fp)
939 return;
940
941 switch (fetch_head_status) {
942 case FETCH_HEAD_NOT_FOR_MERGE:
943 merge_status_marker = "not-for-merge";
944 break;
945 case FETCH_HEAD_MERGE:
946 merge_status_marker = "";
947 break;
948 default:
949 /* do not write anything to FETCH_HEAD */
950 return;
951 }
952
Patrick Steinhardt929d0442021-01-12 13:27:39 +0100953 strbuf_addf(&fetch_head->buf, "%s\t%s\t%s",
954 oid_to_hex_r(old_oid_hex, old_oid), merge_status_marker, note);
Patrick Steinhardt58a646a2021-01-12 13:27:35 +0100955 for (i = 0; i < url_len; ++i)
956 if ('\n' == url[i])
Patrick Steinhardt929d0442021-01-12 13:27:39 +0100957 strbuf_addstr(&fetch_head->buf, "\\n");
Patrick Steinhardt58a646a2021-01-12 13:27:35 +0100958 else
Patrick Steinhardt929d0442021-01-12 13:27:39 +0100959 strbuf_addch(&fetch_head->buf, url[i]);
960 strbuf_addch(&fetch_head->buf, '\n');
961
962 strbuf_write(&fetch_head->buf, fetch_head->fp);
963 strbuf_reset(&fetch_head->buf);
Patrick Steinhardt58a646a2021-01-12 13:27:35 +0100964}
965
966static void commit_fetch_head(struct fetch_head *fetch_head)
967{
968 /* Nothing to commit yet. */
969}
970
971static void close_fetch_head(struct fetch_head *fetch_head)
972{
973 if (!fetch_head->fp)
974 return;
975
976 fclose(fetch_head->fp);
Patrick Steinhardt929d0442021-01-12 13:27:39 +0100977 strbuf_release(&fetch_head->buf);
Patrick Steinhardt58a646a2021-01-12 13:27:35 +0100978}
979
Jean-Noël Avila182f59d2019-08-06 19:19:52 +0200980static const char warn_show_forced_updates[] =
981N_("Fetch normally indicates which branches had a forced update,\n"
982 "but that check has been disabled. To re-enable, use '--show-forced-updates'\n"
983 "flag or run 'git config fetch.showForcedUpdates true'.");
984static const char warn_time_show_forced_updates[] =
985N_("It took %.2f seconds to check forced updates. You can use\n"
986 "'--no-show-forced-updates' or run 'git config fetch.showForcedUpdates false'\n"
987 " to avoid this check.\n");
988
Andreas Ericsson47abd852009-04-17 10:20:11 +0200989static int store_updated_refs(const char *raw_url, const char *remote_name,
Jonathan Tancf1e7c02018-07-02 15:08:43 -0700990 int connectivity_checked, struct ref *ref_map)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400991{
Patrick Steinhardt58a646a2021-01-12 13:27:35 +0100992 struct fetch_head fetch_head;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400993 struct commit *commit;
Tom Miller4b3b33a2014-01-02 20:28:51 -0600994 int url_len, i, rc = 0;
Jeff King5914f2d2011-12-08 03:43:19 -0500995 struct strbuf note = STRBUF_INIT;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400996 const char *what, *kind;
997 struct ref *rm;
Nguyễn Thái Ngọc Duydcf69262014-11-30 15:24:27 +0700998 char *url;
Jeff King900f2812013-05-11 18:15:59 +0200999 int want_status;
Junio C Hamano11fd66d2016-10-21 15:28:07 -07001000 int summary_width = transport_summary_width(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001001
Patrick Steinhardt58a646a2021-01-12 13:27:35 +01001002 rc = open_fetch_head(&fetch_head);
1003 if (rc)
1004 return -1;
Andreas Ericsson47abd852009-04-17 10:20:11 +02001005
Daniel Barkalowfb0cc872009-11-18 02:42:22 +01001006 if (raw_url)
1007 url = transport_anonymize_url(raw_url);
1008 else
1009 url = xstrdup("foreign");
Junio C Hamano6b67e0d2011-09-01 15:43:35 -07001010
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001011 if (!connectivity_checked) {
Jonathan Tan2df1aa22020-01-11 20:15:25 -08001012 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1013
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001014 rm = ref_map;
Jonathan Tan2df1aa22020-01-11 20:15:25 -08001015 if (check_connected(iterate_ref_map, &rm, &opt)) {
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001016 rc = error(_("%s did not send all necessary objects\n"), url);
1017 goto abort;
1018 }
Tay Ray Chuan9516a592011-10-07 15:40:22 +08001019 }
Junio C Hamano6b67e0d2011-09-01 15:43:35 -07001020
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +02001021 prepare_format_display(ref_map);
1022
Joey Hess96890f42011-12-26 12:16:56 -04001023 /*
Jeff King900f2812013-05-11 18:15:59 +02001024 * We do a pass for each fetch_head_status type in their enum order, so
1025 * merged entries are written before not-for-merge. That lets readers
1026 * use FETCH_HEAD as a refname to refer to the ref to be merged.
Joey Hess96890f42011-12-26 12:16:56 -04001027 */
Jeff King900f2812013-05-11 18:15:59 +02001028 for (want_status = FETCH_HEAD_MERGE;
1029 want_status <= FETCH_HEAD_IGNORE;
1030 want_status++) {
Joey Hess96890f42011-12-26 12:16:56 -04001031 for (rm = ref_map; rm; rm = rm->next) {
1032 struct ref *ref = NULL;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001033
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001034 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
1035 if (want_status == FETCH_HEAD_MERGE)
1036 warning(_("reject %s because shallow roots are not allowed to be updated"),
1037 rm->peer_ref ? rm->peer_ref->name : rm->name);
1038 continue;
1039 }
1040
Stefan Beller21e1ee82018-06-28 18:21:57 -07001041 commit = lookup_commit_reference_gently(the_repository,
1042 &rm->old_oid,
brian m. carlsonbc832662017-05-06 22:10:10 +00001043 1);
Joey Hess96890f42011-12-26 12:16:56 -04001044 if (!commit)
Jeff King900f2812013-05-11 18:15:59 +02001045 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001046
Jeff King900f2812013-05-11 18:15:59 +02001047 if (rm->fetch_head_status != want_status)
Joey Hess96890f42011-12-26 12:16:56 -04001048 continue;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001049
Joey Hess96890f42011-12-26 12:16:56 -04001050 if (rm->peer_ref) {
Jeff King6f687c22015-09-24 17:08:09 -04001051 ref = alloc_ref(rm->peer_ref->name);
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001052 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
1053 oidcpy(&ref->new_oid, &rm->old_oid);
Joey Hess96890f42011-12-26 12:16:56 -04001054 ref->force = rm->peer_ref->force;
Nicolas Pitre165f3902007-11-03 01:32:48 -04001055 }
Joey Hess96890f42011-12-26 12:16:56 -04001056
Orgad Shaneh7ea0c2f2020-09-04 13:50:49 +00001057 if (recurse_submodules != RECURSE_SUBMODULES_OFF &&
1058 (!rm->peer_ref || !oideq(&ref->old_oid, &ref->new_oid))) {
Stefan Bellerbe76c212018-12-06 13:26:55 -08001059 check_for_new_submodule_commits(&rm->old_oid);
Orgad Shaneh7ea0c2f2020-09-04 13:50:49 +00001060 }
Joey Hess96890f42011-12-26 12:16:56 -04001061
1062 if (!strcmp(rm->name, "HEAD")) {
1063 kind = "";
1064 what = "";
1065 }
René Scharfea6293f52019-11-26 12:18:26 +01001066 else if (skip_prefix(rm->name, "refs/heads/", &what))
Joey Hess96890f42011-12-26 12:16:56 -04001067 kind = "branch";
René Scharfea6293f52019-11-26 12:18:26 +01001068 else if (skip_prefix(rm->name, "refs/tags/", &what))
Joey Hess96890f42011-12-26 12:16:56 -04001069 kind = "tag";
René Scharfea6293f52019-11-26 12:18:26 +01001070 else if (skip_prefix(rm->name, "refs/remotes/", &what))
Joey Hess96890f42011-12-26 12:16:56 -04001071 kind = "remote-tracking branch";
Joey Hess96890f42011-12-26 12:16:56 -04001072 else {
1073 kind = "";
1074 what = rm->name;
1075 }
1076
1077 url_len = strlen(url);
1078 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
1079 ;
1080 url_len = i + 1;
1081 if (4 < i && !strncmp(".git", url + i - 3, 4))
1082 url_len = i - 3;
1083
1084 strbuf_reset(&note);
1085 if (*what) {
1086 if (*kind)
1087 strbuf_addf(&note, "%s ", kind);
1088 strbuf_addf(&note, "'%s' of ", what);
1089 }
Patrick Steinhardt58a646a2021-01-12 13:27:35 +01001090
1091 append_fetch_head(&fetch_head, &rm->old_oid,
1092 rm->fetch_head_status,
1093 note.buf, url, url_len);
Joey Hess96890f42011-12-26 12:16:56 -04001094
1095 strbuf_reset(&note);
1096 if (ref) {
Junio C Hamano901f3d42016-10-21 15:22:55 -07001097 rc |= update_local_ref(ref, what, rm, &note,
1098 summary_width);
Joey Hess96890f42011-12-26 12:16:56 -04001099 free(ref);
Jonathan Tandb3c2932020-09-02 14:05:39 -07001100 } else if (write_fetch_head || dry_run) {
1101 /*
1102 * Display fetches written to FETCH_HEAD (or
1103 * would be written to FETCH_HEAD, if --dry-run
1104 * is set).
1105 */
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +02001106 format_display(&note, '*',
1107 *kind ? kind : "branch", NULL,
1108 *what ? what : "HEAD",
Junio C Hamano901f3d42016-10-21 15:22:55 -07001109 "FETCH_HEAD", summary_width);
Jonathan Tandb3c2932020-09-02 14:05:39 -07001110 }
Joey Hess96890f42011-12-26 12:16:56 -04001111 if (note.len) {
1112 if (verbosity >= 0 && !shown_url) {
1113 fprintf(stderr, _("From %.*s\n"),
1114 url_len, url);
1115 shown_url = 1;
1116 }
1117 if (verbosity >= 0)
1118 fprintf(stderr, " %s\n", note.buf);
1119 }
Nicolas Pitre165f3902007-11-03 01:32:48 -04001120 }
Daniel Barkalowb888d612007-09-10 23:03:25 -04001121 }
Tay Ray Chuan9516a592011-10-07 15:40:22 +08001122
Patrick Steinhardt58a646a2021-01-12 13:27:35 +01001123 if (!rc)
1124 commit_fetch_head(&fetch_head);
1125
Jeff Kingfa250752009-05-25 06:40:54 -04001126 if (rc & STORE_REF_ERROR_DF_CONFLICT)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001127 error(_("some local refs could not be updated; try running\n"
Jeff Kingf3cb1692008-06-27 00:01:41 -04001128 " 'git remote prune %s' to remove any old, conflicting "
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001129 "branches"), remote_name);
Tay Ray Chuan9516a592011-10-07 15:40:22 +08001130
Derrick Stolee377444b2019-06-18 13:25:27 -07001131 if (advice_fetch_show_forced_updates) {
1132 if (!fetch_show_forced_updates) {
Jean-Noël Avila182f59d2019-08-06 19:19:52 +02001133 warning(_(warn_show_forced_updates));
Derrick Stolee377444b2019-06-18 13:25:27 -07001134 } else if (forced_updates_ms > FORCED_UPDATES_DELAY_WARNING_IN_MS) {
Jean-Noël Avila182f59d2019-08-06 19:19:52 +02001135 warning(_(warn_time_show_forced_updates),
Derrick Stolee377444b2019-06-18 13:25:27 -07001136 forced_updates_ms / 1000.0);
Derrick Stolee377444b2019-06-18 13:25:27 -07001137 }
1138 }
1139
Tay Ray Chuan9516a592011-10-07 15:40:22 +08001140 abort:
Jeff King5914f2d2011-12-08 03:43:19 -05001141 strbuf_release(&note);
Tay Ray Chuan9516a592011-10-07 15:40:22 +08001142 free(url);
Patrick Steinhardt58a646a2021-01-12 13:27:35 +01001143 close_fetch_head(&fetch_head);
Dmitry V. Levinefb98b42008-05-28 19:29:36 +04001144 return rc;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001145}
1146
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001147/*
1148 * We would want to bypass the object transfer altogether if
Johan Herlandd9eb0202009-07-10 01:52:30 +02001149 * everything we are going to fetch already exists and is connected
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001150 * locally.
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001151 */
Jonathan Tan35f9e3e2018-09-21 11:22:38 -07001152static int check_exist_and_connected(struct ref *ref_map)
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001153{
Junio C Hamanof0e278b2011-09-02 16:22:47 -07001154 struct ref *rm = ref_map;
Jeff King7043c702016-07-15 06:30:40 -04001155 struct check_connected_options opt = CHECK_CONNECTED_INIT;
Jonathan Tan35f9e3e2018-09-21 11:22:38 -07001156 struct ref *r;
Junio C Hamanof0e278b2011-09-02 16:22:47 -07001157
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001158 /*
1159 * If we are deepening a shallow clone we already have these
1160 * objects reachable. Running rev-list here will return with
1161 * a good (0) exit status and we'll bypass the fetch that we
1162 * really need to perform. Claiming failure now will ensure
1163 * we perform the network exchange to deepen our history.
1164 */
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001165 if (deepen)
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001166 return -1;
Jonathan Tan35f9e3e2018-09-21 11:22:38 -07001167
1168 /*
1169 * check_connected() allows objects to merely be promised, but
1170 * we need all direct targets to exist.
1171 */
1172 for (r = rm; r; r = r->next) {
Jonathan Tan6462d5e2019-11-05 10:56:19 -08001173 if (!has_object_file_with_flags(&r->old_oid,
1174 OBJECT_INFO_SKIP_FETCH_OBJECT))
Jonathan Tan35f9e3e2018-09-21 11:22:38 -07001175 return -1;
1176 }
1177
Jeff King7043c702016-07-15 06:30:40 -04001178 opt.quiet = 1;
1179 return check_connected(iterate_ref_map, &rm, &opt);
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001180}
1181
Jonathan Tane2842b32018-08-01 13:13:20 -07001182static int fetch_refs(struct transport *transport, struct ref *ref_map)
Daniel Barkalowb888d612007-09-10 23:03:25 -04001183{
Jonathan Tan35f9e3e2018-09-21 11:22:38 -07001184 int ret = check_exist_and_connected(ref_map);
Josh Steadmon5fc31182019-10-02 16:49:28 -07001185 if (ret) {
1186 trace2_region_enter("fetch", "fetch_refs", the_repository);
Jonathan Tane2842b32018-08-01 13:13:20 -07001187 ret = transport_fetch_refs(transport, ref_map);
Josh Steadmon5fc31182019-10-02 16:49:28 -07001188 trace2_region_leave("fetch", "fetch_refs", the_repository);
1189 }
Daniel Barkalowb888d612007-09-10 23:03:25 -04001190 if (!ret)
Brandon Williams05c44222018-06-27 15:30:20 -07001191 /*
1192 * Keep the new pack's ".keep" file around to allow the caller
1193 * time to update refs to reference the new objects.
1194 */
1195 return 0;
1196 transport_unlock_pack(transport);
1197 return ret;
1198}
1199
1200/* Update local refs based on the ref values fetched from a remote */
1201static int consume_refs(struct transport *transport, struct ref *ref_map)
1202{
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001203 int connectivity_checked = transport->smart_options
1204 ? transport->smart_options->connectivity_checked : 0;
Josh Steadmon5fc31182019-10-02 16:49:28 -07001205 int ret;
1206 trace2_region_enter("fetch", "consume_refs", the_repository);
1207 ret = store_updated_refs(transport->url,
1208 transport->remote->name,
1209 connectivity_checked,
1210 ref_map);
Shawn O. Pearce1788c392007-09-14 03:31:23 -04001211 transport_unlock_pack(transport);
Josh Steadmon5fc31182019-10-02 16:49:28 -07001212 trace2_region_leave("fetch", "consume_refs", the_repository);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001213 return ret;
1214}
1215
Brandon Williamsdef11e72018-05-16 15:58:09 -07001216static int prune_refs(struct refspec *rs, struct ref *ref_map,
1217 const char *raw_url)
Jay Soffianf360d842009-11-10 09:15:47 +01001218{
Tom Miller4b3b33a2014-01-02 20:28:51 -06001219 int url_len, i, result = 0;
Brandon Williamsa2ac50c2018-05-16 15:58:10 -07001220 struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map);
Tom Miller4b3b33a2014-01-02 20:28:51 -06001221 char *url;
Junio C Hamano11fd66d2016-10-21 15:28:07 -07001222 int summary_width = transport_summary_width(stale_refs);
Jay Soffianf360d842009-11-10 09:15:47 +01001223 const char *dangling_msg = dry_run
Nguyễn Thái Ngọc Duy18986d52012-04-23 19:30:25 +07001224 ? _(" (%s will become dangling)")
1225 : _(" (%s has become dangling)");
Jay Soffianf360d842009-11-10 09:15:47 +01001226
Tom Miller4b3b33a2014-01-02 20:28:51 -06001227 if (raw_url)
1228 url = transport_anonymize_url(raw_url);
1229 else
1230 url = xstrdup("foreign");
1231
1232 url_len = strlen(url);
1233 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
1234 ;
1235
1236 url_len = i + 1;
1237 if (4 < i && !strncmp(".git", url + i - 3, 4))
1238 url_len = i - 3;
1239
Michael Haggertya087b432015-06-22 16:02:59 +02001240 if (!dry_run) {
1241 struct string_list refnames = STRING_LIST_INIT_NODUP;
1242
1243 for (ref = stale_refs; ref; ref = ref->next)
1244 string_list_append(&refnames, ref->name);
1245
Michael Haggerty64da4192017-05-22 16:17:38 +02001246 result = delete_refs("fetch: prune", &refnames, 0);
Michael Haggertya087b432015-06-22 16:02:59 +02001247 string_list_clear(&refnames, 0);
1248 }
1249
1250 if (verbosity >= 0) {
1251 for (ref = stale_refs; ref; ref = ref->next) {
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +02001252 struct strbuf sb = STRBUF_INIT;
Michael Haggertya087b432015-06-22 16:02:59 +02001253 if (!shown_url) {
1254 fprintf(stderr, _("From %.*s\n"), url_len, url);
1255 shown_url = 1;
1256 }
Nguyễn Thái Ngọc Duy2cb040b2016-06-26 07:58:08 +02001257 format_display(&sb, '-', _("[deleted]"), NULL,
Junio C Hamano901f3d42016-10-21 15:22:55 -07001258 _("(none)"), prettify_refname(ref->name),
1259 summary_width);
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +02001260 fprintf(stderr, " %s\n",sb.buf);
1261 strbuf_release(&sb);
Jay Soffianf360d842009-11-10 09:15:47 +01001262 warn_dangling_symref(stderr, dangling_msg, ref->name);
1263 }
1264 }
Michael Haggertya087b432015-06-22 16:02:59 +02001265
Tom Miller4b3b33a2014-01-02 20:28:51 -06001266 free(url);
Jay Soffianf360d842009-11-10 09:15:47 +01001267 free_refs(stale_refs);
1268 return result;
1269}
1270
Johannes Schindelin8ee5d732008-10-13 11:36:52 +02001271static void check_not_current_branch(struct ref *ref_map)
1272{
1273 struct branch *current_branch = branch_get(NULL);
1274
1275 if (is_bare_repository() || !current_branch)
1276 return;
1277
1278 for (; ref_map; ref_map = ref_map->next)
1279 if (ref_map->peer_ref && !strcmp(current_branch->refname,
1280 ref_map->peer_ref->name))
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001281 die(_("Refusing to fetch into current branch %s "
1282 "of non-bare repository"), current_branch->refname);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +02001283}
1284
Junio C Hamanoe6cc5102010-02-24 11:02:05 -08001285static int truncate_fetch_head(void)
1286{
Stefan Beller102de882018-05-17 15:51:51 -07001287 const char *filename = git_path_fetch_head(the_repository);
Johannes Schindelinea565182016-01-11 19:35:54 +01001288 FILE *fp = fopen_for_writing(filename);
Junio C Hamanoe6cc5102010-02-24 11:02:05 -08001289
1290 if (!fp)
Nguyễn Thái Ngọc Duy6da31d72016-05-08 16:47:26 +07001291 return error_errno(_("cannot open %s"), filename);
Junio C Hamanoe6cc5102010-02-24 11:02:05 -08001292 fclose(fp);
1293 return 0;
1294}
1295
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001296static void set_option(struct transport *transport, const char *name, const char *value)
1297{
1298 int r = transport_set_option(transport, name, value);
1299 if (r < 0)
1300 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1301 name, value, transport->url);
1302 if (r > 0)
1303 warning(_("Option \"%s\" is ignored for %s\n"),
1304 name, transport->url);
1305}
1306
Jonathan Tan3390e422018-07-02 15:39:44 -07001307
1308static int add_oid(const char *refname, const struct object_id *oid, int flags,
1309 void *cb_data)
1310{
1311 struct oid_array *oids = cb_data;
1312
1313 oid_array_append(oids, oid);
1314 return 0;
1315}
1316
1317static void add_negotiation_tips(struct git_transport_options *smart_options)
1318{
1319 struct oid_array *oids = xcalloc(1, sizeof(*oids));
1320 int i;
1321
1322 for (i = 0; i < negotiation_tip.nr; i++) {
1323 const char *s = negotiation_tip.items[i].string;
1324 int old_nr;
1325 if (!has_glob_specials(s)) {
1326 struct object_id oid;
1327 if (get_oid(s, &oid))
1328 die("%s is not a valid object", s);
1329 oid_array_append(oids, &oid);
1330 continue;
1331 }
1332 old_nr = oids->nr;
1333 for_each_glob_ref(add_oid, s, oids);
1334 if (old_nr == oids->nr)
1335 warning("Ignoring --negotiation-tip=%s because it does not match any refs",
1336 s);
1337 }
1338 smart_options->negotiation_tips = oids;
1339}
1340
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001341static struct transport *prepare_transport(struct remote *remote, int deepen)
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001342{
1343 struct transport *transport;
Josh Steadmon87c2d9d2019-01-07 16:17:09 -08001344
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001345 transport = transport_get(remote, NULL);
1346 transport_set_verbosity(transport, verbosity, progress);
Eric Wongc915f112016-02-03 04:09:14 +00001347 transport->family = family;
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001348 if (upload_pack)
1349 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
1350 if (keep)
1351 set_option(transport, TRANS_OPT_KEEP, "yes");
1352 if (depth)
1353 set_option(transport, TRANS_OPT_DEPTH, depth);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001354 if (deepen && deepen_since)
1355 set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since);
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001356 if (deepen && deepen_not.nr)
1357 set_option(transport, TRANS_OPT_DEEPEN_NOT,
1358 (const char *)&deepen_not);
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001359 if (deepen_relative)
1360 set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes");
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001361 if (update_shallow)
1362 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001363 if (filter_options.choice) {
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001364 const char *spec =
1365 expand_list_objects_filter_spec(&filter_options);
1366 set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER, spec);
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001367 set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
1368 }
Jonathan Tan3390e422018-07-02 15:39:44 -07001369 if (negotiation_tip.nr) {
1370 if (transport->smart_options)
1371 add_negotiation_tips(transport->smart_options);
1372 else
1373 warning("Ignoring --negotiation-tip because the protocol does not support it.");
1374 }
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001375 return transport;
1376}
1377
Junio C Hamano069d5032013-08-07 15:14:45 -07001378static void backfill_tags(struct transport *transport, struct ref *ref_map)
1379{
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001380 int cannot_reuse;
1381
1382 /*
1383 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1384 * when remote helper is used (setting it to an empty string
1385 * is not unsetting). We could extend the remote helper
1386 * protocol for that, but for now, just force a new connection
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001387 * without deepen-since. Similar story for deepen-not.
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001388 */
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001389 cannot_reuse = transport->cannot_reuse ||
1390 deepen_since || deepen_not.nr;
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001391 if (cannot_reuse) {
1392 gsecondary = prepare_transport(transport->remote, 0);
Junio C Hamanob26ed432013-08-07 15:47:18 -07001393 transport = gsecondary;
1394 }
1395
Junio C Hamano069d5032013-08-07 15:14:45 -07001396 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
1397 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001398 transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
Jonathan Tane2842b32018-08-01 13:13:20 -07001399 if (!fetch_refs(transport, ref_map))
Brandon Williams05c44222018-06-27 15:30:20 -07001400 consume_refs(transport, ref_map);
Junio C Hamanob26ed432013-08-07 15:47:18 -07001401
1402 if (gsecondary) {
1403 transport_disconnect(gsecondary);
1404 gsecondary = NULL;
1405 }
Junio C Hamano069d5032013-08-07 15:14:45 -07001406}
1407
Daniel Barkalowb888d612007-09-10 23:03:25 -04001408static int do_fetch(struct transport *transport,
Brandon Williams65a13012018-05-16 15:58:07 -07001409 struct refspec *rs)
Daniel Barkalowb888d612007-09-10 23:03:25 -04001410{
Shawn O. Pearce7f984282008-03-02 21:34:43 -05001411 struct ref *ref_map;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001412 int autotags = (transport->remote->fetch_tags == 1);
Michael Haggerty5b87d8d2013-05-25 11:08:16 +02001413 int retcode = 0;
Brandon Williams6d1700d2018-06-27 15:30:21 -07001414 const struct ref *remote_refs;
Jeff King22f9b7f2020-07-28 16:24:27 -04001415 struct strvec ref_prefixes = STRVEC_INIT;
Jonathan Tane70a3032018-09-27 12:24:07 -07001416 int must_list_refs = 1;
Julian Phillipsb1a01e12009-10-25 21:28:12 +00001417
Daniel Johnsoned368542010-08-11 18:57:20 -04001418 if (tags == TAGS_DEFAULT) {
1419 if (transport->remote->fetch_tags == 2)
1420 tags = TAGS_SET;
1421 if (transport->remote->fetch_tags == -1)
1422 tags = TAGS_UNSET;
1423 }
Daniel Barkalowb888d612007-09-10 23:03:25 -04001424
Daniel Barkalowb888d612007-09-10 23:03:25 -04001425 /* if not appending, truncate FETCH_HEAD */
Junio C Hamano887952b2020-08-18 14:25:22 +00001426 if (!append && write_fetch_head) {
Michael Haggerty5b87d8d2013-05-25 11:08:16 +02001427 retcode = truncate_fetch_head();
1428 if (retcode)
1429 goto cleanup;
André Goddard Rosad6617c72007-11-22 20:22:23 -02001430 }
Daniel Barkalowb888d612007-09-10 23:03:25 -04001431
Jonathan Tane70a3032018-09-27 12:24:07 -07001432 if (rs->nr) {
1433 int i;
1434
Brandon Williams6d1700d2018-06-27 15:30:21 -07001435 refspec_ref_prefixes(rs, &ref_prefixes);
Jonathan Tane70a3032018-09-27 12:24:07 -07001436
1437 /*
1438 * We can avoid listing refs if all of them are exact
1439 * OIDs
1440 */
1441 must_list_refs = 0;
1442 for (i = 0; i < rs->nr; i++) {
1443 if (!rs->items[i].exact_sha1) {
1444 must_list_refs = 1;
1445 break;
1446 }
1447 }
1448 } else if (transport->remote && transport->remote->fetch.nr)
Brandon Williams6d1700d2018-06-27 15:30:21 -07001449 refspec_ref_prefixes(&transport->remote->fetch, &ref_prefixes);
1450
Jonathan Tane70a3032018-09-27 12:24:07 -07001451 if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
1452 must_list_refs = 1;
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001453 if (ref_prefixes.nr)
Jeff King22f9b7f2020-07-28 16:24:27 -04001454 strvec_push(&ref_prefixes, "refs/tags/");
Brandon Williams6d1700d2018-06-27 15:30:21 -07001455 }
1456
Josh Steadmon5fc31182019-10-02 16:49:28 -07001457 if (must_list_refs) {
1458 trace2_region_enter("fetch", "remote_refs", the_repository);
Jonathan Tane70a3032018-09-27 12:24:07 -07001459 remote_refs = transport_get_remote_refs(transport, &ref_prefixes);
Josh Steadmon5fc31182019-10-02 16:49:28 -07001460 trace2_region_leave("fetch", "remote_refs", the_repository);
1461 } else
Jonathan Tane70a3032018-09-27 12:24:07 -07001462 remote_refs = NULL;
1463
Jeff King22f9b7f2020-07-28 16:24:27 -04001464 strvec_clear(&ref_prefixes);
Brandon Williams6d1700d2018-06-27 15:30:21 -07001465
1466 ref_map = get_ref_map(transport->remote, remote_refs, rs,
1467 tags, &autotags);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +02001468 if (!update_head_ok)
1469 check_not_current_branch(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001470
Shawn O. Pearce41fa7d22008-03-03 22:27:40 -05001471 if (tags == TAGS_DEFAULT && autotags)
1472 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02001473 if (prune) {
Michael Haggerty0838bf42013-10-30 06:33:00 +01001474 /*
1475 * We only prune based on refspecs specified
1476 * explicitly (via command line or configuration); we
1477 * don't care whether --tags was specified.
1478 */
Brandon Williams65a13012018-05-16 15:58:07 -07001479 if (rs->nr) {
Brandon Williamsdef11e72018-05-16 15:58:09 -07001480 prune_refs(rs, ref_map, transport->url);
Michael Haggertyc5a84e92013-10-30 06:32:59 +01001481 } else {
Brandon Williamsdef11e72018-05-16 15:58:09 -07001482 prune_refs(&transport->remote->fetch,
Tom Miller4b3b33a2014-01-02 20:28:51 -06001483 ref_map,
1484 transport->url);
Carlos Martín Nietoe8c1e6c2011-10-15 07:04:26 +02001485 }
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02001486 }
Jonathan Tane2842b32018-08-01 13:13:20 -07001487 if (fetch_refs(transport, ref_map) || consume_refs(transport, ref_map)) {
Tom Miller10a6cc82014-01-02 20:28:52 -06001488 free_refs(ref_map);
1489 retcode = 1;
1490 goto cleanup;
1491 }
Corentin BOMPARD24bc1a12019-08-19 11:11:20 +02001492
1493 if (set_upstream) {
1494 struct branch *branch = branch_get("HEAD");
1495 struct ref *rm;
1496 struct ref *source_ref = NULL;
1497
1498 /*
1499 * We're setting the upstream configuration for the
Elijah Newren15beaaa2019-11-05 17:07:23 +00001500 * current branch. The relevant upstream is the
Corentin BOMPARD24bc1a12019-08-19 11:11:20 +02001501 * fetched branch that is meant to be merged with the
1502 * current one, i.e. the one fetched to FETCH_HEAD.
1503 *
1504 * When there are several such branches, consider the
1505 * request ambiguous and err on the safe side by doing
1506 * nothing and just emit a warning.
1507 */
1508 for (rm = ref_map; rm; rm = rm->next) {
1509 if (!rm->peer_ref) {
1510 if (source_ref) {
Ralf Thielow391c7e42019-10-31 20:41:46 +00001511 warning(_("multiple branches detected, incompatible with --set-upstream"));
Corentin BOMPARD24bc1a12019-08-19 11:11:20 +02001512 goto skip;
1513 } else {
1514 source_ref = rm;
1515 }
1516 }
1517 }
1518 if (source_ref) {
1519 if (!strcmp(source_ref->name, "HEAD") ||
1520 starts_with(source_ref->name, "refs/heads/"))
1521 install_branch_config(0,
1522 branch->name,
1523 transport->remote->name,
1524 source_ref->name);
1525 else if (starts_with(source_ref->name, "refs/remotes/"))
1526 warning(_("not setting upstream for a remote remote-tracking branch"));
1527 else if (starts_with(source_ref->name, "refs/tags/"))
1528 warning(_("not setting upstream for a remote tag"));
1529 else
1530 warning(_("unknown branch type"));
1531 } else {
1532 warning(_("no source branch found.\n"
1533 "you need to specify exactly one branch with the --set-upstream option."));
1534 }
1535 }
1536 skip:
Shawn O. Pearce7f984282008-03-02 21:34:43 -05001537 free_refs(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001538
1539 /* if neither --no-tags nor --tags was specified, do automated tag
1540 * following ... */
Kristian Høgsberg83201992007-12-04 02:25:47 -05001541 if (tags == TAGS_DEFAULT && autotags) {
Shawn O. Pearcec50b2b42008-03-02 21:35:00 -05001542 struct ref **tail = &ref_map;
1543 ref_map = NULL;
Brandon Williams6d1700d2018-06-27 15:30:21 -07001544 find_non_local_tags(remote_refs, &ref_map, &tail);
Junio C Hamano069d5032013-08-07 15:14:45 -07001545 if (ref_map)
1546 backfill_tags(transport, ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001547 free_refs(ref_map);
1548 }
1549
Michael Haggerty5b87d8d2013-05-25 11:08:16 +02001550 cleanup:
Michael Haggerty5b87d8d2013-05-25 11:08:16 +02001551 return retcode;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001552}
1553
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001554static int get_one_remote_for_fetch(struct remote *remote, void *priv)
Daniel Barkalowb888d612007-09-10 23:03:25 -04001555{
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001556 struct string_list *list = priv;
Björn Gustavsson7cc91a22009-11-09 21:11:06 +01001557 if (!remote->skip_default_update)
Julian Phillips1d2f80f2010-06-26 00:41:38 +01001558 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001559 return 0;
1560}
1561
1562struct remote_group_data {
1563 const char *name;
1564 struct string_list *list;
1565};
1566
1567static int get_remote_group(const char *key, const char *value, void *priv)
1568{
1569 struct remote_group_data *g = priv;
1570
Michael Haggertybc598c32015-07-28 23:08:21 +02001571 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001572 /* split list by white space */
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001573 while (*value) {
Michael Haggerty5f654992015-07-28 23:08:20 +02001574 size_t wordlen = strcspn(value, " \t\n");
1575
Michael Haggertye2865422015-07-28 23:08:19 +02001576 if (wordlen >= 1)
Keith McGuiganb7410f62016-06-14 14:28:56 -04001577 string_list_append_nodup(g->list,
Michael Haggertye2865422015-07-28 23:08:19 +02001578 xstrndup(value, wordlen));
1579 value += wordlen + (value[wordlen] != '\0');
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001580 }
1581 }
1582
1583 return 0;
1584}
1585
1586static int add_remote_or_group(const char *name, struct string_list *list)
1587{
1588 int prev_nr = list->nr;
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +00001589 struct remote_group_data g;
1590 g.name = name; g.list = list;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001591
1592 git_config(get_remote_group, &g);
1593 if (list->nr == prev_nr) {
Thomas Gummerer674468b2016-02-16 10:47:50 +01001594 struct remote *remote = remote_get(name);
Johannes Schindeline459b072017-01-19 22:20:02 +01001595 if (!remote_is_configured(remote, 0))
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001596 return 0;
Julian Phillips1d2f80f2010-06-26 00:41:38 +01001597 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001598 }
1599 return 1;
1600}
1601
Jeff King22f9b7f2020-07-28 16:24:27 -04001602static void add_options_to_argv(struct strvec *argv)
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001603{
1604 if (dry_run)
Jeff King22f9b7f2020-07-28 16:24:27 -04001605 strvec_push(argv, "--dry-run");
Michael Haggerty90765fa2013-10-30 06:33:04 +01001606 if (prune != -1)
Jeff King22f9b7f2020-07-28 16:24:27 -04001607 strvec_push(argv, prune ? "--prune" : "--no-prune");
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +00001608 if (prune_tags != -1)
Jeff King22f9b7f2020-07-28 16:24:27 -04001609 strvec_push(argv, prune_tags ? "--prune-tags" : "--no-prune-tags");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001610 if (update_head_ok)
Jeff King22f9b7f2020-07-28 16:24:27 -04001611 strvec_push(argv, "--update-head-ok");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001612 if (force)
Jeff King22f9b7f2020-07-28 16:24:27 -04001613 strvec_push(argv, "--force");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001614 if (keep)
Jeff King22f9b7f2020-07-28 16:24:27 -04001615 strvec_push(argv, "--keep");
Jens Lehmannbe254a02010-11-11 00:55:02 +01001616 if (recurse_submodules == RECURSE_SUBMODULES_ON)
Jeff King22f9b7f2020-07-28 16:24:27 -04001617 strvec_push(argv, "--recurse-submodules");
Jens Lehmann8f0700d2011-03-06 23:11:21 +01001618 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
Jeff King22f9b7f2020-07-28 16:24:27 -04001619 strvec_push(argv, "--recurse-submodules=on-demand");
Dan Johnson85566462012-09-05 17:22:19 -04001620 if (tags == TAGS_SET)
Jeff King22f9b7f2020-07-28 16:24:27 -04001621 strvec_push(argv, "--tags");
Dan Johnson85566462012-09-05 17:22:19 -04001622 else if (tags == TAGS_UNSET)
Jeff King22f9b7f2020-07-28 16:24:27 -04001623 strvec_push(argv, "--no-tags");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001624 if (verbosity >= 2)
Jeff King22f9b7f2020-07-28 16:24:27 -04001625 strvec_push(argv, "-v");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001626 if (verbosity >= 1)
Jeff King22f9b7f2020-07-28 16:24:27 -04001627 strvec_push(argv, "-v");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001628 else if (verbosity < 0)
Jeff King22f9b7f2020-07-28 16:24:27 -04001629 strvec_push(argv, "-q");
Alex Riesen4e735c12020-09-15 13:54:07 +02001630 if (family == TRANSPORT_FAMILY_IPV4)
Junio C Hamano68546892020-09-22 12:36:34 -07001631 strvec_push(argv, "--ipv4");
Alex Riesen4e735c12020-09-15 13:54:07 +02001632 else if (family == TRANSPORT_FAMILY_IPV6)
Junio C Hamano68546892020-09-22 12:36:34 -07001633 strvec_push(argv, "--ipv6");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001634}
1635
Johannes Schindelind54dea72019-10-05 11:46:40 -07001636/* Fetch multiple remotes in parallel */
1637
1638struct parallel_fetch_state {
1639 const char **argv;
1640 struct string_list *remotes;
1641 int next, result;
1642};
1643
1644static int fetch_next_remote(struct child_process *cp, struct strbuf *out,
1645 void *cb, void **task_cb)
1646{
1647 struct parallel_fetch_state *state = cb;
1648 char *remote;
1649
1650 if (state->next < 0 || state->next >= state->remotes->nr)
1651 return 0;
1652
1653 remote = state->remotes->items[state->next++].string;
1654 *task_cb = remote;
1655
Jeff King22f9b7f2020-07-28 16:24:27 -04001656 strvec_pushv(&cp->args, state->argv);
1657 strvec_push(&cp->args, remote);
Johannes Schindelind54dea72019-10-05 11:46:40 -07001658 cp->git_cmd = 1;
1659
1660 if (verbosity >= 0)
1661 printf(_("Fetching %s\n"), remote);
1662
1663 return 1;
1664}
1665
1666static int fetch_failed_to_start(struct strbuf *out, void *cb, void *task_cb)
1667{
1668 struct parallel_fetch_state *state = cb;
1669 const char *remote = task_cb;
1670
1671 state->result = error(_("Could not fetch %s"), remote);
1672
1673 return 0;
1674}
1675
1676static int fetch_finished(int result, struct strbuf *out,
1677 void *cb, void *task_cb)
1678{
1679 struct parallel_fetch_state *state = cb;
1680 const char *remote = task_cb;
1681
1682 if (result) {
1683 strbuf_addf(out, _("could not fetch '%s' (exit code: %d)\n"),
1684 remote, result);
1685 state->result = -1;
1686 }
1687
1688 return 0;
1689}
1690
1691static int fetch_multiple(struct string_list *list, int max_children)
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001692{
1693 int i, result = 0;
Jeff King22f9b7f2020-07-28 16:24:27 -04001694 struct strvec argv = STRVEC_INIT;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001695
Junio C Hamano887952b2020-08-18 14:25:22 +00001696 if (!append && write_fetch_head) {
Junio C Hamanoe6cc5102010-02-24 11:02:05 -08001697 int errcode = truncate_fetch_head();
1698 if (errcode)
1699 return errcode;
1700 }
1701
Jeff King22f9b7f2020-07-28 16:24:27 -04001702 strvec_pushl(&argv, "fetch", "--append", "--no-auto-gc",
Jeff Kingf6d89422020-07-28 16:26:31 -04001703 "--no-write-commit-graph", NULL);
Jeff King85556d42012-09-01 07:27:35 -04001704 add_options_to_argv(&argv);
1705
Johannes Schindelind54dea72019-10-05 11:46:40 -07001706 if (max_children != 1 && list->nr != 1) {
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001707 struct parallel_fetch_state state = { argv.v, list, 0, 0 };
Johannes Schindelind54dea72019-10-05 11:46:40 -07001708
Jeff King22f9b7f2020-07-28 16:24:27 -04001709 strvec_push(&argv, "--end-of-options");
Johannes Schindelind54dea72019-10-05 11:46:40 -07001710 result = run_processes_parallel_tr2(max_children,
1711 &fetch_next_remote,
1712 &fetch_failed_to_start,
1713 &fetch_finished,
1714 &state,
1715 "fetch", "parallel/fetch");
1716
1717 if (!result)
1718 result = state.result;
1719 } else
1720 for (i = 0; i < list->nr; i++) {
1721 const char *name = list->items[i].string;
Jeff King22f9b7f2020-07-28 16:24:27 -04001722 strvec_push(&argv, name);
Johannes Schindelind54dea72019-10-05 11:46:40 -07001723 if (verbosity >= 0)
1724 printf(_("Fetching %s\n"), name);
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001725 if (run_command_v_opt(argv.v, RUN_GIT_CMD)) {
Johannes Schindelind54dea72019-10-05 11:46:40 -07001726 error(_("Could not fetch %s"), name);
1727 result = 1;
1728 }
Jeff King22f9b7f2020-07-28 16:24:27 -04001729 strvec_pop(&argv);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001730 }
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001731
Jeff King22f9b7f2020-07-28 16:24:27 -04001732 strvec_clear(&argv);
Johannes Schindelind54dea72019-10-05 11:46:40 -07001733 return !!result;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001734}
1735
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001736/*
1737 * Fetching from the promisor remote should use the given filter-spec
1738 * or inherit the default filter-spec from the config.
1739 */
1740static inline void fetch_one_setup_partial(struct remote *remote)
1741{
1742 /*
1743 * Explicit --no-filter argument overrides everything, regardless
1744 * of any prior partial clones and fetches.
1745 */
1746 if (filter_options.no_filter)
1747 return;
1748
1749 /*
1750 * If no prior partial clone/fetch and the current fetch DID NOT
1751 * request a partial-fetch, do a normal fetch.
1752 */
Christian Couderb14ed5a2019-06-25 15:40:31 +02001753 if (!has_promisor_remote() && !filter_options.choice)
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001754 return;
1755
1756 /*
Christian Couder5e461392019-06-25 15:40:33 +02001757 * If this is a partial-fetch request, we enable partial on
1758 * this repo if not already enabled and remember the given
1759 * filter-spec as the default for subsequent fetches to this
Jonathan Tan23547c42020-09-28 15:26:38 -07001760 * remote if there is currently no default filter-spec.
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001761 */
Christian Couder5e461392019-06-25 15:40:33 +02001762 if (filter_options.choice) {
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001763 partial_clone_register(remote->name, &filter_options);
1764 return;
1765 }
1766
1767 /*
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001768 * Do a partial-fetch from the promisor remote using either the
1769 * explicitly given filter-spec or inherit the filter-spec from
1770 * the config.
1771 */
1772 if (!filter_options.choice)
Christian Couderfa3d1b62019-06-25 15:40:32 +02001773 partial_clone_get_default_filter_spec(&filter_options, remote->name);
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001774 return;
1775}
1776
Jonathan Tan2b713c22020-08-17 21:01:32 -07001777static int fetch_one(struct remote *remote, int argc, const char **argv,
1778 int prune_tags_ok, int use_stdin_refspecs)
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001779{
Brandon Williamsd7c8e302018-05-16 15:58:04 -07001780 struct refspec rs = REFSPEC_INIT_FETCH;
1781 int i;
Alex Riesen7b7f39e2008-04-28 22:23:35 +02001782 int exit_code;
Ævar Arnfjörð Bjarmason63179722018-02-09 20:32:16 +00001783 int maybe_prune_tags;
1784 int remote_via_config = remote_is_configured(remote, 0);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001785
Daniel Barkalowfa685bd2009-03-11 01:47:20 -04001786 if (!remote)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001787 die(_("No remote repository specified. Please, specify either a URL or a\n"
1788 "remote name from which new revisions should be fetched."));
Daniel Barkalowfa685bd2009-03-11 01:47:20 -04001789
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001790 gtransport = prepare_transport(remote, 1);
Michael Schubert737c5a92013-07-13 11:36:24 +02001791
1792 if (prune < 0) {
1793 /* no command line request */
Ævar Arnfjörð Bjarmason07118832018-02-09 20:32:02 +00001794 if (0 <= remote->prune)
1795 prune = remote->prune;
Michael Schubert737c5a92013-07-13 11:36:24 +02001796 else if (0 <= fetch_prune_config)
1797 prune = fetch_prune_config;
1798 else
1799 prune = PRUNE_BY_DEFAULT;
1800 }
1801
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +00001802 if (prune_tags < 0) {
1803 /* no command line request */
1804 if (0 <= remote->prune_tags)
1805 prune_tags = remote->prune_tags;
1806 else if (0 <= fetch_prune_tags_config)
1807 prune_tags = fetch_prune_tags_config;
1808 else
1809 prune_tags = PRUNE_TAGS_BY_DEFAULT;
1810 }
1811
Ævar Arnfjörð Bjarmason63179722018-02-09 20:32:16 +00001812 maybe_prune_tags = prune_tags_ok && prune_tags;
1813 if (maybe_prune_tags && remote_via_config)
Brandon Williams95303502018-05-16 15:58:02 -07001814 refspec_append(&remote->fetch, TAG_REFSPEC);
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +00001815
Brandon Williamsd7c8e302018-05-16 15:58:04 -07001816 if (maybe_prune_tags && (argc || !remote_via_config))
1817 refspec_append(&rs, TAG_REFSPEC);
Ævar Arnfjörð Bjarmason63179722018-02-09 20:32:16 +00001818
Brandon Williamsd7c8e302018-05-16 15:58:04 -07001819 for (i = 0; i < argc; i++) {
1820 if (!strcmp(argv[i], "tag")) {
Brandon Williamsd7c8e302018-05-16 15:58:04 -07001821 i++;
1822 if (i >= argc)
1823 die(_("You need to specify a tag name."));
1824
René Scharfe1af8b8c2020-09-05 16:49:30 +02001825 refspec_appendf(&rs, "refs/tags/%s:refs/tags/%s",
1826 argv[i], argv[i]);
Brandon Williamsd7c8e302018-05-16 15:58:04 -07001827 } else {
1828 refspec_append(&rs, argv[i]);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001829 }
Daniel Barkalowb888d612007-09-10 23:03:25 -04001830 }
1831
Jonathan Tan2b713c22020-08-17 21:01:32 -07001832 if (use_stdin_refspecs) {
1833 struct strbuf line = STRBUF_INIT;
1834 while (strbuf_getline_lf(&line, stdin) != EOF)
1835 refspec_append(&rs, line.buf);
1836 strbuf_release(&line);
1837 }
1838
Brandon Williams5e3548e2018-04-23 15:46:24 -07001839 if (server_options.nr)
1840 gtransport->server_options = &server_options;
1841
Jeff King57b235a2009-01-22 01:03:08 -05001842 sigchain_push_common(unlock_pack_on_signal);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -04001843 atexit(unlock_pack);
Jeff King14358892019-03-03 11:58:43 -05001844 sigchain_push(SIGPIPE, SIG_IGN);
Brandon Williams65a13012018-05-16 15:58:07 -07001845 exit_code = do_fetch(gtransport, &rs);
Jeff King14358892019-03-03 11:58:43 -05001846 sigchain_pop(SIGPIPE);
Brandon Williamsd7c8e302018-05-16 15:58:04 -07001847 refspec_clear(&rs);
Junio C Hamanoaf234452013-08-07 15:38:45 -07001848 transport_disconnect(gtransport);
1849 gtransport = NULL;
Alex Riesen7b7f39e2008-04-28 22:23:35 +02001850 return exit_code;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001851}
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001852
1853int cmd_fetch(int argc, const char **argv, const char *prefix)
1854{
1855 int i;
Keith McGuiganb7410f62016-06-14 14:28:56 -04001856 struct string_list list = STRING_LIST_INIT_DUP;
Jonathan Tana1743342017-12-08 15:58:43 +00001857 struct remote *remote = NULL;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001858 int result = 0;
Junio C Hamanoc1a79022018-03-06 14:54:01 -08001859 int prune_tags_ok = 1;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001860
Jeff Kingbbc30f92011-02-24 09:30:19 -05001861 packet_trace_identity("fetch");
1862
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001863 /* Record the command line for the reflog */
1864 strbuf_addstr(&default_rla, "fetch");
Johannes Schindelin46da2952020-06-04 20:08:29 +00001865 for (i = 1; i < argc; i++) {
1866 /* This handles non-URLs gracefully */
1867 char *anon = transport_anonymize_url(argv[i]);
1868
1869 strbuf_addf(&default_rla, " %s", anon);
1870 free(anon);
1871 }
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001872
Michael Schubert737c5a92013-07-13 11:36:24 +02001873 git_config(git_fetch_config, NULL);
1874
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001875 argc = parse_options(argc, argv, prefix,
1876 builtin_fetch_options, builtin_fetch_usage, 0);
Jonathan Tane5b94212020-08-17 21:01:33 -07001877 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1878 int *sfjc = submodule_fetch_jobs_config == -1
1879 ? &submodule_fetch_jobs_config : NULL;
1880 int *rs = recurse_submodules == RECURSE_SUBMODULES_DEFAULT
1881 ? &recurse_submodules : NULL;
1882
1883 fetch_config_from_gitmodules(sfjc, rs);
1884 }
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001885
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001886 if (deepen_relative) {
1887 if (deepen_relative < 0)
1888 die(_("Negative depth in --deepen is not supported"));
1889 if (depth)
1890 die(_("--deepen and --depth are mutually exclusive"));
1891 depth = xstrfmt("%d", deepen_relative);
1892 }
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +07001893 if (unshallow) {
1894 if (depth)
1895 die(_("--depth and --unshallow cannot be used together"));
Stefan Bellerc8813482018-05-17 15:51:46 -07001896 else if (!is_repository_shallow(the_repository))
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +07001897 die(_("--unshallow on a complete repository does not make sense"));
Jeff King2805bb52015-09-24 17:07:07 -04001898 else
1899 depth = xstrfmt("%d", INFINITE_DEPTH);
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +07001900 }
1901
Nguyễn Thái Ngọc Duy5594bca2013-12-05 10:31:11 +07001902 /* no need to be strict, transport_set_option() will validate it again */
1903 if (depth && atoi(depth) < 1)
1904 die(_("depth %s is not a positive number"), depth);
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001905 if (depth || deepen_since || deepen_not.nr)
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001906 deepen = 1;
Nguyễn Thái Ngọc Duy5594bca2013-12-05 10:31:11 +07001907
Junio C Hamano887952b2020-08-18 14:25:22 +00001908 /* FETCH_HEAD never gets updated in --dry-run mode */
1909 if (dry_run)
1910 write_fetch_head = 0;
1911
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001912 if (all) {
1913 if (argc == 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001914 die(_("fetch --all does not take a repository argument"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001915 else if (argc > 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001916 die(_("fetch --all does not make sense with refspecs"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001917 (void) for_each_remote(get_one_remote_for_fetch, &list);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001918 } else if (argc == 0) {
1919 /* No arguments -- use default remote */
1920 remote = remote_get(NULL);
Björn Gustavsson16679e32009-11-09 21:10:32 +01001921 } else if (multiple) {
1922 /* All arguments are assumed to be remotes or groups */
1923 for (i = 0; i < argc; i++)
1924 if (!add_remote_or_group(argv[i], &list))
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001925 die(_("No such remote or remote group: %s"), argv[i]);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001926 } else {
1927 /* Single remote or group */
1928 (void) add_remote_or_group(argv[0], &list);
1929 if (list.nr > 1) {
1930 /* More than one remote */
1931 if (argc > 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001932 die(_("Fetching a group and specifying refspecs does not make sense"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001933 } else {
1934 /* Zero or one remotes */
1935 remote = remote_get(argv[0]);
Junio C Hamanoc1a79022018-03-06 14:54:01 -08001936 prune_tags_ok = (argc == 1);
Jonathan Tana1743342017-12-08 15:58:43 +00001937 argc--;
1938 argv++;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001939 }
1940 }
1941
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001942 if (remote) {
Christian Couderb14ed5a2019-06-25 15:40:31 +02001943 if (filter_options.choice || has_promisor_remote())
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001944 fetch_one_setup_partial(remote);
Jonathan Tan2b713c22020-08-17 21:01:32 -07001945 result = fetch_one(remote, argc, argv, prune_tags_ok, stdin_refspecs);
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001946 } else {
Johannes Schindelind54dea72019-10-05 11:46:40 -07001947 int max_children = max_jobs;
1948
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001949 if (filter_options.choice)
Christian Coudere0137872019-01-13 09:52:19 +01001950 die(_("--filter can only be used with the remote "
1951 "configured in extensions.partialclone"));
Johannes Schindelind54dea72019-10-05 11:46:40 -07001952
Jonathan Tan2b713c22020-08-17 21:01:32 -07001953 if (stdin_refspecs)
1954 die(_("--stdin can only be used when fetching "
1955 "from one remote"));
1956
Johannes Schindelind54dea72019-10-05 11:46:40 -07001957 if (max_children < 0)
1958 max_children = fetch_parallel_config;
1959
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001960 /* TODO should this also die if we have a previous partial-clone? */
Johannes Schindelind54dea72019-10-05 11:46:40 -07001961 result = fetch_multiple(&list, max_children);
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001962 }
Jonathan Tana1743342017-12-08 15:58:43 +00001963
Jens Lehmannbe254a02010-11-11 00:55:02 +01001964 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
Jeff King22f9b7f2020-07-28 16:24:27 -04001965 struct strvec options = STRVEC_INIT;
Johannes Schindelind54dea72019-10-05 11:46:40 -07001966 int max_children = max_jobs;
1967
1968 if (max_children < 0)
1969 max_children = submodule_fetch_jobs_config;
1970 if (max_children < 0)
1971 max_children = fetch_parallel_config;
Jeff King85556d42012-09-01 07:27:35 -04001972
1973 add_options_to_argv(&options);
Brandon Williamse7241972017-12-12 11:53:52 -08001974 result = fetch_populated_submodules(the_repository,
1975 &options,
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001976 submodule_prefix,
Jens Lehmann8f0700d2011-03-06 23:11:21 +01001977 recurse_submodules,
Brandon Williams8fa29152017-08-02 12:49:19 -07001978 recurse_submodules_default,
Stefan Beller62104ba2015-12-15 16:04:12 -08001979 verbosity < 0,
1980 max_children);
Jeff King22f9b7f2020-07-28 16:24:27 -04001981 strvec_clear(&options);
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001982 }
1983
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001984 string_list_clear(&list, 0);
1985
Derrick Stolee50f26bd2019-09-02 19:22:02 -07001986 prepare_repo_settings(the_repository);
Johannes Schindelinc14e6e72019-11-03 00:21:56 +00001987 if (fetch_write_commit_graph > 0 ||
1988 (fetch_write_commit_graph < 0 &&
1989 the_repository->settings.fetch_write_commit_graph)) {
Junio C Hamano5a535092019-09-30 13:19:32 +09001990 int commit_graph_flags = COMMIT_GRAPH_WRITE_SPLIT;
Derrick Stolee50f26bd2019-09-02 19:22:02 -07001991
1992 if (progress)
Junio C Hamano5a535092019-09-30 13:19:32 +09001993 commit_graph_flags |= COMMIT_GRAPH_WRITE_PROGRESS;
Derrick Stolee50f26bd2019-09-02 19:22:02 -07001994
Taylor Blau0bd52e22020-02-03 21:51:50 -08001995 write_commit_graph_reachable(the_repository->objects->odb,
Derrick Stolee50f26bd2019-09-02 19:22:02 -07001996 commit_graph_flags,
Derrick Stolee63020f12020-01-02 16:14:14 +00001997 NULL);
Derrick Stolee50f26bd2019-09-02 19:22:02 -07001998 }
1999
Derrick Stolee2d511cf2019-05-17 11:41:49 -07002000 close_object_store(the_repository->objects);
Johannes Schindelin0898c962016-01-13 18:20:11 +01002001
Junio C Hamano850b6ed2020-05-06 13:18:29 -07002002 if (enable_auto_gc)
Derrick Stoleea95ce122020-09-17 18:11:44 +00002003 run_auto_maintenance(verbosity < 0);
Jeff King131b8fc2013-01-26 17:40:38 -05002004
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01002005 return result;
2006}