blob: 0f23dd4b8ce942dae6b9a5a0ee6352b225b99e43 [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;
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -040083
Michael Schubert737c5a92013-07-13 11:36:24 +020084static int git_fetch_config(const char *k, const char *v, void *cb)
85{
86 if (!strcmp(k, "fetch.prune")) {
87 fetch_prune_config = git_config_bool(k, v);
88 return 0;
89 }
Stefan Beller58f42032017-05-31 17:30:50 -070090
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +000091 if (!strcmp(k, "fetch.prunetags")) {
92 fetch_prune_tags_config = git_config_bool(k, v);
93 return 0;
94 }
95
Derrick Stoleecdbd70c2019-06-18 13:25:26 -070096 if (!strcmp(k, "fetch.showforcedupdates")) {
97 fetch_show_forced_updates = git_config_bool(k, v);
98 return 0;
99 }
100
Stefan Beller58f42032017-05-31 17:30:50 -0700101 if (!strcmp(k, "submodule.recurse")) {
102 int r = git_config_bool(k, v) ?
103 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
104 recurse_submodules = r;
105 }
106
Brandon Williamsf20e7c12017-08-02 12:49:18 -0700107 if (!strcmp(k, "submodule.fetchjobs")) {
Johannes Schindelind54dea72019-10-05 11:46:40 -0700108 submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v);
Brandon Williamsf20e7c12017-08-02 12:49:18 -0700109 return 0;
Brandon Williams8fa29152017-08-02 12:49:19 -0700110 } else if (!strcmp(k, "fetch.recursesubmodules")) {
111 recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
112 return 0;
Brandon Williamsf20e7c12017-08-02 12:49:18 -0700113 }
114
Johannes Schindelind54dea72019-10-05 11:46:40 -0700115 if (!strcmp(k, "fetch.parallel")) {
116 fetch_parallel_config = git_config_int(k, v);
117 if (fetch_parallel_config < 0)
118 die(_("fetch.parallel cannot be negative"));
119 return 0;
120 }
121
Jeff King72549df2014-11-04 08:11:19 -0500122 return git_default_config(k, v, cb);
Michael Schubert737c5a92013-07-13 11:36:24 +0200123}
124
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700125static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
126{
Jeff King517fe802018-11-05 01:45:42 -0500127 BUG_ON_OPT_NEG(unset);
128
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700129 /*
130 * "git fetch --refmap='' origin foo"
131 * can be used to tell the command not to store anywhere
132 */
Brandon Williamse4cffac2018-05-16 15:58:05 -0700133 refspec_append(&refmap, arg);
134
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700135 return 0;
136}
137
Kristian Høgsberg83201992007-12-04 02:25:47 -0500138static struct option builtin_fetch_options[] = {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100139 OPT__VERBOSITY(&verbosity),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200140 OPT_BOOL(0, "all", &all,
141 N_("fetch from all remotes")),
Corentin BOMPARD24bc1a12019-08-19 11:11:20 +0200142 OPT_BOOL(0, "set-upstream", &set_upstream,
143 N_("set upstream for git pull/fetch")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200144 OPT_BOOL('a', "append", &append,
145 N_("append to .git/FETCH_HEAD instead of overwriting")),
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700146 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
147 N_("path to upload pack on remote end")),
Ævar Arnfjörð Bjarmason8cd4b7c2018-08-31 20:09:56 +0000148 OPT__FORCE(&force, N_("force overwrite of local reference"), 0),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200149 OPT_BOOL('m', "multiple", &multiple,
150 N_("fetch from multiple remotes")),
Kristian Høgsberg83201992007-12-04 02:25:47 -0500151 OPT_SET_INT('t', "tags", &tags,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700152 N_("fetch all tags and associated objects"), TAGS_SET),
Johannes Schindeline7951292008-03-13 08:13:15 +0100153 OPT_SET_INT('n', NULL, &tags,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700154 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
Johannes Schindelind54dea72019-10-05 11:46:40 -0700155 OPT_INTEGER('j', "jobs", &max_jobs,
Stefan Beller62104ba2015-12-15 16:04:12 -0800156 N_("number of submodules fetched in parallel")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200157 OPT_BOOL('p', "prune", &prune,
158 N_("prune remote-tracking branches no longer on remote")),
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +0000159 OPT_BOOL('P', "prune-tags", &prune_tags,
160 N_("prune local tags no longer on remote and clobber changed tags")),
Denton Liu203c8532020-04-28 04:36:28 -0400161 OPT_CALLBACK_F(0, "recurse-submodules", &recurse_submodules, N_("on-demand"),
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700162 N_("control recursive fetching of submodules"),
Denton Liu203c8532020-04-28 04:36:28 -0400163 PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200164 OPT_BOOL(0, "dry-run", &dry_run,
165 N_("dry run")),
Junio C Hamano887952b2020-08-18 14:25:22 +0000166 OPT_BOOL(0, "write-fetch-head", &write_fetch_head,
167 N_("write fetched references to the FETCH_HEAD file")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200168 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
169 OPT_BOOL('u', "update-head-ok", &update_head_ok,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700170 N_("allow updating of HEAD ref")),
171 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
172 OPT_STRING(0, "depth", &depth, N_("depth"),
173 N_("deepen history of shallow clone")),
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700174 OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
175 N_("deepen history of shallow repository based on time")),
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700176 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"),
Alex Henrie6d873862016-12-04 15:03:59 -0700177 N_("deepen history of shallow clone, excluding rev")),
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700178 OPT_INTEGER(0, "deepen", &deepen_relative,
179 N_("deepen history of shallow clone")),
Nguyễn Thái Ngọc Duy3e4a67b2018-05-20 17:42:58 +0200180 OPT_SET_INT_F(0, "unshallow", &unshallow,
181 N_("convert to a complete repository"),
182 1, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700183 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
184 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
Denton Liu203c8532020-04-28 04:36:28 -0400185 OPT_CALLBACK_F(0, "recurse-submodules-default",
Stefan Beller8c698322017-06-23 12:13:01 -0700186 &recurse_submodules_default, N_("on-demand"),
187 N_("default for recursive fetching of submodules "
188 "(lower priority than config files)"),
Denton Liu203c8532020-04-28 04:36:28 -0400189 PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules),
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +0700190 OPT_BOOL(0, "update-shallow", &update_shallow,
191 N_("accept refs that update .git/shallow")),
Denton Liu203c8532020-04-28 04:36:28 -0400192 OPT_CALLBACK_F(0, "refmap", NULL, N_("refmap"),
193 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg),
Brandon Williams5e3548e2018-04-23 15:46:24 -0700194 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
Eric Wongc915f112016-02-03 04:09:14 +0000195 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
196 TRANSPORT_FAMILY_IPV4),
197 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
198 TRANSPORT_FAMILY_IPV6),
Jonathan Tan3390e422018-07-02 15:39:44 -0700199 OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"),
200 N_("report that we have only objects reachable from this object")),
Jeff Hostetleracb0c572017-12-08 15:58:44 +0000201 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
Nguyễn Thái Ngọc Duyc3d6b702019-06-19 16:46:30 +0700202 OPT_BOOL(0, "auto-gc", &enable_auto_gc,
203 N_("run 'gc --auto' after fetching")),
Derrick Stoleecdbd70c2019-06-18 13:25:26 -0700204 OPT_BOOL(0, "show-forced-updates", &fetch_show_forced_updates,
205 N_("check for forced-updates on all updated branches")),
Johannes Schindelinc14e6e72019-11-03 00:21:56 +0000206 OPT_BOOL(0, "write-commit-graph", &fetch_write_commit_graph,
207 N_("write the commit-graph after fetching")),
Kristian Høgsberg83201992007-12-04 02:25:47 -0500208 OPT_END()
209};
210
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400211static void unlock_pack(void)
212{
Junio C Hamanoaf234452013-08-07 15:38:45 -0700213 if (gtransport)
214 transport_unlock_pack(gtransport);
Junio C Hamanob26ed432013-08-07 15:47:18 -0700215 if (gsecondary)
216 transport_unlock_pack(gsecondary);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400217}
218
219static void unlock_pack_on_signal(int signo)
220{
221 unlock_pack();
Jeff King4a16d072009-01-22 01:02:35 -0500222 sigchain_pop(signo);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400223 raise(signo);
224}
Daniel Barkalowb888d612007-09-10 23:03:25 -0400225
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400226static void add_merge_config(struct ref **head,
Daniel Barkalow45773702007-10-29 21:05:40 -0400227 const struct ref *remote_refs,
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400228 struct branch *branch,
229 struct ref ***tail)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400230{
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400231 int i;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400232
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400233 for (i = 0; i < branch->merge_nr; i++) {
234 struct ref *rm, **old_tail = *tail;
Brandon Williams0ad4a5f2018-05-16 15:57:49 -0700235 struct refspec_item refspec;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400236
237 for (rm = *head; rm; rm = rm->next) {
238 if (branch_merge_matches(branch, i, rm->name)) {
Jeff King900f2812013-05-11 18:15:59 +0200239 rm->fetch_head_status = FETCH_HEAD_MERGE;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400240 break;
241 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400242 }
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400243 if (rm)
244 continue;
245
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700246 /*
Matthieu Moy8b3f3f82010-11-02 16:31:23 +0100247 * Not fetched to a remote-tracking branch? We need to fetch
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400248 * it anyway to allow this branch's "branch.$name.merge"
Heikki Orsila05207a22008-09-09 13:28:30 +0300249 * to be honored by 'git pull', but we do not have to
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700250 * fail if branch.$name.merge is misconfigured to point
251 * at a nonexisting branch. If we were indeed called by
Heikki Orsila05207a22008-09-09 13:28:30 +0300252 * 'git pull', it will notice the misconfiguration because
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700253 * there is no entry in the resulting FETCH_HEAD marked
254 * for merging.
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400255 */
Andreas Gruenbacher8da61a22010-03-12 23:27:33 +0100256 memset(&refspec, 0, sizeof(refspec));
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400257 refspec.src = branch->merge[i]->src;
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700258 get_fetch_map(remote_refs, &refspec, tail, 1);
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400259 for (rm = *old_tail; rm; rm = rm->next)
Jeff King900f2812013-05-11 18:15:59 +0200260 rm->fetch_head_status = FETCH_HEAD_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400261 }
262}
263
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700264static void create_fetch_oidset(struct ref **head, struct oidset *out)
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100265{
266 struct ref *rm = *head;
267 while (rm) {
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700268 oidset_insert(out, &rm->old_oid);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100269 rm = rm->next;
270 }
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100271}
272
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700273struct refname_hash_entry {
Eric Wonge2b50382019-10-06 23:30:43 +0000274 struct hashmap_entry ent;
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700275 struct object_id oid;
Felipe Contrerasf80d9222019-06-03 21:13:30 -0500276 int ignore;
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700277 char refname[FLEX_ARRAY];
278};
279
280static int refname_hash_entry_cmp(const void *hashmap_cmp_fn_data,
Eric Wong939af162019-10-06 23:30:37 +0000281 const struct hashmap_entry *eptr,
282 const struct hashmap_entry *entry_or_key,
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700283 const void *keydata)
284{
Eric Wong939af162019-10-06 23:30:37 +0000285 const struct refname_hash_entry *e1, *e2;
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700286
Eric Wong939af162019-10-06 23:30:37 +0000287 e1 = container_of(eptr, const struct refname_hash_entry, ent);
288 e2 = container_of(entry_or_key, const struct refname_hash_entry, ent);
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700289 return strcmp(e1->refname, keydata ? keydata : e2->refname);
290}
291
292static struct refname_hash_entry *refname_hash_add(struct hashmap *map,
293 const char *refname,
294 const struct object_id *oid)
295{
296 struct refname_hash_entry *ent;
297 size_t len = strlen(refname);
298
299 FLEX_ALLOC_MEM(ent, refname, refname, len);
Eric Wongd22245a2019-10-06 23:30:27 +0000300 hashmap_entry_init(&ent->ent, strhash(refname));
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700301 oidcpy(&ent->oid, oid);
Eric Wongb94e5c12019-10-06 23:30:29 +0000302 hashmap_add(map, &ent->ent);
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700303 return ent;
304}
305
306static int add_one_refname(const char *refname,
307 const struct object_id *oid,
308 int flag, void *cbdata)
309{
310 struct hashmap *refname_map = cbdata;
311
312 (void) refname_hash_add(refname_map, refname, oid);
313 return 0;
314}
315
316static void refname_hash_init(struct hashmap *map)
317{
318 hashmap_init(map, refname_hash_entry_cmp, NULL, 0);
319}
320
321static int refname_hash_exists(struct hashmap *map, const char *refname)
322{
323 return !!hashmap_get_from_hash(map, strhash(refname), refname);
324}
325
Felipe Contrerasa8363b52019-06-03 21:13:28 -0500326static void clear_item(struct refname_hash_entry *item)
327{
Felipe Contrerasf80d9222019-06-03 21:13:30 -0500328 item->ignore = 1;
Felipe Contrerasa8363b52019-06-03 21:13:28 -0500329}
330
Brandon Williams6d1700d2018-06-27 15:30:21 -0700331static void find_non_local_tags(const struct ref *refs,
332 struct ref **head,
333 struct ref ***tail)
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100334{
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700335 struct hashmap existing_refs;
336 struct hashmap remote_refs;
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700337 struct oidset fetch_oids = OIDSET_INIT;
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700338 struct string_list remote_refs_list = STRING_LIST_INIT_NODUP;
339 struct string_list_item *remote_ref_item;
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100340 const struct ref *ref;
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700341 struct refname_hash_entry *item = NULL;
Derrick Stolee3e96c662020-02-21 21:47:28 +0000342 const int quick_flags = OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT;
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100343
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700344 refname_hash_init(&existing_refs);
345 refname_hash_init(&remote_refs);
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700346 create_fetch_oidset(head, &fetch_oids);
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700347
348 for_each_ref(add_one_refname, &existing_refs);
Brandon Williams6d1700d2018-06-27 15:30:21 -0700349 for (ref = refs; ref; ref = ref->next) {
Junio C Hamanoad704482013-12-17 11:47:35 -0800350 if (!starts_with(ref->name, "refs/tags/"))
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100351 continue;
352
353 /*
354 * The peeled ref always follows the matching base
355 * ref, so if we see a peeled ref that we don't want
356 * to fetch then we can mark the ref entry in the list
357 * as one to ignore by setting util to NULL.
358 */
Junio C Hamanoad704482013-12-17 11:47:35 -0800359 if (ends_with(ref->name, "^{}")) {
Jeff King5827a032016-10-13 12:53:44 -0400360 if (item &&
Derrick Stolee3e96c662020-02-21 21:47:28 +0000361 !has_object_file_with_flags(&ref->old_oid, quick_flags) &&
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700362 !oidset_contains(&fetch_oids, &ref->old_oid) &&
Derrick Stolee3e96c662020-02-21 21:47:28 +0000363 !has_object_file_with_flags(&item->oid, quick_flags) &&
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700364 !oidset_contains(&fetch_oids, &item->oid))
Felipe Contrerasa8363b52019-06-03 21:13:28 -0500365 clear_item(item);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100366 item = NULL;
367 continue;
368 }
369
370 /*
371 * If item is non-NULL here, then we previously saw a
372 * ref not followed by a peeled reference, so we need
373 * to check if it is a lightweight tag that we want to
374 * fetch.
375 */
Jeff King5827a032016-10-13 12:53:44 -0400376 if (item &&
Derrick Stolee3e96c662020-02-21 21:47:28 +0000377 !has_object_file_with_flags(&item->oid, quick_flags) &&
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700378 !oidset_contains(&fetch_oids, &item->oid))
Felipe Contrerasa8363b52019-06-03 21:13:28 -0500379 clear_item(item);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100380
381 item = NULL;
382
383 /* skip duplicates and refs that we already have */
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700384 if (refname_hash_exists(&remote_refs, ref->name) ||
385 refname_hash_exists(&existing_refs, ref->name))
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100386 continue;
387
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700388 item = refname_hash_add(&remote_refs, ref->name, &ref->old_oid);
389 string_list_insert(&remote_refs_list, ref->name);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100390 }
Eric Wongc8e424c2019-10-06 23:30:40 +0000391 hashmap_free_entries(&existing_refs, struct refname_hash_entry, ent);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100392
393 /*
394 * We may have a final lightweight tag that needs to be
395 * checked to see if it needs fetching.
396 */
Jeff King5827a032016-10-13 12:53:44 -0400397 if (item &&
Derrick Stolee3e96c662020-02-21 21:47:28 +0000398 !has_object_file_with_flags(&item->oid, quick_flags) &&
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700399 !oidset_contains(&fetch_oids, &item->oid))
Felipe Contrerasa8363b52019-06-03 21:13:28 -0500400 clear_item(item);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100401
402 /*
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700403 * For all the tags in the remote_refs_list,
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100404 * add them to the list of refs to be fetched
405 */
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700406 for_each_string_list_item(remote_ref_item, &remote_refs_list) {
407 const char *refname = remote_ref_item->string;
Felipe Contreras9528b802019-06-03 21:13:29 -0500408 struct ref *rm;
Eric Wongf23a4652019-10-06 23:30:36 +0000409 unsigned int hash = strhash(refname);
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700410
Eric Wongf23a4652019-10-06 23:30:36 +0000411 item = hashmap_get_entry_from_hash(&remote_refs, hash, refname,
412 struct refname_hash_entry, ent);
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700413 if (!item)
414 BUG("unseen remote ref?");
415
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100416 /* Unless we have already decided to ignore this item... */
Felipe Contrerasf80d9222019-06-03 21:13:30 -0500417 if (item->ignore)
Felipe Contreras9528b802019-06-03 21:13:29 -0500418 continue;
419
420 rm = alloc_ref(item->refname);
421 rm->peer_ref = alloc_ref(item->refname);
422 oidcpy(&rm->old_oid, &item->oid);
423 **tail = rm;
424 *tail = &rm->next;
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100425 }
Eric Wongc8e424c2019-10-06 23:30:40 +0000426 hashmap_free_entries(&remote_refs, struct refname_hash_entry, ent);
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700427 string_list_clear(&remote_refs_list, 0);
Masaya Suzukib7e2d8b2019-09-15 14:18:02 -0700428 oidset_clear(&fetch_oids);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100429}
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500430
Brandon Williams6d1700d2018-06-27 15:30:21 -0700431static struct ref *get_ref_map(struct remote *remote,
432 const struct ref *remote_refs,
Brandon Williams65d96c82018-05-16 15:58:08 -0700433 struct refspec *rs,
Michael Haggertyf137a452013-10-23 17:50:38 +0200434 int tags, int *autotags)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400435{
436 int i;
437 struct ref *rm;
438 struct ref *ref_map = NULL;
439 struct ref **tail = &ref_map;
440
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100441 /* opportunistically-updated references: */
442 struct ref *orefs = NULL, **oref_tail = &orefs;
443
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700444 struct hashmap existing_refs;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400445
Brandon Williams65d96c82018-05-16 15:58:08 -0700446 if (rs->nr) {
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700447 struct refspec *fetch_refspec;
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700448
Brandon Williams65d96c82018-05-16 15:58:08 -0700449 for (i = 0; i < rs->nr; i++) {
450 get_fetch_map(remote_refs, &rs->items[i], &tail, 0);
451 if (rs->items[i].dst && rs->items[i].dst[0])
Daniel Barkalowb888d612007-09-10 23:03:25 -0400452 *autotags = 1;
453 }
Michael Haggerty0281c932013-10-30 06:32:58 +0100454 /* Merge everything on the command line (but not --tags) */
Daniel Barkalowb888d612007-09-10 23:03:25 -0400455 for (rm = ref_map; rm; rm = rm->next)
Jeff King900f2812013-05-11 18:15:59 +0200456 rm->fetch_head_status = FETCH_HEAD_MERGE;
Michael Haggerty0281c932013-10-30 06:32:58 +0100457
458 /*
459 * For any refs that we happen to be fetching via
460 * command-line arguments, the destination ref might
461 * have been missing or have been different than the
462 * remote-tracking ref that would be derived from the
463 * configured refspec. In these cases, we want to
464 * take the opportunity to update their configured
465 * remote-tracking reference. However, we do not want
466 * to mention these entries in FETCH_HEAD at all, as
467 * they would simply be duplicates of existing
468 * entries, so we set them FETCH_HEAD_IGNORE below.
469 *
470 * We compute these entries now, based only on the
471 * refspecs specified on the command line. But we add
472 * them to the list following the refspecs resulting
473 * from the tags option so that one of the latter,
474 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
475 * by ref_remove_duplicates() in favor of one of these
476 * opportunistic entries with FETCH_HEAD_IGNORE.
477 */
Brandon Williams65d96c82018-05-16 15:58:08 -0700478 if (refmap.nr)
479 fetch_refspec = &refmap;
480 else
Brandon Williams6d1700d2018-06-27 15:30:21 -0700481 fetch_refspec = &remote->fetch;
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700482
Brandon Williams65d96c82018-05-16 15:58:08 -0700483 for (i = 0; i < fetch_refspec->nr; i++)
484 get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
Brandon Williamse4cffac2018-05-16 15:58:05 -0700485 } else if (refmap.nr) {
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700486 die("--refmap option is only meaningful with command-line refspec(s).");
Daniel Barkalowb888d612007-09-10 23:03:25 -0400487 } else {
488 /* Use the defaults */
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400489 struct branch *branch = branch_get(NULL);
490 int has_merge = branch_has_merge_config(branch);
Brandon Casey3ee17572010-08-25 12:52:56 -0500491 if (remote &&
Brandon Williamse5349ab2018-05-16 15:58:01 -0700492 (remote->fetch.nr ||
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500493 /* Note: has_merge implies non-NULL branch->remote_name */
Brandon Casey3ee17572010-08-25 12:52:56 -0500494 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
Brandon Williamse5349ab2018-05-16 15:58:01 -0700495 for (i = 0; i < remote->fetch.nr; i++) {
496 get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);
497 if (remote->fetch.items[i].dst &&
498 remote->fetch.items[i].dst[0])
Daniel Barkalowb888d612007-09-10 23:03:25 -0400499 *autotags = 1;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400500 if (!i && !has_merge && ref_map &&
Brandon Williamse5349ab2018-05-16 15:58:01 -0700501 !remote->fetch.items[0].pattern)
Jeff King900f2812013-05-11 18:15:59 +0200502 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400503 }
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100504 /*
505 * if the remote we're fetching from is the same
506 * as given in branch.<name>.remote, we add the
507 * ref given in branch.<name>.merge, too.
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500508 *
509 * Note: has_merge implies non-NULL branch->remote_name
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100510 */
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700511 if (has_merge &&
512 !strcmp(branch->remote_name, remote->name))
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400513 add_merge_config(&ref_map, remote_refs, branch, &tail);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400514 } else {
515 ref_map = get_remote_ref(remote_refs, "HEAD");
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700516 if (!ref_map)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000517 die(_("Couldn't find remote ref HEAD"));
Jeff King900f2812013-05-11 18:15:59 +0200518 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
Shawn O. Pearce5aaf7f22008-03-02 21:34:51 -0500519 tail = &ref_map->next;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400520 }
521 }
Michael Haggerty0281c932013-10-30 06:32:58 +0100522
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100523 if (tags == TAGS_SET)
524 /* also fetch all tags */
525 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
526 else if (tags == TAGS_DEFAULT && *autotags)
Brandon Williams6d1700d2018-06-27 15:30:21 -0700527 find_non_local_tags(remote_refs, &ref_map, &tail);
Michael Haggerty0281c932013-10-30 06:32:58 +0100528
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100529 /* Now append any refs to be updated opportunistically: */
530 *tail = orefs;
531 for (rm = orefs; rm; rm = rm->next) {
532 rm->fetch_head_status = FETCH_HEAD_IGNORE;
533 tail = &rm->next;
534 }
535
Brandon Williams14b8ced2018-06-27 15:30:19 -0700536 ref_map = ref_remove_duplicates(ref_map);
537
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700538 refname_hash_init(&existing_refs);
539 for_each_ref(add_one_refname, &existing_refs);
540
Brandon Williams14b8ced2018-06-27 15:30:19 -0700541 for (rm = ref_map; rm; rm = rm->next) {
542 if (rm->peer_ref) {
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700543 const char *refname = rm->peer_ref->name;
544 struct refname_hash_entry *peer_item;
Eric Wongf23a4652019-10-06 23:30:36 +0000545 unsigned int hash = strhash(refname);
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700546
Eric Wongf23a4652019-10-06 23:30:36 +0000547 peer_item = hashmap_get_entry_from_hash(&existing_refs,
548 hash, refname,
549 struct refname_hash_entry, ent);
Brandon Williams14b8ced2018-06-27 15:30:19 -0700550 if (peer_item) {
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700551 struct object_id *old_oid = &peer_item->oid;
Brandon Williams14b8ced2018-06-27 15:30:19 -0700552 oidcpy(&rm->peer_ref->old_oid, old_oid);
553 }
554 }
555 }
Eric Wongc8e424c2019-10-06 23:30:40 +0000556 hashmap_free_entries(&existing_refs, struct refname_hash_entry, ent);
Brandon Williams14b8ced2018-06-27 15:30:19 -0700557
558 return ref_map;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400559}
560
Jeff Kingfa250752009-05-25 06:40:54 -0400561#define STORE_REF_ERROR_OTHER 1
562#define STORE_REF_ERROR_DF_CONFLICT 2
563
Daniel Barkalowb888d612007-09-10 23:03:25 -0400564static int s_update_ref(const char *action,
565 struct ref *ref,
566 int check_old)
567{
Jeff King1412f762017-03-28 15:46:26 -0400568 char *msg;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400569 char *rla = getenv("GIT_REFLOG_ACTION");
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700570 struct ref_transaction *transaction;
571 struct strbuf err = STRBUF_INIT;
572 int ret, df_conflict = 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400573
Jay Soffian28a15402009-11-10 09:19:43 +0100574 if (dry_run)
575 return 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400576 if (!rla)
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -0500577 rla = default_rla.buf;
Jeff King1412f762017-03-28 15:46:26 -0400578 msg = xstrfmt("%s: %s", rla, action);
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700579
580 transaction = ref_transaction_begin(&err);
581 if (!transaction ||
Michael Haggerty1d147bd2015-02-17 18:00:15 +0100582 ref_transaction_update(transaction, ref->name,
brian m. carlson89f3bbd2017-10-15 22:06:53 +0000583 &ref->new_oid,
584 check_old ? &ref->old_oid : NULL,
Michael Haggerty1d147bd2015-02-17 18:00:15 +0100585 0, msg, &err))
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700586 goto fail;
587
588 ret = ref_transaction_commit(transaction, &err);
589 if (ret) {
590 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
591 goto fail;
592 }
593
594 ref_transaction_free(transaction);
595 strbuf_release(&err);
Jeff King1412f762017-03-28 15:46:26 -0400596 free(msg);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400597 return 0;
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700598fail:
599 ref_transaction_free(transaction);
600 error("%s", err.buf);
601 strbuf_release(&err);
Jeff King1412f762017-03-28 15:46:26 -0400602 free(msg);
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700603 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
604 : STORE_REF_ERROR_OTHER;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400605}
606
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200607static int refcol_width = 10;
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200608static int compact_format;
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200609
610static void adjust_refcol_width(const struct ref *ref)
611{
612 int max, rlen, llen, len;
613
614 /* uptodate lines are only shown on high verbosity level */
Jeff King4a7e27e2018-08-28 17:22:40 -0400615 if (!verbosity && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200616 return;
617
618 max = term_columns();
619 rlen = utf8_strwidth(prettify_refname(ref->name));
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200620
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200621 llen = utf8_strwidth(prettify_refname(ref->peer_ref->name));
622
623 /*
624 * rough estimation to see if the output line is too long and
625 * should not be counted (we can't do precise calculation
626 * anyway because we don't know if the error explanation part
627 * will be printed in update_local_ref)
628 */
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200629 if (compact_format) {
630 llen = 0;
631 max = max * 2 / 3;
632 }
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200633 len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
634 if (len >= max)
635 return;
636
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200637 /*
638 * Not precise calculation for compact mode because '*' can
639 * appear on the left hand side of '->' and shrink the column
640 * back.
641 */
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200642 if (refcol_width < rlen)
643 refcol_width = rlen;
644}
645
646static void prepare_format_display(struct ref *ref_map)
647{
648 struct ref *rm;
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200649 const char *format = "full";
650
Jeff Kingf1de9812020-08-14 12:17:36 -0400651 git_config_get_string_tmp("fetch.output", &format);
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200652 if (!strcasecmp(format, "full"))
653 compact_format = 0;
654 else if (!strcasecmp(format, "compact"))
655 compact_format = 1;
656 else
657 die(_("configuration fetch.output contains invalid value %s"),
658 format);
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200659
660 for (rm = ref_map; rm; rm = rm->next) {
661 if (rm->status == REF_STATUS_REJECT_SHALLOW ||
662 !rm->peer_ref ||
663 !strcmp(rm->name, "HEAD"))
664 continue;
665
666 adjust_refcol_width(rm);
667 }
668}
Nicolas Pitre165f3902007-11-03 01:32:48 -0400669
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200670static void print_remote_to_local(struct strbuf *display,
671 const char *remote, const char *local)
672{
673 strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local);
674}
675
676static int find_and_replace(struct strbuf *haystack,
677 const char *needle,
678 const char *placeholder)
679{
Nguyễn Thái Ngọc Duydc40b242019-01-25 16:51:22 +0700680 const char *p = NULL;
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200681 int plen, nlen;
682
Nguyễn Thái Ngọc Duydc40b242019-01-25 16:51:22 +0700683 nlen = strlen(needle);
684 if (ends_with(haystack->buf, needle))
685 p = haystack->buf + haystack->len - nlen;
686 else
687 p = strstr(haystack->buf, needle);
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200688 if (!p)
689 return 0;
690
691 if (p > haystack->buf && p[-1] != '/')
692 return 0;
693
694 plen = strlen(p);
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200695 if (plen > nlen && p[nlen] != '/')
696 return 0;
697
698 strbuf_splice(haystack, p - haystack->buf, nlen,
699 placeholder, strlen(placeholder));
700 return 1;
701}
702
703static void print_compact(struct strbuf *display,
704 const char *remote, const char *local)
705{
706 struct strbuf r = STRBUF_INIT;
707 struct strbuf l = STRBUF_INIT;
708
709 if (!strcmp(remote, local)) {
710 strbuf_addf(display, "%-*s -> *", refcol_width, remote);
711 return;
712 }
713
714 strbuf_addstr(&r, remote);
715 strbuf_addstr(&l, local);
716
717 if (!find_and_replace(&r, local, "*"))
718 find_and_replace(&l, remote, "*");
719 print_remote_to_local(display, r.buf, l.buf);
720
721 strbuf_release(&r);
722 strbuf_release(&l);
723}
724
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200725static void format_display(struct strbuf *display, char code,
726 const char *summary, const char *error,
Junio C Hamano901f3d42016-10-21 15:22:55 -0700727 const char *remote, const char *local,
728 int summary_width)
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200729{
Junio C Hamano901f3d42016-10-21 15:22:55 -0700730 int width = (summary_width + strlen(summary) - gettext_width(summary));
731
732 strbuf_addf(display, "%c %-*s ", code, width, summary);
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200733 if (!compact_format)
734 print_remote_to_local(display, remote, local);
735 else
736 print_compact(display, remote, local);
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200737 if (error)
738 strbuf_addf(display, " (%s)", error);
739}
Daniel Barkalowb888d612007-09-10 23:03:25 -0400740
741static int update_local_ref(struct ref *ref,
Nicolas Pitre165f3902007-11-03 01:32:48 -0400742 const char *remote,
Marc Branchaud6da618d2012-04-16 18:08:49 -0400743 const struct ref *remote_ref,
Junio C Hamano901f3d42016-10-21 15:22:55 -0700744 struct strbuf *display,
745 int summary_width)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400746{
Daniel Barkalowb888d612007-09-10 23:03:25 -0400747 struct commit *current = NULL, *updated;
748 enum object_type type;
749 struct branch *current_branch = branch_get(NULL);
Felipe Contreras4577e482009-05-14 00:22:04 +0300750 const char *pretty_ref = prettify_refname(ref->name);
Derrick Stolee377444b2019-06-18 13:25:27 -0700751 int fast_forward = 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400752
Stefan Beller0df8e962018-04-25 11:20:59 -0700753 type = oid_object_info(the_repository, &ref->new_oid, NULL);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400754 if (type < 0)
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000755 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
Daniel Barkalowb888d612007-09-10 23:03:25 -0400756
Jeff King4a7e27e2018-08-28 17:22:40 -0400757 if (oideq(&ref->old_oid, &ref->new_oid)) {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100758 if (verbosity > 0)
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200759 format_display(display, '=', _("[up to date]"), NULL,
Junio C Hamano901f3d42016-10-21 15:22:55 -0700760 remote, pretty_ref, summary_width);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400761 return 0;
762 }
763
Shawn O. Pearceb3abdd92007-09-16 02:31:26 -0400764 if (current_branch &&
765 !strcmp(ref->name, current_branch->name) &&
Daniel Barkalowb888d612007-09-10 23:03:25 -0400766 !(update_head_ok || is_bare_repository()) &&
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000767 !is_null_oid(&ref->old_oid)) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400768 /*
769 * If this is the head, and it's not okay to update
770 * the head, and the old value of the head isn't empty...
771 */
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200772 format_display(display, '!', _("[rejected]"),
773 _("can't fetch in current branch"),
Junio C Hamano901f3d42016-10-21 15:22:55 -0700774 remote, pretty_ref, summary_width);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400775 return 1;
776 }
777
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000778 if (!is_null_oid(&ref->old_oid) &&
Christian Couder59556542013-11-30 21:55:40 +0100779 starts_with(ref->name, "refs/tags/")) {
Ævar Arnfjörð Bjarmason0bc8d712018-08-31 20:10:04 +0000780 if (force || ref->force) {
781 int r;
782 r = s_update_ref("updating tag", ref, 0);
783 format_display(display, r ? '!' : 't', _("[tag update]"),
784 r ? _("unable to update local ref") : NULL,
785 remote, pretty_ref, summary_width);
786 return r;
787 } else {
788 format_display(display, '!', _("[rejected]"), _("would clobber existing tag"),
789 remote, pretty_ref, summary_width);
790 return 1;
791 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400792 }
793
Stefan Beller21e1ee82018-06-28 18:21:57 -0700794 current = lookup_commit_reference_gently(the_repository,
795 &ref->old_oid, 1);
796 updated = lookup_commit_reference_gently(the_repository,
797 &ref->new_oid, 1);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400798 if (!current || !updated) {
Nicolas Pitre165f3902007-11-03 01:32:48 -0400799 const char *msg;
800 const char *what;
Jeff King63154722008-06-26 23:59:50 -0400801 int r;
Marc Branchaud0997ada2012-04-16 18:08:50 -0400802 /*
803 * Nicely describe the new ref we're fetching.
804 * Base this on the remote's ref name, as it's
805 * more likely to follow a standard layout.
806 */
807 const char *name = remote_ref ? remote_ref->name : "";
Christian Couder59556542013-11-30 21:55:40 +0100808 if (starts_with(name, "refs/tags/")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400809 msg = "storing tag";
Ævar Arnfjörð Bjarmasonf7b37422011-02-22 23:41:53 +0000810 what = _("[new tag]");
Christian Couder59556542013-11-30 21:55:40 +0100811 } else if (starts_with(name, "refs/heads/")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400812 msg = "storing head";
Ævar Arnfjörð Bjarmasonf7b37422011-02-22 23:41:53 +0000813 what = _("[new branch]");
Marc Branchaud0997ada2012-04-16 18:08:50 -0400814 } else {
815 msg = "storing ref";
816 what = _("[new ref]");
Nicolas Pitre165f3902007-11-03 01:32:48 -0400817 }
818
Jeff King63154722008-06-26 23:59:50 -0400819 r = s_update_ref(msg, ref, 0);
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200820 format_display(display, r ? '!' : '*', what,
821 r ? _("unable to update local ref") : NULL,
Junio C Hamano901f3d42016-10-21 15:22:55 -0700822 remote, pretty_ref, summary_width);
Jeff King63154722008-06-26 23:59:50 -0400823 return r;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400824 }
825
Derrick Stolee377444b2019-06-18 13:25:27 -0700826 if (fetch_show_forced_updates) {
827 uint64_t t_before = getnanotime();
828 fast_forward = in_merge_bases(current, updated);
829 forced_updates_ms += (getnanotime() - t_before) / 1000000;
830 } else {
831 fast_forward = 1;
832 }
833
834 if (fast_forward) {
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400835 struct strbuf quickref = STRBUF_INIT;
Jeff King63154722008-06-26 23:59:50 -0400836 int r;
Derrick Stoleecdbd70c2019-06-18 13:25:26 -0700837
brian m. carlson30e677e2018-03-12 02:27:28 +0000838 strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400839 strbuf_addstr(&quickref, "..");
brian m. carlson30e677e2018-03-12 02:27:28 +0000840 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300841 r = s_update_ref("fast-forward", ref, 1);
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200842 format_display(display, r ? '!' : ' ', quickref.buf,
843 r ? _("unable to update local ref") : NULL,
Junio C Hamano901f3d42016-10-21 15:22:55 -0700844 remote, pretty_ref, summary_width);
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400845 strbuf_release(&quickref);
Jeff King63154722008-06-26 23:59:50 -0400846 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400847 } else if (force || ref->force) {
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400848 struct strbuf quickref = STRBUF_INIT;
Jeff King63154722008-06-26 23:59:50 -0400849 int r;
brian m. carlson30e677e2018-03-12 02:27:28 +0000850 strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400851 strbuf_addstr(&quickref, "...");
brian m. carlson30e677e2018-03-12 02:27:28 +0000852 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
Jeff King63154722008-06-26 23:59:50 -0400853 r = s_update_ref("forced-update", ref, 1);
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200854 format_display(display, r ? '!' : '+', quickref.buf,
855 r ? _("unable to update local ref") : _("forced update"),
Junio C Hamano901f3d42016-10-21 15:22:55 -0700856 remote, pretty_ref, summary_width);
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400857 strbuf_release(&quickref);
Jeff King63154722008-06-26 23:59:50 -0400858 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400859 } else {
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200860 format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
Junio C Hamano901f3d42016-10-21 15:22:55 -0700861 remote, pretty_ref, summary_width);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400862 return 1;
863 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400864}
865
brian m. carlson6ccac9e2017-10-15 22:06:54 +0000866static int iterate_ref_map(void *cb_data, struct object_id *oid)
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700867{
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700868 struct ref **rm = cb_data;
869 struct ref *ref = *rm;
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700870
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +0700871 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
872 ref = ref->next;
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700873 if (!ref)
874 return -1; /* end of the list */
875 *rm = ref->next;
brian m. carlson6ccac9e2017-10-15 22:06:54 +0000876 oidcpy(oid, &ref->old_oid);
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700877 return 0;
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700878}
879
Jean-Noël Avila182f59d2019-08-06 19:19:52 +0200880static const char warn_show_forced_updates[] =
881N_("Fetch normally indicates which branches had a forced update,\n"
882 "but that check has been disabled. To re-enable, use '--show-forced-updates'\n"
883 "flag or run 'git config fetch.showForcedUpdates true'.");
884static const char warn_time_show_forced_updates[] =
885N_("It took %.2f seconds to check forced updates. You can use\n"
886 "'--no-show-forced-updates' or run 'git config fetch.showForcedUpdates false'\n"
887 " to avoid this check.\n");
888
Andreas Ericsson47abd852009-04-17 10:20:11 +0200889static int store_updated_refs(const char *raw_url, const char *remote_name,
Jonathan Tancf1e7c02018-07-02 15:08:43 -0700890 int connectivity_checked, struct ref *ref_map)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400891{
892 FILE *fp;
893 struct commit *commit;
Tom Miller4b3b33a2014-01-02 20:28:51 -0600894 int url_len, i, rc = 0;
Jeff King5914f2d2011-12-08 03:43:19 -0500895 struct strbuf note = STRBUF_INIT;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400896 const char *what, *kind;
897 struct ref *rm;
Nguyễn Thái Ngọc Duydcf69262014-11-30 15:24:27 +0700898 char *url;
Junio C Hamano887952b2020-08-18 14:25:22 +0000899 const char *filename = (!write_fetch_head
900 ? "/dev/null"
901 : git_path_fetch_head(the_repository));
Jeff King900f2812013-05-11 18:15:59 +0200902 int want_status;
Junio C Hamano11fd66d2016-10-21 15:28:07 -0700903 int summary_width = transport_summary_width(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400904
André Goddard Rosad6617c72007-11-22 20:22:23 -0200905 fp = fopen(filename, "a");
906 if (!fp)
Nguyễn Thái Ngọc Duy6da31d72016-05-08 16:47:26 +0700907 return error_errno(_("cannot open %s"), filename);
Andreas Ericsson47abd852009-04-17 10:20:11 +0200908
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100909 if (raw_url)
910 url = transport_anonymize_url(raw_url);
911 else
912 url = xstrdup("foreign");
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700913
Jonathan Tancf1e7c02018-07-02 15:08:43 -0700914 if (!connectivity_checked) {
Jonathan Tan2df1aa22020-01-11 20:15:25 -0800915 struct check_connected_options opt = CHECK_CONNECTED_INIT;
916
Jonathan Tancf1e7c02018-07-02 15:08:43 -0700917 rm = ref_map;
Jonathan Tan2df1aa22020-01-11 20:15:25 -0800918 if (check_connected(iterate_ref_map, &rm, &opt)) {
Jonathan Tancf1e7c02018-07-02 15:08:43 -0700919 rc = error(_("%s did not send all necessary objects\n"), url);
920 goto abort;
921 }
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800922 }
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700923
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200924 prepare_format_display(ref_map);
925
Joey Hess96890f42011-12-26 12:16:56 -0400926 /*
Jeff King900f2812013-05-11 18:15:59 +0200927 * We do a pass for each fetch_head_status type in their enum order, so
928 * merged entries are written before not-for-merge. That lets readers
929 * use FETCH_HEAD as a refname to refer to the ref to be merged.
Joey Hess96890f42011-12-26 12:16:56 -0400930 */
Jeff King900f2812013-05-11 18:15:59 +0200931 for (want_status = FETCH_HEAD_MERGE;
932 want_status <= FETCH_HEAD_IGNORE;
933 want_status++) {
Joey Hess96890f42011-12-26 12:16:56 -0400934 for (rm = ref_map; rm; rm = rm->next) {
935 struct ref *ref = NULL;
Jeff King900f2812013-05-11 18:15:59 +0200936 const char *merge_status_marker = "";
Daniel Barkalowb888d612007-09-10 23:03:25 -0400937
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +0700938 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
939 if (want_status == FETCH_HEAD_MERGE)
940 warning(_("reject %s because shallow roots are not allowed to be updated"),
941 rm->peer_ref ? rm->peer_ref->name : rm->name);
942 continue;
943 }
944
Stefan Beller21e1ee82018-06-28 18:21:57 -0700945 commit = lookup_commit_reference_gently(the_repository,
946 &rm->old_oid,
brian m. carlsonbc832662017-05-06 22:10:10 +0000947 1);
Joey Hess96890f42011-12-26 12:16:56 -0400948 if (!commit)
Jeff King900f2812013-05-11 18:15:59 +0200949 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400950
Jeff King900f2812013-05-11 18:15:59 +0200951 if (rm->fetch_head_status != want_status)
Joey Hess96890f42011-12-26 12:16:56 -0400952 continue;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400953
Joey Hess96890f42011-12-26 12:16:56 -0400954 if (rm->peer_ref) {
Jeff King6f687c22015-09-24 17:08:09 -0400955 ref = alloc_ref(rm->peer_ref->name);
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000956 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
957 oidcpy(&ref->new_oid, &rm->old_oid);
Joey Hess96890f42011-12-26 12:16:56 -0400958 ref->force = rm->peer_ref->force;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400959 }
Joey Hess96890f42011-12-26 12:16:56 -0400960
Stefan Bellerbe76c212018-12-06 13:26:55 -0800961 if (recurse_submodules != RECURSE_SUBMODULES_OFF)
962 check_for_new_submodule_commits(&rm->old_oid);
Joey Hess96890f42011-12-26 12:16:56 -0400963
964 if (!strcmp(rm->name, "HEAD")) {
965 kind = "";
966 what = "";
967 }
René Scharfea6293f52019-11-26 12:18:26 +0100968 else if (skip_prefix(rm->name, "refs/heads/", &what))
Joey Hess96890f42011-12-26 12:16:56 -0400969 kind = "branch";
René Scharfea6293f52019-11-26 12:18:26 +0100970 else if (skip_prefix(rm->name, "refs/tags/", &what))
Joey Hess96890f42011-12-26 12:16:56 -0400971 kind = "tag";
René Scharfea6293f52019-11-26 12:18:26 +0100972 else if (skip_prefix(rm->name, "refs/remotes/", &what))
Joey Hess96890f42011-12-26 12:16:56 -0400973 kind = "remote-tracking branch";
Joey Hess96890f42011-12-26 12:16:56 -0400974 else {
975 kind = "";
976 what = rm->name;
977 }
978
979 url_len = strlen(url);
980 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
981 ;
982 url_len = i + 1;
983 if (4 < i && !strncmp(".git", url + i - 3, 4))
984 url_len = i - 3;
985
986 strbuf_reset(&note);
987 if (*what) {
988 if (*kind)
989 strbuf_addf(&note, "%s ", kind);
990 strbuf_addf(&note, "'%s' of ", what);
991 }
Jeff King900f2812013-05-11 18:15:59 +0200992 switch (rm->fetch_head_status) {
993 case FETCH_HEAD_NOT_FOR_MERGE:
994 merge_status_marker = "not-for-merge";
995 /* fall-through */
996 case FETCH_HEAD_MERGE:
997 fprintf(fp, "%s\t%s\t%s",
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000998 oid_to_hex(&rm->old_oid),
Jeff King900f2812013-05-11 18:15:59 +0200999 merge_status_marker,
1000 note.buf);
1001 for (i = 0; i < url_len; ++i)
1002 if ('\n' == url[i])
1003 fputs("\\n", fp);
1004 else
1005 fputc(url[i], fp);
1006 fputc('\n', fp);
1007 break;
1008 default:
1009 /* do not write anything to FETCH_HEAD */
1010 break;
1011 }
Joey Hess96890f42011-12-26 12:16:56 -04001012
1013 strbuf_reset(&note);
1014 if (ref) {
Junio C Hamano901f3d42016-10-21 15:22:55 -07001015 rc |= update_local_ref(ref, what, rm, &note,
1016 summary_width);
Joey Hess96890f42011-12-26 12:16:56 -04001017 free(ref);
1018 } else
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +02001019 format_display(&note, '*',
1020 *kind ? kind : "branch", NULL,
1021 *what ? what : "HEAD",
Junio C Hamano901f3d42016-10-21 15:22:55 -07001022 "FETCH_HEAD", summary_width);
Joey Hess96890f42011-12-26 12:16:56 -04001023 if (note.len) {
1024 if (verbosity >= 0 && !shown_url) {
1025 fprintf(stderr, _("From %.*s\n"),
1026 url_len, url);
1027 shown_url = 1;
1028 }
1029 if (verbosity >= 0)
1030 fprintf(stderr, " %s\n", note.buf);
1031 }
Nicolas Pitre165f3902007-11-03 01:32:48 -04001032 }
Daniel Barkalowb888d612007-09-10 23:03:25 -04001033 }
Tay Ray Chuan9516a592011-10-07 15:40:22 +08001034
Jeff Kingfa250752009-05-25 06:40:54 -04001035 if (rc & STORE_REF_ERROR_DF_CONFLICT)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001036 error(_("some local refs could not be updated; try running\n"
Jeff Kingf3cb1692008-06-27 00:01:41 -04001037 " 'git remote prune %s' to remove any old, conflicting "
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001038 "branches"), remote_name);
Tay Ray Chuan9516a592011-10-07 15:40:22 +08001039
Derrick Stolee377444b2019-06-18 13:25:27 -07001040 if (advice_fetch_show_forced_updates) {
1041 if (!fetch_show_forced_updates) {
Jean-Noël Avila182f59d2019-08-06 19:19:52 +02001042 warning(_(warn_show_forced_updates));
Derrick Stolee377444b2019-06-18 13:25:27 -07001043 } else if (forced_updates_ms > FORCED_UPDATES_DELAY_WARNING_IN_MS) {
Jean-Noël Avila182f59d2019-08-06 19:19:52 +02001044 warning(_(warn_time_show_forced_updates),
Derrick Stolee377444b2019-06-18 13:25:27 -07001045 forced_updates_ms / 1000.0);
Derrick Stolee377444b2019-06-18 13:25:27 -07001046 }
1047 }
1048
Tay Ray Chuan9516a592011-10-07 15:40:22 +08001049 abort:
Jeff King5914f2d2011-12-08 03:43:19 -05001050 strbuf_release(&note);
Tay Ray Chuan9516a592011-10-07 15:40:22 +08001051 free(url);
1052 fclose(fp);
Dmitry V. Levinefb98b42008-05-28 19:29:36 +04001053 return rc;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001054}
1055
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001056/*
1057 * We would want to bypass the object transfer altogether if
Johan Herlandd9eb0202009-07-10 01:52:30 +02001058 * everything we are going to fetch already exists and is connected
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001059 * locally.
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001060 */
Jonathan Tan35f9e3e2018-09-21 11:22:38 -07001061static int check_exist_and_connected(struct ref *ref_map)
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001062{
Junio C Hamanof0e278b2011-09-02 16:22:47 -07001063 struct ref *rm = ref_map;
Jeff King7043c702016-07-15 06:30:40 -04001064 struct check_connected_options opt = CHECK_CONNECTED_INIT;
Jonathan Tan35f9e3e2018-09-21 11:22:38 -07001065 struct ref *r;
Junio C Hamanof0e278b2011-09-02 16:22:47 -07001066
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001067 /*
1068 * If we are deepening a shallow clone we already have these
1069 * objects reachable. Running rev-list here will return with
1070 * a good (0) exit status and we'll bypass the fetch that we
1071 * really need to perform. Claiming failure now will ensure
1072 * we perform the network exchange to deepen our history.
1073 */
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001074 if (deepen)
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001075 return -1;
Jonathan Tan35f9e3e2018-09-21 11:22:38 -07001076
1077 /*
1078 * check_connected() allows objects to merely be promised, but
1079 * we need all direct targets to exist.
1080 */
1081 for (r = rm; r; r = r->next) {
Jonathan Tan6462d5e2019-11-05 10:56:19 -08001082 if (!has_object_file_with_flags(&r->old_oid,
1083 OBJECT_INFO_SKIP_FETCH_OBJECT))
Jonathan Tan35f9e3e2018-09-21 11:22:38 -07001084 return -1;
1085 }
1086
Jeff King7043c702016-07-15 06:30:40 -04001087 opt.quiet = 1;
1088 return check_connected(iterate_ref_map, &rm, &opt);
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001089}
1090
Jonathan Tane2842b32018-08-01 13:13:20 -07001091static int fetch_refs(struct transport *transport, struct ref *ref_map)
Daniel Barkalowb888d612007-09-10 23:03:25 -04001092{
Jonathan Tan35f9e3e2018-09-21 11:22:38 -07001093 int ret = check_exist_and_connected(ref_map);
Josh Steadmon5fc31182019-10-02 16:49:28 -07001094 if (ret) {
1095 trace2_region_enter("fetch", "fetch_refs", the_repository);
Jonathan Tane2842b32018-08-01 13:13:20 -07001096 ret = transport_fetch_refs(transport, ref_map);
Josh Steadmon5fc31182019-10-02 16:49:28 -07001097 trace2_region_leave("fetch", "fetch_refs", the_repository);
1098 }
Daniel Barkalowb888d612007-09-10 23:03:25 -04001099 if (!ret)
Brandon Williams05c44222018-06-27 15:30:20 -07001100 /*
1101 * Keep the new pack's ".keep" file around to allow the caller
1102 * time to update refs to reference the new objects.
1103 */
1104 return 0;
1105 transport_unlock_pack(transport);
1106 return ret;
1107}
1108
1109/* Update local refs based on the ref values fetched from a remote */
1110static int consume_refs(struct transport *transport, struct ref *ref_map)
1111{
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001112 int connectivity_checked = transport->smart_options
1113 ? transport->smart_options->connectivity_checked : 0;
Josh Steadmon5fc31182019-10-02 16:49:28 -07001114 int ret;
1115 trace2_region_enter("fetch", "consume_refs", the_repository);
1116 ret = store_updated_refs(transport->url,
1117 transport->remote->name,
1118 connectivity_checked,
1119 ref_map);
Shawn O. Pearce1788c392007-09-14 03:31:23 -04001120 transport_unlock_pack(transport);
Josh Steadmon5fc31182019-10-02 16:49:28 -07001121 trace2_region_leave("fetch", "consume_refs", the_repository);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001122 return ret;
1123}
1124
Brandon Williamsdef11e72018-05-16 15:58:09 -07001125static int prune_refs(struct refspec *rs, struct ref *ref_map,
1126 const char *raw_url)
Jay Soffianf360d842009-11-10 09:15:47 +01001127{
Tom Miller4b3b33a2014-01-02 20:28:51 -06001128 int url_len, i, result = 0;
Brandon Williamsa2ac50c2018-05-16 15:58:10 -07001129 struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map);
Tom Miller4b3b33a2014-01-02 20:28:51 -06001130 char *url;
Junio C Hamano11fd66d2016-10-21 15:28:07 -07001131 int summary_width = transport_summary_width(stale_refs);
Jay Soffianf360d842009-11-10 09:15:47 +01001132 const char *dangling_msg = dry_run
Nguyễn Thái Ngọc Duy18986d52012-04-23 19:30:25 +07001133 ? _(" (%s will become dangling)")
1134 : _(" (%s has become dangling)");
Jay Soffianf360d842009-11-10 09:15:47 +01001135
Tom Miller4b3b33a2014-01-02 20:28:51 -06001136 if (raw_url)
1137 url = transport_anonymize_url(raw_url);
1138 else
1139 url = xstrdup("foreign");
1140
1141 url_len = strlen(url);
1142 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
1143 ;
1144
1145 url_len = i + 1;
1146 if (4 < i && !strncmp(".git", url + i - 3, 4))
1147 url_len = i - 3;
1148
Michael Haggertya087b432015-06-22 16:02:59 +02001149 if (!dry_run) {
1150 struct string_list refnames = STRING_LIST_INIT_NODUP;
1151
1152 for (ref = stale_refs; ref; ref = ref->next)
1153 string_list_append(&refnames, ref->name);
1154
Michael Haggerty64da4192017-05-22 16:17:38 +02001155 result = delete_refs("fetch: prune", &refnames, 0);
Michael Haggertya087b432015-06-22 16:02:59 +02001156 string_list_clear(&refnames, 0);
1157 }
1158
1159 if (verbosity >= 0) {
1160 for (ref = stale_refs; ref; ref = ref->next) {
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +02001161 struct strbuf sb = STRBUF_INIT;
Michael Haggertya087b432015-06-22 16:02:59 +02001162 if (!shown_url) {
1163 fprintf(stderr, _("From %.*s\n"), url_len, url);
1164 shown_url = 1;
1165 }
Nguyễn Thái Ngọc Duy2cb040b2016-06-26 07:58:08 +02001166 format_display(&sb, '-', _("[deleted]"), NULL,
Junio C Hamano901f3d42016-10-21 15:22:55 -07001167 _("(none)"), prettify_refname(ref->name),
1168 summary_width);
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +02001169 fprintf(stderr, " %s\n",sb.buf);
1170 strbuf_release(&sb);
Jay Soffianf360d842009-11-10 09:15:47 +01001171 warn_dangling_symref(stderr, dangling_msg, ref->name);
1172 }
1173 }
Michael Haggertya087b432015-06-22 16:02:59 +02001174
Tom Miller4b3b33a2014-01-02 20:28:51 -06001175 free(url);
Jay Soffianf360d842009-11-10 09:15:47 +01001176 free_refs(stale_refs);
1177 return result;
1178}
1179
Johannes Schindelin8ee5d732008-10-13 11:36:52 +02001180static void check_not_current_branch(struct ref *ref_map)
1181{
1182 struct branch *current_branch = branch_get(NULL);
1183
1184 if (is_bare_repository() || !current_branch)
1185 return;
1186
1187 for (; ref_map; ref_map = ref_map->next)
1188 if (ref_map->peer_ref && !strcmp(current_branch->refname,
1189 ref_map->peer_ref->name))
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001190 die(_("Refusing to fetch into current branch %s "
1191 "of non-bare repository"), current_branch->refname);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +02001192}
1193
Junio C Hamanoe6cc5102010-02-24 11:02:05 -08001194static int truncate_fetch_head(void)
1195{
Stefan Beller102de882018-05-17 15:51:51 -07001196 const char *filename = git_path_fetch_head(the_repository);
Johannes Schindelinea565182016-01-11 19:35:54 +01001197 FILE *fp = fopen_for_writing(filename);
Junio C Hamanoe6cc5102010-02-24 11:02:05 -08001198
1199 if (!fp)
Nguyễn Thái Ngọc Duy6da31d72016-05-08 16:47:26 +07001200 return error_errno(_("cannot open %s"), filename);
Junio C Hamanoe6cc5102010-02-24 11:02:05 -08001201 fclose(fp);
1202 return 0;
1203}
1204
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001205static void set_option(struct transport *transport, const char *name, const char *value)
1206{
1207 int r = transport_set_option(transport, name, value);
1208 if (r < 0)
1209 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1210 name, value, transport->url);
1211 if (r > 0)
1212 warning(_("Option \"%s\" is ignored for %s\n"),
1213 name, transport->url);
1214}
1215
Jonathan Tan3390e422018-07-02 15:39:44 -07001216
1217static int add_oid(const char *refname, const struct object_id *oid, int flags,
1218 void *cb_data)
1219{
1220 struct oid_array *oids = cb_data;
1221
1222 oid_array_append(oids, oid);
1223 return 0;
1224}
1225
1226static void add_negotiation_tips(struct git_transport_options *smart_options)
1227{
1228 struct oid_array *oids = xcalloc(1, sizeof(*oids));
1229 int i;
1230
1231 for (i = 0; i < negotiation_tip.nr; i++) {
1232 const char *s = negotiation_tip.items[i].string;
1233 int old_nr;
1234 if (!has_glob_specials(s)) {
1235 struct object_id oid;
1236 if (get_oid(s, &oid))
1237 die("%s is not a valid object", s);
1238 oid_array_append(oids, &oid);
1239 continue;
1240 }
1241 old_nr = oids->nr;
1242 for_each_glob_ref(add_oid, s, oids);
1243 if (old_nr == oids->nr)
1244 warning("Ignoring --negotiation-tip=%s because it does not match any refs",
1245 s);
1246 }
1247 smart_options->negotiation_tips = oids;
1248}
1249
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001250static struct transport *prepare_transport(struct remote *remote, int deepen)
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001251{
1252 struct transport *transport;
Josh Steadmon87c2d9d2019-01-07 16:17:09 -08001253
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001254 transport = transport_get(remote, NULL);
1255 transport_set_verbosity(transport, verbosity, progress);
Eric Wongc915f112016-02-03 04:09:14 +00001256 transport->family = family;
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001257 if (upload_pack)
1258 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
1259 if (keep)
1260 set_option(transport, TRANS_OPT_KEEP, "yes");
1261 if (depth)
1262 set_option(transport, TRANS_OPT_DEPTH, depth);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001263 if (deepen && deepen_since)
1264 set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since);
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001265 if (deepen && deepen_not.nr)
1266 set_option(transport, TRANS_OPT_DEEPEN_NOT,
1267 (const char *)&deepen_not);
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001268 if (deepen_relative)
1269 set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes");
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001270 if (update_shallow)
1271 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001272 if (filter_options.choice) {
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001273 const char *spec =
1274 expand_list_objects_filter_spec(&filter_options);
1275 set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER, spec);
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001276 set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
1277 }
Jonathan Tan3390e422018-07-02 15:39:44 -07001278 if (negotiation_tip.nr) {
1279 if (transport->smart_options)
1280 add_negotiation_tips(transport->smart_options);
1281 else
1282 warning("Ignoring --negotiation-tip because the protocol does not support it.");
1283 }
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001284 return transport;
1285}
1286
Junio C Hamano069d5032013-08-07 15:14:45 -07001287static void backfill_tags(struct transport *transport, struct ref *ref_map)
1288{
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001289 int cannot_reuse;
1290
1291 /*
1292 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1293 * when remote helper is used (setting it to an empty string
1294 * is not unsetting). We could extend the remote helper
1295 * protocol for that, but for now, just force a new connection
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001296 * without deepen-since. Similar story for deepen-not.
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001297 */
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001298 cannot_reuse = transport->cannot_reuse ||
1299 deepen_since || deepen_not.nr;
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001300 if (cannot_reuse) {
1301 gsecondary = prepare_transport(transport->remote, 0);
Junio C Hamanob26ed432013-08-07 15:47:18 -07001302 transport = gsecondary;
1303 }
1304
Junio C Hamano069d5032013-08-07 15:14:45 -07001305 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
1306 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001307 transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
Jonathan Tane2842b32018-08-01 13:13:20 -07001308 if (!fetch_refs(transport, ref_map))
Brandon Williams05c44222018-06-27 15:30:20 -07001309 consume_refs(transport, ref_map);
Junio C Hamanob26ed432013-08-07 15:47:18 -07001310
1311 if (gsecondary) {
1312 transport_disconnect(gsecondary);
1313 gsecondary = NULL;
1314 }
Junio C Hamano069d5032013-08-07 15:14:45 -07001315}
1316
Daniel Barkalowb888d612007-09-10 23:03:25 -04001317static int do_fetch(struct transport *transport,
Brandon Williams65a13012018-05-16 15:58:07 -07001318 struct refspec *rs)
Daniel Barkalowb888d612007-09-10 23:03:25 -04001319{
Shawn O. Pearce7f984282008-03-02 21:34:43 -05001320 struct ref *ref_map;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001321 int autotags = (transport->remote->fetch_tags == 1);
Michael Haggerty5b87d8d2013-05-25 11:08:16 +02001322 int retcode = 0;
Brandon Williams6d1700d2018-06-27 15:30:21 -07001323 const struct ref *remote_refs;
Jeff King22f9b7f2020-07-28 16:24:27 -04001324 struct strvec ref_prefixes = STRVEC_INIT;
Jonathan Tane70a3032018-09-27 12:24:07 -07001325 int must_list_refs = 1;
Julian Phillipsb1a01e12009-10-25 21:28:12 +00001326
Daniel Johnsoned368542010-08-11 18:57:20 -04001327 if (tags == TAGS_DEFAULT) {
1328 if (transport->remote->fetch_tags == 2)
1329 tags = TAGS_SET;
1330 if (transport->remote->fetch_tags == -1)
1331 tags = TAGS_UNSET;
1332 }
Daniel Barkalowb888d612007-09-10 23:03:25 -04001333
Daniel Barkalowb888d612007-09-10 23:03:25 -04001334 /* if not appending, truncate FETCH_HEAD */
Junio C Hamano887952b2020-08-18 14:25:22 +00001335 if (!append && write_fetch_head) {
Michael Haggerty5b87d8d2013-05-25 11:08:16 +02001336 retcode = truncate_fetch_head();
1337 if (retcode)
1338 goto cleanup;
André Goddard Rosad6617c72007-11-22 20:22:23 -02001339 }
Daniel Barkalowb888d612007-09-10 23:03:25 -04001340
Jonathan Tane70a3032018-09-27 12:24:07 -07001341 if (rs->nr) {
1342 int i;
1343
Brandon Williams6d1700d2018-06-27 15:30:21 -07001344 refspec_ref_prefixes(rs, &ref_prefixes);
Jonathan Tane70a3032018-09-27 12:24:07 -07001345
1346 /*
1347 * We can avoid listing refs if all of them are exact
1348 * OIDs
1349 */
1350 must_list_refs = 0;
1351 for (i = 0; i < rs->nr; i++) {
1352 if (!rs->items[i].exact_sha1) {
1353 must_list_refs = 1;
1354 break;
1355 }
1356 }
1357 } else if (transport->remote && transport->remote->fetch.nr)
Brandon Williams6d1700d2018-06-27 15:30:21 -07001358 refspec_ref_prefixes(&transport->remote->fetch, &ref_prefixes);
1359
Jonathan Tane70a3032018-09-27 12:24:07 -07001360 if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
1361 must_list_refs = 1;
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001362 if (ref_prefixes.nr)
Jeff King22f9b7f2020-07-28 16:24:27 -04001363 strvec_push(&ref_prefixes, "refs/tags/");
Brandon Williams6d1700d2018-06-27 15:30:21 -07001364 }
1365
Josh Steadmon5fc31182019-10-02 16:49:28 -07001366 if (must_list_refs) {
1367 trace2_region_enter("fetch", "remote_refs", the_repository);
Jonathan Tane70a3032018-09-27 12:24:07 -07001368 remote_refs = transport_get_remote_refs(transport, &ref_prefixes);
Josh Steadmon5fc31182019-10-02 16:49:28 -07001369 trace2_region_leave("fetch", "remote_refs", the_repository);
1370 } else
Jonathan Tane70a3032018-09-27 12:24:07 -07001371 remote_refs = NULL;
1372
Jeff King22f9b7f2020-07-28 16:24:27 -04001373 strvec_clear(&ref_prefixes);
Brandon Williams6d1700d2018-06-27 15:30:21 -07001374
1375 ref_map = get_ref_map(transport->remote, remote_refs, rs,
1376 tags, &autotags);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +02001377 if (!update_head_ok)
1378 check_not_current_branch(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001379
Shawn O. Pearce41fa7d22008-03-03 22:27:40 -05001380 if (tags == TAGS_DEFAULT && autotags)
1381 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02001382 if (prune) {
Michael Haggerty0838bf42013-10-30 06:33:00 +01001383 /*
1384 * We only prune based on refspecs specified
1385 * explicitly (via command line or configuration); we
1386 * don't care whether --tags was specified.
1387 */
Brandon Williams65a13012018-05-16 15:58:07 -07001388 if (rs->nr) {
Brandon Williamsdef11e72018-05-16 15:58:09 -07001389 prune_refs(rs, ref_map, transport->url);
Michael Haggertyc5a84e92013-10-30 06:32:59 +01001390 } else {
Brandon Williamsdef11e72018-05-16 15:58:09 -07001391 prune_refs(&transport->remote->fetch,
Tom Miller4b3b33a2014-01-02 20:28:51 -06001392 ref_map,
1393 transport->url);
Carlos Martín Nietoe8c1e6c2011-10-15 07:04:26 +02001394 }
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02001395 }
Jonathan Tane2842b32018-08-01 13:13:20 -07001396 if (fetch_refs(transport, ref_map) || consume_refs(transport, ref_map)) {
Tom Miller10a6cc82014-01-02 20:28:52 -06001397 free_refs(ref_map);
1398 retcode = 1;
1399 goto cleanup;
1400 }
Corentin BOMPARD24bc1a12019-08-19 11:11:20 +02001401
1402 if (set_upstream) {
1403 struct branch *branch = branch_get("HEAD");
1404 struct ref *rm;
1405 struct ref *source_ref = NULL;
1406
1407 /*
1408 * We're setting the upstream configuration for the
Elijah Newren15beaaa2019-11-05 17:07:23 +00001409 * current branch. The relevant upstream is the
Corentin BOMPARD24bc1a12019-08-19 11:11:20 +02001410 * fetched branch that is meant to be merged with the
1411 * current one, i.e. the one fetched to FETCH_HEAD.
1412 *
1413 * When there are several such branches, consider the
1414 * request ambiguous and err on the safe side by doing
1415 * nothing and just emit a warning.
1416 */
1417 for (rm = ref_map; rm; rm = rm->next) {
1418 if (!rm->peer_ref) {
1419 if (source_ref) {
Ralf Thielow391c7e42019-10-31 20:41:46 +00001420 warning(_("multiple branches detected, incompatible with --set-upstream"));
Corentin BOMPARD24bc1a12019-08-19 11:11:20 +02001421 goto skip;
1422 } else {
1423 source_ref = rm;
1424 }
1425 }
1426 }
1427 if (source_ref) {
1428 if (!strcmp(source_ref->name, "HEAD") ||
1429 starts_with(source_ref->name, "refs/heads/"))
1430 install_branch_config(0,
1431 branch->name,
1432 transport->remote->name,
1433 source_ref->name);
1434 else if (starts_with(source_ref->name, "refs/remotes/"))
1435 warning(_("not setting upstream for a remote remote-tracking branch"));
1436 else if (starts_with(source_ref->name, "refs/tags/"))
1437 warning(_("not setting upstream for a remote tag"));
1438 else
1439 warning(_("unknown branch type"));
1440 } else {
1441 warning(_("no source branch found.\n"
1442 "you need to specify exactly one branch with the --set-upstream option."));
1443 }
1444 }
1445 skip:
Shawn O. Pearce7f984282008-03-02 21:34:43 -05001446 free_refs(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001447
1448 /* if neither --no-tags nor --tags was specified, do automated tag
1449 * following ... */
Kristian Høgsberg83201992007-12-04 02:25:47 -05001450 if (tags == TAGS_DEFAULT && autotags) {
Shawn O. Pearcec50b2b42008-03-02 21:35:00 -05001451 struct ref **tail = &ref_map;
1452 ref_map = NULL;
Brandon Williams6d1700d2018-06-27 15:30:21 -07001453 find_non_local_tags(remote_refs, &ref_map, &tail);
Junio C Hamano069d5032013-08-07 15:14:45 -07001454 if (ref_map)
1455 backfill_tags(transport, ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001456 free_refs(ref_map);
1457 }
1458
Michael Haggerty5b87d8d2013-05-25 11:08:16 +02001459 cleanup:
Michael Haggerty5b87d8d2013-05-25 11:08:16 +02001460 return retcode;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001461}
1462
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001463static int get_one_remote_for_fetch(struct remote *remote, void *priv)
Daniel Barkalowb888d612007-09-10 23:03:25 -04001464{
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001465 struct string_list *list = priv;
Björn Gustavsson7cc91a22009-11-09 21:11:06 +01001466 if (!remote->skip_default_update)
Julian Phillips1d2f80f2010-06-26 00:41:38 +01001467 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001468 return 0;
1469}
1470
1471struct remote_group_data {
1472 const char *name;
1473 struct string_list *list;
1474};
1475
1476static int get_remote_group(const char *key, const char *value, void *priv)
1477{
1478 struct remote_group_data *g = priv;
1479
Michael Haggertybc598c32015-07-28 23:08:21 +02001480 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001481 /* split list by white space */
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001482 while (*value) {
Michael Haggerty5f654992015-07-28 23:08:20 +02001483 size_t wordlen = strcspn(value, " \t\n");
1484
Michael Haggertye2865422015-07-28 23:08:19 +02001485 if (wordlen >= 1)
Keith McGuiganb7410f62016-06-14 14:28:56 -04001486 string_list_append_nodup(g->list,
Michael Haggertye2865422015-07-28 23:08:19 +02001487 xstrndup(value, wordlen));
1488 value += wordlen + (value[wordlen] != '\0');
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001489 }
1490 }
1491
1492 return 0;
1493}
1494
1495static int add_remote_or_group(const char *name, struct string_list *list)
1496{
1497 int prev_nr = list->nr;
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +00001498 struct remote_group_data g;
1499 g.name = name; g.list = list;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001500
1501 git_config(get_remote_group, &g);
1502 if (list->nr == prev_nr) {
Thomas Gummerer674468b2016-02-16 10:47:50 +01001503 struct remote *remote = remote_get(name);
Johannes Schindeline459b072017-01-19 22:20:02 +01001504 if (!remote_is_configured(remote, 0))
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001505 return 0;
Julian Phillips1d2f80f2010-06-26 00:41:38 +01001506 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001507 }
1508 return 1;
1509}
1510
Jeff King22f9b7f2020-07-28 16:24:27 -04001511static void add_options_to_argv(struct strvec *argv)
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001512{
1513 if (dry_run)
Jeff King22f9b7f2020-07-28 16:24:27 -04001514 strvec_push(argv, "--dry-run");
Michael Haggerty90765fa2013-10-30 06:33:04 +01001515 if (prune != -1)
Jeff King22f9b7f2020-07-28 16:24:27 -04001516 strvec_push(argv, prune ? "--prune" : "--no-prune");
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +00001517 if (prune_tags != -1)
Jeff King22f9b7f2020-07-28 16:24:27 -04001518 strvec_push(argv, prune_tags ? "--prune-tags" : "--no-prune-tags");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001519 if (update_head_ok)
Jeff King22f9b7f2020-07-28 16:24:27 -04001520 strvec_push(argv, "--update-head-ok");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001521 if (force)
Jeff King22f9b7f2020-07-28 16:24:27 -04001522 strvec_push(argv, "--force");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001523 if (keep)
Jeff King22f9b7f2020-07-28 16:24:27 -04001524 strvec_push(argv, "--keep");
Jens Lehmannbe254a02010-11-11 00:55:02 +01001525 if (recurse_submodules == RECURSE_SUBMODULES_ON)
Jeff King22f9b7f2020-07-28 16:24:27 -04001526 strvec_push(argv, "--recurse-submodules");
Jens Lehmann8f0700d2011-03-06 23:11:21 +01001527 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
Jeff King22f9b7f2020-07-28 16:24:27 -04001528 strvec_push(argv, "--recurse-submodules=on-demand");
Dan Johnson85566462012-09-05 17:22:19 -04001529 if (tags == TAGS_SET)
Jeff King22f9b7f2020-07-28 16:24:27 -04001530 strvec_push(argv, "--tags");
Dan Johnson85566462012-09-05 17:22:19 -04001531 else if (tags == TAGS_UNSET)
Jeff King22f9b7f2020-07-28 16:24:27 -04001532 strvec_push(argv, "--no-tags");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001533 if (verbosity >= 2)
Jeff King22f9b7f2020-07-28 16:24:27 -04001534 strvec_push(argv, "-v");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001535 if (verbosity >= 1)
Jeff King22f9b7f2020-07-28 16:24:27 -04001536 strvec_push(argv, "-v");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001537 else if (verbosity < 0)
Jeff King22f9b7f2020-07-28 16:24:27 -04001538 strvec_push(argv, "-q");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001539
1540}
1541
Johannes Schindelind54dea72019-10-05 11:46:40 -07001542/* Fetch multiple remotes in parallel */
1543
1544struct parallel_fetch_state {
1545 const char **argv;
1546 struct string_list *remotes;
1547 int next, result;
1548};
1549
1550static int fetch_next_remote(struct child_process *cp, struct strbuf *out,
1551 void *cb, void **task_cb)
1552{
1553 struct parallel_fetch_state *state = cb;
1554 char *remote;
1555
1556 if (state->next < 0 || state->next >= state->remotes->nr)
1557 return 0;
1558
1559 remote = state->remotes->items[state->next++].string;
1560 *task_cb = remote;
1561
Jeff King22f9b7f2020-07-28 16:24:27 -04001562 strvec_pushv(&cp->args, state->argv);
1563 strvec_push(&cp->args, remote);
Johannes Schindelind54dea72019-10-05 11:46:40 -07001564 cp->git_cmd = 1;
1565
1566 if (verbosity >= 0)
1567 printf(_("Fetching %s\n"), remote);
1568
1569 return 1;
1570}
1571
1572static int fetch_failed_to_start(struct strbuf *out, void *cb, void *task_cb)
1573{
1574 struct parallel_fetch_state *state = cb;
1575 const char *remote = task_cb;
1576
1577 state->result = error(_("Could not fetch %s"), remote);
1578
1579 return 0;
1580}
1581
1582static int fetch_finished(int result, struct strbuf *out,
1583 void *cb, void *task_cb)
1584{
1585 struct parallel_fetch_state *state = cb;
1586 const char *remote = task_cb;
1587
1588 if (result) {
1589 strbuf_addf(out, _("could not fetch '%s' (exit code: %d)\n"),
1590 remote, result);
1591 state->result = -1;
1592 }
1593
1594 return 0;
1595}
1596
1597static int fetch_multiple(struct string_list *list, int max_children)
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001598{
1599 int i, result = 0;
Jeff King22f9b7f2020-07-28 16:24:27 -04001600 struct strvec argv = STRVEC_INIT;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001601
Junio C Hamano887952b2020-08-18 14:25:22 +00001602 if (!append && write_fetch_head) {
Junio C Hamanoe6cc5102010-02-24 11:02:05 -08001603 int errcode = truncate_fetch_head();
1604 if (errcode)
1605 return errcode;
1606 }
1607
Jeff King22f9b7f2020-07-28 16:24:27 -04001608 strvec_pushl(&argv, "fetch", "--append", "--no-auto-gc",
Jeff Kingf6d89422020-07-28 16:26:31 -04001609 "--no-write-commit-graph", NULL);
Jeff King85556d42012-09-01 07:27:35 -04001610 add_options_to_argv(&argv);
1611
Johannes Schindelind54dea72019-10-05 11:46:40 -07001612 if (max_children != 1 && list->nr != 1) {
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001613 struct parallel_fetch_state state = { argv.v, list, 0, 0 };
Johannes Schindelind54dea72019-10-05 11:46:40 -07001614
Jeff King22f9b7f2020-07-28 16:24:27 -04001615 strvec_push(&argv, "--end-of-options");
Johannes Schindelind54dea72019-10-05 11:46:40 -07001616 result = run_processes_parallel_tr2(max_children,
1617 &fetch_next_remote,
1618 &fetch_failed_to_start,
1619 &fetch_finished,
1620 &state,
1621 "fetch", "parallel/fetch");
1622
1623 if (!result)
1624 result = state.result;
1625 } else
1626 for (i = 0; i < list->nr; i++) {
1627 const char *name = list->items[i].string;
Jeff King22f9b7f2020-07-28 16:24:27 -04001628 strvec_push(&argv, name);
Johannes Schindelind54dea72019-10-05 11:46:40 -07001629 if (verbosity >= 0)
1630 printf(_("Fetching %s\n"), name);
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001631 if (run_command_v_opt(argv.v, RUN_GIT_CMD)) {
Johannes Schindelind54dea72019-10-05 11:46:40 -07001632 error(_("Could not fetch %s"), name);
1633 result = 1;
1634 }
Jeff King22f9b7f2020-07-28 16:24:27 -04001635 strvec_pop(&argv);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001636 }
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001637
Jeff King22f9b7f2020-07-28 16:24:27 -04001638 strvec_clear(&argv);
Johannes Schindelind54dea72019-10-05 11:46:40 -07001639 return !!result;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001640}
1641
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001642/*
1643 * Fetching from the promisor remote should use the given filter-spec
1644 * or inherit the default filter-spec from the config.
1645 */
1646static inline void fetch_one_setup_partial(struct remote *remote)
1647{
1648 /*
1649 * Explicit --no-filter argument overrides everything, regardless
1650 * of any prior partial clones and fetches.
1651 */
1652 if (filter_options.no_filter)
1653 return;
1654
1655 /*
1656 * If no prior partial clone/fetch and the current fetch DID NOT
1657 * request a partial-fetch, do a normal fetch.
1658 */
Christian Couderb14ed5a2019-06-25 15:40:31 +02001659 if (!has_promisor_remote() && !filter_options.choice)
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001660 return;
1661
1662 /*
Christian Couder5e461392019-06-25 15:40:33 +02001663 * If this is a partial-fetch request, we enable partial on
1664 * this repo if not already enabled and remember the given
1665 * filter-spec as the default for subsequent fetches to this
1666 * remote.
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001667 */
Christian Couder5e461392019-06-25 15:40:33 +02001668 if (filter_options.choice) {
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001669 partial_clone_register(remote->name, &filter_options);
1670 return;
1671 }
1672
1673 /*
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001674 * Do a partial-fetch from the promisor remote using either the
1675 * explicitly given filter-spec or inherit the filter-spec from
1676 * the config.
1677 */
1678 if (!filter_options.choice)
Christian Couderfa3d1b62019-06-25 15:40:32 +02001679 partial_clone_get_default_filter_spec(&filter_options, remote->name);
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001680 return;
1681}
1682
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +00001683static int fetch_one(struct remote *remote, int argc, const char **argv, int prune_tags_ok)
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001684{
Brandon Williamsd7c8e302018-05-16 15:58:04 -07001685 struct refspec rs = REFSPEC_INIT_FETCH;
1686 int i;
Alex Riesen7b7f39e2008-04-28 22:23:35 +02001687 int exit_code;
Ævar Arnfjörð Bjarmason63179722018-02-09 20:32:16 +00001688 int maybe_prune_tags;
1689 int remote_via_config = remote_is_configured(remote, 0);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001690
Daniel Barkalowfa685bd2009-03-11 01:47:20 -04001691 if (!remote)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001692 die(_("No remote repository specified. Please, specify either a URL or a\n"
1693 "remote name from which new revisions should be fetched."));
Daniel Barkalowfa685bd2009-03-11 01:47:20 -04001694
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001695 gtransport = prepare_transport(remote, 1);
Michael Schubert737c5a92013-07-13 11:36:24 +02001696
1697 if (prune < 0) {
1698 /* no command line request */
Ævar Arnfjörð Bjarmason07118832018-02-09 20:32:02 +00001699 if (0 <= remote->prune)
1700 prune = remote->prune;
Michael Schubert737c5a92013-07-13 11:36:24 +02001701 else if (0 <= fetch_prune_config)
1702 prune = fetch_prune_config;
1703 else
1704 prune = PRUNE_BY_DEFAULT;
1705 }
1706
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +00001707 if (prune_tags < 0) {
1708 /* no command line request */
1709 if (0 <= remote->prune_tags)
1710 prune_tags = remote->prune_tags;
1711 else if (0 <= fetch_prune_tags_config)
1712 prune_tags = fetch_prune_tags_config;
1713 else
1714 prune_tags = PRUNE_TAGS_BY_DEFAULT;
1715 }
1716
Ævar Arnfjörð Bjarmason63179722018-02-09 20:32:16 +00001717 maybe_prune_tags = prune_tags_ok && prune_tags;
1718 if (maybe_prune_tags && remote_via_config)
Brandon Williams95303502018-05-16 15:58:02 -07001719 refspec_append(&remote->fetch, TAG_REFSPEC);
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +00001720
Brandon Williamsd7c8e302018-05-16 15:58:04 -07001721 if (maybe_prune_tags && (argc || !remote_via_config))
1722 refspec_append(&rs, TAG_REFSPEC);
Ævar Arnfjörð Bjarmason63179722018-02-09 20:32:16 +00001723
Brandon Williamsd7c8e302018-05-16 15:58:04 -07001724 for (i = 0; i < argc; i++) {
1725 if (!strcmp(argv[i], "tag")) {
1726 char *tag;
1727 i++;
1728 if (i >= argc)
1729 die(_("You need to specify a tag name."));
1730
1731 tag = xstrfmt("refs/tags/%s:refs/tags/%s",
1732 argv[i], argv[i]);
1733 refspec_append(&rs, tag);
1734 free(tag);
1735 } else {
1736 refspec_append(&rs, argv[i]);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001737 }
Daniel Barkalowb888d612007-09-10 23:03:25 -04001738 }
1739
Brandon Williams5e3548e2018-04-23 15:46:24 -07001740 if (server_options.nr)
1741 gtransport->server_options = &server_options;
1742
Jeff King57b235a2009-01-22 01:03:08 -05001743 sigchain_push_common(unlock_pack_on_signal);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -04001744 atexit(unlock_pack);
Jeff King14358892019-03-03 11:58:43 -05001745 sigchain_push(SIGPIPE, SIG_IGN);
Brandon Williams65a13012018-05-16 15:58:07 -07001746 exit_code = do_fetch(gtransport, &rs);
Jeff King14358892019-03-03 11:58:43 -05001747 sigchain_pop(SIGPIPE);
Brandon Williamsd7c8e302018-05-16 15:58:04 -07001748 refspec_clear(&rs);
Junio C Hamanoaf234452013-08-07 15:38:45 -07001749 transport_disconnect(gtransport);
1750 gtransport = NULL;
Alex Riesen7b7f39e2008-04-28 22:23:35 +02001751 return exit_code;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001752}
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001753
1754int cmd_fetch(int argc, const char **argv, const char *prefix)
1755{
1756 int i;
Keith McGuiganb7410f62016-06-14 14:28:56 -04001757 struct string_list list = STRING_LIST_INIT_DUP;
Jonathan Tana1743342017-12-08 15:58:43 +00001758 struct remote *remote = NULL;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001759 int result = 0;
Junio C Hamanoc1a79022018-03-06 14:54:01 -08001760 int prune_tags_ok = 1;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001761
Jeff Kingbbc30f92011-02-24 09:30:19 -05001762 packet_trace_identity("fetch");
1763
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001764 /* Record the command line for the reflog */
1765 strbuf_addstr(&default_rla, "fetch");
Johannes Schindelin46da2952020-06-04 20:08:29 +00001766 for (i = 1; i < argc; i++) {
1767 /* This handles non-URLs gracefully */
1768 char *anon = transport_anonymize_url(argv[i]);
1769
1770 strbuf_addf(&default_rla, " %s", anon);
1771 free(anon);
1772 }
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001773
Johannes Schindelind54dea72019-10-05 11:46:40 -07001774 fetch_config_from_gitmodules(&submodule_fetch_jobs_config,
1775 &recurse_submodules);
Michael Schubert737c5a92013-07-13 11:36:24 +02001776 git_config(git_fetch_config, NULL);
1777
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001778 argc = parse_options(argc, argv, prefix,
1779 builtin_fetch_options, builtin_fetch_usage, 0);
1780
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001781 if (deepen_relative) {
1782 if (deepen_relative < 0)
1783 die(_("Negative depth in --deepen is not supported"));
1784 if (depth)
1785 die(_("--deepen and --depth are mutually exclusive"));
1786 depth = xstrfmt("%d", deepen_relative);
1787 }
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +07001788 if (unshallow) {
1789 if (depth)
1790 die(_("--depth and --unshallow cannot be used together"));
Stefan Bellerc8813482018-05-17 15:51:46 -07001791 else if (!is_repository_shallow(the_repository))
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +07001792 die(_("--unshallow on a complete repository does not make sense"));
Jeff King2805bb52015-09-24 17:07:07 -04001793 else
1794 depth = xstrfmt("%d", INFINITE_DEPTH);
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +07001795 }
1796
Nguyễn Thái Ngọc Duy5594bca2013-12-05 10:31:11 +07001797 /* no need to be strict, transport_set_option() will validate it again */
1798 if (depth && atoi(depth) < 1)
1799 die(_("depth %s is not a positive number"), depth);
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001800 if (depth || deepen_since || deepen_not.nr)
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001801 deepen = 1;
Nguyễn Thái Ngọc Duy5594bca2013-12-05 10:31:11 +07001802
Junio C Hamano887952b2020-08-18 14:25:22 +00001803 /* FETCH_HEAD never gets updated in --dry-run mode */
1804 if (dry_run)
1805 write_fetch_head = 0;
1806
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001807 if (all) {
1808 if (argc == 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001809 die(_("fetch --all does not take a repository argument"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001810 else if (argc > 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001811 die(_("fetch --all does not make sense with refspecs"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001812 (void) for_each_remote(get_one_remote_for_fetch, &list);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001813 } else if (argc == 0) {
1814 /* No arguments -- use default remote */
1815 remote = remote_get(NULL);
Björn Gustavsson16679e32009-11-09 21:10:32 +01001816 } else if (multiple) {
1817 /* All arguments are assumed to be remotes or groups */
1818 for (i = 0; i < argc; i++)
1819 if (!add_remote_or_group(argv[i], &list))
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001820 die(_("No such remote or remote group: %s"), argv[i]);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001821 } else {
1822 /* Single remote or group */
1823 (void) add_remote_or_group(argv[0], &list);
1824 if (list.nr > 1) {
1825 /* More than one remote */
1826 if (argc > 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001827 die(_("Fetching a group and specifying refspecs does not make sense"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001828 } else {
1829 /* Zero or one remotes */
1830 remote = remote_get(argv[0]);
Junio C Hamanoc1a79022018-03-06 14:54:01 -08001831 prune_tags_ok = (argc == 1);
Jonathan Tana1743342017-12-08 15:58:43 +00001832 argc--;
1833 argv++;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001834 }
1835 }
1836
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001837 if (remote) {
Christian Couderb14ed5a2019-06-25 15:40:31 +02001838 if (filter_options.choice || has_promisor_remote())
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001839 fetch_one_setup_partial(remote);
Junio C Hamanoc1a79022018-03-06 14:54:01 -08001840 result = fetch_one(remote, argc, argv, prune_tags_ok);
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001841 } else {
Johannes Schindelind54dea72019-10-05 11:46:40 -07001842 int max_children = max_jobs;
1843
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001844 if (filter_options.choice)
Christian Coudere0137872019-01-13 09:52:19 +01001845 die(_("--filter can only be used with the remote "
1846 "configured in extensions.partialclone"));
Johannes Schindelind54dea72019-10-05 11:46:40 -07001847
1848 if (max_children < 0)
1849 max_children = fetch_parallel_config;
1850
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001851 /* TODO should this also die if we have a previous partial-clone? */
Johannes Schindelind54dea72019-10-05 11:46:40 -07001852 result = fetch_multiple(&list, max_children);
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001853 }
Jonathan Tana1743342017-12-08 15:58:43 +00001854
Jens Lehmannbe254a02010-11-11 00:55:02 +01001855 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
Jeff King22f9b7f2020-07-28 16:24:27 -04001856 struct strvec options = STRVEC_INIT;
Johannes Schindelind54dea72019-10-05 11:46:40 -07001857 int max_children = max_jobs;
1858
1859 if (max_children < 0)
1860 max_children = submodule_fetch_jobs_config;
1861 if (max_children < 0)
1862 max_children = fetch_parallel_config;
Jeff King85556d42012-09-01 07:27:35 -04001863
1864 add_options_to_argv(&options);
Brandon Williamse7241972017-12-12 11:53:52 -08001865 result = fetch_populated_submodules(the_repository,
1866 &options,
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001867 submodule_prefix,
Jens Lehmann8f0700d2011-03-06 23:11:21 +01001868 recurse_submodules,
Brandon Williams8fa29152017-08-02 12:49:19 -07001869 recurse_submodules_default,
Stefan Beller62104ba2015-12-15 16:04:12 -08001870 verbosity < 0,
1871 max_children);
Jeff King22f9b7f2020-07-28 16:24:27 -04001872 strvec_clear(&options);
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001873 }
1874
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001875 string_list_clear(&list, 0);
1876
Derrick Stolee50f26bd2019-09-02 19:22:02 -07001877 prepare_repo_settings(the_repository);
Johannes Schindelinc14e6e72019-11-03 00:21:56 +00001878 if (fetch_write_commit_graph > 0 ||
1879 (fetch_write_commit_graph < 0 &&
1880 the_repository->settings.fetch_write_commit_graph)) {
Junio C Hamano5a535092019-09-30 13:19:32 +09001881 int commit_graph_flags = COMMIT_GRAPH_WRITE_SPLIT;
Derrick Stolee50f26bd2019-09-02 19:22:02 -07001882
1883 if (progress)
Junio C Hamano5a535092019-09-30 13:19:32 +09001884 commit_graph_flags |= COMMIT_GRAPH_WRITE_PROGRESS;
Derrick Stolee50f26bd2019-09-02 19:22:02 -07001885
Taylor Blau0bd52e22020-02-03 21:51:50 -08001886 write_commit_graph_reachable(the_repository->objects->odb,
Derrick Stolee50f26bd2019-09-02 19:22:02 -07001887 commit_graph_flags,
Derrick Stolee63020f12020-01-02 16:14:14 +00001888 NULL);
Derrick Stolee50f26bd2019-09-02 19:22:02 -07001889 }
1890
Derrick Stolee2d511cf2019-05-17 11:41:49 -07001891 close_object_store(the_repository->objects);
Johannes Schindelin0898c962016-01-13 18:20:11 +01001892
Junio C Hamano850b6ed2020-05-06 13:18:29 -07001893 if (enable_auto_gc)
1894 run_auto_gc(verbosity < 0);
Jeff King131b8fc2013-01-26 17:40:38 -05001895
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001896 return result;
1897}