blob: 719bf4f244ba5e6dcb40c9c8e578aa1cdfeee53e [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"
Michael Lukashovf1863d02010-02-16 23:42:52 +000014#include "transport.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;
Shawn O. Pearce4191c352007-11-11 02:29:47 -050040static const char *depth;
Kristian Høgsberg83201992007-12-04 02:25:47 -050041static const char *upload_pack;
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -050042static struct strbuf default_rla = STRBUF_INIT;
Junio C Hamanoaf234452013-08-07 15:38:45 -070043static struct transport *gtransport;
Junio C Hamanob26ed432013-08-07 15:47:18 -070044static struct transport *gsecondary;
Jens Lehmann7dce19d2010-11-12 13:54:52 +010045static const char *submodule_prefix = "";
Jens Lehmann88a21972011-03-06 23:10:46 +010046static const char *recurse_submodules_default;
Tom Miller4b3b33a2014-01-02 20:28:51 -060047static int shown_url = 0;
Junio C Hamanoc5558f82014-05-29 15:21:31 -070048static int refmap_alloc, refmap_nr;
49static const char **refmap_array;
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -040050
Jens Lehmann8f0700d2011-03-06 23:11:21 +010051static int option_parse_recurse_submodules(const struct option *opt,
52 const char *arg, int unset)
53{
54 if (unset) {
55 recurse_submodules = RECURSE_SUBMODULES_OFF;
56 } else {
57 if (arg)
58 recurse_submodules = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
59 else
60 recurse_submodules = RECURSE_SUBMODULES_ON;
61 }
62 return 0;
63}
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -040064
Michael Schubert737c5a92013-07-13 11:36:24 +020065static int git_fetch_config(const char *k, const char *v, void *cb)
66{
67 if (!strcmp(k, "fetch.prune")) {
68 fetch_prune_config = git_config_bool(k, v);
69 return 0;
70 }
Jeff King72549df2014-11-04 08:11:19 -050071 return git_default_config(k, v, cb);
Michael Schubert737c5a92013-07-13 11:36:24 +020072}
73
Junio C Hamanoc5558f82014-05-29 15:21:31 -070074static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
75{
76 ALLOC_GROW(refmap_array, refmap_nr + 1, refmap_alloc);
77
78 /*
79 * "git fetch --refmap='' origin foo"
80 * can be used to tell the command not to store anywhere
81 */
82 if (*arg)
83 refmap_array[refmap_nr++] = arg;
84 return 0;
85}
86
Kristian Høgsberg83201992007-12-04 02:25:47 -050087static struct option builtin_fetch_options[] = {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +010088 OPT__VERBOSITY(&verbosity),
Stefan Bellerd5d09d42013-08-03 13:51:19 +020089 OPT_BOOL(0, "all", &all,
90 N_("fetch from all remotes")),
91 OPT_BOOL('a', "append", &append,
92 N_("append to .git/FETCH_HEAD instead of overwriting")),
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070093 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
94 N_("path to upload pack on remote end")),
95 OPT__FORCE(&force, N_("force overwrite of local branch")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +020096 OPT_BOOL('m', "multiple", &multiple,
97 N_("fetch from multiple remotes")),
Kristian Høgsberg83201992007-12-04 02:25:47 -050098 OPT_SET_INT('t', "tags", &tags,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070099 N_("fetch all tags and associated objects"), TAGS_SET),
Johannes Schindeline7951292008-03-13 08:13:15 +0100100 OPT_SET_INT('n', NULL, &tags,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700101 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200102 OPT_BOOL('p', "prune", &prune,
103 N_("prune remote-tracking branches no longer on remote")),
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700104 { OPTION_CALLBACK, 0, "recurse-submodules", NULL, N_("on-demand"),
105 N_("control recursive fetching of submodules"),
Jens Lehmann8f0700d2011-03-06 23:11:21 +0100106 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200107 OPT_BOOL(0, "dry-run", &dry_run,
108 N_("dry run")),
109 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
110 OPT_BOOL('u', "update-head-ok", &update_head_ok,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700111 N_("allow updating of HEAD ref")),
112 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
113 OPT_STRING(0, "depth", &depth, N_("depth"),
114 N_("deepen history of shallow clone")),
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +0700115 { OPTION_SET_INT, 0, "unshallow", &unshallow, NULL,
116 N_("convert to a complete repository"),
117 PARSE_OPT_NONEG | PARSE_OPT_NOARG, NULL, 1 },
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700118 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
119 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
Jens Lehmann88a21972011-03-06 23:10:46 +0100120 { OPTION_STRING, 0, "recurse-submodules-default",
121 &recurse_submodules_default, NULL,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700122 N_("default mode for recursion"), PARSE_OPT_HIDDEN },
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +0700123 OPT_BOOL(0, "update-shallow", &update_shallow,
124 N_("accept refs that update .git/shallow")),
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700125 { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"),
126 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg },
Kristian Høgsberg83201992007-12-04 02:25:47 -0500127 OPT_END()
128};
129
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400130static void unlock_pack(void)
131{
Junio C Hamanoaf234452013-08-07 15:38:45 -0700132 if (gtransport)
133 transport_unlock_pack(gtransport);
Junio C Hamanob26ed432013-08-07 15:47:18 -0700134 if (gsecondary)
135 transport_unlock_pack(gsecondary);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400136}
137
138static void unlock_pack_on_signal(int signo)
139{
140 unlock_pack();
Jeff King4a16d072009-01-22 01:02:35 -0500141 sigchain_pop(signo);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400142 raise(signo);
143}
Daniel Barkalowb888d612007-09-10 23:03:25 -0400144
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400145static void add_merge_config(struct ref **head,
Daniel Barkalow45773702007-10-29 21:05:40 -0400146 const struct ref *remote_refs,
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400147 struct branch *branch,
148 struct ref ***tail)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400149{
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400150 int i;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400151
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400152 for (i = 0; i < branch->merge_nr; i++) {
153 struct ref *rm, **old_tail = *tail;
154 struct refspec refspec;
155
156 for (rm = *head; rm; rm = rm->next) {
157 if (branch_merge_matches(branch, i, rm->name)) {
Jeff King900f2812013-05-11 18:15:59 +0200158 rm->fetch_head_status = FETCH_HEAD_MERGE;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400159 break;
160 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400161 }
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400162 if (rm)
163 continue;
164
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700165 /*
Matthieu Moy8b3f3f82010-11-02 16:31:23 +0100166 * Not fetched to a remote-tracking branch? We need to fetch
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400167 * it anyway to allow this branch's "branch.$name.merge"
Heikki Orsila05207a22008-09-09 13:28:30 +0300168 * to be honored by 'git pull', but we do not have to
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700169 * fail if branch.$name.merge is misconfigured to point
170 * at a nonexisting branch. If we were indeed called by
Heikki Orsila05207a22008-09-09 13:28:30 +0300171 * 'git pull', it will notice the misconfiguration because
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700172 * there is no entry in the resulting FETCH_HEAD marked
173 * for merging.
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400174 */
Andreas Gruenbacher8da61a22010-03-12 23:27:33 +0100175 memset(&refspec, 0, sizeof(refspec));
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400176 refspec.src = branch->merge[i]->src;
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700177 get_fetch_map(remote_refs, &refspec, tail, 1);
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400178 for (rm = *old_tail; rm; rm = rm->next)
Jeff King900f2812013-05-11 18:15:59 +0200179 rm->fetch_head_status = FETCH_HEAD_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400180 }
181}
182
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100183static int add_existing(const char *refname, const unsigned char *sha1,
184 int flag, void *cbdata)
185{
186 struct string_list *list = (struct string_list *)cbdata;
187 struct string_list_item *item = string_list_insert(list, refname);
188 item->util = xmalloc(20);
189 hashcpy(item->util, sha1);
190 return 0;
191}
192
193static int will_fetch(struct ref **head, const unsigned char *sha1)
194{
195 struct ref *rm = *head;
196 while (rm) {
197 if (!hashcmp(rm->old_sha1, sha1))
198 return 1;
199 rm = rm->next;
200 }
201 return 0;
202}
203
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500204static void find_non_local_tags(struct transport *transport,
205 struct ref **head,
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100206 struct ref ***tail)
207{
208 struct string_list existing_refs = STRING_LIST_INIT_DUP;
209 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
210 const struct ref *ref;
211 struct string_list_item *item = NULL;
212
213 for_each_ref(add_existing, &existing_refs);
214 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
Junio C Hamanoad704482013-12-17 11:47:35 -0800215 if (!starts_with(ref->name, "refs/tags/"))
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100216 continue;
217
218 /*
219 * The peeled ref always follows the matching base
220 * ref, so if we see a peeled ref that we don't want
221 * to fetch then we can mark the ref entry in the list
222 * as one to ignore by setting util to NULL.
223 */
Junio C Hamanoad704482013-12-17 11:47:35 -0800224 if (ends_with(ref->name, "^{}")) {
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100225 if (item && !has_sha1_file(ref->old_sha1) &&
226 !will_fetch(head, ref->old_sha1) &&
227 !has_sha1_file(item->util) &&
228 !will_fetch(head, item->util))
229 item->util = NULL;
230 item = NULL;
231 continue;
232 }
233
234 /*
235 * If item is non-NULL here, then we previously saw a
236 * ref not followed by a peeled reference, so we need
237 * to check if it is a lightweight tag that we want to
238 * fetch.
239 */
240 if (item && !has_sha1_file(item->util) &&
241 !will_fetch(head, item->util))
242 item->util = NULL;
243
244 item = NULL;
245
246 /* skip duplicates and refs that we already have */
247 if (string_list_has_string(&remote_refs, ref->name) ||
248 string_list_has_string(&existing_refs, ref->name))
249 continue;
250
251 item = string_list_insert(&remote_refs, ref->name);
252 item->util = (void *)ref->old_sha1;
253 }
254 string_list_clear(&existing_refs, 1);
255
256 /*
257 * We may have a final lightweight tag that needs to be
258 * checked to see if it needs fetching.
259 */
260 if (item && !has_sha1_file(item->util) &&
261 !will_fetch(head, item->util))
262 item->util = NULL;
263
264 /*
265 * For all the tags in the remote_refs string list,
266 * add them to the list of refs to be fetched
267 */
268 for_each_string_list_item(item, &remote_refs) {
269 /* Unless we have already decided to ignore this item... */
270 if (item->util)
271 {
272 struct ref *rm = alloc_ref(item->string);
273 rm->peer_ref = alloc_ref(item->string);
274 hashcpy(rm->old_sha1, item->util);
275 **tail = rm;
276 *tail = &rm->next;
277 }
278 }
279
280 string_list_clear(&remote_refs, 0);
281}
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500282
Daniel Barkalowb888d612007-09-10 23:03:25 -0400283static struct ref *get_ref_map(struct transport *transport,
Michael Haggertyf137a452013-10-23 17:50:38 +0200284 struct refspec *refspecs, int refspec_count,
285 int tags, int *autotags)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400286{
287 int i;
288 struct ref *rm;
289 struct ref *ref_map = NULL;
290 struct ref **tail = &ref_map;
291
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100292 /* opportunistically-updated references: */
293 struct ref *orefs = NULL, **oref_tail = &orefs;
294
Daniel Barkalow45773702007-10-29 21:05:40 -0400295 const struct ref *remote_refs = transport_get_remote_refs(transport);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400296
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100297 if (refspec_count) {
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700298 struct refspec *fetch_refspec;
299 int fetch_refspec_nr;
300
Michael Haggertyf137a452013-10-23 17:50:38 +0200301 for (i = 0; i < refspec_count; i++) {
302 get_fetch_map(remote_refs, &refspecs[i], &tail, 0);
303 if (refspecs[i].dst && refspecs[i].dst[0])
Daniel Barkalowb888d612007-09-10 23:03:25 -0400304 *autotags = 1;
305 }
Michael Haggerty0281c932013-10-30 06:32:58 +0100306 /* Merge everything on the command line (but not --tags) */
Daniel Barkalowb888d612007-09-10 23:03:25 -0400307 for (rm = ref_map; rm; rm = rm->next)
Jeff King900f2812013-05-11 18:15:59 +0200308 rm->fetch_head_status = FETCH_HEAD_MERGE;
Michael Haggerty0281c932013-10-30 06:32:58 +0100309
310 /*
311 * For any refs that we happen to be fetching via
312 * command-line arguments, the destination ref might
313 * have been missing or have been different than the
314 * remote-tracking ref that would be derived from the
315 * configured refspec. In these cases, we want to
316 * take the opportunity to update their configured
317 * remote-tracking reference. However, we do not want
318 * to mention these entries in FETCH_HEAD at all, as
319 * they would simply be duplicates of existing
320 * entries, so we set them FETCH_HEAD_IGNORE below.
321 *
322 * We compute these entries now, based only on the
323 * refspecs specified on the command line. But we add
324 * them to the list following the refspecs resulting
325 * from the tags option so that one of the latter,
326 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
327 * by ref_remove_duplicates() in favor of one of these
328 * opportunistic entries with FETCH_HEAD_IGNORE.
329 */
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700330 if (refmap_array) {
331 fetch_refspec = parse_fetch_refspec(refmap_nr, refmap_array);
332 fetch_refspec_nr = refmap_nr;
333 } else {
334 fetch_refspec = transport->remote->fetch;
335 fetch_refspec_nr = transport->remote->fetch_refspec_nr;
336 }
337
338 for (i = 0; i < fetch_refspec_nr; i++)
339 get_fetch_map(ref_map, &fetch_refspec[i], &oref_tail, 1);
Michael Haggerty0281c932013-10-30 06:32:58 +0100340
Daniel Barkalowe0aaa292008-04-17 19:32:35 -0400341 if (tags == TAGS_SET)
342 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700343 } else if (refmap_array) {
344 die("--refmap option is only meaningful with command-line refspec(s).");
Daniel Barkalowb888d612007-09-10 23:03:25 -0400345 } else {
346 /* Use the defaults */
347 struct remote *remote = transport->remote;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400348 struct branch *branch = branch_get(NULL);
349 int has_merge = branch_has_merge_config(branch);
Brandon Casey3ee17572010-08-25 12:52:56 -0500350 if (remote &&
351 (remote->fetch_refspec_nr ||
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500352 /* Note: has_merge implies non-NULL branch->remote_name */
Brandon Casey3ee17572010-08-25 12:52:56 -0500353 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400354 for (i = 0; i < remote->fetch_refspec_nr; i++) {
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700355 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400356 if (remote->fetch[i].dst &&
357 remote->fetch[i].dst[0])
358 *autotags = 1;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400359 if (!i && !has_merge && ref_map &&
Daniel Barkalowcfb8f892007-09-28 19:34:17 -0400360 !remote->fetch[0].pattern)
Jeff King900f2812013-05-11 18:15:59 +0200361 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400362 }
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100363 /*
364 * if the remote we're fetching from is the same
365 * as given in branch.<name>.remote, we add the
366 * ref given in branch.<name>.merge, too.
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500367 *
368 * Note: has_merge implies non-NULL branch->remote_name
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100369 */
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700370 if (has_merge &&
371 !strcmp(branch->remote_name, remote->name))
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400372 add_merge_config(&ref_map, remote_refs, branch, &tail);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400373 } else {
374 ref_map = get_remote_ref(remote_refs, "HEAD");
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700375 if (!ref_map)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000376 die(_("Couldn't find remote ref HEAD"));
Jeff King900f2812013-05-11 18:15:59 +0200377 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
Shawn O. Pearce5aaf7f22008-03-02 21:34:51 -0500378 tail = &ref_map->next;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400379 }
380 }
Michael Haggerty0281c932013-10-30 06:32:58 +0100381
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100382 if (tags == TAGS_SET)
383 /* also fetch all tags */
384 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
385 else if (tags == TAGS_DEFAULT && *autotags)
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500386 find_non_local_tags(transport, &ref_map, &tail);
Michael Haggerty0281c932013-10-30 06:32:58 +0100387
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100388 /* Now append any refs to be updated opportunistically: */
389 *tail = orefs;
390 for (rm = orefs; rm; rm = rm->next) {
391 rm->fetch_head_status = FETCH_HEAD_IGNORE;
392 tail = &rm->next;
393 }
394
Michael Haggertyb9afe662013-10-30 06:33:09 +0100395 return ref_remove_duplicates(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400396}
397
Jeff Kingfa250752009-05-25 06:40:54 -0400398#define STORE_REF_ERROR_OTHER 1
399#define STORE_REF_ERROR_DF_CONFLICT 2
400
Daniel Barkalowb888d612007-09-10 23:03:25 -0400401static int s_update_ref(const char *action,
402 struct ref *ref,
403 int check_old)
404{
405 char msg[1024];
406 char *rla = getenv("GIT_REFLOG_ACTION");
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700407 struct ref_transaction *transaction;
408 struct strbuf err = STRBUF_INIT;
409 int ret, df_conflict = 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400410
Jay Soffian28a15402009-11-10 09:19:43 +0100411 if (dry_run)
412 return 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400413 if (!rla)
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -0500414 rla = default_rla.buf;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400415 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700416
417 transaction = ref_transaction_begin(&err);
418 if (!transaction ||
Michael Haggerty1d147bd2015-02-17 18:00:15 +0100419 ref_transaction_update(transaction, ref->name,
420 ref->new_sha1,
421 check_old ? ref->old_sha1 : NULL,
422 0, msg, &err))
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700423 goto fail;
424
425 ret = ref_transaction_commit(transaction, &err);
426 if (ret) {
427 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
428 goto fail;
429 }
430
431 ref_transaction_free(transaction);
432 strbuf_release(&err);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400433 return 0;
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700434fail:
435 ref_transaction_free(transaction);
436 error("%s", err.buf);
437 strbuf_release(&err);
438 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
439 : STORE_REF_ERROR_OTHER;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400440}
441
Pierre Habouzit9ef42722007-11-04 15:05:45 -0800442#define REFCOL_WIDTH 10
Nicolas Pitre165f3902007-11-03 01:32:48 -0400443
Daniel Barkalowb888d612007-09-10 23:03:25 -0400444static int update_local_ref(struct ref *ref,
Nicolas Pitre165f3902007-11-03 01:32:48 -0400445 const char *remote,
Marc Branchaud6da618d2012-04-16 18:08:49 -0400446 const struct ref *remote_ref,
Jeff King5914f2d2011-12-08 03:43:19 -0500447 struct strbuf *display)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400448{
Daniel Barkalowb888d612007-09-10 23:03:25 -0400449 struct commit *current = NULL, *updated;
450 enum object_type type;
451 struct branch *current_branch = branch_get(NULL);
Felipe Contreras4577e482009-05-14 00:22:04 +0300452 const char *pretty_ref = prettify_refname(ref->name);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400453
454 type = sha1_object_info(ref->new_sha1, NULL);
455 if (type < 0)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000456 die(_("object %s not found"), sha1_to_hex(ref->new_sha1));
Daniel Barkalowb888d612007-09-10 23:03:25 -0400457
Daniel Barkalowb888d612007-09-10 23:03:25 -0400458 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100459 if (verbosity > 0)
Jeff King5914f2d2011-12-08 03:43:19 -0500460 strbuf_addf(display, "= %-*s %-*s -> %s",
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700461 TRANSPORT_SUMMARY(_("[up to date]")),
462 REFCOL_WIDTH, remote, pretty_ref);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400463 return 0;
464 }
465
Shawn O. Pearceb3abdd92007-09-16 02:31:26 -0400466 if (current_branch &&
467 !strcmp(ref->name, current_branch->name) &&
Daniel Barkalowb888d612007-09-10 23:03:25 -0400468 !(update_head_ok || is_bare_repository()) &&
469 !is_null_sha1(ref->old_sha1)) {
470 /*
471 * If this is the head, and it's not okay to update
472 * the head, and the old value of the head isn't empty...
473 */
Jeff King5914f2d2011-12-08 03:43:19 -0500474 strbuf_addf(display,
475 _("! %-*s %-*s -> %s (can't fetch in current branch)"),
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700476 TRANSPORT_SUMMARY(_("[rejected]")),
Jeff King5914f2d2011-12-08 03:43:19 -0500477 REFCOL_WIDTH, remote, pretty_ref);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400478 return 1;
479 }
480
481 if (!is_null_sha1(ref->old_sha1) &&
Christian Couder59556542013-11-30 21:55:40 +0100482 starts_with(ref->name, "refs/tags/")) {
Jeff King63154722008-06-26 23:59:50 -0400483 int r;
484 r = s_update_ref("updating tag", ref, 0);
Jeff King5914f2d2011-12-08 03:43:19 -0500485 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
486 r ? '!' : '-',
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700487 TRANSPORT_SUMMARY(_("[tag update]")),
Jeff King5914f2d2011-12-08 03:43:19 -0500488 REFCOL_WIDTH, remote, pretty_ref,
489 r ? _(" (unable to update local ref)") : "");
Jeff King63154722008-06-26 23:59:50 -0400490 return r;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400491 }
492
Shawn O. Pearcecfa5b2b2007-10-19 00:54:59 -0400493 current = lookup_commit_reference_gently(ref->old_sha1, 1);
494 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400495 if (!current || !updated) {
Nicolas Pitre165f3902007-11-03 01:32:48 -0400496 const char *msg;
497 const char *what;
Jeff King63154722008-06-26 23:59:50 -0400498 int r;
Marc Branchaud0997ada2012-04-16 18:08:50 -0400499 /*
500 * Nicely describe the new ref we're fetching.
501 * Base this on the remote's ref name, as it's
502 * more likely to follow a standard layout.
503 */
504 const char *name = remote_ref ? remote_ref->name : "";
Christian Couder59556542013-11-30 21:55:40 +0100505 if (starts_with(name, "refs/tags/")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400506 msg = "storing tag";
Ævar Arnfjörð Bjarmasonf7b37422011-02-22 23:41:53 +0000507 what = _("[new tag]");
Christian Couder59556542013-11-30 21:55:40 +0100508 } else if (starts_with(name, "refs/heads/")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400509 msg = "storing head";
Ævar Arnfjörð Bjarmasonf7b37422011-02-22 23:41:53 +0000510 what = _("[new branch]");
Marc Branchaud0997ada2012-04-16 18:08:50 -0400511 } else {
512 msg = "storing ref";
513 what = _("[new ref]");
Nicolas Pitre165f3902007-11-03 01:32:48 -0400514 }
515
Jens Lehmanna6801ad2012-04-13 18:25:16 +0200516 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
517 (recurse_submodules != RECURSE_SUBMODULES_ON))
518 check_for_new_submodule_commits(ref->new_sha1);
Jeff King63154722008-06-26 23:59:50 -0400519 r = s_update_ref(msg, ref, 0);
Jeff King5914f2d2011-12-08 03:43:19 -0500520 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
521 r ? '!' : '*',
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700522 TRANSPORT_SUMMARY(what),
Jeff King5914f2d2011-12-08 03:43:19 -0500523 REFCOL_WIDTH, remote, pretty_ref,
524 r ? _(" (unable to update local ref)") : "");
Jeff King63154722008-06-26 23:59:50 -0400525 return r;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400526 }
527
Junio C Hamanoa20efee2012-08-27 14:46:01 -0700528 if (in_merge_bases(current, updated)) {
Nicolas Pitre165f3902007-11-03 01:32:48 -0400529 char quickref[83];
Jeff King63154722008-06-26 23:59:50 -0400530 int r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400531 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
532 strcat(quickref, "..");
533 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
Jens Lehmann88a21972011-03-06 23:10:46 +0100534 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
535 (recurse_submodules != RECURSE_SUBMODULES_ON))
536 check_for_new_submodule_commits(ref->new_sha1);
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300537 r = s_update_ref("fast-forward", ref, 1);
Jeff King5914f2d2011-12-08 03:43:19 -0500538 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
539 r ? '!' : ' ',
540 TRANSPORT_SUMMARY_WIDTH, quickref,
541 REFCOL_WIDTH, remote, pretty_ref,
542 r ? _(" (unable to update local ref)") : "");
Jeff King63154722008-06-26 23:59:50 -0400543 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400544 } else if (force || ref->force) {
545 char quickref[84];
Jeff King63154722008-06-26 23:59:50 -0400546 int r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400547 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
548 strcat(quickref, "...");
549 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
Jens Lehmann88a21972011-03-06 23:10:46 +0100550 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
551 (recurse_submodules != RECURSE_SUBMODULES_ON))
552 check_for_new_submodule_commits(ref->new_sha1);
Jeff King63154722008-06-26 23:59:50 -0400553 r = s_update_ref("forced-update", ref, 1);
Jeff King5914f2d2011-12-08 03:43:19 -0500554 strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
555 r ? '!' : '+',
556 TRANSPORT_SUMMARY_WIDTH, quickref,
557 REFCOL_WIDTH, remote, pretty_ref,
558 r ? _("unable to update local ref") : _("forced update"));
Jeff King63154722008-06-26 23:59:50 -0400559 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400560 } else {
Jeff King5914f2d2011-12-08 03:43:19 -0500561 strbuf_addf(display, "! %-*s %-*s -> %s %s",
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700562 TRANSPORT_SUMMARY(_("[rejected]")),
Jeff King5914f2d2011-12-08 03:43:19 -0500563 REFCOL_WIDTH, remote, pretty_ref,
564 _("(non-fast-forward)"));
Daniel Barkalowb888d612007-09-10 23:03:25 -0400565 return 1;
566 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400567}
568
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700569static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700570{
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700571 struct ref **rm = cb_data;
572 struct ref *ref = *rm;
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700573
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +0700574 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
575 ref = ref->next;
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700576 if (!ref)
577 return -1; /* end of the list */
578 *rm = ref->next;
579 hashcpy(sha1, ref->old_sha1);
580 return 0;
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700581}
582
Andreas Ericsson47abd852009-04-17 10:20:11 +0200583static int store_updated_refs(const char *raw_url, const char *remote_name,
Jeff Kingf3cb1692008-06-27 00:01:41 -0400584 struct ref *ref_map)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400585{
586 FILE *fp;
587 struct commit *commit;
Tom Miller4b3b33a2014-01-02 20:28:51 -0600588 int url_len, i, rc = 0;
Jeff King5914f2d2011-12-08 03:43:19 -0500589 struct strbuf note = STRBUF_INIT;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400590 const char *what, *kind;
591 struct ref *rm;
Jay Soffian28a15402009-11-10 09:19:43 +0100592 char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
Jeff King900f2812013-05-11 18:15:59 +0200593 int want_status;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400594
André Goddard Rosad6617c72007-11-22 20:22:23 -0200595 fp = fopen(filename, "a");
596 if (!fp)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000597 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
Andreas Ericsson47abd852009-04-17 10:20:11 +0200598
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100599 if (raw_url)
600 url = transport_anonymize_url(raw_url);
601 else
602 url = xstrdup("foreign");
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700603
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700604 rm = ref_map;
Junio C Hamano3f7d11c2011-10-17 21:37:12 -0700605 if (check_everything_connected(iterate_ref_map, 0, &rm)) {
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800606 rc = error(_("%s did not send all necessary objects\n"), url);
607 goto abort;
608 }
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700609
Joey Hess96890f42011-12-26 12:16:56 -0400610 /*
Jeff King900f2812013-05-11 18:15:59 +0200611 * We do a pass for each fetch_head_status type in their enum order, so
612 * merged entries are written before not-for-merge. That lets readers
613 * use FETCH_HEAD as a refname to refer to the ref to be merged.
Joey Hess96890f42011-12-26 12:16:56 -0400614 */
Jeff King900f2812013-05-11 18:15:59 +0200615 for (want_status = FETCH_HEAD_MERGE;
616 want_status <= FETCH_HEAD_IGNORE;
617 want_status++) {
Joey Hess96890f42011-12-26 12:16:56 -0400618 for (rm = ref_map; rm; rm = rm->next) {
619 struct ref *ref = NULL;
Jeff King900f2812013-05-11 18:15:59 +0200620 const char *merge_status_marker = "";
Daniel Barkalowb888d612007-09-10 23:03:25 -0400621
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +0700622 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
623 if (want_status == FETCH_HEAD_MERGE)
624 warning(_("reject %s because shallow roots are not allowed to be updated"),
625 rm->peer_ref ? rm->peer_ref->name : rm->name);
626 continue;
627 }
628
Joey Hess96890f42011-12-26 12:16:56 -0400629 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
630 if (!commit)
Jeff King900f2812013-05-11 18:15:59 +0200631 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400632
Jeff King900f2812013-05-11 18:15:59 +0200633 if (rm->fetch_head_status != want_status)
Joey Hess96890f42011-12-26 12:16:56 -0400634 continue;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400635
Joey Hess96890f42011-12-26 12:16:56 -0400636 if (rm->peer_ref) {
637 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
638 strcpy(ref->name, rm->peer_ref->name);
639 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
640 hashcpy(ref->new_sha1, rm->old_sha1);
641 ref->force = rm->peer_ref->force;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400642 }
Joey Hess96890f42011-12-26 12:16:56 -0400643
644
645 if (!strcmp(rm->name, "HEAD")) {
646 kind = "";
647 what = "";
648 }
Christian Couder59556542013-11-30 21:55:40 +0100649 else if (starts_with(rm->name, "refs/heads/")) {
Joey Hess96890f42011-12-26 12:16:56 -0400650 kind = "branch";
651 what = rm->name + 11;
652 }
Christian Couder59556542013-11-30 21:55:40 +0100653 else if (starts_with(rm->name, "refs/tags/")) {
Joey Hess96890f42011-12-26 12:16:56 -0400654 kind = "tag";
655 what = rm->name + 10;
656 }
Christian Couder59556542013-11-30 21:55:40 +0100657 else if (starts_with(rm->name, "refs/remotes/")) {
Joey Hess96890f42011-12-26 12:16:56 -0400658 kind = "remote-tracking branch";
659 what = rm->name + 13;
660 }
661 else {
662 kind = "";
663 what = rm->name;
664 }
665
666 url_len = strlen(url);
667 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
668 ;
669 url_len = i + 1;
670 if (4 < i && !strncmp(".git", url + i - 3, 4))
671 url_len = i - 3;
672
673 strbuf_reset(&note);
674 if (*what) {
675 if (*kind)
676 strbuf_addf(&note, "%s ", kind);
677 strbuf_addf(&note, "'%s' of ", what);
678 }
Jeff King900f2812013-05-11 18:15:59 +0200679 switch (rm->fetch_head_status) {
680 case FETCH_HEAD_NOT_FOR_MERGE:
681 merge_status_marker = "not-for-merge";
682 /* fall-through */
683 case FETCH_HEAD_MERGE:
684 fprintf(fp, "%s\t%s\t%s",
685 sha1_to_hex(rm->old_sha1),
686 merge_status_marker,
687 note.buf);
688 for (i = 0; i < url_len; ++i)
689 if ('\n' == url[i])
690 fputs("\\n", fp);
691 else
692 fputc(url[i], fp);
693 fputc('\n', fp);
694 break;
695 default:
696 /* do not write anything to FETCH_HEAD */
697 break;
698 }
Joey Hess96890f42011-12-26 12:16:56 -0400699
700 strbuf_reset(&note);
701 if (ref) {
Marc Branchaud6da618d2012-04-16 18:08:49 -0400702 rc |= update_local_ref(ref, what, rm, &note);
Joey Hess96890f42011-12-26 12:16:56 -0400703 free(ref);
704 } else
705 strbuf_addf(&note, "* %-*s %-*s -> FETCH_HEAD",
706 TRANSPORT_SUMMARY_WIDTH,
707 *kind ? kind : "branch",
708 REFCOL_WIDTH,
709 *what ? what : "HEAD");
710 if (note.len) {
711 if (verbosity >= 0 && !shown_url) {
712 fprintf(stderr, _("From %.*s\n"),
713 url_len, url);
714 shown_url = 1;
715 }
716 if (verbosity >= 0)
717 fprintf(stderr, " %s\n", note.buf);
718 }
Nicolas Pitre165f3902007-11-03 01:32:48 -0400719 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400720 }
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800721
Jeff Kingfa250752009-05-25 06:40:54 -0400722 if (rc & STORE_REF_ERROR_DF_CONFLICT)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000723 error(_("some local refs could not be updated; try running\n"
Jeff Kingf3cb1692008-06-27 00:01:41 -0400724 " 'git remote prune %s' to remove any old, conflicting "
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000725 "branches"), remote_name);
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800726
727 abort:
Jeff King5914f2d2011-12-08 03:43:19 -0500728 strbuf_release(&note);
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800729 free(url);
730 fclose(fp);
Dmitry V. Levinefb98b42008-05-28 19:29:36 +0400731 return rc;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400732}
733
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500734/*
735 * We would want to bypass the object transfer altogether if
Johan Herlandd9eb0202009-07-10 01:52:30 +0200736 * everything we are going to fetch already exists and is connected
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500737 * locally.
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500738 */
739static int quickfetch(struct ref *ref_map)
740{
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700741 struct ref *rm = ref_map;
742
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500743 /*
744 * If we are deepening a shallow clone we already have these
745 * objects reachable. Running rev-list here will return with
746 * a good (0) exit status and we'll bypass the fetch that we
747 * really need to perform. Claiming failure now will ensure
748 * we perform the network exchange to deepen our history.
749 */
750 if (depth)
751 return -1;
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700752 return check_everything_connected(iterate_ref_map, 1, &rm);
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500753}
754
Daniel Barkalowb888d612007-09-10 23:03:25 -0400755static int fetch_refs(struct transport *transport, struct ref *ref_map)
756{
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500757 int ret = quickfetch(ref_map);
758 if (ret)
759 ret = transport_fetch_refs(transport, ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400760 if (!ret)
Jeff Kingf3cb1692008-06-27 00:01:41 -0400761 ret |= store_updated_refs(transport->url,
762 transport->remote->name,
763 ref_map);
Shawn O. Pearce1788c392007-09-14 03:31:23 -0400764 transport_unlock_pack(transport);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400765 return ret;
766}
767
Tom Miller4b3b33a2014-01-02 20:28:51 -0600768static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
769 const char *raw_url)
Jay Soffianf360d842009-11-10 09:15:47 +0100770{
Tom Miller4b3b33a2014-01-02 20:28:51 -0600771 int url_len, i, result = 0;
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200772 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
Tom Miller4b3b33a2014-01-02 20:28:51 -0600773 char *url;
Jay Soffianf360d842009-11-10 09:15:47 +0100774 const char *dangling_msg = dry_run
Nguyễn Thái Ngọc Duy18986d52012-04-23 19:30:25 +0700775 ? _(" (%s will become dangling)")
776 : _(" (%s has become dangling)");
Jay Soffianf360d842009-11-10 09:15:47 +0100777
Tom Miller4b3b33a2014-01-02 20:28:51 -0600778 if (raw_url)
779 url = transport_anonymize_url(raw_url);
780 else
781 url = xstrdup("foreign");
782
783 url_len = strlen(url);
784 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
785 ;
786
787 url_len = i + 1;
788 if (4 < i && !strncmp(".git", url + i - 3, 4))
789 url_len = i - 3;
790
Jay Soffianf360d842009-11-10 09:15:47 +0100791 for (ref = stale_refs; ref; ref = ref->next) {
792 if (!dry_run)
793 result |= delete_ref(ref->name, NULL, 0);
Tom Miller4b3b33a2014-01-02 20:28:51 -0600794 if (verbosity >= 0 && !shown_url) {
795 fprintf(stderr, _("From %.*s\n"), url_len, url);
796 shown_url = 1;
797 }
Jay Soffianf360d842009-11-10 09:15:47 +0100798 if (verbosity >= 0) {
799 fprintf(stderr, " x %-*s %-*s -> %s\n",
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700800 TRANSPORT_SUMMARY(_("[deleted]")),
Ævar Arnfjörð Bjarmason502681c2011-02-22 23:41:52 +0000801 REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
Jay Soffianf360d842009-11-10 09:15:47 +0100802 warn_dangling_symref(stderr, dangling_msg, ref->name);
803 }
804 }
Tom Miller4b3b33a2014-01-02 20:28:51 -0600805 free(url);
Jay Soffianf360d842009-11-10 09:15:47 +0100806 free_refs(stale_refs);
807 return result;
808}
809
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200810static void check_not_current_branch(struct ref *ref_map)
811{
812 struct branch *current_branch = branch_get(NULL);
813
814 if (is_bare_repository() || !current_branch)
815 return;
816
817 for (; ref_map; ref_map = ref_map->next)
818 if (ref_map->peer_ref && !strcmp(current_branch->refname,
819 ref_map->peer_ref->name))
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000820 die(_("Refusing to fetch into current branch %s "
821 "of non-bare repository"), current_branch->refname);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200822}
823
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800824static int truncate_fetch_head(void)
825{
826 char *filename = git_path("FETCH_HEAD");
827 FILE *fp = fopen(filename, "w");
828
829 if (!fp)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000830 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800831 fclose(fp);
832 return 0;
833}
834
Junio C Hamanodb5723c2013-08-07 14:43:20 -0700835static void set_option(struct transport *transport, const char *name, const char *value)
836{
837 int r = transport_set_option(transport, name, value);
838 if (r < 0)
839 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
840 name, value, transport->url);
841 if (r > 0)
842 warning(_("Option \"%s\" is ignored for %s\n"),
843 name, transport->url);
844}
845
Ramsay Jones0f73f8b2013-08-28 19:56:17 +0100846static struct transport *prepare_transport(struct remote *remote)
Junio C Hamanodb5723c2013-08-07 14:43:20 -0700847{
848 struct transport *transport;
849 transport = transport_get(remote, NULL);
850 transport_set_verbosity(transport, verbosity, progress);
851 if (upload_pack)
852 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
853 if (keep)
854 set_option(transport, TRANS_OPT_KEEP, "yes");
855 if (depth)
856 set_option(transport, TRANS_OPT_DEPTH, depth);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +0700857 if (update_shallow)
858 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
Junio C Hamanodb5723c2013-08-07 14:43:20 -0700859 return transport;
860}
861
Junio C Hamano069d5032013-08-07 15:14:45 -0700862static void backfill_tags(struct transport *transport, struct ref *ref_map)
863{
Junio C Hamanob26ed432013-08-07 15:47:18 -0700864 if (transport->cannot_reuse) {
865 gsecondary = prepare_transport(transport->remote);
866 transport = gsecondary;
867 }
868
Junio C Hamano069d5032013-08-07 15:14:45 -0700869 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
870 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
871 fetch_refs(transport, ref_map);
Junio C Hamanob26ed432013-08-07 15:47:18 -0700872
873 if (gsecondary) {
874 transport_disconnect(gsecondary);
875 gsecondary = NULL;
876 }
Junio C Hamano069d5032013-08-07 15:14:45 -0700877}
878
Daniel Barkalowb888d612007-09-10 23:03:25 -0400879static int do_fetch(struct transport *transport,
880 struct refspec *refs, int ref_count)
881{
Michael Haggertyb87dbcc2013-05-25 11:08:01 +0200882 struct string_list existing_refs = STRING_LIST_INIT_DUP;
Shawn O. Pearce7f984282008-03-02 21:34:43 -0500883 struct ref *ref_map;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400884 struct ref *rm;
885 int autotags = (transport->remote->fetch_tags == 1);
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200886 int retcode = 0;
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000887
888 for_each_ref(add_existing, &existing_refs);
889
Daniel Johnsoned368542010-08-11 18:57:20 -0400890 if (tags == TAGS_DEFAULT) {
891 if (transport->remote->fetch_tags == 2)
892 tags = TAGS_SET;
893 if (transport->remote->fetch_tags == -1)
894 tags = TAGS_UNSET;
895 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400896
Shawn O. Pearce824d5772007-09-19 00:49:31 -0400897 if (!transport->get_refs_list || !transport->fetch)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000898 die(_("Don't know how to fetch from %s"), transport->url);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400899
900 /* if not appending, truncate FETCH_HEAD */
Jay Soffian28a15402009-11-10 09:19:43 +0100901 if (!append && !dry_run) {
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200902 retcode = truncate_fetch_head();
903 if (retcode)
904 goto cleanup;
André Goddard Rosad6617c72007-11-22 20:22:23 -0200905 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400906
907 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200908 if (!update_head_ok)
909 check_not_current_branch(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400910
911 for (rm = ref_map; rm; rm = rm->next) {
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000912 if (rm->peer_ref) {
Michael Haggerty6f64a162013-05-25 11:08:15 +0200913 struct string_list_item *peer_item =
914 string_list_lookup(&existing_refs,
915 rm->peer_ref->name);
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000916 if (peer_item)
917 hashcpy(rm->peer_ref->old_sha1,
918 peer_item->util);
919 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400920 }
921
Shawn O. Pearce41fa7d22008-03-03 22:27:40 -0500922 if (tags == TAGS_DEFAULT && autotags)
923 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200924 if (prune) {
Michael Haggerty0838bf42013-10-30 06:33:00 +0100925 /*
926 * We only prune based on refspecs specified
927 * explicitly (via command line or configuration); we
928 * don't care whether --tags was specified.
929 */
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100930 if (ref_count) {
Tom Miller4b3b33a2014-01-02 20:28:51 -0600931 prune_refs(refs, ref_count, ref_map, transport->url);
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100932 } else {
Michael Haggerty0838bf42013-10-30 06:33:00 +0100933 prune_refs(transport->remote->fetch,
934 transport->remote->fetch_refspec_nr,
Tom Miller4b3b33a2014-01-02 20:28:51 -0600935 ref_map,
936 transport->url);
Carlos Martín Nietoe8c1e6c2011-10-15 07:04:26 +0200937 }
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200938 }
Tom Miller10a6cc82014-01-02 20:28:52 -0600939 if (fetch_refs(transport, ref_map)) {
940 free_refs(ref_map);
941 retcode = 1;
942 goto cleanup;
943 }
Shawn O. Pearce7f984282008-03-02 21:34:43 -0500944 free_refs(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400945
946 /* if neither --no-tags nor --tags was specified, do automated tag
947 * following ... */
Kristian Høgsberg83201992007-12-04 02:25:47 -0500948 if (tags == TAGS_DEFAULT && autotags) {
Shawn O. Pearcec50b2b42008-03-02 21:35:00 -0500949 struct ref **tail = &ref_map;
950 ref_map = NULL;
951 find_non_local_tags(transport, &ref_map, &tail);
Junio C Hamano069d5032013-08-07 15:14:45 -0700952 if (ref_map)
953 backfill_tags(transport, ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400954 free_refs(ref_map);
955 }
956
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200957 cleanup:
Michael Haggertyf83918e2013-05-25 11:08:17 +0200958 string_list_clear(&existing_refs, 1);
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200959 return retcode;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400960}
961
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100962static int get_one_remote_for_fetch(struct remote *remote, void *priv)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400963{
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100964 struct string_list *list = priv;
Björn Gustavsson7cc91a22009-11-09 21:11:06 +0100965 if (!remote->skip_default_update)
Julian Phillips1d2f80f2010-06-26 00:41:38 +0100966 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100967 return 0;
968}
969
970struct remote_group_data {
971 const char *name;
972 struct string_list *list;
973};
974
975static int get_remote_group(const char *key, const char *value, void *priv)
976{
977 struct remote_group_data *g = priv;
978
Christian Couder59556542013-11-30 21:55:40 +0100979 if (starts_with(key, "remotes.") &&
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100980 !strcmp(key + 8, g->name)) {
981 /* split list by white space */
982 int space = strcspn(value, " \t\n");
983 while (*value) {
984 if (space > 1) {
Julian Phillips1d2f80f2010-06-26 00:41:38 +0100985 string_list_append(g->list,
986 xstrndup(value, space));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100987 }
988 value += space + (value[space] != '\0');
989 space = strcspn(value, " \t\n");
990 }
991 }
992
993 return 0;
994}
995
996static int add_remote_or_group(const char *name, struct string_list *list)
997{
998 int prev_nr = list->nr;
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000999 struct remote_group_data g;
1000 g.name = name; g.list = list;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001001
1002 git_config(get_remote_group, &g);
1003 if (list->nr == prev_nr) {
1004 struct remote *remote;
1005 if (!remote_is_configured(name))
1006 return 0;
1007 remote = remote_get(name);
Julian Phillips1d2f80f2010-06-26 00:41:38 +01001008 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001009 }
1010 return 1;
1011}
1012
Jeff King85556d42012-09-01 07:27:35 -04001013static void add_options_to_argv(struct argv_array *argv)
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001014{
1015 if (dry_run)
Jeff King85556d42012-09-01 07:27:35 -04001016 argv_array_push(argv, "--dry-run");
Michael Haggerty90765fa2013-10-30 06:33:04 +01001017 if (prune != -1)
1018 argv_array_push(argv, prune ? "--prune" : "--no-prune");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001019 if (update_head_ok)
Jeff King85556d42012-09-01 07:27:35 -04001020 argv_array_push(argv, "--update-head-ok");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001021 if (force)
Jeff King85556d42012-09-01 07:27:35 -04001022 argv_array_push(argv, "--force");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001023 if (keep)
Jeff King85556d42012-09-01 07:27:35 -04001024 argv_array_push(argv, "--keep");
Jens Lehmannbe254a02010-11-11 00:55:02 +01001025 if (recurse_submodules == RECURSE_SUBMODULES_ON)
Jeff King85556d42012-09-01 07:27:35 -04001026 argv_array_push(argv, "--recurse-submodules");
Jens Lehmann8f0700d2011-03-06 23:11:21 +01001027 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
Jeff King85556d42012-09-01 07:27:35 -04001028 argv_array_push(argv, "--recurse-submodules=on-demand");
Dan Johnson85566462012-09-05 17:22:19 -04001029 if (tags == TAGS_SET)
1030 argv_array_push(argv, "--tags");
1031 else if (tags == TAGS_UNSET)
1032 argv_array_push(argv, "--no-tags");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001033 if (verbosity >= 2)
Jeff King85556d42012-09-01 07:27:35 -04001034 argv_array_push(argv, "-v");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001035 if (verbosity >= 1)
Jeff King85556d42012-09-01 07:27:35 -04001036 argv_array_push(argv, "-v");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001037 else if (verbosity < 0)
Jeff King85556d42012-09-01 07:27:35 -04001038 argv_array_push(argv, "-q");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001039
1040}
1041
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001042static int fetch_multiple(struct string_list *list)
1043{
1044 int i, result = 0;
Jeff King85556d42012-09-01 07:27:35 -04001045 struct argv_array argv = ARGV_ARRAY_INIT;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001046
Junio C Hamanoe6cc5102010-02-24 11:02:05 -08001047 if (!append && !dry_run) {
1048 int errcode = truncate_fetch_head();
1049 if (errcode)
1050 return errcode;
1051 }
1052
Jeff King85556d42012-09-01 07:27:35 -04001053 argv_array_pushl(&argv, "fetch", "--append", NULL);
1054 add_options_to_argv(&argv);
1055
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001056 for (i = 0; i < list->nr; i++) {
1057 const char *name = list->items[i].string;
Jeff King85556d42012-09-01 07:27:35 -04001058 argv_array_push(&argv, name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001059 if (verbosity >= 0)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001060 printf(_("Fetching %s\n"), name);
Jeff King85556d42012-09-01 07:27:35 -04001061 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001062 error(_("Could not fetch %s"), name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001063 result = 1;
1064 }
Jeff King85556d42012-09-01 07:27:35 -04001065 argv_array_pop(&argv);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001066 }
1067
Jeff King85556d42012-09-01 07:27:35 -04001068 argv_array_clear(&argv);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001069 return result;
1070}
1071
1072static int fetch_one(struct remote *remote, int argc, const char **argv)
1073{
Daniel Barkalowb888d612007-09-10 23:03:25 -04001074 static const char **refs = NULL;
Jim Meyeringd8ead152011-06-08 22:06:33 +02001075 struct refspec *refspec;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001076 int ref_nr = 0;
Alex Riesen7b7f39e2008-04-28 22:23:35 +02001077 int exit_code;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001078
Daniel Barkalowfa685bd2009-03-11 01:47:20 -04001079 if (!remote)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001080 die(_("No remote repository specified. Please, specify either a URL or a\n"
1081 "remote name from which new revisions should be fetched."));
Daniel Barkalowfa685bd2009-03-11 01:47:20 -04001082
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001083 gtransport = prepare_transport(remote);
Michael Schubert737c5a92013-07-13 11:36:24 +02001084
1085 if (prune < 0) {
1086 /* no command line request */
Junio C Hamano20419de2013-09-09 14:50:37 -07001087 if (0 <= gtransport->remote->prune)
1088 prune = gtransport->remote->prune;
Michael Schubert737c5a92013-07-13 11:36:24 +02001089 else if (0 <= fetch_prune_config)
1090 prune = fetch_prune_config;
1091 else
1092 prune = PRUNE_BY_DEFAULT;
1093 }
1094
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001095 if (argc > 0) {
Daniel Barkalowb888d612007-09-10 23:03:25 -04001096 int j = 0;
Elia Pintobf7e6452014-01-29 08:54:16 -08001097 int i;
Kristian Høgsberg83201992007-12-04 02:25:47 -05001098 refs = xcalloc(argc + 1, sizeof(const char *));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001099 for (i = 0; i < argc; i++) {
Daniel Barkalowb888d612007-09-10 23:03:25 -04001100 if (!strcmp(argv[i], "tag")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -04001101 i++;
Kevin Ballardf53423b2008-04-05 14:28:53 -04001102 if (i >= argc)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001103 die(_("You need to specify a tag name."));
Jeff Kingb2724c82014-06-19 17:26:56 -04001104 refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1105 argv[i], argv[i]);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001106 } else
1107 refs[j++] = argv[i];
Daniel Barkalowb888d612007-09-10 23:03:25 -04001108 }
1109 refs[j] = NULL;
1110 ref_nr = j;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001111 }
1112
Jeff King57b235a2009-01-22 01:03:08 -05001113 sigchain_push_common(unlock_pack_on_signal);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -04001114 atexit(unlock_pack);
Jim Meyeringd8ead152011-06-08 22:06:33 +02001115 refspec = parse_fetch_refspec(ref_nr, refs);
Junio C Hamanoaf234452013-08-07 15:38:45 -07001116 exit_code = do_fetch(gtransport, refspec, ref_nr);
Carlos Martín Nieto5caf1972011-10-08 00:51:06 +02001117 free_refspec(ref_nr, refspec);
Junio C Hamanoaf234452013-08-07 15:38:45 -07001118 transport_disconnect(gtransport);
1119 gtransport = NULL;
Alex Riesen7b7f39e2008-04-28 22:23:35 +02001120 return exit_code;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001121}
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001122
1123int cmd_fetch(int argc, const char **argv, const char *prefix)
1124{
1125 int i;
Thiago Farina183113a2010-07-04 16:46:19 -03001126 struct string_list list = STRING_LIST_INIT_NODUP;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001127 struct remote *remote;
1128 int result = 0;
Nguyễn Thái Ngọc Duy19910062014-08-16 08:19:27 +07001129 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001130
Jeff Kingbbc30f92011-02-24 09:30:19 -05001131 packet_trace_identity("fetch");
1132
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001133 /* Record the command line for the reflog */
1134 strbuf_addstr(&default_rla, "fetch");
1135 for (i = 1; i < argc; i++)
1136 strbuf_addf(&default_rla, " %s", argv[i]);
1137
Michael Schubert737c5a92013-07-13 11:36:24 +02001138 git_config(git_fetch_config, NULL);
1139
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001140 argc = parse_options(argc, argv, prefix,
1141 builtin_fetch_options, builtin_fetch_usage, 0);
1142
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +07001143 if (unshallow) {
1144 if (depth)
1145 die(_("--depth and --unshallow cannot be used together"));
1146 else if (!is_repository_shallow())
1147 die(_("--unshallow on a complete repository does not make sense"));
1148 else {
1149 static char inf_depth[12];
1150 sprintf(inf_depth, "%d", INFINITE_DEPTH);
1151 depth = inf_depth;
1152 }
1153 }
1154
Nguyễn Thái Ngọc Duy5594bca2013-12-05 10:31:11 +07001155 /* no need to be strict, transport_set_option() will validate it again */
1156 if (depth && atoi(depth) < 1)
1157 die(_("depth %s is not a positive number"), depth);
1158
Jens Lehmann18322ba2011-09-09 20:22:03 +02001159 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1160 if (recurse_submodules_default) {
1161 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
1162 set_config_fetch_recurse_submodules(arg);
1163 }
1164 gitmodules_config();
1165 git_config(submodule_config, NULL);
1166 }
1167
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001168 if (all) {
1169 if (argc == 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001170 die(_("fetch --all does not take a repository argument"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001171 else if (argc > 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001172 die(_("fetch --all does not make sense with refspecs"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001173 (void) for_each_remote(get_one_remote_for_fetch, &list);
1174 result = fetch_multiple(&list);
1175 } else if (argc == 0) {
1176 /* No arguments -- use default remote */
1177 remote = remote_get(NULL);
1178 result = fetch_one(remote, argc, argv);
Björn Gustavsson16679e32009-11-09 21:10:32 +01001179 } else if (multiple) {
1180 /* All arguments are assumed to be remotes or groups */
1181 for (i = 0; i < argc; i++)
1182 if (!add_remote_or_group(argv[i], &list))
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001183 die(_("No such remote or remote group: %s"), argv[i]);
Björn Gustavsson16679e32009-11-09 21:10:32 +01001184 result = fetch_multiple(&list);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001185 } else {
1186 /* Single remote or group */
1187 (void) add_remote_or_group(argv[0], &list);
1188 if (list.nr > 1) {
1189 /* More than one remote */
1190 if (argc > 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001191 die(_("Fetching a group and specifying refspecs does not make sense"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001192 result = fetch_multiple(&list);
1193 } else {
1194 /* Zero or one remotes */
1195 remote = remote_get(argv[0]);
1196 result = fetch_one(remote, argc-1, argv+1);
1197 }
1198 }
1199
Jens Lehmannbe254a02010-11-11 00:55:02 +01001200 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
Jeff King85556d42012-09-01 07:27:35 -04001201 struct argv_array options = ARGV_ARRAY_INIT;
1202
1203 add_options_to_argv(&options);
Jens Lehmann50d89ad2012-09-01 17:27:06 +02001204 result = fetch_populated_submodules(&options,
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001205 submodule_prefix,
Jens Lehmann8f0700d2011-03-06 23:11:21 +01001206 recurse_submodules,
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001207 verbosity < 0);
Jeff King85556d42012-09-01 07:27:35 -04001208 argv_array_clear(&options);
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001209 }
1210
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001211 /* All names were strdup()ed or strndup()ed */
1212 list.strdup_strings = 1;
1213 string_list_clear(&list, 0);
1214
Nguyễn Thái Ngọc Duy19910062014-08-16 08:19:27 +07001215 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
Nguyễn Thái Ngọc Duy6fceed32014-08-16 08:19:28 +07001216 if (verbosity < 0)
1217 argv_array_push(&argv_gc_auto, "--quiet");
Nguyễn Thái Ngọc Duy19910062014-08-16 08:19:27 +07001218 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1219 argv_array_clear(&argv_gc_auto);
Jeff King131b8fc2013-01-26 17:40:38 -05001220
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001221 return result;
1222}