blob: e4639d8eb1d5fda586520f10271c05a0897f2ea5 [file] [log] [blame]
Daniel Barkalowb888d612007-09-10 23:03:25 -04001/*
2 * "git fetch"
3 */
4#include "cache.h"
5#include "refs.h"
6#include "commit.h"
7#include "builtin.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01008#include "string-list.h"
Daniel Barkalowb888d612007-09-10 23:03:25 -04009#include "remote.h"
10#include "transport.h"
Shawn O. Pearce4191c352007-11-11 02:29:47 -050011#include "run-command.h"
Kristian Høgsberg83201992007-12-04 02:25:47 -050012#include "parse-options.h"
Jeff King4a16d072009-01-22 01:02:35 -050013#include "sigchain.h"
Heiko Voigt027771f2015-08-17 17:22:00 -070014#include "submodule-config.h"
Jens Lehmann7dce19d2010-11-12 13:54:52 +010015#include "submodule.h"
Junio C Hamanof96400c2011-09-02 16:33:22 -070016#include "connected.h"
Jeff King85556d42012-09-01 07:27:35 -040017#include "argv-array.h"
Daniel Barkalowb888d612007-09-10 23:03:25 -040018
Kristian Høgsberg83201992007-12-04 02:25:47 -050019static const char * const builtin_fetch_usage[] = {
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070020 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
21 N_("git fetch [<options>] <group>"),
22 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
23 N_("git fetch --all [<options>]"),
Kristian Høgsberg83201992007-12-04 02:25:47 -050024 NULL
25};
Daniel Barkalowb888d612007-09-10 23:03:25 -040026
Kristian Høgsberg83201992007-12-04 02:25:47 -050027enum {
28 TAGS_UNSET = 0,
29 TAGS_DEFAULT = 1,
30 TAGS_SET = 2
31};
32
Michael Schubert737c5a92013-07-13 11:36:24 +020033static int fetch_prune_config = -1; /* unspecified */
34static int prune = -1; /* unspecified */
35#define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
36
37static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity;
Clemens Buchacher01fdc212012-02-13 21:17:15 +010038static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +070039static int tags = TAGS_DEFAULT, unshallow, update_shallow;
Stefan Beller62104ba2015-12-15 16:04:12 -080040static int max_children = 1;
Eric Wongc915f112016-02-03 04:09:14 +000041static enum transport_family family;
Shawn O. Pearce4191c352007-11-11 02:29:47 -050042static const char *depth;
Kristian Høgsberg83201992007-12-04 02:25:47 -050043static const char *upload_pack;
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -050044static struct strbuf default_rla = STRBUF_INIT;
Junio C Hamanoaf234452013-08-07 15:38:45 -070045static struct transport *gtransport;
Junio C Hamanob26ed432013-08-07 15:47:18 -070046static struct transport *gsecondary;
Jens Lehmann7dce19d2010-11-12 13:54:52 +010047static const char *submodule_prefix = "";
Jens Lehmann88a21972011-03-06 23:10:46 +010048static const char *recurse_submodules_default;
Tom Miller4b3b33a2014-01-02 20:28:51 -060049static int shown_url = 0;
Junio C Hamanoc5558f82014-05-29 15:21:31 -070050static int refmap_alloc, refmap_nr;
51static const char **refmap_array;
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -040052
Jens Lehmann8f0700d2011-03-06 23:11:21 +010053static int option_parse_recurse_submodules(const struct option *opt,
54 const char *arg, int unset)
55{
56 if (unset) {
57 recurse_submodules = RECURSE_SUBMODULES_OFF;
58 } else {
59 if (arg)
60 recurse_submodules = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
61 else
62 recurse_submodules = RECURSE_SUBMODULES_ON;
63 }
64 return 0;
65}
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -040066
Michael Schubert737c5a92013-07-13 11:36:24 +020067static int git_fetch_config(const char *k, const char *v, void *cb)
68{
69 if (!strcmp(k, "fetch.prune")) {
70 fetch_prune_config = git_config_bool(k, v);
71 return 0;
72 }
Jeff King72549df2014-11-04 08:11:19 -050073 return git_default_config(k, v, cb);
Michael Schubert737c5a92013-07-13 11:36:24 +020074}
75
Junio C Hamanoc5558f82014-05-29 15:21:31 -070076static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
77{
78 ALLOC_GROW(refmap_array, refmap_nr + 1, refmap_alloc);
79
80 /*
81 * "git fetch --refmap='' origin foo"
82 * can be used to tell the command not to store anywhere
83 */
84 if (*arg)
85 refmap_array[refmap_nr++] = arg;
86 return 0;
87}
88
Kristian Høgsberg83201992007-12-04 02:25:47 -050089static struct option builtin_fetch_options[] = {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +010090 OPT__VERBOSITY(&verbosity),
Stefan Bellerd5d09d42013-08-03 13:51:19 +020091 OPT_BOOL(0, "all", &all,
92 N_("fetch from all remotes")),
93 OPT_BOOL('a', "append", &append,
94 N_("append to .git/FETCH_HEAD instead of overwriting")),
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070095 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
96 N_("path to upload pack on remote end")),
97 OPT__FORCE(&force, N_("force overwrite of local branch")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +020098 OPT_BOOL('m', "multiple", &multiple,
99 N_("fetch from multiple remotes")),
Kristian Høgsberg83201992007-12-04 02:25:47 -0500100 OPT_SET_INT('t', "tags", &tags,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700101 N_("fetch all tags and associated objects"), TAGS_SET),
Johannes Schindeline7951292008-03-13 08:13:15 +0100102 OPT_SET_INT('n', NULL, &tags,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700103 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
Stefan Beller62104ba2015-12-15 16:04:12 -0800104 OPT_INTEGER('j', "jobs", &max_children,
105 N_("number of submodules fetched in parallel")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200106 OPT_BOOL('p', "prune", &prune,
107 N_("prune remote-tracking branches no longer on remote")),
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700108 { OPTION_CALLBACK, 0, "recurse-submodules", NULL, N_("on-demand"),
109 N_("control recursive fetching of submodules"),
Jens Lehmann8f0700d2011-03-06 23:11:21 +0100110 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200111 OPT_BOOL(0, "dry-run", &dry_run,
112 N_("dry run")),
113 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
114 OPT_BOOL('u', "update-head-ok", &update_head_ok,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700115 N_("allow updating of HEAD ref")),
116 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
117 OPT_STRING(0, "depth", &depth, N_("depth"),
118 N_("deepen history of shallow clone")),
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +0700119 { OPTION_SET_INT, 0, "unshallow", &unshallow, NULL,
120 N_("convert to a complete repository"),
121 PARSE_OPT_NONEG | PARSE_OPT_NOARG, NULL, 1 },
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700122 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
123 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
Jens Lehmann88a21972011-03-06 23:10:46 +0100124 { OPTION_STRING, 0, "recurse-submodules-default",
125 &recurse_submodules_default, NULL,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700126 N_("default mode for recursion"), PARSE_OPT_HIDDEN },
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +0700127 OPT_BOOL(0, "update-shallow", &update_shallow,
128 N_("accept refs that update .git/shallow")),
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700129 { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"),
130 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg },
Eric Wongc915f112016-02-03 04:09:14 +0000131 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
132 TRANSPORT_FAMILY_IPV4),
133 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
134 TRANSPORT_FAMILY_IPV6),
Kristian Høgsberg83201992007-12-04 02:25:47 -0500135 OPT_END()
136};
137
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400138static void unlock_pack(void)
139{
Junio C Hamanoaf234452013-08-07 15:38:45 -0700140 if (gtransport)
141 transport_unlock_pack(gtransport);
Junio C Hamanob26ed432013-08-07 15:47:18 -0700142 if (gsecondary)
143 transport_unlock_pack(gsecondary);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400144}
145
146static void unlock_pack_on_signal(int signo)
147{
148 unlock_pack();
Jeff King4a16d072009-01-22 01:02:35 -0500149 sigchain_pop(signo);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400150 raise(signo);
151}
Daniel Barkalowb888d612007-09-10 23:03:25 -0400152
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400153static void add_merge_config(struct ref **head,
Daniel Barkalow45773702007-10-29 21:05:40 -0400154 const struct ref *remote_refs,
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400155 struct branch *branch,
156 struct ref ***tail)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400157{
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400158 int i;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400159
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400160 for (i = 0; i < branch->merge_nr; i++) {
161 struct ref *rm, **old_tail = *tail;
162 struct refspec refspec;
163
164 for (rm = *head; rm; rm = rm->next) {
165 if (branch_merge_matches(branch, i, rm->name)) {
Jeff King900f2812013-05-11 18:15:59 +0200166 rm->fetch_head_status = FETCH_HEAD_MERGE;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400167 break;
168 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400169 }
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400170 if (rm)
171 continue;
172
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700173 /*
Matthieu Moy8b3f3f82010-11-02 16:31:23 +0100174 * Not fetched to a remote-tracking branch? We need to fetch
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400175 * it anyway to allow this branch's "branch.$name.merge"
Heikki Orsila05207a22008-09-09 13:28:30 +0300176 * to be honored by 'git pull', but we do not have to
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700177 * fail if branch.$name.merge is misconfigured to point
178 * at a nonexisting branch. If we were indeed called by
Heikki Orsila05207a22008-09-09 13:28:30 +0300179 * 'git pull', it will notice the misconfiguration because
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700180 * there is no entry in the resulting FETCH_HEAD marked
181 * for merging.
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400182 */
Andreas Gruenbacher8da61a22010-03-12 23:27:33 +0100183 memset(&refspec, 0, sizeof(refspec));
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400184 refspec.src = branch->merge[i]->src;
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700185 get_fetch_map(remote_refs, &refspec, tail, 1);
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400186 for (rm = *old_tail; rm; rm = rm->next)
Jeff King900f2812013-05-11 18:15:59 +0200187 rm->fetch_head_status = FETCH_HEAD_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400188 }
189}
190
Michael Haggerty0e0b7de2015-05-25 18:38:35 +0000191static int add_existing(const char *refname, const struct object_id *oid,
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100192 int flag, void *cbdata)
193{
194 struct string_list *list = (struct string_list *)cbdata;
195 struct string_list_item *item = string_list_insert(list, refname);
Michael Haggerty0e0b7de2015-05-25 18:38:35 +0000196 struct object_id *old_oid = xmalloc(sizeof(*old_oid));
197
198 oidcpy(old_oid, oid);
199 item->util = old_oid;
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100200 return 0;
201}
202
203static int will_fetch(struct ref **head, const unsigned char *sha1)
204{
205 struct ref *rm = *head;
206 while (rm) {
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000207 if (!hashcmp(rm->old_oid.hash, sha1))
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100208 return 1;
209 rm = rm->next;
210 }
211 return 0;
212}
213
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500214static void find_non_local_tags(struct transport *transport,
215 struct ref **head,
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100216 struct ref ***tail)
217{
218 struct string_list existing_refs = STRING_LIST_INIT_DUP;
219 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
220 const struct ref *ref;
221 struct string_list_item *item = NULL;
222
Michael Haggerty0e0b7de2015-05-25 18:38:35 +0000223 for_each_ref(add_existing, &existing_refs);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100224 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
Junio C Hamanoad704482013-12-17 11:47:35 -0800225 if (!starts_with(ref->name, "refs/tags/"))
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100226 continue;
227
228 /*
229 * The peeled ref always follows the matching base
230 * ref, so if we see a peeled ref that we don't want
231 * to fetch then we can mark the ref entry in the list
232 * as one to ignore by setting util to NULL.
233 */
Junio C Hamanoad704482013-12-17 11:47:35 -0800234 if (ends_with(ref->name, "^{}")) {
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000235 if (item && !has_object_file(&ref->old_oid) &&
236 !will_fetch(head, ref->old_oid.hash) &&
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100237 !has_sha1_file(item->util) &&
238 !will_fetch(head, item->util))
239 item->util = NULL;
240 item = NULL;
241 continue;
242 }
243
244 /*
245 * If item is non-NULL here, then we previously saw a
246 * ref not followed by a peeled reference, so we need
247 * to check if it is a lightweight tag that we want to
248 * fetch.
249 */
250 if (item && !has_sha1_file(item->util) &&
251 !will_fetch(head, item->util))
252 item->util = NULL;
253
254 item = NULL;
255
256 /* skip duplicates and refs that we already have */
257 if (string_list_has_string(&remote_refs, ref->name) ||
258 string_list_has_string(&existing_refs, ref->name))
259 continue;
260
261 item = string_list_insert(&remote_refs, ref->name);
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000262 item->util = (void *)&ref->old_oid;
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100263 }
264 string_list_clear(&existing_refs, 1);
265
266 /*
267 * We may have a final lightweight tag that needs to be
268 * checked to see if it needs fetching.
269 */
270 if (item && !has_sha1_file(item->util) &&
271 !will_fetch(head, item->util))
272 item->util = NULL;
273
274 /*
275 * For all the tags in the remote_refs string list,
276 * add them to the list of refs to be fetched
277 */
278 for_each_string_list_item(item, &remote_refs) {
279 /* Unless we have already decided to ignore this item... */
280 if (item->util)
281 {
282 struct ref *rm = alloc_ref(item->string);
283 rm->peer_ref = alloc_ref(item->string);
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000284 oidcpy(&rm->old_oid, item->util);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100285 **tail = rm;
286 *tail = &rm->next;
287 }
288 }
289
290 string_list_clear(&remote_refs, 0);
291}
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500292
Daniel Barkalowb888d612007-09-10 23:03:25 -0400293static struct ref *get_ref_map(struct transport *transport,
Michael Haggertyf137a452013-10-23 17:50:38 +0200294 struct refspec *refspecs, int refspec_count,
295 int tags, int *autotags)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400296{
297 int i;
298 struct ref *rm;
299 struct ref *ref_map = NULL;
300 struct ref **tail = &ref_map;
301
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100302 /* opportunistically-updated references: */
303 struct ref *orefs = NULL, **oref_tail = &orefs;
304
Daniel Barkalow45773702007-10-29 21:05:40 -0400305 const struct ref *remote_refs = transport_get_remote_refs(transport);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400306
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100307 if (refspec_count) {
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700308 struct refspec *fetch_refspec;
309 int fetch_refspec_nr;
310
Michael Haggertyf137a452013-10-23 17:50:38 +0200311 for (i = 0; i < refspec_count; i++) {
312 get_fetch_map(remote_refs, &refspecs[i], &tail, 0);
313 if (refspecs[i].dst && refspecs[i].dst[0])
Daniel Barkalowb888d612007-09-10 23:03:25 -0400314 *autotags = 1;
315 }
Michael Haggerty0281c932013-10-30 06:32:58 +0100316 /* Merge everything on the command line (but not --tags) */
Daniel Barkalowb888d612007-09-10 23:03:25 -0400317 for (rm = ref_map; rm; rm = rm->next)
Jeff King900f2812013-05-11 18:15:59 +0200318 rm->fetch_head_status = FETCH_HEAD_MERGE;
Michael Haggerty0281c932013-10-30 06:32:58 +0100319
320 /*
321 * For any refs that we happen to be fetching via
322 * command-line arguments, the destination ref might
323 * have been missing or have been different than the
324 * remote-tracking ref that would be derived from the
325 * configured refspec. In these cases, we want to
326 * take the opportunity to update their configured
327 * remote-tracking reference. However, we do not want
328 * to mention these entries in FETCH_HEAD at all, as
329 * they would simply be duplicates of existing
330 * entries, so we set them FETCH_HEAD_IGNORE below.
331 *
332 * We compute these entries now, based only on the
333 * refspecs specified on the command line. But we add
334 * them to the list following the refspecs resulting
335 * from the tags option so that one of the latter,
336 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
337 * by ref_remove_duplicates() in favor of one of these
338 * opportunistic entries with FETCH_HEAD_IGNORE.
339 */
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700340 if (refmap_array) {
341 fetch_refspec = parse_fetch_refspec(refmap_nr, refmap_array);
342 fetch_refspec_nr = refmap_nr;
343 } else {
344 fetch_refspec = transport->remote->fetch;
345 fetch_refspec_nr = transport->remote->fetch_refspec_nr;
346 }
347
348 for (i = 0; i < fetch_refspec_nr; i++)
349 get_fetch_map(ref_map, &fetch_refspec[i], &oref_tail, 1);
Michael Haggerty0281c932013-10-30 06:32:58 +0100350
Daniel Barkalowe0aaa292008-04-17 19:32:35 -0400351 if (tags == TAGS_SET)
352 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700353 } else if (refmap_array) {
354 die("--refmap option is only meaningful with command-line refspec(s).");
Daniel Barkalowb888d612007-09-10 23:03:25 -0400355 } else {
356 /* Use the defaults */
357 struct remote *remote = transport->remote;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400358 struct branch *branch = branch_get(NULL);
359 int has_merge = branch_has_merge_config(branch);
Brandon Casey3ee17572010-08-25 12:52:56 -0500360 if (remote &&
361 (remote->fetch_refspec_nr ||
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500362 /* Note: has_merge implies non-NULL branch->remote_name */
Brandon Casey3ee17572010-08-25 12:52:56 -0500363 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400364 for (i = 0; i < remote->fetch_refspec_nr; i++) {
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700365 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400366 if (remote->fetch[i].dst &&
367 remote->fetch[i].dst[0])
368 *autotags = 1;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400369 if (!i && !has_merge && ref_map &&
Daniel Barkalowcfb8f892007-09-28 19:34:17 -0400370 !remote->fetch[0].pattern)
Jeff King900f2812013-05-11 18:15:59 +0200371 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400372 }
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100373 /*
374 * if the remote we're fetching from is the same
375 * as given in branch.<name>.remote, we add the
376 * ref given in branch.<name>.merge, too.
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500377 *
378 * Note: has_merge implies non-NULL branch->remote_name
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100379 */
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700380 if (has_merge &&
381 !strcmp(branch->remote_name, remote->name))
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400382 add_merge_config(&ref_map, remote_refs, branch, &tail);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400383 } else {
384 ref_map = get_remote_ref(remote_refs, "HEAD");
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700385 if (!ref_map)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000386 die(_("Couldn't find remote ref HEAD"));
Jeff King900f2812013-05-11 18:15:59 +0200387 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
Shawn O. Pearce5aaf7f22008-03-02 21:34:51 -0500388 tail = &ref_map->next;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400389 }
390 }
Michael Haggerty0281c932013-10-30 06:32:58 +0100391
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100392 if (tags == TAGS_SET)
393 /* also fetch all tags */
394 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
395 else if (tags == TAGS_DEFAULT && *autotags)
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500396 find_non_local_tags(transport, &ref_map, &tail);
Michael Haggerty0281c932013-10-30 06:32:58 +0100397
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100398 /* Now append any refs to be updated opportunistically: */
399 *tail = orefs;
400 for (rm = orefs; rm; rm = rm->next) {
401 rm->fetch_head_status = FETCH_HEAD_IGNORE;
402 tail = &rm->next;
403 }
404
Michael Haggertyb9afe662013-10-30 06:33:09 +0100405 return ref_remove_duplicates(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400406}
407
Jeff Kingfa250752009-05-25 06:40:54 -0400408#define STORE_REF_ERROR_OTHER 1
409#define STORE_REF_ERROR_DF_CONFLICT 2
410
Daniel Barkalowb888d612007-09-10 23:03:25 -0400411static int s_update_ref(const char *action,
412 struct ref *ref,
413 int check_old)
414{
415 char msg[1024];
416 char *rla = getenv("GIT_REFLOG_ACTION");
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700417 struct ref_transaction *transaction;
418 struct strbuf err = STRBUF_INIT;
419 int ret, df_conflict = 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400420
Jay Soffian28a15402009-11-10 09:19:43 +0100421 if (dry_run)
422 return 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400423 if (!rla)
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -0500424 rla = default_rla.buf;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400425 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700426
427 transaction = ref_transaction_begin(&err);
428 if (!transaction ||
Michael Haggerty1d147bd2015-02-17 18:00:15 +0100429 ref_transaction_update(transaction, ref->name,
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000430 ref->new_oid.hash,
431 check_old ? ref->old_oid.hash : NULL,
Michael Haggerty1d147bd2015-02-17 18:00:15 +0100432 0, msg, &err))
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700433 goto fail;
434
435 ret = ref_transaction_commit(transaction, &err);
436 if (ret) {
437 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
438 goto fail;
439 }
440
441 ref_transaction_free(transaction);
442 strbuf_release(&err);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400443 return 0;
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700444fail:
445 ref_transaction_free(transaction);
446 error("%s", err.buf);
447 strbuf_release(&err);
448 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
449 : STORE_REF_ERROR_OTHER;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400450}
451
Pierre Habouzit9ef42722007-11-04 15:05:45 -0800452#define REFCOL_WIDTH 10
Nicolas Pitre165f3902007-11-03 01:32:48 -0400453
Daniel Barkalowb888d612007-09-10 23:03:25 -0400454static int update_local_ref(struct ref *ref,
Nicolas Pitre165f3902007-11-03 01:32:48 -0400455 const char *remote,
Marc Branchaud6da618d2012-04-16 18:08:49 -0400456 const struct ref *remote_ref,
Jeff King5914f2d2011-12-08 03:43:19 -0500457 struct strbuf *display)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400458{
Daniel Barkalowb888d612007-09-10 23:03:25 -0400459 struct commit *current = NULL, *updated;
460 enum object_type type;
461 struct branch *current_branch = branch_get(NULL);
Felipe Contreras4577e482009-05-14 00:22:04 +0300462 const char *pretty_ref = prettify_refname(ref->name);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400463
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000464 type = sha1_object_info(ref->new_oid.hash, NULL);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400465 if (type < 0)
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000466 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
Daniel Barkalowb888d612007-09-10 23:03:25 -0400467
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000468 if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100469 if (verbosity > 0)
Jeff King5914f2d2011-12-08 03:43:19 -0500470 strbuf_addf(display, "= %-*s %-*s -> %s",
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700471 TRANSPORT_SUMMARY(_("[up to date]")),
472 REFCOL_WIDTH, remote, pretty_ref);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400473 return 0;
474 }
475
Shawn O. Pearceb3abdd92007-09-16 02:31:26 -0400476 if (current_branch &&
477 !strcmp(ref->name, current_branch->name) &&
Daniel Barkalowb888d612007-09-10 23:03:25 -0400478 !(update_head_ok || is_bare_repository()) &&
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000479 !is_null_oid(&ref->old_oid)) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400480 /*
481 * If this is the head, and it's not okay to update
482 * the head, and the old value of the head isn't empty...
483 */
Jeff King5914f2d2011-12-08 03:43:19 -0500484 strbuf_addf(display,
485 _("! %-*s %-*s -> %s (can't fetch in current branch)"),
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700486 TRANSPORT_SUMMARY(_("[rejected]")),
Jeff King5914f2d2011-12-08 03:43:19 -0500487 REFCOL_WIDTH, remote, pretty_ref);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400488 return 1;
489 }
490
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000491 if (!is_null_oid(&ref->old_oid) &&
Christian Couder59556542013-11-30 21:55:40 +0100492 starts_with(ref->name, "refs/tags/")) {
Jeff King63154722008-06-26 23:59:50 -0400493 int r;
494 r = s_update_ref("updating tag", ref, 0);
Jeff King5914f2d2011-12-08 03:43:19 -0500495 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
496 r ? '!' : '-',
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700497 TRANSPORT_SUMMARY(_("[tag update]")),
Jeff King5914f2d2011-12-08 03:43:19 -0500498 REFCOL_WIDTH, remote, pretty_ref,
499 r ? _(" (unable to update local ref)") : "");
Jeff King63154722008-06-26 23:59:50 -0400500 return r;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400501 }
502
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000503 current = lookup_commit_reference_gently(ref->old_oid.hash, 1);
504 updated = lookup_commit_reference_gently(ref->new_oid.hash, 1);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400505 if (!current || !updated) {
Nicolas Pitre165f3902007-11-03 01:32:48 -0400506 const char *msg;
507 const char *what;
Jeff King63154722008-06-26 23:59:50 -0400508 int r;
Marc Branchaud0997ada2012-04-16 18:08:50 -0400509 /*
510 * Nicely describe the new ref we're fetching.
511 * Base this on the remote's ref name, as it's
512 * more likely to follow a standard layout.
513 */
514 const char *name = remote_ref ? remote_ref->name : "";
Christian Couder59556542013-11-30 21:55:40 +0100515 if (starts_with(name, "refs/tags/")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400516 msg = "storing tag";
Ævar Arnfjörð Bjarmasonf7b37422011-02-22 23:41:53 +0000517 what = _("[new tag]");
Christian Couder59556542013-11-30 21:55:40 +0100518 } else if (starts_with(name, "refs/heads/")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400519 msg = "storing head";
Ævar Arnfjörð Bjarmasonf7b37422011-02-22 23:41:53 +0000520 what = _("[new branch]");
Marc Branchaud0997ada2012-04-16 18:08:50 -0400521 } else {
522 msg = "storing ref";
523 what = _("[new ref]");
Nicolas Pitre165f3902007-11-03 01:32:48 -0400524 }
525
Jens Lehmanna6801ad2012-04-13 18:25:16 +0200526 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
527 (recurse_submodules != RECURSE_SUBMODULES_ON))
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000528 check_for_new_submodule_commits(ref->new_oid.hash);
Jeff King63154722008-06-26 23:59:50 -0400529 r = s_update_ref(msg, ref, 0);
Jeff King5914f2d2011-12-08 03:43:19 -0500530 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
531 r ? '!' : '*',
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700532 TRANSPORT_SUMMARY(what),
Jeff King5914f2d2011-12-08 03:43:19 -0500533 REFCOL_WIDTH, remote, pretty_ref,
534 r ? _(" (unable to update local ref)") : "");
Jeff King63154722008-06-26 23:59:50 -0400535 return r;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400536 }
537
Junio C Hamanoa20efee2012-08-27 14:46:01 -0700538 if (in_merge_bases(current, updated)) {
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400539 struct strbuf quickref = STRBUF_INIT;
Jeff King63154722008-06-26 23:59:50 -0400540 int r;
brian m. carlsoned1c9972015-11-10 02:22:29 +0000541 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400542 strbuf_addstr(&quickref, "..");
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000543 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
Jens Lehmann88a21972011-03-06 23:10:46 +0100544 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
545 (recurse_submodules != RECURSE_SUBMODULES_ON))
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000546 check_for_new_submodule_commits(ref->new_oid.hash);
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300547 r = s_update_ref("fast-forward", ref, 1);
Jeff King5914f2d2011-12-08 03:43:19 -0500548 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
549 r ? '!' : ' ',
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400550 TRANSPORT_SUMMARY_WIDTH, quickref.buf,
Jeff King5914f2d2011-12-08 03:43:19 -0500551 REFCOL_WIDTH, remote, pretty_ref,
552 r ? _(" (unable to update local ref)") : "");
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400553 strbuf_release(&quickref);
Jeff King63154722008-06-26 23:59:50 -0400554 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400555 } else if (force || ref->force) {
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400556 struct strbuf quickref = STRBUF_INIT;
Jeff King63154722008-06-26 23:59:50 -0400557 int r;
brian m. carlsoned1c9972015-11-10 02:22:29 +0000558 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400559 strbuf_addstr(&quickref, "...");
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000560 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
Jens Lehmann88a21972011-03-06 23:10:46 +0100561 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
562 (recurse_submodules != RECURSE_SUBMODULES_ON))
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000563 check_for_new_submodule_commits(ref->new_oid.hash);
Jeff King63154722008-06-26 23:59:50 -0400564 r = s_update_ref("forced-update", ref, 1);
Jeff King5914f2d2011-12-08 03:43:19 -0500565 strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
566 r ? '!' : '+',
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400567 TRANSPORT_SUMMARY_WIDTH, quickref.buf,
Jeff King5914f2d2011-12-08 03:43:19 -0500568 REFCOL_WIDTH, remote, pretty_ref,
569 r ? _("unable to update local ref") : _("forced update"));
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400570 strbuf_release(&quickref);
Jeff King63154722008-06-26 23:59:50 -0400571 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400572 } else {
Jeff King5914f2d2011-12-08 03:43:19 -0500573 strbuf_addf(display, "! %-*s %-*s -> %s %s",
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700574 TRANSPORT_SUMMARY(_("[rejected]")),
Jeff King5914f2d2011-12-08 03:43:19 -0500575 REFCOL_WIDTH, remote, pretty_ref,
576 _("(non-fast-forward)"));
Daniel Barkalowb888d612007-09-10 23:03:25 -0400577 return 1;
578 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400579}
580
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700581static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700582{
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700583 struct ref **rm = cb_data;
584 struct ref *ref = *rm;
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700585
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +0700586 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
587 ref = ref->next;
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700588 if (!ref)
589 return -1; /* end of the list */
590 *rm = ref->next;
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000591 hashcpy(sha1, ref->old_oid.hash);
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700592 return 0;
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700593}
594
Andreas Ericsson47abd852009-04-17 10:20:11 +0200595static int store_updated_refs(const char *raw_url, const char *remote_name,
Jeff Kingf3cb1692008-06-27 00:01:41 -0400596 struct ref *ref_map)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400597{
598 FILE *fp;
599 struct commit *commit;
Tom Miller4b3b33a2014-01-02 20:28:51 -0600600 int url_len, i, rc = 0;
Jeff King5914f2d2011-12-08 03:43:19 -0500601 struct strbuf note = STRBUF_INIT;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400602 const char *what, *kind;
603 struct ref *rm;
Nguyễn Thái Ngọc Duydcf69262014-11-30 15:24:27 +0700604 char *url;
Jeff Kingf9327292015-08-10 05:38:57 -0400605 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
Jeff King900f2812013-05-11 18:15:59 +0200606 int want_status;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400607
André Goddard Rosad6617c72007-11-22 20:22:23 -0200608 fp = fopen(filename, "a");
609 if (!fp)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000610 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
Andreas Ericsson47abd852009-04-17 10:20:11 +0200611
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100612 if (raw_url)
613 url = transport_anonymize_url(raw_url);
614 else
615 url = xstrdup("foreign");
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700616
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700617 rm = ref_map;
Junio C Hamano3f7d11c2011-10-17 21:37:12 -0700618 if (check_everything_connected(iterate_ref_map, 0, &rm)) {
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800619 rc = error(_("%s did not send all necessary objects\n"), url);
620 goto abort;
621 }
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700622
Joey Hess96890f42011-12-26 12:16:56 -0400623 /*
Jeff King900f2812013-05-11 18:15:59 +0200624 * We do a pass for each fetch_head_status type in their enum order, so
625 * merged entries are written before not-for-merge. That lets readers
626 * use FETCH_HEAD as a refname to refer to the ref to be merged.
Joey Hess96890f42011-12-26 12:16:56 -0400627 */
Jeff King900f2812013-05-11 18:15:59 +0200628 for (want_status = FETCH_HEAD_MERGE;
629 want_status <= FETCH_HEAD_IGNORE;
630 want_status++) {
Joey Hess96890f42011-12-26 12:16:56 -0400631 for (rm = ref_map; rm; rm = rm->next) {
632 struct ref *ref = NULL;
Jeff King900f2812013-05-11 18:15:59 +0200633 const char *merge_status_marker = "";
Daniel Barkalowb888d612007-09-10 23:03:25 -0400634
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +0700635 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
636 if (want_status == FETCH_HEAD_MERGE)
637 warning(_("reject %s because shallow roots are not allowed to be updated"),
638 rm->peer_ref ? rm->peer_ref->name : rm->name);
639 continue;
640 }
641
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000642 commit = lookup_commit_reference_gently(rm->old_oid.hash, 1);
Joey Hess96890f42011-12-26 12:16:56 -0400643 if (!commit)
Jeff King900f2812013-05-11 18:15:59 +0200644 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400645
Jeff King900f2812013-05-11 18:15:59 +0200646 if (rm->fetch_head_status != want_status)
Joey Hess96890f42011-12-26 12:16:56 -0400647 continue;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400648
Joey Hess96890f42011-12-26 12:16:56 -0400649 if (rm->peer_ref) {
Jeff King6f687c22015-09-24 17:08:09 -0400650 ref = alloc_ref(rm->peer_ref->name);
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000651 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
652 oidcpy(&ref->new_oid, &rm->old_oid);
Joey Hess96890f42011-12-26 12:16:56 -0400653 ref->force = rm->peer_ref->force;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400654 }
Joey Hess96890f42011-12-26 12:16:56 -0400655
656
657 if (!strcmp(rm->name, "HEAD")) {
658 kind = "";
659 what = "";
660 }
Christian Couder59556542013-11-30 21:55:40 +0100661 else if (starts_with(rm->name, "refs/heads/")) {
Joey Hess96890f42011-12-26 12:16:56 -0400662 kind = "branch";
663 what = rm->name + 11;
664 }
Christian Couder59556542013-11-30 21:55:40 +0100665 else if (starts_with(rm->name, "refs/tags/")) {
Joey Hess96890f42011-12-26 12:16:56 -0400666 kind = "tag";
667 what = rm->name + 10;
668 }
Christian Couder59556542013-11-30 21:55:40 +0100669 else if (starts_with(rm->name, "refs/remotes/")) {
Joey Hess96890f42011-12-26 12:16:56 -0400670 kind = "remote-tracking branch";
671 what = rm->name + 13;
672 }
673 else {
674 kind = "";
675 what = rm->name;
676 }
677
678 url_len = strlen(url);
679 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
680 ;
681 url_len = i + 1;
682 if (4 < i && !strncmp(".git", url + i - 3, 4))
683 url_len = i - 3;
684
685 strbuf_reset(&note);
686 if (*what) {
687 if (*kind)
688 strbuf_addf(&note, "%s ", kind);
689 strbuf_addf(&note, "'%s' of ", what);
690 }
Jeff King900f2812013-05-11 18:15:59 +0200691 switch (rm->fetch_head_status) {
692 case FETCH_HEAD_NOT_FOR_MERGE:
693 merge_status_marker = "not-for-merge";
694 /* fall-through */
695 case FETCH_HEAD_MERGE:
696 fprintf(fp, "%s\t%s\t%s",
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000697 oid_to_hex(&rm->old_oid),
Jeff King900f2812013-05-11 18:15:59 +0200698 merge_status_marker,
699 note.buf);
700 for (i = 0; i < url_len; ++i)
701 if ('\n' == url[i])
702 fputs("\\n", fp);
703 else
704 fputc(url[i], fp);
705 fputc('\n', fp);
706 break;
707 default:
708 /* do not write anything to FETCH_HEAD */
709 break;
710 }
Joey Hess96890f42011-12-26 12:16:56 -0400711
712 strbuf_reset(&note);
713 if (ref) {
Marc Branchaud6da618d2012-04-16 18:08:49 -0400714 rc |= update_local_ref(ref, what, rm, &note);
Joey Hess96890f42011-12-26 12:16:56 -0400715 free(ref);
716 } else
717 strbuf_addf(&note, "* %-*s %-*s -> FETCH_HEAD",
718 TRANSPORT_SUMMARY_WIDTH,
719 *kind ? kind : "branch",
720 REFCOL_WIDTH,
721 *what ? what : "HEAD");
722 if (note.len) {
723 if (verbosity >= 0 && !shown_url) {
724 fprintf(stderr, _("From %.*s\n"),
725 url_len, url);
726 shown_url = 1;
727 }
728 if (verbosity >= 0)
729 fprintf(stderr, " %s\n", note.buf);
730 }
Nicolas Pitre165f3902007-11-03 01:32:48 -0400731 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400732 }
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800733
Jeff Kingfa250752009-05-25 06:40:54 -0400734 if (rc & STORE_REF_ERROR_DF_CONFLICT)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000735 error(_("some local refs could not be updated; try running\n"
Jeff Kingf3cb1692008-06-27 00:01:41 -0400736 " 'git remote prune %s' to remove any old, conflicting "
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000737 "branches"), remote_name);
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800738
739 abort:
Jeff King5914f2d2011-12-08 03:43:19 -0500740 strbuf_release(&note);
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800741 free(url);
742 fclose(fp);
Dmitry V. Levinefb98b42008-05-28 19:29:36 +0400743 return rc;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400744}
745
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500746/*
747 * We would want to bypass the object transfer altogether if
Johan Herlandd9eb0202009-07-10 01:52:30 +0200748 * everything we are going to fetch already exists and is connected
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500749 * locally.
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500750 */
751static int quickfetch(struct ref *ref_map)
752{
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700753 struct ref *rm = ref_map;
754
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500755 /*
756 * If we are deepening a shallow clone we already have these
757 * objects reachable. Running rev-list here will return with
758 * a good (0) exit status and we'll bypass the fetch that we
759 * really need to perform. Claiming failure now will ensure
760 * we perform the network exchange to deepen our history.
761 */
762 if (depth)
763 return -1;
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700764 return check_everything_connected(iterate_ref_map, 1, &rm);
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500765}
766
Daniel Barkalowb888d612007-09-10 23:03:25 -0400767static int fetch_refs(struct transport *transport, struct ref *ref_map)
768{
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500769 int ret = quickfetch(ref_map);
770 if (ret)
771 ret = transport_fetch_refs(transport, ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400772 if (!ret)
Jeff Kingf3cb1692008-06-27 00:01:41 -0400773 ret |= store_updated_refs(transport->url,
774 transport->remote->name,
775 ref_map);
Shawn O. Pearce1788c392007-09-14 03:31:23 -0400776 transport_unlock_pack(transport);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400777 return ret;
778}
779
Tom Miller4b3b33a2014-01-02 20:28:51 -0600780static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
781 const char *raw_url)
Jay Soffianf360d842009-11-10 09:15:47 +0100782{
Tom Miller4b3b33a2014-01-02 20:28:51 -0600783 int url_len, i, result = 0;
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200784 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
Tom Miller4b3b33a2014-01-02 20:28:51 -0600785 char *url;
Jay Soffianf360d842009-11-10 09:15:47 +0100786 const char *dangling_msg = dry_run
Nguyễn Thái Ngọc Duy18986d52012-04-23 19:30:25 +0700787 ? _(" (%s will become dangling)")
788 : _(" (%s has become dangling)");
Jay Soffianf360d842009-11-10 09:15:47 +0100789
Tom Miller4b3b33a2014-01-02 20:28:51 -0600790 if (raw_url)
791 url = transport_anonymize_url(raw_url);
792 else
793 url = xstrdup("foreign");
794
795 url_len = strlen(url);
796 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
797 ;
798
799 url_len = i + 1;
800 if (4 < i && !strncmp(".git", url + i - 3, 4))
801 url_len = i - 3;
802
Michael Haggertya087b432015-06-22 16:02:59 +0200803 if (!dry_run) {
804 struct string_list refnames = STRING_LIST_INIT_NODUP;
805
806 for (ref = stale_refs; ref; ref = ref->next)
807 string_list_append(&refnames, ref->name);
808
809 result = delete_refs(&refnames);
810 string_list_clear(&refnames, 0);
811 }
812
813 if (verbosity >= 0) {
814 for (ref = stale_refs; ref; ref = ref->next) {
815 if (!shown_url) {
816 fprintf(stderr, _("From %.*s\n"), url_len, url);
817 shown_url = 1;
818 }
Jay Soffianf360d842009-11-10 09:15:47 +0100819 fprintf(stderr, " x %-*s %-*s -> %s\n",
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700820 TRANSPORT_SUMMARY(_("[deleted]")),
Ævar Arnfjörð Bjarmason502681c2011-02-22 23:41:52 +0000821 REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
Jay Soffianf360d842009-11-10 09:15:47 +0100822 warn_dangling_symref(stderr, dangling_msg, ref->name);
823 }
824 }
Michael Haggertya087b432015-06-22 16:02:59 +0200825
Tom Miller4b3b33a2014-01-02 20:28:51 -0600826 free(url);
Jay Soffianf360d842009-11-10 09:15:47 +0100827 free_refs(stale_refs);
828 return result;
829}
830
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200831static void check_not_current_branch(struct ref *ref_map)
832{
833 struct branch *current_branch = branch_get(NULL);
834
835 if (is_bare_repository() || !current_branch)
836 return;
837
838 for (; ref_map; ref_map = ref_map->next)
839 if (ref_map->peer_ref && !strcmp(current_branch->refname,
840 ref_map->peer_ref->name))
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000841 die(_("Refusing to fetch into current branch %s "
842 "of non-bare repository"), current_branch->refname);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200843}
844
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800845static int truncate_fetch_head(void)
846{
Jeff Kingf9327292015-08-10 05:38:57 -0400847 const char *filename = git_path_fetch_head();
Johannes Schindelinea565182016-01-11 19:35:54 +0100848 FILE *fp = fopen_for_writing(filename);
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800849
850 if (!fp)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000851 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800852 fclose(fp);
853 return 0;
854}
855
Junio C Hamanodb5723c2013-08-07 14:43:20 -0700856static void set_option(struct transport *transport, const char *name, const char *value)
857{
858 int r = transport_set_option(transport, name, value);
859 if (r < 0)
860 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
861 name, value, transport->url);
862 if (r > 0)
863 warning(_("Option \"%s\" is ignored for %s\n"),
864 name, transport->url);
865}
866
Ramsay Jones0f73f8b2013-08-28 19:56:17 +0100867static struct transport *prepare_transport(struct remote *remote)
Junio C Hamanodb5723c2013-08-07 14:43:20 -0700868{
869 struct transport *transport;
870 transport = transport_get(remote, NULL);
871 transport_set_verbosity(transport, verbosity, progress);
Eric Wongc915f112016-02-03 04:09:14 +0000872 transport->family = family;
Junio C Hamanodb5723c2013-08-07 14:43:20 -0700873 if (upload_pack)
874 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
875 if (keep)
876 set_option(transport, TRANS_OPT_KEEP, "yes");
877 if (depth)
878 set_option(transport, TRANS_OPT_DEPTH, depth);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +0700879 if (update_shallow)
880 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
Junio C Hamanodb5723c2013-08-07 14:43:20 -0700881 return transport;
882}
883
Junio C Hamano069d5032013-08-07 15:14:45 -0700884static void backfill_tags(struct transport *transport, struct ref *ref_map)
885{
Junio C Hamanob26ed432013-08-07 15:47:18 -0700886 if (transport->cannot_reuse) {
887 gsecondary = prepare_transport(transport->remote);
888 transport = gsecondary;
889 }
890
Junio C Hamano069d5032013-08-07 15:14:45 -0700891 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
892 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
893 fetch_refs(transport, ref_map);
Junio C Hamanob26ed432013-08-07 15:47:18 -0700894
895 if (gsecondary) {
896 transport_disconnect(gsecondary);
897 gsecondary = NULL;
898 }
Junio C Hamano069d5032013-08-07 15:14:45 -0700899}
900
Daniel Barkalowb888d612007-09-10 23:03:25 -0400901static int do_fetch(struct transport *transport,
902 struct refspec *refs, int ref_count)
903{
Michael Haggertyb87dbcc2013-05-25 11:08:01 +0200904 struct string_list existing_refs = STRING_LIST_INIT_DUP;
Shawn O. Pearce7f984282008-03-02 21:34:43 -0500905 struct ref *ref_map;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400906 struct ref *rm;
907 int autotags = (transport->remote->fetch_tags == 1);
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200908 int retcode = 0;
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000909
Michael Haggerty0e0b7de2015-05-25 18:38:35 +0000910 for_each_ref(add_existing, &existing_refs);
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000911
Daniel Johnsoned368542010-08-11 18:57:20 -0400912 if (tags == TAGS_DEFAULT) {
913 if (transport->remote->fetch_tags == 2)
914 tags = TAGS_SET;
915 if (transport->remote->fetch_tags == -1)
916 tags = TAGS_UNSET;
917 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400918
Shawn O. Pearce824d5772007-09-19 00:49:31 -0400919 if (!transport->get_refs_list || !transport->fetch)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000920 die(_("Don't know how to fetch from %s"), transport->url);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400921
922 /* if not appending, truncate FETCH_HEAD */
Jay Soffian28a15402009-11-10 09:19:43 +0100923 if (!append && !dry_run) {
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200924 retcode = truncate_fetch_head();
925 if (retcode)
926 goto cleanup;
André Goddard Rosad6617c72007-11-22 20:22:23 -0200927 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400928
929 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200930 if (!update_head_ok)
931 check_not_current_branch(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400932
933 for (rm = ref_map; rm; rm = rm->next) {
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000934 if (rm->peer_ref) {
Michael Haggerty6f64a162013-05-25 11:08:15 +0200935 struct string_list_item *peer_item =
936 string_list_lookup(&existing_refs,
937 rm->peer_ref->name);
Michael Haggerty0e0b7de2015-05-25 18:38:35 +0000938 if (peer_item) {
939 struct object_id *old_oid = peer_item->util;
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000940 oidcpy(&rm->peer_ref->old_oid, old_oid);
Michael Haggerty0e0b7de2015-05-25 18:38:35 +0000941 }
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000942 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400943 }
944
Shawn O. Pearce41fa7d22008-03-03 22:27:40 -0500945 if (tags == TAGS_DEFAULT && autotags)
946 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200947 if (prune) {
Michael Haggerty0838bf42013-10-30 06:33:00 +0100948 /*
949 * We only prune based on refspecs specified
950 * explicitly (via command line or configuration); we
951 * don't care whether --tags was specified.
952 */
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100953 if (ref_count) {
Tom Miller4b3b33a2014-01-02 20:28:51 -0600954 prune_refs(refs, ref_count, ref_map, transport->url);
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100955 } else {
Michael Haggerty0838bf42013-10-30 06:33:00 +0100956 prune_refs(transport->remote->fetch,
957 transport->remote->fetch_refspec_nr,
Tom Miller4b3b33a2014-01-02 20:28:51 -0600958 ref_map,
959 transport->url);
Carlos Martín Nietoe8c1e6c2011-10-15 07:04:26 +0200960 }
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200961 }
Tom Miller10a6cc82014-01-02 20:28:52 -0600962 if (fetch_refs(transport, ref_map)) {
963 free_refs(ref_map);
964 retcode = 1;
965 goto cleanup;
966 }
Shawn O. Pearce7f984282008-03-02 21:34:43 -0500967 free_refs(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400968
969 /* if neither --no-tags nor --tags was specified, do automated tag
970 * following ... */
Kristian Høgsberg83201992007-12-04 02:25:47 -0500971 if (tags == TAGS_DEFAULT && autotags) {
Shawn O. Pearcec50b2b42008-03-02 21:35:00 -0500972 struct ref **tail = &ref_map;
973 ref_map = NULL;
974 find_non_local_tags(transport, &ref_map, &tail);
Junio C Hamano069d5032013-08-07 15:14:45 -0700975 if (ref_map)
976 backfill_tags(transport, ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400977 free_refs(ref_map);
978 }
979
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200980 cleanup:
Michael Haggertyf83918e2013-05-25 11:08:17 +0200981 string_list_clear(&existing_refs, 1);
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200982 return retcode;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400983}
984
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100985static int get_one_remote_for_fetch(struct remote *remote, void *priv)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400986{
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100987 struct string_list *list = priv;
Björn Gustavsson7cc91a22009-11-09 21:11:06 +0100988 if (!remote->skip_default_update)
Julian Phillips1d2f80f2010-06-26 00:41:38 +0100989 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100990 return 0;
991}
992
993struct remote_group_data {
994 const char *name;
995 struct string_list *list;
996};
997
998static int get_remote_group(const char *key, const char *value, void *priv)
999{
1000 struct remote_group_data *g = priv;
1001
Michael Haggertybc598c32015-07-28 23:08:21 +02001002 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001003 /* split list by white space */
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001004 while (*value) {
Michael Haggerty5f654992015-07-28 23:08:20 +02001005 size_t wordlen = strcspn(value, " \t\n");
1006
Michael Haggertye2865422015-07-28 23:08:19 +02001007 if (wordlen >= 1)
Julian Phillips1d2f80f2010-06-26 00:41:38 +01001008 string_list_append(g->list,
Michael Haggertye2865422015-07-28 23:08:19 +02001009 xstrndup(value, wordlen));
1010 value += wordlen + (value[wordlen] != '\0');
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001011 }
1012 }
1013
1014 return 0;
1015}
1016
1017static int add_remote_or_group(const char *name, struct string_list *list)
1018{
1019 int prev_nr = list->nr;
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +00001020 struct remote_group_data g;
1021 g.name = name; g.list = list;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001022
1023 git_config(get_remote_group, &g);
1024 if (list->nr == prev_nr) {
Thomas Gummerer674468b2016-02-16 10:47:50 +01001025 struct remote *remote = remote_get(name);
1026 if (!remote_is_configured(remote))
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001027 return 0;
Julian Phillips1d2f80f2010-06-26 00:41:38 +01001028 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001029 }
1030 return 1;
1031}
1032
Jeff King85556d42012-09-01 07:27:35 -04001033static void add_options_to_argv(struct argv_array *argv)
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001034{
1035 if (dry_run)
Jeff King85556d42012-09-01 07:27:35 -04001036 argv_array_push(argv, "--dry-run");
Michael Haggerty90765fa2013-10-30 06:33:04 +01001037 if (prune != -1)
1038 argv_array_push(argv, prune ? "--prune" : "--no-prune");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001039 if (update_head_ok)
Jeff King85556d42012-09-01 07:27:35 -04001040 argv_array_push(argv, "--update-head-ok");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001041 if (force)
Jeff King85556d42012-09-01 07:27:35 -04001042 argv_array_push(argv, "--force");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001043 if (keep)
Jeff King85556d42012-09-01 07:27:35 -04001044 argv_array_push(argv, "--keep");
Jens Lehmannbe254a02010-11-11 00:55:02 +01001045 if (recurse_submodules == RECURSE_SUBMODULES_ON)
Jeff King85556d42012-09-01 07:27:35 -04001046 argv_array_push(argv, "--recurse-submodules");
Jens Lehmann8f0700d2011-03-06 23:11:21 +01001047 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
Jeff King85556d42012-09-01 07:27:35 -04001048 argv_array_push(argv, "--recurse-submodules=on-demand");
Dan Johnson85566462012-09-05 17:22:19 -04001049 if (tags == TAGS_SET)
1050 argv_array_push(argv, "--tags");
1051 else if (tags == TAGS_UNSET)
1052 argv_array_push(argv, "--no-tags");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001053 if (verbosity >= 2)
Jeff King85556d42012-09-01 07:27:35 -04001054 argv_array_push(argv, "-v");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001055 if (verbosity >= 1)
Jeff King85556d42012-09-01 07:27:35 -04001056 argv_array_push(argv, "-v");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001057 else if (verbosity < 0)
Jeff King85556d42012-09-01 07:27:35 -04001058 argv_array_push(argv, "-q");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001059
1060}
1061
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001062static int fetch_multiple(struct string_list *list)
1063{
1064 int i, result = 0;
Jeff King85556d42012-09-01 07:27:35 -04001065 struct argv_array argv = ARGV_ARRAY_INIT;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001066
Junio C Hamanoe6cc5102010-02-24 11:02:05 -08001067 if (!append && !dry_run) {
1068 int errcode = truncate_fetch_head();
1069 if (errcode)
1070 return errcode;
1071 }
1072
Jeff King85556d42012-09-01 07:27:35 -04001073 argv_array_pushl(&argv, "fetch", "--append", NULL);
1074 add_options_to_argv(&argv);
1075
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001076 for (i = 0; i < list->nr; i++) {
1077 const char *name = list->items[i].string;
Jeff King85556d42012-09-01 07:27:35 -04001078 argv_array_push(&argv, name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001079 if (verbosity >= 0)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001080 printf(_("Fetching %s\n"), name);
Jeff King85556d42012-09-01 07:27:35 -04001081 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001082 error(_("Could not fetch %s"), name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001083 result = 1;
1084 }
Jeff King85556d42012-09-01 07:27:35 -04001085 argv_array_pop(&argv);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001086 }
1087
Jeff King85556d42012-09-01 07:27:35 -04001088 argv_array_clear(&argv);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001089 return result;
1090}
1091
1092static int fetch_one(struct remote *remote, int argc, const char **argv)
1093{
Daniel Barkalowb888d612007-09-10 23:03:25 -04001094 static const char **refs = NULL;
Jim Meyeringd8ead152011-06-08 22:06:33 +02001095 struct refspec *refspec;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001096 int ref_nr = 0;
Alex Riesen7b7f39e2008-04-28 22:23:35 +02001097 int exit_code;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001098
Daniel Barkalowfa685bd2009-03-11 01:47:20 -04001099 if (!remote)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001100 die(_("No remote repository specified. Please, specify either a URL or a\n"
1101 "remote name from which new revisions should be fetched."));
Daniel Barkalowfa685bd2009-03-11 01:47:20 -04001102
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001103 gtransport = prepare_transport(remote);
Michael Schubert737c5a92013-07-13 11:36:24 +02001104
1105 if (prune < 0) {
1106 /* no command line request */
Junio C Hamano20419de2013-09-09 14:50:37 -07001107 if (0 <= gtransport->remote->prune)
1108 prune = gtransport->remote->prune;
Michael Schubert737c5a92013-07-13 11:36:24 +02001109 else if (0 <= fetch_prune_config)
1110 prune = fetch_prune_config;
1111 else
1112 prune = PRUNE_BY_DEFAULT;
1113 }
1114
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001115 if (argc > 0) {
Daniel Barkalowb888d612007-09-10 23:03:25 -04001116 int j = 0;
Elia Pintobf7e6452014-01-29 08:54:16 -08001117 int i;
Jeff King50a6c8e2016-02-22 17:44:35 -05001118 refs = xcalloc(st_add(argc, 1), sizeof(const char *));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001119 for (i = 0; i < argc; i++) {
Daniel Barkalowb888d612007-09-10 23:03:25 -04001120 if (!strcmp(argv[i], "tag")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -04001121 i++;
Kevin Ballardf53423b2008-04-05 14:28:53 -04001122 if (i >= argc)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001123 die(_("You need to specify a tag name."));
Jeff Kingb2724c82014-06-19 17:26:56 -04001124 refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1125 argv[i], argv[i]);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001126 } else
1127 refs[j++] = argv[i];
Daniel Barkalowb888d612007-09-10 23:03:25 -04001128 }
1129 refs[j] = NULL;
1130 ref_nr = j;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001131 }
1132
Jeff King57b235a2009-01-22 01:03:08 -05001133 sigchain_push_common(unlock_pack_on_signal);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -04001134 atexit(unlock_pack);
Jim Meyeringd8ead152011-06-08 22:06:33 +02001135 refspec = parse_fetch_refspec(ref_nr, refs);
Junio C Hamanoaf234452013-08-07 15:38:45 -07001136 exit_code = do_fetch(gtransport, refspec, ref_nr);
Carlos Martín Nieto5caf1972011-10-08 00:51:06 +02001137 free_refspec(ref_nr, refspec);
Junio C Hamanoaf234452013-08-07 15:38:45 -07001138 transport_disconnect(gtransport);
1139 gtransport = NULL;
Alex Riesen7b7f39e2008-04-28 22:23:35 +02001140 return exit_code;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001141}
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001142
1143int cmd_fetch(int argc, const char **argv, const char *prefix)
1144{
1145 int i;
Thiago Farina183113a2010-07-04 16:46:19 -03001146 struct string_list list = STRING_LIST_INIT_NODUP;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001147 struct remote *remote;
1148 int result = 0;
Nguyễn Thái Ngọc Duy19910062014-08-16 08:19:27 +07001149 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001150
Jeff Kingbbc30f92011-02-24 09:30:19 -05001151 packet_trace_identity("fetch");
1152
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001153 /* Record the command line for the reflog */
1154 strbuf_addstr(&default_rla, "fetch");
1155 for (i = 1; i < argc; i++)
1156 strbuf_addf(&default_rla, " %s", argv[i]);
1157
Michael Schubert737c5a92013-07-13 11:36:24 +02001158 git_config(git_fetch_config, NULL);
1159
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001160 argc = parse_options(argc, argv, prefix,
1161 builtin_fetch_options, builtin_fetch_usage, 0);
1162
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +07001163 if (unshallow) {
1164 if (depth)
1165 die(_("--depth and --unshallow cannot be used together"));
1166 else if (!is_repository_shallow())
1167 die(_("--unshallow on a complete repository does not make sense"));
Jeff King2805bb52015-09-24 17:07:07 -04001168 else
1169 depth = xstrfmt("%d", INFINITE_DEPTH);
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +07001170 }
1171
Nguyễn Thái Ngọc Duy5594bca2013-12-05 10:31:11 +07001172 /* no need to be strict, transport_set_option() will validate it again */
1173 if (depth && atoi(depth) < 1)
1174 die(_("depth %s is not a positive number"), depth);
1175
Jens Lehmann18322ba2011-09-09 20:22:03 +02001176 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1177 if (recurse_submodules_default) {
1178 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
1179 set_config_fetch_recurse_submodules(arg);
1180 }
1181 gitmodules_config();
1182 git_config(submodule_config, NULL);
1183 }
1184
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001185 if (all) {
1186 if (argc == 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001187 die(_("fetch --all does not take a repository argument"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001188 else if (argc > 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001189 die(_("fetch --all does not make sense with refspecs"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001190 (void) for_each_remote(get_one_remote_for_fetch, &list);
1191 result = fetch_multiple(&list);
1192 } else if (argc == 0) {
1193 /* No arguments -- use default remote */
1194 remote = remote_get(NULL);
1195 result = fetch_one(remote, argc, argv);
Björn Gustavsson16679e32009-11-09 21:10:32 +01001196 } else if (multiple) {
1197 /* All arguments are assumed to be remotes or groups */
1198 for (i = 0; i < argc; i++)
1199 if (!add_remote_or_group(argv[i], &list))
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001200 die(_("No such remote or remote group: %s"), argv[i]);
Björn Gustavsson16679e32009-11-09 21:10:32 +01001201 result = fetch_multiple(&list);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001202 } else {
1203 /* Single remote or group */
1204 (void) add_remote_or_group(argv[0], &list);
1205 if (list.nr > 1) {
1206 /* More than one remote */
1207 if (argc > 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001208 die(_("Fetching a group and specifying refspecs does not make sense"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001209 result = fetch_multiple(&list);
1210 } else {
1211 /* Zero or one remotes */
1212 remote = remote_get(argv[0]);
1213 result = fetch_one(remote, argc-1, argv+1);
1214 }
1215 }
1216
Jens Lehmannbe254a02010-11-11 00:55:02 +01001217 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
Jeff King85556d42012-09-01 07:27:35 -04001218 struct argv_array options = ARGV_ARRAY_INIT;
1219
1220 add_options_to_argv(&options);
Jens Lehmann50d89ad2012-09-01 17:27:06 +02001221 result = fetch_populated_submodules(&options,
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001222 submodule_prefix,
Jens Lehmann8f0700d2011-03-06 23:11:21 +01001223 recurse_submodules,
Stefan Beller62104ba2015-12-15 16:04:12 -08001224 verbosity < 0,
1225 max_children);
Jeff King85556d42012-09-01 07:27:35 -04001226 argv_array_clear(&options);
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001227 }
1228
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001229 /* All names were strdup()ed or strndup()ed */
1230 list.strdup_strings = 1;
1231 string_list_clear(&list, 0);
1232
Johannes Schindelin0898c962016-01-13 18:20:11 +01001233 close_all_packs();
1234
Nguyễn Thái Ngọc Duy19910062014-08-16 08:19:27 +07001235 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
Nguyễn Thái Ngọc Duy6fceed32014-08-16 08:19:28 +07001236 if (verbosity < 0)
1237 argv_array_push(&argv_gc_auto, "--quiet");
Nguyễn Thái Ngọc Duy19910062014-08-16 08:19:27 +07001238 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1239 argv_array_clear(&argv_gc_auto);
Jeff King131b8fc2013-01-26 17:40:38 -05001240
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001241 return result;
1242}