blob: 67c0eb88c655ab949845b626367b3f2192e9ccb6 [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"
Daniel Barkalowb888d612007-09-10 23:03:25 -040010#include "commit.h"
11#include "builtin.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +010012#include "string-list.h"
Daniel Barkalowb888d612007-09-10 23:03:25 -040013#include "remote.h"
14#include "transport.h"
Shawn O. Pearce4191c352007-11-11 02:29:47 -050015#include "run-command.h"
Kristian Høgsberg83201992007-12-04 02:25:47 -050016#include "parse-options.h"
Jeff King4a16d072009-01-22 01:02:35 -050017#include "sigchain.h"
Heiko Voigt027771f2015-08-17 17:22:00 -070018#include "submodule-config.h"
Jens Lehmann7dce19d2010-11-12 13:54:52 +010019#include "submodule.h"
Junio C Hamanof96400c2011-09-02 16:33:22 -070020#include "connected.h"
Jeff King85556d42012-09-01 07:27:35 -040021#include "argv-array.h"
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +020022#include "utf8.h"
Jonathan Tan3836d882017-08-18 15:20:21 -070023#include "packfile.h"
Jeff Hostetleracb0c572017-12-08 15:58:44 +000024#include "list-objects-filter-options.h"
Derrick Stolee64043552018-07-20 16:33:04 +000025#include "commit-reach.h"
Corentin BOMPARD24bc1a12019-08-19 11:11:20 +020026#include "branch.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020027#include "promisor-remote.h"
Derrick Stolee50f26bd2019-09-02 19:22:02 -070028#include "commit-graph.h"
Daniel Barkalowb888d612007-09-10 23:03:25 -040029
Derrick Stolee377444b2019-06-18 13:25:27 -070030#define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000)
31
Kristian Høgsberg83201992007-12-04 02:25:47 -050032static const char * const builtin_fetch_usage[] = {
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070033 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
34 N_("git fetch [<options>] <group>"),
35 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
36 N_("git fetch --all [<options>]"),
Kristian Høgsberg83201992007-12-04 02:25:47 -050037 NULL
38};
Daniel Barkalowb888d612007-09-10 23:03:25 -040039
Kristian Høgsberg83201992007-12-04 02:25:47 -050040enum {
41 TAGS_UNSET = 0,
42 TAGS_DEFAULT = 1,
43 TAGS_SET = 2
44};
45
Michael Schubert737c5a92013-07-13 11:36:24 +020046static int fetch_prune_config = -1; /* unspecified */
Derrick Stoleecdbd70c2019-06-18 13:25:26 -070047static int fetch_show_forced_updates = 1;
Derrick Stolee377444b2019-06-18 13:25:27 -070048static uint64_t forced_updates_ms = 0;
Michael Schubert737c5a92013-07-13 11:36:24 +020049static int prune = -1; /* unspecified */
50#define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
51
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +000052static int fetch_prune_tags_config = -1; /* unspecified */
53static int prune_tags = -1; /* unspecified */
54#define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
55
Corentin BOMPARD24bc1a12019-08-19 11:11:20 +020056static int all, append, dry_run, force, keep, multiple, update_head_ok;
57static int verbosity, deepen_relative, set_upstream;
Stefan Beller8c698322017-06-23 12:13:01 -070058static int progress = -1;
Nguyễn Thái Ngọc Duyc3d6b702019-06-19 16:46:30 +070059static int enable_auto_gc = 1;
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +070060static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
Brandon Williamsf20e7c12017-08-02 12:49:18 -070061static int max_children = 1;
Eric Wongc915f112016-02-03 04:09:14 +000062static enum transport_family family;
Shawn O. Pearce4191c352007-11-11 02:29:47 -050063static const char *depth;
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +070064static const char *deepen_since;
Kristian Høgsberg83201992007-12-04 02:25:47 -050065static const char *upload_pack;
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +070066static struct string_list deepen_not = STRING_LIST_INIT_NODUP;
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -050067static struct strbuf default_rla = STRBUF_INIT;
Junio C Hamanoaf234452013-08-07 15:38:45 -070068static struct transport *gtransport;
Junio C Hamanob26ed432013-08-07 15:47:18 -070069static struct transport *gsecondary;
Jens Lehmann7dce19d2010-11-12 13:54:52 +010070static const char *submodule_prefix = "";
Stefan Beller8c698322017-06-23 12:13:01 -070071static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
Stefan Bellere8906a92017-06-27 14:31:59 -070072static int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND;
Tom Miller4b3b33a2014-01-02 20:28:51 -060073static int shown_url = 0;
Brandon Williamse4cffac2018-05-16 15:58:05 -070074static struct refspec refmap = REFSPEC_INIT_FETCH;
Jeff Hostetleracb0c572017-12-08 15:58:44 +000075static struct list_objects_filter_options filter_options;
Brandon Williams5e3548e2018-04-23 15:46:24 -070076static struct string_list server_options = STRING_LIST_INIT_DUP;
Jonathan Tan3390e422018-07-02 15:39:44 -070077static struct string_list negotiation_tip = STRING_LIST_INIT_NODUP;
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -040078
Michael Schubert737c5a92013-07-13 11:36:24 +020079static int git_fetch_config(const char *k, const char *v, void *cb)
80{
81 if (!strcmp(k, "fetch.prune")) {
82 fetch_prune_config = git_config_bool(k, v);
83 return 0;
84 }
Stefan Beller58f42032017-05-31 17:30:50 -070085
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +000086 if (!strcmp(k, "fetch.prunetags")) {
87 fetch_prune_tags_config = git_config_bool(k, v);
88 return 0;
89 }
90
Derrick Stoleecdbd70c2019-06-18 13:25:26 -070091 if (!strcmp(k, "fetch.showforcedupdates")) {
92 fetch_show_forced_updates = git_config_bool(k, v);
93 return 0;
94 }
95
Stefan Beller58f42032017-05-31 17:30:50 -070096 if (!strcmp(k, "submodule.recurse")) {
97 int r = git_config_bool(k, v) ?
98 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
99 recurse_submodules = r;
100 }
101
Brandon Williamsf20e7c12017-08-02 12:49:18 -0700102 if (!strcmp(k, "submodule.fetchjobs")) {
103 max_children = parse_submodule_fetchjobs(k, v);
104 return 0;
Brandon Williams8fa29152017-08-02 12:49:19 -0700105 } else if (!strcmp(k, "fetch.recursesubmodules")) {
106 recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
107 return 0;
Brandon Williamsf20e7c12017-08-02 12:49:18 -0700108 }
109
Jeff King72549df2014-11-04 08:11:19 -0500110 return git_default_config(k, v, cb);
Michael Schubert737c5a92013-07-13 11:36:24 +0200111}
112
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700113static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
114{
Jeff King517fe802018-11-05 01:45:42 -0500115 BUG_ON_OPT_NEG(unset);
116
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700117 /*
118 * "git fetch --refmap='' origin foo"
119 * can be used to tell the command not to store anywhere
120 */
Brandon Williamse4cffac2018-05-16 15:58:05 -0700121 refspec_append(&refmap, arg);
122
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700123 return 0;
124}
125
Kristian Høgsberg83201992007-12-04 02:25:47 -0500126static struct option builtin_fetch_options[] = {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100127 OPT__VERBOSITY(&verbosity),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200128 OPT_BOOL(0, "all", &all,
129 N_("fetch from all remotes")),
Corentin BOMPARD24bc1a12019-08-19 11:11:20 +0200130 OPT_BOOL(0, "set-upstream", &set_upstream,
131 N_("set upstream for git pull/fetch")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200132 OPT_BOOL('a', "append", &append,
133 N_("append to .git/FETCH_HEAD instead of overwriting")),
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700134 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
135 N_("path to upload pack on remote end")),
Ævar Arnfjörð Bjarmason8cd4b7c2018-08-31 20:09:56 +0000136 OPT__FORCE(&force, N_("force overwrite of local reference"), 0),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200137 OPT_BOOL('m', "multiple", &multiple,
138 N_("fetch from multiple remotes")),
Kristian Høgsberg83201992007-12-04 02:25:47 -0500139 OPT_SET_INT('t', "tags", &tags,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700140 N_("fetch all tags and associated objects"), TAGS_SET),
Johannes Schindeline7951292008-03-13 08:13:15 +0100141 OPT_SET_INT('n', NULL, &tags,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700142 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
Stefan Beller62104ba2015-12-15 16:04:12 -0800143 OPT_INTEGER('j', "jobs", &max_children,
144 N_("number of submodules fetched in parallel")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200145 OPT_BOOL('p', "prune", &prune,
146 N_("prune remote-tracking branches no longer on remote")),
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +0000147 OPT_BOOL('P', "prune-tags", &prune_tags,
148 N_("prune local tags no longer on remote and clobber changed tags")),
Stefan Beller886dc152017-06-23 12:13:00 -0700149 { OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, N_("on-demand"),
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700150 N_("control recursive fetching of submodules"),
Stefan Beller886dc152017-06-23 12:13:00 -0700151 PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules },
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200152 OPT_BOOL(0, "dry-run", &dry_run,
153 N_("dry run")),
154 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
155 OPT_BOOL('u', "update-head-ok", &update_head_ok,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700156 N_("allow updating of HEAD ref")),
157 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
158 OPT_STRING(0, "depth", &depth, N_("depth"),
159 N_("deepen history of shallow clone")),
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700160 OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
161 N_("deepen history of shallow repository based on time")),
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700162 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"),
Alex Henrie6d873862016-12-04 15:03:59 -0700163 N_("deepen history of shallow clone, excluding rev")),
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700164 OPT_INTEGER(0, "deepen", &deepen_relative,
165 N_("deepen history of shallow clone")),
Nguyễn Thái Ngọc Duy3e4a67b2018-05-20 17:42:58 +0200166 OPT_SET_INT_F(0, "unshallow", &unshallow,
167 N_("convert to a complete repository"),
168 1, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700169 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
170 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
Stefan Beller8c698322017-06-23 12:13:01 -0700171 { OPTION_CALLBACK, 0, "recurse-submodules-default",
172 &recurse_submodules_default, N_("on-demand"),
173 N_("default for recursive fetching of submodules "
174 "(lower priority than config files)"),
175 PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules },
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +0700176 OPT_BOOL(0, "update-shallow", &update_shallow,
177 N_("accept refs that update .git/shallow")),
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700178 { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"),
179 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg },
Brandon Williams5e3548e2018-04-23 15:46:24 -0700180 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
Eric Wongc915f112016-02-03 04:09:14 +0000181 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
182 TRANSPORT_FAMILY_IPV4),
183 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
184 TRANSPORT_FAMILY_IPV6),
Jonathan Tan3390e422018-07-02 15:39:44 -0700185 OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"),
186 N_("report that we have only objects reachable from this object")),
Jeff Hostetleracb0c572017-12-08 15:58:44 +0000187 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
Nguyễn Thái Ngọc Duyc3d6b702019-06-19 16:46:30 +0700188 OPT_BOOL(0, "auto-gc", &enable_auto_gc,
189 N_("run 'gc --auto' after fetching")),
Derrick Stoleecdbd70c2019-06-18 13:25:26 -0700190 OPT_BOOL(0, "show-forced-updates", &fetch_show_forced_updates,
191 N_("check for forced-updates on all updated branches")),
Kristian Høgsberg83201992007-12-04 02:25:47 -0500192 OPT_END()
193};
194
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400195static void unlock_pack(void)
196{
Junio C Hamanoaf234452013-08-07 15:38:45 -0700197 if (gtransport)
198 transport_unlock_pack(gtransport);
Junio C Hamanob26ed432013-08-07 15:47:18 -0700199 if (gsecondary)
200 transport_unlock_pack(gsecondary);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400201}
202
203static void unlock_pack_on_signal(int signo)
204{
205 unlock_pack();
Jeff King4a16d072009-01-22 01:02:35 -0500206 sigchain_pop(signo);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400207 raise(signo);
208}
Daniel Barkalowb888d612007-09-10 23:03:25 -0400209
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400210static void add_merge_config(struct ref **head,
Daniel Barkalow45773702007-10-29 21:05:40 -0400211 const struct ref *remote_refs,
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400212 struct branch *branch,
213 struct ref ***tail)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400214{
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400215 int i;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400216
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400217 for (i = 0; i < branch->merge_nr; i++) {
218 struct ref *rm, **old_tail = *tail;
Brandon Williams0ad4a5f2018-05-16 15:57:49 -0700219 struct refspec_item refspec;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400220
221 for (rm = *head; rm; rm = rm->next) {
222 if (branch_merge_matches(branch, i, rm->name)) {
Jeff King900f2812013-05-11 18:15:59 +0200223 rm->fetch_head_status = FETCH_HEAD_MERGE;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400224 break;
225 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400226 }
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400227 if (rm)
228 continue;
229
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700230 /*
Matthieu Moy8b3f3f82010-11-02 16:31:23 +0100231 * Not fetched to a remote-tracking branch? We need to fetch
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400232 * it anyway to allow this branch's "branch.$name.merge"
Heikki Orsila05207a22008-09-09 13:28:30 +0300233 * to be honored by 'git pull', but we do not have to
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700234 * fail if branch.$name.merge is misconfigured to point
235 * at a nonexisting branch. If we were indeed called by
Heikki Orsila05207a22008-09-09 13:28:30 +0300236 * 'git pull', it will notice the misconfiguration because
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700237 * there is no entry in the resulting FETCH_HEAD marked
238 * for merging.
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400239 */
Andreas Gruenbacher8da61a22010-03-12 23:27:33 +0100240 memset(&refspec, 0, sizeof(refspec));
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400241 refspec.src = branch->merge[i]->src;
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700242 get_fetch_map(remote_refs, &refspec, tail, 1);
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400243 for (rm = *old_tail; rm; rm = rm->next)
Jeff King900f2812013-05-11 18:15:59 +0200244 rm->fetch_head_status = FETCH_HEAD_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400245 }
246}
247
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100248static int will_fetch(struct ref **head, const unsigned char *sha1)
249{
250 struct ref *rm = *head;
251 while (rm) {
Jeff Kinge3ff0682018-08-28 17:22:44 -0400252 if (hasheq(rm->old_oid.hash, sha1))
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100253 return 1;
254 rm = rm->next;
255 }
256 return 0;
257}
258
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700259struct refname_hash_entry {
260 struct hashmap_entry ent; /* must be the first member */
261 struct object_id oid;
Felipe Contrerasf80d9222019-06-03 21:13:30 -0500262 int ignore;
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700263 char refname[FLEX_ARRAY];
264};
265
266static int refname_hash_entry_cmp(const void *hashmap_cmp_fn_data,
267 const void *e1_,
268 const void *e2_,
269 const void *keydata)
270{
271 const struct refname_hash_entry *e1 = e1_;
272 const struct refname_hash_entry *e2 = e2_;
273
274 return strcmp(e1->refname, keydata ? keydata : e2->refname);
275}
276
277static struct refname_hash_entry *refname_hash_add(struct hashmap *map,
278 const char *refname,
279 const struct object_id *oid)
280{
281 struct refname_hash_entry *ent;
282 size_t len = strlen(refname);
283
284 FLEX_ALLOC_MEM(ent, refname, refname, len);
285 hashmap_entry_init(ent, strhash(refname));
286 oidcpy(&ent->oid, oid);
287 hashmap_add(map, ent);
288 return ent;
289}
290
291static int add_one_refname(const char *refname,
292 const struct object_id *oid,
293 int flag, void *cbdata)
294{
295 struct hashmap *refname_map = cbdata;
296
297 (void) refname_hash_add(refname_map, refname, oid);
298 return 0;
299}
300
301static void refname_hash_init(struct hashmap *map)
302{
303 hashmap_init(map, refname_hash_entry_cmp, NULL, 0);
304}
305
306static int refname_hash_exists(struct hashmap *map, const char *refname)
307{
308 return !!hashmap_get_from_hash(map, strhash(refname), refname);
309}
310
Felipe Contrerasa8363b52019-06-03 21:13:28 -0500311static void clear_item(struct refname_hash_entry *item)
312{
Felipe Contrerasf80d9222019-06-03 21:13:30 -0500313 item->ignore = 1;
Felipe Contrerasa8363b52019-06-03 21:13:28 -0500314}
315
Brandon Williams6d1700d2018-06-27 15:30:21 -0700316static void find_non_local_tags(const struct ref *refs,
317 struct ref **head,
318 struct ref ***tail)
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100319{
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700320 struct hashmap existing_refs;
321 struct hashmap remote_refs;
322 struct string_list remote_refs_list = STRING_LIST_INIT_NODUP;
323 struct string_list_item *remote_ref_item;
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100324 const struct ref *ref;
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700325 struct refname_hash_entry *item = NULL;
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100326
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700327 refname_hash_init(&existing_refs);
328 refname_hash_init(&remote_refs);
329
330 for_each_ref(add_one_refname, &existing_refs);
Brandon Williams6d1700d2018-06-27 15:30:21 -0700331 for (ref = refs; ref; ref = ref->next) {
Junio C Hamanoad704482013-12-17 11:47:35 -0800332 if (!starts_with(ref->name, "refs/tags/"))
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100333 continue;
334
335 /*
336 * The peeled ref always follows the matching base
337 * ref, so if we see a peeled ref that we don't want
338 * to fetch then we can mark the ref entry in the list
339 * as one to ignore by setting util to NULL.
340 */
Junio C Hamanoad704482013-12-17 11:47:35 -0800341 if (ends_with(ref->name, "^{}")) {
Jeff King5827a032016-10-13 12:53:44 -0400342 if (item &&
Jonathan Tane83e71c2017-06-21 17:40:24 -0700343 !has_object_file_with_flags(&ref->old_oid,
344 OBJECT_INFO_QUICK) &&
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000345 !will_fetch(head, ref->old_oid.hash) &&
Jeff King98374a02019-01-07 03:37:54 -0500346 !has_object_file_with_flags(&item->oid, OBJECT_INFO_QUICK) &&
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700347 !will_fetch(head, item->oid.hash))
Felipe Contrerasa8363b52019-06-03 21:13:28 -0500348 clear_item(item);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100349 item = NULL;
350 continue;
351 }
352
353 /*
354 * If item is non-NULL here, then we previously saw a
355 * ref not followed by a peeled reference, so we need
356 * to check if it is a lightweight tag that we want to
357 * fetch.
358 */
Jeff King5827a032016-10-13 12:53:44 -0400359 if (item &&
Jeff King98374a02019-01-07 03:37:54 -0500360 !has_object_file_with_flags(&item->oid, OBJECT_INFO_QUICK) &&
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700361 !will_fetch(head, item->oid.hash))
Felipe Contrerasa8363b52019-06-03 21:13:28 -0500362 clear_item(item);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100363
364 item = NULL;
365
366 /* skip duplicates and refs that we already have */
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700367 if (refname_hash_exists(&remote_refs, ref->name) ||
368 refname_hash_exists(&existing_refs, ref->name))
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100369 continue;
370
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700371 item = refname_hash_add(&remote_refs, ref->name, &ref->old_oid);
372 string_list_insert(&remote_refs_list, ref->name);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100373 }
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700374 hashmap_free(&existing_refs, 1);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100375
376 /*
377 * We may have a final lightweight tag that needs to be
378 * checked to see if it needs fetching.
379 */
Jeff King5827a032016-10-13 12:53:44 -0400380 if (item &&
Jeff King98374a02019-01-07 03:37:54 -0500381 !has_object_file_with_flags(&item->oid, OBJECT_INFO_QUICK) &&
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700382 !will_fetch(head, item->oid.hash))
Felipe Contrerasa8363b52019-06-03 21:13:28 -0500383 clear_item(item);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100384
385 /*
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700386 * For all the tags in the remote_refs_list,
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100387 * add them to the list of refs to be fetched
388 */
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700389 for_each_string_list_item(remote_ref_item, &remote_refs_list) {
390 const char *refname = remote_ref_item->string;
Felipe Contreras9528b802019-06-03 21:13:29 -0500391 struct ref *rm;
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700392
393 item = hashmap_get_from_hash(&remote_refs, strhash(refname), refname);
394 if (!item)
395 BUG("unseen remote ref?");
396
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100397 /* Unless we have already decided to ignore this item... */
Felipe Contrerasf80d9222019-06-03 21:13:30 -0500398 if (item->ignore)
Felipe Contreras9528b802019-06-03 21:13:29 -0500399 continue;
400
401 rm = alloc_ref(item->refname);
402 rm->peer_ref = alloc_ref(item->refname);
403 oidcpy(&rm->old_oid, &item->oid);
404 **tail = rm;
405 *tail = &rm->next;
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100406 }
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700407 hashmap_free(&remote_refs, 1);
408 string_list_clear(&remote_refs_list, 0);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100409}
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500410
Brandon Williams6d1700d2018-06-27 15:30:21 -0700411static struct ref *get_ref_map(struct remote *remote,
412 const struct ref *remote_refs,
Brandon Williams65d96c82018-05-16 15:58:08 -0700413 struct refspec *rs,
Michael Haggertyf137a452013-10-23 17:50:38 +0200414 int tags, int *autotags)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400415{
416 int i;
417 struct ref *rm;
418 struct ref *ref_map = NULL;
419 struct ref **tail = &ref_map;
420
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100421 /* opportunistically-updated references: */
422 struct ref *orefs = NULL, **oref_tail = &orefs;
423
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700424 struct hashmap existing_refs;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400425
Brandon Williams65d96c82018-05-16 15:58:08 -0700426 if (rs->nr) {
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700427 struct refspec *fetch_refspec;
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700428
Brandon Williams65d96c82018-05-16 15:58:08 -0700429 for (i = 0; i < rs->nr; i++) {
430 get_fetch_map(remote_refs, &rs->items[i], &tail, 0);
431 if (rs->items[i].dst && rs->items[i].dst[0])
Daniel Barkalowb888d612007-09-10 23:03:25 -0400432 *autotags = 1;
433 }
Michael Haggerty0281c932013-10-30 06:32:58 +0100434 /* Merge everything on the command line (but not --tags) */
Daniel Barkalowb888d612007-09-10 23:03:25 -0400435 for (rm = ref_map; rm; rm = rm->next)
Jeff King900f2812013-05-11 18:15:59 +0200436 rm->fetch_head_status = FETCH_HEAD_MERGE;
Michael Haggerty0281c932013-10-30 06:32:58 +0100437
438 /*
439 * For any refs that we happen to be fetching via
440 * command-line arguments, the destination ref might
441 * have been missing or have been different than the
442 * remote-tracking ref that would be derived from the
443 * configured refspec. In these cases, we want to
444 * take the opportunity to update their configured
445 * remote-tracking reference. However, we do not want
446 * to mention these entries in FETCH_HEAD at all, as
447 * they would simply be duplicates of existing
448 * entries, so we set them FETCH_HEAD_IGNORE below.
449 *
450 * We compute these entries now, based only on the
451 * refspecs specified on the command line. But we add
452 * them to the list following the refspecs resulting
453 * from the tags option so that one of the latter,
454 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
455 * by ref_remove_duplicates() in favor of one of these
456 * opportunistic entries with FETCH_HEAD_IGNORE.
457 */
Brandon Williams65d96c82018-05-16 15:58:08 -0700458 if (refmap.nr)
459 fetch_refspec = &refmap;
460 else
Brandon Williams6d1700d2018-06-27 15:30:21 -0700461 fetch_refspec = &remote->fetch;
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700462
Brandon Williams65d96c82018-05-16 15:58:08 -0700463 for (i = 0; i < fetch_refspec->nr; i++)
464 get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
Brandon Williamse4cffac2018-05-16 15:58:05 -0700465 } else if (refmap.nr) {
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700466 die("--refmap option is only meaningful with command-line refspec(s).");
Daniel Barkalowb888d612007-09-10 23:03:25 -0400467 } else {
468 /* Use the defaults */
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400469 struct branch *branch = branch_get(NULL);
470 int has_merge = branch_has_merge_config(branch);
Brandon Casey3ee17572010-08-25 12:52:56 -0500471 if (remote &&
Brandon Williamse5349ab2018-05-16 15:58:01 -0700472 (remote->fetch.nr ||
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500473 /* Note: has_merge implies non-NULL branch->remote_name */
Brandon Casey3ee17572010-08-25 12:52:56 -0500474 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
Brandon Williamse5349ab2018-05-16 15:58:01 -0700475 for (i = 0; i < remote->fetch.nr; i++) {
476 get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);
477 if (remote->fetch.items[i].dst &&
478 remote->fetch.items[i].dst[0])
Daniel Barkalowb888d612007-09-10 23:03:25 -0400479 *autotags = 1;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400480 if (!i && !has_merge && ref_map &&
Brandon Williamse5349ab2018-05-16 15:58:01 -0700481 !remote->fetch.items[0].pattern)
Jeff King900f2812013-05-11 18:15:59 +0200482 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400483 }
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100484 /*
485 * if the remote we're fetching from is the same
486 * as given in branch.<name>.remote, we add the
487 * ref given in branch.<name>.merge, too.
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500488 *
489 * Note: has_merge implies non-NULL branch->remote_name
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100490 */
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700491 if (has_merge &&
492 !strcmp(branch->remote_name, remote->name))
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400493 add_merge_config(&ref_map, remote_refs, branch, &tail);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400494 } else {
495 ref_map = get_remote_ref(remote_refs, "HEAD");
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700496 if (!ref_map)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000497 die(_("Couldn't find remote ref HEAD"));
Jeff King900f2812013-05-11 18:15:59 +0200498 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
Shawn O. Pearce5aaf7f22008-03-02 21:34:51 -0500499 tail = &ref_map->next;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400500 }
501 }
Michael Haggerty0281c932013-10-30 06:32:58 +0100502
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100503 if (tags == TAGS_SET)
504 /* also fetch all tags */
505 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
506 else if (tags == TAGS_DEFAULT && *autotags)
Brandon Williams6d1700d2018-06-27 15:30:21 -0700507 find_non_local_tags(remote_refs, &ref_map, &tail);
Michael Haggerty0281c932013-10-30 06:32:58 +0100508
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100509 /* Now append any refs to be updated opportunistically: */
510 *tail = orefs;
511 for (rm = orefs; rm; rm = rm->next) {
512 rm->fetch_head_status = FETCH_HEAD_IGNORE;
513 tail = &rm->next;
514 }
515
Brandon Williams14b8ced2018-06-27 15:30:19 -0700516 ref_map = ref_remove_duplicates(ref_map);
517
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700518 refname_hash_init(&existing_refs);
519 for_each_ref(add_one_refname, &existing_refs);
520
Brandon Williams14b8ced2018-06-27 15:30:19 -0700521 for (rm = ref_map; rm; rm = rm->next) {
522 if (rm->peer_ref) {
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700523 const char *refname = rm->peer_ref->name;
524 struct refname_hash_entry *peer_item;
525
526 peer_item = hashmap_get_from_hash(&existing_refs,
527 strhash(refname),
528 refname);
Brandon Williams14b8ced2018-06-27 15:30:19 -0700529 if (peer_item) {
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700530 struct object_id *old_oid = &peer_item->oid;
Brandon Williams14b8ced2018-06-27 15:30:19 -0700531 oidcpy(&rm->peer_ref->old_oid, old_oid);
532 }
533 }
534 }
Junio C Hamanoe198b3a2018-09-25 13:25:04 -0700535 hashmap_free(&existing_refs, 1);
Brandon Williams14b8ced2018-06-27 15:30:19 -0700536
537 return ref_map;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400538}
539
Jeff Kingfa250752009-05-25 06:40:54 -0400540#define STORE_REF_ERROR_OTHER 1
541#define STORE_REF_ERROR_DF_CONFLICT 2
542
Daniel Barkalowb888d612007-09-10 23:03:25 -0400543static int s_update_ref(const char *action,
544 struct ref *ref,
545 int check_old)
546{
Jeff King1412f762017-03-28 15:46:26 -0400547 char *msg;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400548 char *rla = getenv("GIT_REFLOG_ACTION");
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700549 struct ref_transaction *transaction;
550 struct strbuf err = STRBUF_INIT;
551 int ret, df_conflict = 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400552
Jay Soffian28a15402009-11-10 09:19:43 +0100553 if (dry_run)
554 return 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400555 if (!rla)
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -0500556 rla = default_rla.buf;
Jeff King1412f762017-03-28 15:46:26 -0400557 msg = xstrfmt("%s: %s", rla, action);
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700558
559 transaction = ref_transaction_begin(&err);
560 if (!transaction ||
Michael Haggerty1d147bd2015-02-17 18:00:15 +0100561 ref_transaction_update(transaction, ref->name,
brian m. carlson89f3bbd2017-10-15 22:06:53 +0000562 &ref->new_oid,
563 check_old ? &ref->old_oid : NULL,
Michael Haggerty1d147bd2015-02-17 18:00:15 +0100564 0, msg, &err))
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700565 goto fail;
566
567 ret = ref_transaction_commit(transaction, &err);
568 if (ret) {
569 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
570 goto fail;
571 }
572
573 ref_transaction_free(transaction);
574 strbuf_release(&err);
Jeff King1412f762017-03-28 15:46:26 -0400575 free(msg);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400576 return 0;
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700577fail:
578 ref_transaction_free(transaction);
579 error("%s", err.buf);
580 strbuf_release(&err);
Jeff King1412f762017-03-28 15:46:26 -0400581 free(msg);
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700582 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
583 : STORE_REF_ERROR_OTHER;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400584}
585
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200586static int refcol_width = 10;
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200587static int compact_format;
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200588
589static void adjust_refcol_width(const struct ref *ref)
590{
591 int max, rlen, llen, len;
592
593 /* uptodate lines are only shown on high verbosity level */
Jeff King4a7e27e2018-08-28 17:22:40 -0400594 if (!verbosity && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200595 return;
596
597 max = term_columns();
598 rlen = utf8_strwidth(prettify_refname(ref->name));
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200599
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200600 llen = utf8_strwidth(prettify_refname(ref->peer_ref->name));
601
602 /*
603 * rough estimation to see if the output line is too long and
604 * should not be counted (we can't do precise calculation
605 * anyway because we don't know if the error explanation part
606 * will be printed in update_local_ref)
607 */
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200608 if (compact_format) {
609 llen = 0;
610 max = max * 2 / 3;
611 }
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200612 len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
613 if (len >= max)
614 return;
615
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200616 /*
617 * Not precise calculation for compact mode because '*' can
618 * appear on the left hand side of '->' and shrink the column
619 * back.
620 */
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200621 if (refcol_width < rlen)
622 refcol_width = rlen;
623}
624
625static void prepare_format_display(struct ref *ref_map)
626{
627 struct ref *rm;
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200628 const char *format = "full";
629
630 git_config_get_string_const("fetch.output", &format);
631 if (!strcasecmp(format, "full"))
632 compact_format = 0;
633 else if (!strcasecmp(format, "compact"))
634 compact_format = 1;
635 else
636 die(_("configuration fetch.output contains invalid value %s"),
637 format);
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200638
639 for (rm = ref_map; rm; rm = rm->next) {
640 if (rm->status == REF_STATUS_REJECT_SHALLOW ||
641 !rm->peer_ref ||
642 !strcmp(rm->name, "HEAD"))
643 continue;
644
645 adjust_refcol_width(rm);
646 }
647}
Nicolas Pitre165f3902007-11-03 01:32:48 -0400648
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200649static void print_remote_to_local(struct strbuf *display,
650 const char *remote, const char *local)
651{
652 strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local);
653}
654
655static int find_and_replace(struct strbuf *haystack,
656 const char *needle,
657 const char *placeholder)
658{
Nguyễn Thái Ngọc Duydc40b242019-01-25 16:51:22 +0700659 const char *p = NULL;
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200660 int plen, nlen;
661
Nguyễn Thái Ngọc Duydc40b242019-01-25 16:51:22 +0700662 nlen = strlen(needle);
663 if (ends_with(haystack->buf, needle))
664 p = haystack->buf + haystack->len - nlen;
665 else
666 p = strstr(haystack->buf, needle);
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200667 if (!p)
668 return 0;
669
670 if (p > haystack->buf && p[-1] != '/')
671 return 0;
672
673 plen = strlen(p);
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200674 if (plen > nlen && p[nlen] != '/')
675 return 0;
676
677 strbuf_splice(haystack, p - haystack->buf, nlen,
678 placeholder, strlen(placeholder));
679 return 1;
680}
681
682static void print_compact(struct strbuf *display,
683 const char *remote, const char *local)
684{
685 struct strbuf r = STRBUF_INIT;
686 struct strbuf l = STRBUF_INIT;
687
688 if (!strcmp(remote, local)) {
689 strbuf_addf(display, "%-*s -> *", refcol_width, remote);
690 return;
691 }
692
693 strbuf_addstr(&r, remote);
694 strbuf_addstr(&l, local);
695
696 if (!find_and_replace(&r, local, "*"))
697 find_and_replace(&l, remote, "*");
698 print_remote_to_local(display, r.buf, l.buf);
699
700 strbuf_release(&r);
701 strbuf_release(&l);
702}
703
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200704static void format_display(struct strbuf *display, char code,
705 const char *summary, const char *error,
Junio C Hamano901f3d42016-10-21 15:22:55 -0700706 const char *remote, const char *local,
707 int summary_width)
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200708{
Junio C Hamano901f3d42016-10-21 15:22:55 -0700709 int width = (summary_width + strlen(summary) - gettext_width(summary));
710
711 strbuf_addf(display, "%c %-*s ", code, width, summary);
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +0200712 if (!compact_format)
713 print_remote_to_local(display, remote, local);
714 else
715 print_compact(display, remote, local);
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200716 if (error)
717 strbuf_addf(display, " (%s)", error);
718}
Daniel Barkalowb888d612007-09-10 23:03:25 -0400719
720static int update_local_ref(struct ref *ref,
Nicolas Pitre165f3902007-11-03 01:32:48 -0400721 const char *remote,
Marc Branchaud6da618d2012-04-16 18:08:49 -0400722 const struct ref *remote_ref,
Junio C Hamano901f3d42016-10-21 15:22:55 -0700723 struct strbuf *display,
724 int summary_width)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400725{
Daniel Barkalowb888d612007-09-10 23:03:25 -0400726 struct commit *current = NULL, *updated;
727 enum object_type type;
728 struct branch *current_branch = branch_get(NULL);
Felipe Contreras4577e482009-05-14 00:22:04 +0300729 const char *pretty_ref = prettify_refname(ref->name);
Derrick Stolee377444b2019-06-18 13:25:27 -0700730 int fast_forward = 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400731
Stefan Beller0df8e962018-04-25 11:20:59 -0700732 type = oid_object_info(the_repository, &ref->new_oid, NULL);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400733 if (type < 0)
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000734 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
Daniel Barkalowb888d612007-09-10 23:03:25 -0400735
Jeff King4a7e27e2018-08-28 17:22:40 -0400736 if (oideq(&ref->old_oid, &ref->new_oid)) {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100737 if (verbosity > 0)
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200738 format_display(display, '=', _("[up to date]"), NULL,
Junio C Hamano901f3d42016-10-21 15:22:55 -0700739 remote, pretty_ref, summary_width);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400740 return 0;
741 }
742
Shawn O. Pearceb3abdd92007-09-16 02:31:26 -0400743 if (current_branch &&
744 !strcmp(ref->name, current_branch->name) &&
Daniel Barkalowb888d612007-09-10 23:03:25 -0400745 !(update_head_ok || is_bare_repository()) &&
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000746 !is_null_oid(&ref->old_oid)) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400747 /*
748 * If this is the head, and it's not okay to update
749 * the head, and the old value of the head isn't empty...
750 */
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200751 format_display(display, '!', _("[rejected]"),
752 _("can't fetch in current branch"),
Junio C Hamano901f3d42016-10-21 15:22:55 -0700753 remote, pretty_ref, summary_width);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400754 return 1;
755 }
756
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000757 if (!is_null_oid(&ref->old_oid) &&
Christian Couder59556542013-11-30 21:55:40 +0100758 starts_with(ref->name, "refs/tags/")) {
Ævar Arnfjörð Bjarmason0bc8d712018-08-31 20:10:04 +0000759 if (force || ref->force) {
760 int r;
761 r = s_update_ref("updating tag", ref, 0);
762 format_display(display, r ? '!' : 't', _("[tag update]"),
763 r ? _("unable to update local ref") : NULL,
764 remote, pretty_ref, summary_width);
765 return r;
766 } else {
767 format_display(display, '!', _("[rejected]"), _("would clobber existing tag"),
768 remote, pretty_ref, summary_width);
769 return 1;
770 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400771 }
772
Stefan Beller21e1ee82018-06-28 18:21:57 -0700773 current = lookup_commit_reference_gently(the_repository,
774 &ref->old_oid, 1);
775 updated = lookup_commit_reference_gently(the_repository,
776 &ref->new_oid, 1);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400777 if (!current || !updated) {
Nicolas Pitre165f3902007-11-03 01:32:48 -0400778 const char *msg;
779 const char *what;
Jeff King63154722008-06-26 23:59:50 -0400780 int r;
Marc Branchaud0997ada2012-04-16 18:08:50 -0400781 /*
782 * Nicely describe the new ref we're fetching.
783 * Base this on the remote's ref name, as it's
784 * more likely to follow a standard layout.
785 */
786 const char *name = remote_ref ? remote_ref->name : "";
Christian Couder59556542013-11-30 21:55:40 +0100787 if (starts_with(name, "refs/tags/")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400788 msg = "storing tag";
Ævar Arnfjörð Bjarmasonf7b37422011-02-22 23:41:53 +0000789 what = _("[new tag]");
Christian Couder59556542013-11-30 21:55:40 +0100790 } else if (starts_with(name, "refs/heads/")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400791 msg = "storing head";
Ævar Arnfjörð Bjarmasonf7b37422011-02-22 23:41:53 +0000792 what = _("[new branch]");
Marc Branchaud0997ada2012-04-16 18:08:50 -0400793 } else {
794 msg = "storing ref";
795 what = _("[new ref]");
Nicolas Pitre165f3902007-11-03 01:32:48 -0400796 }
797
Jeff King63154722008-06-26 23:59:50 -0400798 r = s_update_ref(msg, ref, 0);
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200799 format_display(display, r ? '!' : '*', what,
800 r ? _("unable to update local ref") : NULL,
Junio C Hamano901f3d42016-10-21 15:22:55 -0700801 remote, pretty_ref, summary_width);
Jeff King63154722008-06-26 23:59:50 -0400802 return r;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400803 }
804
Derrick Stolee377444b2019-06-18 13:25:27 -0700805 if (fetch_show_forced_updates) {
806 uint64_t t_before = getnanotime();
807 fast_forward = in_merge_bases(current, updated);
808 forced_updates_ms += (getnanotime() - t_before) / 1000000;
809 } else {
810 fast_forward = 1;
811 }
812
813 if (fast_forward) {
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400814 struct strbuf quickref = STRBUF_INIT;
Jeff King63154722008-06-26 23:59:50 -0400815 int r;
Derrick Stoleecdbd70c2019-06-18 13:25:26 -0700816
brian m. carlson30e677e2018-03-12 02:27:28 +0000817 strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400818 strbuf_addstr(&quickref, "..");
brian m. carlson30e677e2018-03-12 02:27:28 +0000819 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300820 r = s_update_ref("fast-forward", ref, 1);
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200821 format_display(display, r ? '!' : ' ', quickref.buf,
822 r ? _("unable to update local ref") : NULL,
Junio C Hamano901f3d42016-10-21 15:22:55 -0700823 remote, pretty_ref, summary_width);
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400824 strbuf_release(&quickref);
Jeff King63154722008-06-26 23:59:50 -0400825 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400826 } else if (force || ref->force) {
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400827 struct strbuf quickref = STRBUF_INIT;
Jeff King63154722008-06-26 23:59:50 -0400828 int r;
brian m. carlson30e677e2018-03-12 02:27:28 +0000829 strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400830 strbuf_addstr(&quickref, "...");
brian m. carlson30e677e2018-03-12 02:27:28 +0000831 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
Jeff King63154722008-06-26 23:59:50 -0400832 r = s_update_ref("forced-update", ref, 1);
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200833 format_display(display, r ? '!' : '+', quickref.buf,
834 r ? _("unable to update local ref") : _("forced update"),
Junio C Hamano901f3d42016-10-21 15:22:55 -0700835 remote, pretty_ref, summary_width);
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400836 strbuf_release(&quickref);
Jeff King63154722008-06-26 23:59:50 -0400837 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400838 } else {
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +0200839 format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
Junio C Hamano901f3d42016-10-21 15:22:55 -0700840 remote, pretty_ref, summary_width);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400841 return 1;
842 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400843}
844
brian m. carlson6ccac9e2017-10-15 22:06:54 +0000845static int iterate_ref_map(void *cb_data, struct object_id *oid)
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700846{
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700847 struct ref **rm = cb_data;
848 struct ref *ref = *rm;
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700849
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +0700850 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
851 ref = ref->next;
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700852 if (!ref)
853 return -1; /* end of the list */
854 *rm = ref->next;
brian m. carlson6ccac9e2017-10-15 22:06:54 +0000855 oidcpy(oid, &ref->old_oid);
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700856 return 0;
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700857}
858
Jean-Noël Avila182f59d2019-08-06 19:19:52 +0200859static const char warn_show_forced_updates[] =
860N_("Fetch normally indicates which branches had a forced update,\n"
861 "but that check has been disabled. To re-enable, use '--show-forced-updates'\n"
862 "flag or run 'git config fetch.showForcedUpdates true'.");
863static const char warn_time_show_forced_updates[] =
864N_("It took %.2f seconds to check forced updates. You can use\n"
865 "'--no-show-forced-updates' or run 'git config fetch.showForcedUpdates false'\n"
866 " to avoid this check.\n");
867
Andreas Ericsson47abd852009-04-17 10:20:11 +0200868static int store_updated_refs(const char *raw_url, const char *remote_name,
Jonathan Tancf1e7c02018-07-02 15:08:43 -0700869 int connectivity_checked, struct ref *ref_map)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400870{
871 FILE *fp;
872 struct commit *commit;
Tom Miller4b3b33a2014-01-02 20:28:51 -0600873 int url_len, i, rc = 0;
Jeff King5914f2d2011-12-08 03:43:19 -0500874 struct strbuf note = STRBUF_INIT;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400875 const char *what, *kind;
876 struct ref *rm;
Nguyễn Thái Ngọc Duydcf69262014-11-30 15:24:27 +0700877 char *url;
Stefan Beller102de882018-05-17 15:51:51 -0700878 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head(the_repository);
Jeff King900f2812013-05-11 18:15:59 +0200879 int want_status;
Junio C Hamano11fd66d2016-10-21 15:28:07 -0700880 int summary_width = transport_summary_width(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400881
André Goddard Rosad6617c72007-11-22 20:22:23 -0200882 fp = fopen(filename, "a");
883 if (!fp)
Nguyễn Thái Ngọc Duy6da31d72016-05-08 16:47:26 +0700884 return error_errno(_("cannot open %s"), filename);
Andreas Ericsson47abd852009-04-17 10:20:11 +0200885
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100886 if (raw_url)
887 url = transport_anonymize_url(raw_url);
888 else
889 url = xstrdup("foreign");
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700890
Jonathan Tancf1e7c02018-07-02 15:08:43 -0700891 if (!connectivity_checked) {
892 rm = ref_map;
893 if (check_connected(iterate_ref_map, &rm, NULL)) {
894 rc = error(_("%s did not send all necessary objects\n"), url);
895 goto abort;
896 }
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800897 }
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700898
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +0200899 prepare_format_display(ref_map);
900
Joey Hess96890f42011-12-26 12:16:56 -0400901 /*
Jeff King900f2812013-05-11 18:15:59 +0200902 * We do a pass for each fetch_head_status type in their enum order, so
903 * merged entries are written before not-for-merge. That lets readers
904 * use FETCH_HEAD as a refname to refer to the ref to be merged.
Joey Hess96890f42011-12-26 12:16:56 -0400905 */
Jeff King900f2812013-05-11 18:15:59 +0200906 for (want_status = FETCH_HEAD_MERGE;
907 want_status <= FETCH_HEAD_IGNORE;
908 want_status++) {
Joey Hess96890f42011-12-26 12:16:56 -0400909 for (rm = ref_map; rm; rm = rm->next) {
910 struct ref *ref = NULL;
Jeff King900f2812013-05-11 18:15:59 +0200911 const char *merge_status_marker = "";
Daniel Barkalowb888d612007-09-10 23:03:25 -0400912
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +0700913 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
914 if (want_status == FETCH_HEAD_MERGE)
915 warning(_("reject %s because shallow roots are not allowed to be updated"),
916 rm->peer_ref ? rm->peer_ref->name : rm->name);
917 continue;
918 }
919
Stefan Beller21e1ee82018-06-28 18:21:57 -0700920 commit = lookup_commit_reference_gently(the_repository,
921 &rm->old_oid,
brian m. carlsonbc832662017-05-06 22:10:10 +0000922 1);
Joey Hess96890f42011-12-26 12:16:56 -0400923 if (!commit)
Jeff King900f2812013-05-11 18:15:59 +0200924 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400925
Jeff King900f2812013-05-11 18:15:59 +0200926 if (rm->fetch_head_status != want_status)
Joey Hess96890f42011-12-26 12:16:56 -0400927 continue;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400928
Joey Hess96890f42011-12-26 12:16:56 -0400929 if (rm->peer_ref) {
Jeff King6f687c22015-09-24 17:08:09 -0400930 ref = alloc_ref(rm->peer_ref->name);
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000931 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
932 oidcpy(&ref->new_oid, &rm->old_oid);
Joey Hess96890f42011-12-26 12:16:56 -0400933 ref->force = rm->peer_ref->force;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400934 }
Joey Hess96890f42011-12-26 12:16:56 -0400935
Stefan Bellerbe76c212018-12-06 13:26:55 -0800936 if (recurse_submodules != RECURSE_SUBMODULES_OFF)
937 check_for_new_submodule_commits(&rm->old_oid);
Joey Hess96890f42011-12-26 12:16:56 -0400938
939 if (!strcmp(rm->name, "HEAD")) {
940 kind = "";
941 what = "";
942 }
Christian Couder59556542013-11-30 21:55:40 +0100943 else if (starts_with(rm->name, "refs/heads/")) {
Joey Hess96890f42011-12-26 12:16:56 -0400944 kind = "branch";
945 what = rm->name + 11;
946 }
Christian Couder59556542013-11-30 21:55:40 +0100947 else if (starts_with(rm->name, "refs/tags/")) {
Joey Hess96890f42011-12-26 12:16:56 -0400948 kind = "tag";
949 what = rm->name + 10;
950 }
Christian Couder59556542013-11-30 21:55:40 +0100951 else if (starts_with(rm->name, "refs/remotes/")) {
Joey Hess96890f42011-12-26 12:16:56 -0400952 kind = "remote-tracking branch";
953 what = rm->name + 13;
954 }
955 else {
956 kind = "";
957 what = rm->name;
958 }
959
960 url_len = strlen(url);
961 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
962 ;
963 url_len = i + 1;
964 if (4 < i && !strncmp(".git", url + i - 3, 4))
965 url_len = i - 3;
966
967 strbuf_reset(&note);
968 if (*what) {
969 if (*kind)
970 strbuf_addf(&note, "%s ", kind);
971 strbuf_addf(&note, "'%s' of ", what);
972 }
Jeff King900f2812013-05-11 18:15:59 +0200973 switch (rm->fetch_head_status) {
974 case FETCH_HEAD_NOT_FOR_MERGE:
975 merge_status_marker = "not-for-merge";
976 /* fall-through */
977 case FETCH_HEAD_MERGE:
978 fprintf(fp, "%s\t%s\t%s",
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000979 oid_to_hex(&rm->old_oid),
Jeff King900f2812013-05-11 18:15:59 +0200980 merge_status_marker,
981 note.buf);
982 for (i = 0; i < url_len; ++i)
983 if ('\n' == url[i])
984 fputs("\\n", fp);
985 else
986 fputc(url[i], fp);
987 fputc('\n', fp);
988 break;
989 default:
990 /* do not write anything to FETCH_HEAD */
991 break;
992 }
Joey Hess96890f42011-12-26 12:16:56 -0400993
994 strbuf_reset(&note);
995 if (ref) {
Junio C Hamano901f3d42016-10-21 15:22:55 -0700996 rc |= update_local_ref(ref, what, rm, &note,
997 summary_width);
Joey Hess96890f42011-12-26 12:16:56 -0400998 free(ref);
999 } else
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +02001000 format_display(&note, '*',
1001 *kind ? kind : "branch", NULL,
1002 *what ? what : "HEAD",
Junio C Hamano901f3d42016-10-21 15:22:55 -07001003 "FETCH_HEAD", summary_width);
Joey Hess96890f42011-12-26 12:16:56 -04001004 if (note.len) {
1005 if (verbosity >= 0 && !shown_url) {
1006 fprintf(stderr, _("From %.*s\n"),
1007 url_len, url);
1008 shown_url = 1;
1009 }
1010 if (verbosity >= 0)
1011 fprintf(stderr, " %s\n", note.buf);
1012 }
Nicolas Pitre165f3902007-11-03 01:32:48 -04001013 }
Daniel Barkalowb888d612007-09-10 23:03:25 -04001014 }
Tay Ray Chuan9516a592011-10-07 15:40:22 +08001015
Jeff Kingfa250752009-05-25 06:40:54 -04001016 if (rc & STORE_REF_ERROR_DF_CONFLICT)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001017 error(_("some local refs could not be updated; try running\n"
Jeff Kingf3cb1692008-06-27 00:01:41 -04001018 " 'git remote prune %s' to remove any old, conflicting "
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001019 "branches"), remote_name);
Tay Ray Chuan9516a592011-10-07 15:40:22 +08001020
Derrick Stolee377444b2019-06-18 13:25:27 -07001021 if (advice_fetch_show_forced_updates) {
1022 if (!fetch_show_forced_updates) {
Jean-Noël Avila182f59d2019-08-06 19:19:52 +02001023 warning(_(warn_show_forced_updates));
Derrick Stolee377444b2019-06-18 13:25:27 -07001024 } else if (forced_updates_ms > FORCED_UPDATES_DELAY_WARNING_IN_MS) {
Jean-Noël Avila182f59d2019-08-06 19:19:52 +02001025 warning(_(warn_time_show_forced_updates),
Derrick Stolee377444b2019-06-18 13:25:27 -07001026 forced_updates_ms / 1000.0);
Derrick Stolee377444b2019-06-18 13:25:27 -07001027 }
1028 }
1029
Tay Ray Chuan9516a592011-10-07 15:40:22 +08001030 abort:
Jeff King5914f2d2011-12-08 03:43:19 -05001031 strbuf_release(&note);
Tay Ray Chuan9516a592011-10-07 15:40:22 +08001032 free(url);
1033 fclose(fp);
Dmitry V. Levinefb98b42008-05-28 19:29:36 +04001034 return rc;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001035}
1036
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001037/*
1038 * We would want to bypass the object transfer altogether if
Johan Herlandd9eb0202009-07-10 01:52:30 +02001039 * everything we are going to fetch already exists and is connected
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001040 * locally.
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001041 */
Jonathan Tan35f9e3e2018-09-21 11:22:38 -07001042static int check_exist_and_connected(struct ref *ref_map)
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001043{
Junio C Hamanof0e278b2011-09-02 16:22:47 -07001044 struct ref *rm = ref_map;
Jeff King7043c702016-07-15 06:30:40 -04001045 struct check_connected_options opt = CHECK_CONNECTED_INIT;
Jonathan Tan35f9e3e2018-09-21 11:22:38 -07001046 struct ref *r;
Junio C Hamanof0e278b2011-09-02 16:22:47 -07001047
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001048 /*
1049 * If we are deepening a shallow clone we already have these
1050 * objects reachable. Running rev-list here will return with
1051 * a good (0) exit status and we'll bypass the fetch that we
1052 * really need to perform. Claiming failure now will ensure
1053 * we perform the network exchange to deepen our history.
1054 */
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001055 if (deepen)
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001056 return -1;
Jonathan Tan35f9e3e2018-09-21 11:22:38 -07001057
1058 /*
1059 * check_connected() allows objects to merely be promised, but
1060 * we need all direct targets to exist.
1061 */
1062 for (r = rm; r; r = r->next) {
1063 if (!has_object_file(&r->old_oid))
1064 return -1;
1065 }
1066
Jeff King7043c702016-07-15 06:30:40 -04001067 opt.quiet = 1;
1068 return check_connected(iterate_ref_map, &rm, &opt);
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001069}
1070
Jonathan Tane2842b32018-08-01 13:13:20 -07001071static int fetch_refs(struct transport *transport, struct ref *ref_map)
Daniel Barkalowb888d612007-09-10 23:03:25 -04001072{
Jonathan Tan35f9e3e2018-09-21 11:22:38 -07001073 int ret = check_exist_and_connected(ref_map);
Shawn O. Pearce4191c352007-11-11 02:29:47 -05001074 if (ret)
Jonathan Tane2842b32018-08-01 13:13:20 -07001075 ret = transport_fetch_refs(transport, ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001076 if (!ret)
Brandon Williams05c44222018-06-27 15:30:20 -07001077 /*
1078 * Keep the new pack's ".keep" file around to allow the caller
1079 * time to update refs to reference the new objects.
1080 */
1081 return 0;
1082 transport_unlock_pack(transport);
1083 return ret;
1084}
1085
1086/* Update local refs based on the ref values fetched from a remote */
1087static int consume_refs(struct transport *transport, struct ref *ref_map)
1088{
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001089 int connectivity_checked = transport->smart_options
1090 ? transport->smart_options->connectivity_checked : 0;
Brandon Williams05c44222018-06-27 15:30:20 -07001091 int ret = store_updated_refs(transport->url,
1092 transport->remote->name,
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001093 connectivity_checked,
Brandon Williams05c44222018-06-27 15:30:20 -07001094 ref_map);
Shawn O. Pearce1788c392007-09-14 03:31:23 -04001095 transport_unlock_pack(transport);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001096 return ret;
1097}
1098
Brandon Williamsdef11e72018-05-16 15:58:09 -07001099static int prune_refs(struct refspec *rs, struct ref *ref_map,
1100 const char *raw_url)
Jay Soffianf360d842009-11-10 09:15:47 +01001101{
Tom Miller4b3b33a2014-01-02 20:28:51 -06001102 int url_len, i, result = 0;
Brandon Williamsa2ac50c2018-05-16 15:58:10 -07001103 struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map);
Tom Miller4b3b33a2014-01-02 20:28:51 -06001104 char *url;
Junio C Hamano11fd66d2016-10-21 15:28:07 -07001105 int summary_width = transport_summary_width(stale_refs);
Jay Soffianf360d842009-11-10 09:15:47 +01001106 const char *dangling_msg = dry_run
Nguyễn Thái Ngọc Duy18986d52012-04-23 19:30:25 +07001107 ? _(" (%s will become dangling)")
1108 : _(" (%s has become dangling)");
Jay Soffianf360d842009-11-10 09:15:47 +01001109
Tom Miller4b3b33a2014-01-02 20:28:51 -06001110 if (raw_url)
1111 url = transport_anonymize_url(raw_url);
1112 else
1113 url = xstrdup("foreign");
1114
1115 url_len = strlen(url);
1116 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
1117 ;
1118
1119 url_len = i + 1;
1120 if (4 < i && !strncmp(".git", url + i - 3, 4))
1121 url_len = i - 3;
1122
Michael Haggertya087b432015-06-22 16:02:59 +02001123 if (!dry_run) {
1124 struct string_list refnames = STRING_LIST_INIT_NODUP;
1125
1126 for (ref = stale_refs; ref; ref = ref->next)
1127 string_list_append(&refnames, ref->name);
1128
Michael Haggerty64da4192017-05-22 16:17:38 +02001129 result = delete_refs("fetch: prune", &refnames, 0);
Michael Haggertya087b432015-06-22 16:02:59 +02001130 string_list_clear(&refnames, 0);
1131 }
1132
1133 if (verbosity >= 0) {
1134 for (ref = stale_refs; ref; ref = ref->next) {
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +02001135 struct strbuf sb = STRBUF_INIT;
Michael Haggertya087b432015-06-22 16:02:59 +02001136 if (!shown_url) {
1137 fprintf(stderr, _("From %.*s\n"), url_len, url);
1138 shown_url = 1;
1139 }
Nguyễn Thái Ngọc Duy2cb040b2016-06-26 07:58:08 +02001140 format_display(&sb, '-', _("[deleted]"), NULL,
Junio C Hamano901f3d42016-10-21 15:22:55 -07001141 _("(none)"), prettify_refname(ref->name),
1142 summary_width);
Nguyễn Thái Ngọc Duyd0b39a02016-06-26 07:58:07 +02001143 fprintf(stderr, " %s\n",sb.buf);
1144 strbuf_release(&sb);
Jay Soffianf360d842009-11-10 09:15:47 +01001145 warn_dangling_symref(stderr, dangling_msg, ref->name);
1146 }
1147 }
Michael Haggertya087b432015-06-22 16:02:59 +02001148
Tom Miller4b3b33a2014-01-02 20:28:51 -06001149 free(url);
Jay Soffianf360d842009-11-10 09:15:47 +01001150 free_refs(stale_refs);
1151 return result;
1152}
1153
Johannes Schindelin8ee5d732008-10-13 11:36:52 +02001154static void check_not_current_branch(struct ref *ref_map)
1155{
1156 struct branch *current_branch = branch_get(NULL);
1157
1158 if (is_bare_repository() || !current_branch)
1159 return;
1160
1161 for (; ref_map; ref_map = ref_map->next)
1162 if (ref_map->peer_ref && !strcmp(current_branch->refname,
1163 ref_map->peer_ref->name))
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001164 die(_("Refusing to fetch into current branch %s "
1165 "of non-bare repository"), current_branch->refname);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +02001166}
1167
Junio C Hamanoe6cc5102010-02-24 11:02:05 -08001168static int truncate_fetch_head(void)
1169{
Stefan Beller102de882018-05-17 15:51:51 -07001170 const char *filename = git_path_fetch_head(the_repository);
Johannes Schindelinea565182016-01-11 19:35:54 +01001171 FILE *fp = fopen_for_writing(filename);
Junio C Hamanoe6cc5102010-02-24 11:02:05 -08001172
1173 if (!fp)
Nguyễn Thái Ngọc Duy6da31d72016-05-08 16:47:26 +07001174 return error_errno(_("cannot open %s"), filename);
Junio C Hamanoe6cc5102010-02-24 11:02:05 -08001175 fclose(fp);
1176 return 0;
1177}
1178
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001179static void set_option(struct transport *transport, const char *name, const char *value)
1180{
1181 int r = transport_set_option(transport, name, value);
1182 if (r < 0)
1183 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1184 name, value, transport->url);
1185 if (r > 0)
1186 warning(_("Option \"%s\" is ignored for %s\n"),
1187 name, transport->url);
1188}
1189
Jonathan Tan3390e422018-07-02 15:39:44 -07001190
1191static int add_oid(const char *refname, const struct object_id *oid, int flags,
1192 void *cb_data)
1193{
1194 struct oid_array *oids = cb_data;
1195
1196 oid_array_append(oids, oid);
1197 return 0;
1198}
1199
1200static void add_negotiation_tips(struct git_transport_options *smart_options)
1201{
1202 struct oid_array *oids = xcalloc(1, sizeof(*oids));
1203 int i;
1204
1205 for (i = 0; i < negotiation_tip.nr; i++) {
1206 const char *s = negotiation_tip.items[i].string;
1207 int old_nr;
1208 if (!has_glob_specials(s)) {
1209 struct object_id oid;
1210 if (get_oid(s, &oid))
1211 die("%s is not a valid object", s);
1212 oid_array_append(oids, &oid);
1213 continue;
1214 }
1215 old_nr = oids->nr;
1216 for_each_glob_ref(add_oid, s, oids);
1217 if (old_nr == oids->nr)
1218 warning("Ignoring --negotiation-tip=%s because it does not match any refs",
1219 s);
1220 }
1221 smart_options->negotiation_tips = oids;
1222}
1223
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001224static struct transport *prepare_transport(struct remote *remote, int deepen)
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001225{
1226 struct transport *transport;
Josh Steadmon87c2d9d2019-01-07 16:17:09 -08001227
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001228 transport = transport_get(remote, NULL);
1229 transport_set_verbosity(transport, verbosity, progress);
Eric Wongc915f112016-02-03 04:09:14 +00001230 transport->family = family;
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001231 if (upload_pack)
1232 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
1233 if (keep)
1234 set_option(transport, TRANS_OPT_KEEP, "yes");
1235 if (depth)
1236 set_option(transport, TRANS_OPT_DEPTH, depth);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001237 if (deepen && deepen_since)
1238 set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since);
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001239 if (deepen && deepen_not.nr)
1240 set_option(transport, TRANS_OPT_DEEPEN_NOT,
1241 (const char *)&deepen_not);
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001242 if (deepen_relative)
1243 set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes");
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001244 if (update_shallow)
1245 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001246 if (filter_options.choice) {
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001247 const char *spec =
1248 expand_list_objects_filter_spec(&filter_options);
1249 set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER, spec);
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001250 set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001251 }
Jonathan Tan3390e422018-07-02 15:39:44 -07001252 if (negotiation_tip.nr) {
1253 if (transport->smart_options)
1254 add_negotiation_tips(transport->smart_options);
1255 else
1256 warning("Ignoring --negotiation-tip because the protocol does not support it.");
1257 }
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001258 return transport;
1259}
1260
Junio C Hamano069d5032013-08-07 15:14:45 -07001261static void backfill_tags(struct transport *transport, struct ref *ref_map)
1262{
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001263 int cannot_reuse;
1264
1265 /*
1266 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1267 * when remote helper is used (setting it to an empty string
1268 * is not unsetting). We could extend the remote helper
1269 * protocol for that, but for now, just force a new connection
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001270 * without deepen-since. Similar story for deepen-not.
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001271 */
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001272 cannot_reuse = transport->cannot_reuse ||
1273 deepen_since || deepen_not.nr;
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001274 if (cannot_reuse) {
1275 gsecondary = prepare_transport(transport->remote, 0);
Junio C Hamanob26ed432013-08-07 15:47:18 -07001276 transport = gsecondary;
1277 }
1278
Junio C Hamano069d5032013-08-07 15:14:45 -07001279 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
1280 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001281 transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
Jonathan Tane2842b32018-08-01 13:13:20 -07001282 if (!fetch_refs(transport, ref_map))
Brandon Williams05c44222018-06-27 15:30:20 -07001283 consume_refs(transport, ref_map);
Junio C Hamanob26ed432013-08-07 15:47:18 -07001284
1285 if (gsecondary) {
1286 transport_disconnect(gsecondary);
1287 gsecondary = NULL;
1288 }
Junio C Hamano069d5032013-08-07 15:14:45 -07001289}
1290
Daniel Barkalowb888d612007-09-10 23:03:25 -04001291static int do_fetch(struct transport *transport,
Brandon Williams65a13012018-05-16 15:58:07 -07001292 struct refspec *rs)
Daniel Barkalowb888d612007-09-10 23:03:25 -04001293{
Shawn O. Pearce7f984282008-03-02 21:34:43 -05001294 struct ref *ref_map;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001295 int autotags = (transport->remote->fetch_tags == 1);
Michael Haggerty5b87d8d2013-05-25 11:08:16 +02001296 int retcode = 0;
Brandon Williams6d1700d2018-06-27 15:30:21 -07001297 const struct ref *remote_refs;
1298 struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
Jonathan Tane70a3032018-09-27 12:24:07 -07001299 int must_list_refs = 1;
Julian Phillipsb1a01e12009-10-25 21:28:12 +00001300
Daniel Johnsoned368542010-08-11 18:57:20 -04001301 if (tags == TAGS_DEFAULT) {
1302 if (transport->remote->fetch_tags == 2)
1303 tags = TAGS_SET;
1304 if (transport->remote->fetch_tags == -1)
1305 tags = TAGS_UNSET;
1306 }
Daniel Barkalowb888d612007-09-10 23:03:25 -04001307
Daniel Barkalowb888d612007-09-10 23:03:25 -04001308 /* if not appending, truncate FETCH_HEAD */
Jay Soffian28a15402009-11-10 09:19:43 +01001309 if (!append && !dry_run) {
Michael Haggerty5b87d8d2013-05-25 11:08:16 +02001310 retcode = truncate_fetch_head();
1311 if (retcode)
1312 goto cleanup;
André Goddard Rosad6617c72007-11-22 20:22:23 -02001313 }
Daniel Barkalowb888d612007-09-10 23:03:25 -04001314
Jonathan Tane70a3032018-09-27 12:24:07 -07001315 if (rs->nr) {
1316 int i;
1317
Brandon Williams6d1700d2018-06-27 15:30:21 -07001318 refspec_ref_prefixes(rs, &ref_prefixes);
Jonathan Tane70a3032018-09-27 12:24:07 -07001319
1320 /*
1321 * We can avoid listing refs if all of them are exact
1322 * OIDs
1323 */
1324 must_list_refs = 0;
1325 for (i = 0; i < rs->nr; i++) {
1326 if (!rs->items[i].exact_sha1) {
1327 must_list_refs = 1;
1328 break;
1329 }
1330 }
1331 } else if (transport->remote && transport->remote->fetch.nr)
Brandon Williams6d1700d2018-06-27 15:30:21 -07001332 refspec_ref_prefixes(&transport->remote->fetch, &ref_prefixes);
1333
Jonathan Tane70a3032018-09-27 12:24:07 -07001334 if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
1335 must_list_refs = 1;
1336 if (ref_prefixes.argc)
1337 argv_array_push(&ref_prefixes, "refs/tags/");
Brandon Williams6d1700d2018-06-27 15:30:21 -07001338 }
1339
Jonathan Tane70a3032018-09-27 12:24:07 -07001340 if (must_list_refs)
1341 remote_refs = transport_get_remote_refs(transport, &ref_prefixes);
1342 else
1343 remote_refs = NULL;
1344
Brandon Williams6d1700d2018-06-27 15:30:21 -07001345 argv_array_clear(&ref_prefixes);
1346
1347 ref_map = get_ref_map(transport->remote, remote_refs, rs,
1348 tags, &autotags);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +02001349 if (!update_head_ok)
1350 check_not_current_branch(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001351
Shawn O. Pearce41fa7d22008-03-03 22:27:40 -05001352 if (tags == TAGS_DEFAULT && autotags)
1353 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02001354 if (prune) {
Michael Haggerty0838bf42013-10-30 06:33:00 +01001355 /*
1356 * We only prune based on refspecs specified
1357 * explicitly (via command line or configuration); we
1358 * don't care whether --tags was specified.
1359 */
Brandon Williams65a13012018-05-16 15:58:07 -07001360 if (rs->nr) {
Brandon Williamsdef11e72018-05-16 15:58:09 -07001361 prune_refs(rs, ref_map, transport->url);
Michael Haggertyc5a84e92013-10-30 06:32:59 +01001362 } else {
Brandon Williamsdef11e72018-05-16 15:58:09 -07001363 prune_refs(&transport->remote->fetch,
Tom Miller4b3b33a2014-01-02 20:28:51 -06001364 ref_map,
1365 transport->url);
Carlos Martín Nietoe8c1e6c2011-10-15 07:04:26 +02001366 }
Carlos Martín Nietoed43de62011-10-15 07:04:25 +02001367 }
Jonathan Tane2842b32018-08-01 13:13:20 -07001368 if (fetch_refs(transport, ref_map) || consume_refs(transport, ref_map)) {
Tom Miller10a6cc82014-01-02 20:28:52 -06001369 free_refs(ref_map);
1370 retcode = 1;
1371 goto cleanup;
1372 }
Corentin BOMPARD24bc1a12019-08-19 11:11:20 +02001373
1374 if (set_upstream) {
1375 struct branch *branch = branch_get("HEAD");
1376 struct ref *rm;
1377 struct ref *source_ref = NULL;
1378
1379 /*
1380 * We're setting the upstream configuration for the
1381 * current branch. The relevent upstream is the
1382 * fetched branch that is meant to be merged with the
1383 * current one, i.e. the one fetched to FETCH_HEAD.
1384 *
1385 * When there are several such branches, consider the
1386 * request ambiguous and err on the safe side by doing
1387 * nothing and just emit a warning.
1388 */
1389 for (rm = ref_map; rm; rm = rm->next) {
1390 if (!rm->peer_ref) {
1391 if (source_ref) {
1392 warning(_("multiple branch detected, incompatible with --set-upstream"));
1393 goto skip;
1394 } else {
1395 source_ref = rm;
1396 }
1397 }
1398 }
1399 if (source_ref) {
1400 if (!strcmp(source_ref->name, "HEAD") ||
1401 starts_with(source_ref->name, "refs/heads/"))
1402 install_branch_config(0,
1403 branch->name,
1404 transport->remote->name,
1405 source_ref->name);
1406 else if (starts_with(source_ref->name, "refs/remotes/"))
1407 warning(_("not setting upstream for a remote remote-tracking branch"));
1408 else if (starts_with(source_ref->name, "refs/tags/"))
1409 warning(_("not setting upstream for a remote tag"));
1410 else
1411 warning(_("unknown branch type"));
1412 } else {
1413 warning(_("no source branch found.\n"
1414 "you need to specify exactly one branch with the --set-upstream option."));
1415 }
1416 }
1417 skip:
Shawn O. Pearce7f984282008-03-02 21:34:43 -05001418 free_refs(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001419
1420 /* if neither --no-tags nor --tags was specified, do automated tag
1421 * following ... */
Kristian Høgsberg83201992007-12-04 02:25:47 -05001422 if (tags == TAGS_DEFAULT && autotags) {
Shawn O. Pearcec50b2b42008-03-02 21:35:00 -05001423 struct ref **tail = &ref_map;
1424 ref_map = NULL;
Brandon Williams6d1700d2018-06-27 15:30:21 -07001425 find_non_local_tags(remote_refs, &ref_map, &tail);
Junio C Hamano069d5032013-08-07 15:14:45 -07001426 if (ref_map)
1427 backfill_tags(transport, ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001428 free_refs(ref_map);
1429 }
1430
Michael Haggerty5b87d8d2013-05-25 11:08:16 +02001431 cleanup:
Michael Haggerty5b87d8d2013-05-25 11:08:16 +02001432 return retcode;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001433}
1434
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001435static int get_one_remote_for_fetch(struct remote *remote, void *priv)
Daniel Barkalowb888d612007-09-10 23:03:25 -04001436{
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001437 struct string_list *list = priv;
Björn Gustavsson7cc91a22009-11-09 21:11:06 +01001438 if (!remote->skip_default_update)
Julian Phillips1d2f80f2010-06-26 00:41:38 +01001439 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001440 return 0;
1441}
1442
1443struct remote_group_data {
1444 const char *name;
1445 struct string_list *list;
1446};
1447
1448static int get_remote_group(const char *key, const char *value, void *priv)
1449{
1450 struct remote_group_data *g = priv;
1451
Michael Haggertybc598c32015-07-28 23:08:21 +02001452 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001453 /* split list by white space */
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001454 while (*value) {
Michael Haggerty5f654992015-07-28 23:08:20 +02001455 size_t wordlen = strcspn(value, " \t\n");
1456
Michael Haggertye2865422015-07-28 23:08:19 +02001457 if (wordlen >= 1)
Keith McGuiganb7410f62016-06-14 14:28:56 -04001458 string_list_append_nodup(g->list,
Michael Haggertye2865422015-07-28 23:08:19 +02001459 xstrndup(value, wordlen));
1460 value += wordlen + (value[wordlen] != '\0');
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001461 }
1462 }
1463
1464 return 0;
1465}
1466
1467static int add_remote_or_group(const char *name, struct string_list *list)
1468{
1469 int prev_nr = list->nr;
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +00001470 struct remote_group_data g;
1471 g.name = name; g.list = list;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001472
1473 git_config(get_remote_group, &g);
1474 if (list->nr == prev_nr) {
Thomas Gummerer674468b2016-02-16 10:47:50 +01001475 struct remote *remote = remote_get(name);
Johannes Schindeline459b072017-01-19 22:20:02 +01001476 if (!remote_is_configured(remote, 0))
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001477 return 0;
Julian Phillips1d2f80f2010-06-26 00:41:38 +01001478 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001479 }
1480 return 1;
1481}
1482
Jeff King85556d42012-09-01 07:27:35 -04001483static void add_options_to_argv(struct argv_array *argv)
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001484{
1485 if (dry_run)
Jeff King85556d42012-09-01 07:27:35 -04001486 argv_array_push(argv, "--dry-run");
Michael Haggerty90765fa2013-10-30 06:33:04 +01001487 if (prune != -1)
1488 argv_array_push(argv, prune ? "--prune" : "--no-prune");
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +00001489 if (prune_tags != -1)
1490 argv_array_push(argv, prune_tags ? "--prune-tags" : "--no-prune-tags");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001491 if (update_head_ok)
Jeff King85556d42012-09-01 07:27:35 -04001492 argv_array_push(argv, "--update-head-ok");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001493 if (force)
Jeff King85556d42012-09-01 07:27:35 -04001494 argv_array_push(argv, "--force");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001495 if (keep)
Jeff King85556d42012-09-01 07:27:35 -04001496 argv_array_push(argv, "--keep");
Jens Lehmannbe254a02010-11-11 00:55:02 +01001497 if (recurse_submodules == RECURSE_SUBMODULES_ON)
Jeff King85556d42012-09-01 07:27:35 -04001498 argv_array_push(argv, "--recurse-submodules");
Jens Lehmann8f0700d2011-03-06 23:11:21 +01001499 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
Jeff King85556d42012-09-01 07:27:35 -04001500 argv_array_push(argv, "--recurse-submodules=on-demand");
Dan Johnson85566462012-09-05 17:22:19 -04001501 if (tags == TAGS_SET)
1502 argv_array_push(argv, "--tags");
1503 else if (tags == TAGS_UNSET)
1504 argv_array_push(argv, "--no-tags");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001505 if (verbosity >= 2)
Jeff King85556d42012-09-01 07:27:35 -04001506 argv_array_push(argv, "-v");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001507 if (verbosity >= 1)
Jeff King85556d42012-09-01 07:27:35 -04001508 argv_array_push(argv, "-v");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001509 else if (verbosity < 0)
Jeff King85556d42012-09-01 07:27:35 -04001510 argv_array_push(argv, "-q");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001511
1512}
1513
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001514static int fetch_multiple(struct string_list *list)
1515{
1516 int i, result = 0;
Jeff King85556d42012-09-01 07:27:35 -04001517 struct argv_array argv = ARGV_ARRAY_INIT;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001518
Junio C Hamanoe6cc5102010-02-24 11:02:05 -08001519 if (!append && !dry_run) {
1520 int errcode = truncate_fetch_head();
1521 if (errcode)
1522 return errcode;
1523 }
1524
Nguyễn Thái Ngọc Duyc3d6b702019-06-19 16:46:30 +07001525 argv_array_pushl(&argv, "fetch", "--append", "--no-auto-gc", NULL);
Jeff King85556d42012-09-01 07:27:35 -04001526 add_options_to_argv(&argv);
1527
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001528 for (i = 0; i < list->nr; i++) {
1529 const char *name = list->items[i].string;
Jeff King85556d42012-09-01 07:27:35 -04001530 argv_array_push(&argv, name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001531 if (verbosity >= 0)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001532 printf(_("Fetching %s\n"), name);
Jeff King85556d42012-09-01 07:27:35 -04001533 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001534 error(_("Could not fetch %s"), name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001535 result = 1;
1536 }
Jeff King85556d42012-09-01 07:27:35 -04001537 argv_array_pop(&argv);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001538 }
1539
Jeff King85556d42012-09-01 07:27:35 -04001540 argv_array_clear(&argv);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001541 return result;
1542}
1543
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001544/*
1545 * Fetching from the promisor remote should use the given filter-spec
1546 * or inherit the default filter-spec from the config.
1547 */
1548static inline void fetch_one_setup_partial(struct remote *remote)
1549{
1550 /*
1551 * Explicit --no-filter argument overrides everything, regardless
1552 * of any prior partial clones and fetches.
1553 */
1554 if (filter_options.no_filter)
1555 return;
1556
1557 /*
1558 * If no prior partial clone/fetch and the current fetch DID NOT
1559 * request a partial-fetch, do a normal fetch.
1560 */
Christian Couderb14ed5a2019-06-25 15:40:31 +02001561 if (!has_promisor_remote() && !filter_options.choice)
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001562 return;
1563
1564 /*
Christian Couder5e461392019-06-25 15:40:33 +02001565 * If this is a partial-fetch request, we enable partial on
1566 * this repo if not already enabled and remember the given
1567 * filter-spec as the default for subsequent fetches to this
1568 * remote.
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001569 */
Christian Couder5e461392019-06-25 15:40:33 +02001570 if (filter_options.choice) {
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001571 partial_clone_register(remote->name, &filter_options);
1572 return;
1573 }
1574
1575 /*
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001576 * Do a partial-fetch from the promisor remote using either the
1577 * explicitly given filter-spec or inherit the filter-spec from
1578 * the config.
1579 */
1580 if (!filter_options.choice)
Christian Couderfa3d1b62019-06-25 15:40:32 +02001581 partial_clone_get_default_filter_spec(&filter_options, remote->name);
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001582 return;
1583}
1584
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +00001585static int fetch_one(struct remote *remote, int argc, const char **argv, int prune_tags_ok)
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001586{
Brandon Williamsd7c8e302018-05-16 15:58:04 -07001587 struct refspec rs = REFSPEC_INIT_FETCH;
1588 int i;
Alex Riesen7b7f39e2008-04-28 22:23:35 +02001589 int exit_code;
Ævar Arnfjörð Bjarmason63179722018-02-09 20:32:16 +00001590 int maybe_prune_tags;
1591 int remote_via_config = remote_is_configured(remote, 0);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001592
Daniel Barkalowfa685bd2009-03-11 01:47:20 -04001593 if (!remote)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001594 die(_("No remote repository specified. Please, specify either a URL or a\n"
1595 "remote name from which new revisions should be fetched."));
Daniel Barkalowfa685bd2009-03-11 01:47:20 -04001596
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001597 gtransport = prepare_transport(remote, 1);
Michael Schubert737c5a92013-07-13 11:36:24 +02001598
1599 if (prune < 0) {
1600 /* no command line request */
Ævar Arnfjörð Bjarmason07118832018-02-09 20:32:02 +00001601 if (0 <= remote->prune)
1602 prune = remote->prune;
Michael Schubert737c5a92013-07-13 11:36:24 +02001603 else if (0 <= fetch_prune_config)
1604 prune = fetch_prune_config;
1605 else
1606 prune = PRUNE_BY_DEFAULT;
1607 }
1608
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +00001609 if (prune_tags < 0) {
1610 /* no command line request */
1611 if (0 <= remote->prune_tags)
1612 prune_tags = remote->prune_tags;
1613 else if (0 <= fetch_prune_tags_config)
1614 prune_tags = fetch_prune_tags_config;
1615 else
1616 prune_tags = PRUNE_TAGS_BY_DEFAULT;
1617 }
1618
Ævar Arnfjörð Bjarmason63179722018-02-09 20:32:16 +00001619 maybe_prune_tags = prune_tags_ok && prune_tags;
1620 if (maybe_prune_tags && remote_via_config)
Brandon Williams95303502018-05-16 15:58:02 -07001621 refspec_append(&remote->fetch, TAG_REFSPEC);
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +00001622
Brandon Williamsd7c8e302018-05-16 15:58:04 -07001623 if (maybe_prune_tags && (argc || !remote_via_config))
1624 refspec_append(&rs, TAG_REFSPEC);
Ævar Arnfjörð Bjarmason63179722018-02-09 20:32:16 +00001625
Brandon Williamsd7c8e302018-05-16 15:58:04 -07001626 for (i = 0; i < argc; i++) {
1627 if (!strcmp(argv[i], "tag")) {
1628 char *tag;
1629 i++;
1630 if (i >= argc)
1631 die(_("You need to specify a tag name."));
1632
1633 tag = xstrfmt("refs/tags/%s:refs/tags/%s",
1634 argv[i], argv[i]);
1635 refspec_append(&rs, tag);
1636 free(tag);
1637 } else {
1638 refspec_append(&rs, argv[i]);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001639 }
Daniel Barkalowb888d612007-09-10 23:03:25 -04001640 }
1641
Brandon Williams5e3548e2018-04-23 15:46:24 -07001642 if (server_options.nr)
1643 gtransport->server_options = &server_options;
1644
Jeff King57b235a2009-01-22 01:03:08 -05001645 sigchain_push_common(unlock_pack_on_signal);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -04001646 atexit(unlock_pack);
Jeff King14358892019-03-03 11:58:43 -05001647 sigchain_push(SIGPIPE, SIG_IGN);
Brandon Williams65a13012018-05-16 15:58:07 -07001648 exit_code = do_fetch(gtransport, &rs);
Jeff King14358892019-03-03 11:58:43 -05001649 sigchain_pop(SIGPIPE);
Brandon Williamsd7c8e302018-05-16 15:58:04 -07001650 refspec_clear(&rs);
Junio C Hamanoaf234452013-08-07 15:38:45 -07001651 transport_disconnect(gtransport);
1652 gtransport = NULL;
Alex Riesen7b7f39e2008-04-28 22:23:35 +02001653 return exit_code;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001654}
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001655
1656int cmd_fetch(int argc, const char **argv, const char *prefix)
1657{
1658 int i;
Keith McGuiganb7410f62016-06-14 14:28:56 -04001659 struct string_list list = STRING_LIST_INIT_DUP;
Jonathan Tana1743342017-12-08 15:58:43 +00001660 struct remote *remote = NULL;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001661 int result = 0;
Junio C Hamanoc1a79022018-03-06 14:54:01 -08001662 int prune_tags_ok = 1;
Nguyễn Thái Ngọc Duy19910062014-08-16 08:19:27 +07001663 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001664
Jeff Kingbbc30f92011-02-24 09:30:19 -05001665 packet_trace_identity("fetch");
1666
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001667 fetch_if_missing = 0;
1668
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001669 /* Record the command line for the reflog */
1670 strbuf_addstr(&default_rla, "fetch");
1671 for (i = 1; i < argc; i++)
1672 strbuf_addf(&default_rla, " %s", argv[i]);
1673
Antonio Ospite71a69532018-06-26 12:47:06 +02001674 fetch_config_from_gitmodules(&max_children, &recurse_submodules);
Michael Schubert737c5a92013-07-13 11:36:24 +02001675 git_config(git_fetch_config, NULL);
1676
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001677 argc = parse_options(argc, argv, prefix,
1678 builtin_fetch_options, builtin_fetch_usage, 0);
1679
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001680 if (deepen_relative) {
1681 if (deepen_relative < 0)
1682 die(_("Negative depth in --deepen is not supported"));
1683 if (depth)
1684 die(_("--deepen and --depth are mutually exclusive"));
1685 depth = xstrfmt("%d", deepen_relative);
1686 }
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +07001687 if (unshallow) {
1688 if (depth)
1689 die(_("--depth and --unshallow cannot be used together"));
Stefan Bellerc8813482018-05-17 15:51:46 -07001690 else if (!is_repository_shallow(the_repository))
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +07001691 die(_("--unshallow on a complete repository does not make sense"));
Jeff King2805bb52015-09-24 17:07:07 -04001692 else
1693 depth = xstrfmt("%d", INFINITE_DEPTH);
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +07001694 }
1695
Nguyễn Thái Ngọc Duy5594bca2013-12-05 10:31:11 +07001696 /* no need to be strict, transport_set_option() will validate it again */
1697 if (depth && atoi(depth) < 1)
1698 die(_("depth %s is not a positive number"), depth);
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001699 if (depth || deepen_since || deepen_not.nr)
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001700 deepen = 1;
Nguyễn Thái Ngọc Duy5594bca2013-12-05 10:31:11 +07001701
Christian Couderb14ed5a2019-06-25 15:40:31 +02001702 if (filter_options.choice && !has_promisor_remote())
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001703 die("--filter can only be used when extensions.partialClone is set");
1704
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001705 if (all) {
1706 if (argc == 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001707 die(_("fetch --all does not take a repository argument"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001708 else if (argc > 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001709 die(_("fetch --all does not make sense with refspecs"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001710 (void) for_each_remote(get_one_remote_for_fetch, &list);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001711 } else if (argc == 0) {
1712 /* No arguments -- use default remote */
1713 remote = remote_get(NULL);
Björn Gustavsson16679e32009-11-09 21:10:32 +01001714 } else if (multiple) {
1715 /* All arguments are assumed to be remotes or groups */
1716 for (i = 0; i < argc; i++)
1717 if (!add_remote_or_group(argv[i], &list))
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001718 die(_("No such remote or remote group: %s"), argv[i]);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001719 } else {
1720 /* Single remote or group */
1721 (void) add_remote_or_group(argv[0], &list);
1722 if (list.nr > 1) {
1723 /* More than one remote */
1724 if (argc > 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001725 die(_("Fetching a group and specifying refspecs does not make sense"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001726 } else {
1727 /* Zero or one remotes */
1728 remote = remote_get(argv[0]);
Junio C Hamanoc1a79022018-03-06 14:54:01 -08001729 prune_tags_ok = (argc == 1);
Jonathan Tana1743342017-12-08 15:58:43 +00001730 argc--;
1731 argv++;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001732 }
1733 }
1734
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001735 if (remote) {
Christian Couderb14ed5a2019-06-25 15:40:31 +02001736 if (filter_options.choice || has_promisor_remote())
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001737 fetch_one_setup_partial(remote);
Junio C Hamanoc1a79022018-03-06 14:54:01 -08001738 result = fetch_one(remote, argc, argv, prune_tags_ok);
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001739 } else {
1740 if (filter_options.choice)
Christian Coudere0137872019-01-13 09:52:19 +01001741 die(_("--filter can only be used with the remote "
1742 "configured in extensions.partialclone"));
Jeff Hostetleraa57b872017-12-08 15:58:50 +00001743 /* TODO should this also die if we have a previous partial-clone? */
Jonathan Tana1743342017-12-08 15:58:43 +00001744 result = fetch_multiple(&list);
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001745 }
Jonathan Tana1743342017-12-08 15:58:43 +00001746
Jens Lehmannbe254a02010-11-11 00:55:02 +01001747 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
Jeff King85556d42012-09-01 07:27:35 -04001748 struct argv_array options = ARGV_ARRAY_INIT;
1749
1750 add_options_to_argv(&options);
Brandon Williamse7241972017-12-12 11:53:52 -08001751 result = fetch_populated_submodules(the_repository,
1752 &options,
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001753 submodule_prefix,
Jens Lehmann8f0700d2011-03-06 23:11:21 +01001754 recurse_submodules,
Brandon Williams8fa29152017-08-02 12:49:19 -07001755 recurse_submodules_default,
Stefan Beller62104ba2015-12-15 16:04:12 -08001756 verbosity < 0,
1757 max_children);
Jeff King85556d42012-09-01 07:27:35 -04001758 argv_array_clear(&options);
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001759 }
1760
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001761 string_list_clear(&list, 0);
1762
Derrick Stolee50f26bd2019-09-02 19:22:02 -07001763 prepare_repo_settings(the_repository);
1764 if (the_repository->settings.fetch_write_commit_graph) {
Junio C Hamano5a535092019-09-30 13:19:32 +09001765 int commit_graph_flags = COMMIT_GRAPH_WRITE_SPLIT;
Derrick Stolee50f26bd2019-09-02 19:22:02 -07001766 struct split_commit_graph_opts split_opts;
1767 memset(&split_opts, 0, sizeof(struct split_commit_graph_opts));
1768
1769 if (progress)
Junio C Hamano5a535092019-09-30 13:19:32 +09001770 commit_graph_flags |= COMMIT_GRAPH_WRITE_PROGRESS;
Derrick Stolee50f26bd2019-09-02 19:22:02 -07001771
1772 write_commit_graph_reachable(get_object_directory(),
1773 commit_graph_flags,
1774 &split_opts);
1775 }
1776
Derrick Stolee2d511cf2019-05-17 11:41:49 -07001777 close_object_store(the_repository->objects);
Johannes Schindelin0898c962016-01-13 18:20:11 +01001778
Nguyễn Thái Ngọc Duyc3d6b702019-06-19 16:46:30 +07001779 if (enable_auto_gc) {
1780 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
1781 if (verbosity < 0)
1782 argv_array_push(&argv_gc_auto, "--quiet");
1783 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1784 argv_array_clear(&argv_gc_auto);
1785 }
Jeff King131b8fc2013-01-26 17:40:38 -05001786
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001787 return result;
1788}