blob: d52931854474e566a7da6273addf1e5c20a28414 [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;
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 Haggerty0e0b7de2015-05-25 18:38:35 +0000183static int add_existing(const char *refname, const struct object_id *oid,
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100184 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);
Michael Haggerty0e0b7de2015-05-25 18:38:35 +0000188 struct object_id *old_oid = xmalloc(sizeof(*old_oid));
189
190 oidcpy(old_oid, oid);
191 item->util = old_oid;
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100192 return 0;
193}
194
195static int will_fetch(struct ref **head, const unsigned char *sha1)
196{
197 struct ref *rm = *head;
198 while (rm) {
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000199 if (!hashcmp(rm->old_oid.hash, sha1))
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100200 return 1;
201 rm = rm->next;
202 }
203 return 0;
204}
205
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500206static void find_non_local_tags(struct transport *transport,
207 struct ref **head,
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100208 struct ref ***tail)
209{
210 struct string_list existing_refs = STRING_LIST_INIT_DUP;
211 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
212 const struct ref *ref;
213 struct string_list_item *item = NULL;
214
Michael Haggerty0e0b7de2015-05-25 18:38:35 +0000215 for_each_ref(add_existing, &existing_refs);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100216 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
Junio C Hamanoad704482013-12-17 11:47:35 -0800217 if (!starts_with(ref->name, "refs/tags/"))
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100218 continue;
219
220 /*
221 * The peeled ref always follows the matching base
222 * ref, so if we see a peeled ref that we don't want
223 * to fetch then we can mark the ref entry in the list
224 * as one to ignore by setting util to NULL.
225 */
Junio C Hamanoad704482013-12-17 11:47:35 -0800226 if (ends_with(ref->name, "^{}")) {
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000227 if (item && !has_object_file(&ref->old_oid) &&
228 !will_fetch(head, ref->old_oid.hash) &&
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100229 !has_sha1_file(item->util) &&
230 !will_fetch(head, item->util))
231 item->util = NULL;
232 item = NULL;
233 continue;
234 }
235
236 /*
237 * If item is non-NULL here, then we previously saw a
238 * ref not followed by a peeled reference, so we need
239 * to check if it is a lightweight tag that we want to
240 * fetch.
241 */
242 if (item && !has_sha1_file(item->util) &&
243 !will_fetch(head, item->util))
244 item->util = NULL;
245
246 item = NULL;
247
248 /* skip duplicates and refs that we already have */
249 if (string_list_has_string(&remote_refs, ref->name) ||
250 string_list_has_string(&existing_refs, ref->name))
251 continue;
252
253 item = string_list_insert(&remote_refs, ref->name);
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000254 item->util = (void *)&ref->old_oid;
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100255 }
256 string_list_clear(&existing_refs, 1);
257
258 /*
259 * We may have a final lightweight tag that needs to be
260 * checked to see if it needs fetching.
261 */
262 if (item && !has_sha1_file(item->util) &&
263 !will_fetch(head, item->util))
264 item->util = NULL;
265
266 /*
267 * For all the tags in the remote_refs string list,
268 * add them to the list of refs to be fetched
269 */
270 for_each_string_list_item(item, &remote_refs) {
271 /* Unless we have already decided to ignore this item... */
272 if (item->util)
273 {
274 struct ref *rm = alloc_ref(item->string);
275 rm->peer_ref = alloc_ref(item->string);
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000276 oidcpy(&rm->old_oid, item->util);
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100277 **tail = rm;
278 *tail = &rm->next;
279 }
280 }
281
282 string_list_clear(&remote_refs, 0);
283}
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500284
Daniel Barkalowb888d612007-09-10 23:03:25 -0400285static struct ref *get_ref_map(struct transport *transport,
Michael Haggertyf137a452013-10-23 17:50:38 +0200286 struct refspec *refspecs, int refspec_count,
287 int tags, int *autotags)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400288{
289 int i;
290 struct ref *rm;
291 struct ref *ref_map = NULL;
292 struct ref **tail = &ref_map;
293
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100294 /* opportunistically-updated references: */
295 struct ref *orefs = NULL, **oref_tail = &orefs;
296
Daniel Barkalow45773702007-10-29 21:05:40 -0400297 const struct ref *remote_refs = transport_get_remote_refs(transport);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400298
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100299 if (refspec_count) {
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700300 struct refspec *fetch_refspec;
301 int fetch_refspec_nr;
302
Michael Haggertyf137a452013-10-23 17:50:38 +0200303 for (i = 0; i < refspec_count; i++) {
304 get_fetch_map(remote_refs, &refspecs[i], &tail, 0);
305 if (refspecs[i].dst && refspecs[i].dst[0])
Daniel Barkalowb888d612007-09-10 23:03:25 -0400306 *autotags = 1;
307 }
Michael Haggerty0281c932013-10-30 06:32:58 +0100308 /* Merge everything on the command line (but not --tags) */
Daniel Barkalowb888d612007-09-10 23:03:25 -0400309 for (rm = ref_map; rm; rm = rm->next)
Jeff King900f2812013-05-11 18:15:59 +0200310 rm->fetch_head_status = FETCH_HEAD_MERGE;
Michael Haggerty0281c932013-10-30 06:32:58 +0100311
312 /*
313 * For any refs that we happen to be fetching via
314 * command-line arguments, the destination ref might
315 * have been missing or have been different than the
316 * remote-tracking ref that would be derived from the
317 * configured refspec. In these cases, we want to
318 * take the opportunity to update their configured
319 * remote-tracking reference. However, we do not want
320 * to mention these entries in FETCH_HEAD at all, as
321 * they would simply be duplicates of existing
322 * entries, so we set them FETCH_HEAD_IGNORE below.
323 *
324 * We compute these entries now, based only on the
325 * refspecs specified on the command line. But we add
326 * them to the list following the refspecs resulting
327 * from the tags option so that one of the latter,
328 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
329 * by ref_remove_duplicates() in favor of one of these
330 * opportunistic entries with FETCH_HEAD_IGNORE.
331 */
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700332 if (refmap_array) {
333 fetch_refspec = parse_fetch_refspec(refmap_nr, refmap_array);
334 fetch_refspec_nr = refmap_nr;
335 } else {
336 fetch_refspec = transport->remote->fetch;
337 fetch_refspec_nr = transport->remote->fetch_refspec_nr;
338 }
339
340 for (i = 0; i < fetch_refspec_nr; i++)
341 get_fetch_map(ref_map, &fetch_refspec[i], &oref_tail, 1);
Michael Haggerty0281c932013-10-30 06:32:58 +0100342
Daniel Barkalowe0aaa292008-04-17 19:32:35 -0400343 if (tags == TAGS_SET)
344 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700345 } else if (refmap_array) {
346 die("--refmap option is only meaningful with command-line refspec(s).");
Daniel Barkalowb888d612007-09-10 23:03:25 -0400347 } else {
348 /* Use the defaults */
349 struct remote *remote = transport->remote;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400350 struct branch *branch = branch_get(NULL);
351 int has_merge = branch_has_merge_config(branch);
Brandon Casey3ee17572010-08-25 12:52:56 -0500352 if (remote &&
353 (remote->fetch_refspec_nr ||
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500354 /* Note: has_merge implies non-NULL branch->remote_name */
Brandon Casey3ee17572010-08-25 12:52:56 -0500355 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400356 for (i = 0; i < remote->fetch_refspec_nr; i++) {
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700357 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400358 if (remote->fetch[i].dst &&
359 remote->fetch[i].dst[0])
360 *autotags = 1;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400361 if (!i && !has_merge && ref_map &&
Daniel Barkalowcfb8f892007-09-28 19:34:17 -0400362 !remote->fetch[0].pattern)
Jeff King900f2812013-05-11 18:15:59 +0200363 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400364 }
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100365 /*
366 * if the remote we're fetching from is the same
367 * as given in branch.<name>.remote, we add the
368 * ref given in branch.<name>.merge, too.
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500369 *
370 * Note: has_merge implies non-NULL branch->remote_name
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100371 */
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700372 if (has_merge &&
373 !strcmp(branch->remote_name, remote->name))
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400374 add_merge_config(&ref_map, remote_refs, branch, &tail);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400375 } else {
376 ref_map = get_remote_ref(remote_refs, "HEAD");
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700377 if (!ref_map)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000378 die(_("Couldn't find remote ref HEAD"));
Jeff King900f2812013-05-11 18:15:59 +0200379 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
Shawn O. Pearce5aaf7f22008-03-02 21:34:51 -0500380 tail = &ref_map->next;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400381 }
382 }
Michael Haggerty0281c932013-10-30 06:32:58 +0100383
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100384 if (tags == TAGS_SET)
385 /* also fetch all tags */
386 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
387 else if (tags == TAGS_DEFAULT && *autotags)
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500388 find_non_local_tags(transport, &ref_map, &tail);
Michael Haggerty0281c932013-10-30 06:32:58 +0100389
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100390 /* Now append any refs to be updated opportunistically: */
391 *tail = orefs;
392 for (rm = orefs; rm; rm = rm->next) {
393 rm->fetch_head_status = FETCH_HEAD_IGNORE;
394 tail = &rm->next;
395 }
396
Michael Haggertyb9afe662013-10-30 06:33:09 +0100397 return ref_remove_duplicates(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400398}
399
Jeff Kingfa250752009-05-25 06:40:54 -0400400#define STORE_REF_ERROR_OTHER 1
401#define STORE_REF_ERROR_DF_CONFLICT 2
402
Daniel Barkalowb888d612007-09-10 23:03:25 -0400403static int s_update_ref(const char *action,
404 struct ref *ref,
405 int check_old)
406{
407 char msg[1024];
408 char *rla = getenv("GIT_REFLOG_ACTION");
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700409 struct ref_transaction *transaction;
410 struct strbuf err = STRBUF_INIT;
411 int ret, df_conflict = 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400412
Jay Soffian28a15402009-11-10 09:19:43 +0100413 if (dry_run)
414 return 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400415 if (!rla)
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -0500416 rla = default_rla.buf;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400417 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700418
419 transaction = ref_transaction_begin(&err);
420 if (!transaction ||
Michael Haggerty1d147bd2015-02-17 18:00:15 +0100421 ref_transaction_update(transaction, ref->name,
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000422 ref->new_oid.hash,
423 check_old ? ref->old_oid.hash : NULL,
Michael Haggerty1d147bd2015-02-17 18:00:15 +0100424 0, msg, &err))
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700425 goto fail;
426
427 ret = ref_transaction_commit(transaction, &err);
428 if (ret) {
429 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
430 goto fail;
431 }
432
433 ref_transaction_free(transaction);
434 strbuf_release(&err);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400435 return 0;
Ronnie Sahlbergcd94f762014-04-28 13:49:07 -0700436fail:
437 ref_transaction_free(transaction);
438 error("%s", err.buf);
439 strbuf_release(&err);
440 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
441 : STORE_REF_ERROR_OTHER;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400442}
443
Pierre Habouzit9ef42722007-11-04 15:05:45 -0800444#define REFCOL_WIDTH 10
Nicolas Pitre165f3902007-11-03 01:32:48 -0400445
Daniel Barkalowb888d612007-09-10 23:03:25 -0400446static int update_local_ref(struct ref *ref,
Nicolas Pitre165f3902007-11-03 01:32:48 -0400447 const char *remote,
Marc Branchaud6da618d2012-04-16 18:08:49 -0400448 const struct ref *remote_ref,
Jeff King5914f2d2011-12-08 03:43:19 -0500449 struct strbuf *display)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400450{
Daniel Barkalowb888d612007-09-10 23:03:25 -0400451 struct commit *current = NULL, *updated;
452 enum object_type type;
453 struct branch *current_branch = branch_get(NULL);
Felipe Contreras4577e482009-05-14 00:22:04 +0300454 const char *pretty_ref = prettify_refname(ref->name);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400455
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000456 type = sha1_object_info(ref->new_oid.hash, NULL);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400457 if (type < 0)
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000458 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
Daniel Barkalowb888d612007-09-10 23:03:25 -0400459
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000460 if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100461 if (verbosity > 0)
Jeff King5914f2d2011-12-08 03:43:19 -0500462 strbuf_addf(display, "= %-*s %-*s -> %s",
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700463 TRANSPORT_SUMMARY(_("[up to date]")),
464 REFCOL_WIDTH, remote, pretty_ref);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400465 return 0;
466 }
467
Shawn O. Pearceb3abdd92007-09-16 02:31:26 -0400468 if (current_branch &&
469 !strcmp(ref->name, current_branch->name) &&
Daniel Barkalowb888d612007-09-10 23:03:25 -0400470 !(update_head_ok || is_bare_repository()) &&
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000471 !is_null_oid(&ref->old_oid)) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400472 /*
473 * If this is the head, and it's not okay to update
474 * the head, and the old value of the head isn't empty...
475 */
Jeff King5914f2d2011-12-08 03:43:19 -0500476 strbuf_addf(display,
477 _("! %-*s %-*s -> %s (can't fetch in current branch)"),
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700478 TRANSPORT_SUMMARY(_("[rejected]")),
Jeff King5914f2d2011-12-08 03:43:19 -0500479 REFCOL_WIDTH, remote, pretty_ref);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400480 return 1;
481 }
482
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000483 if (!is_null_oid(&ref->old_oid) &&
Christian Couder59556542013-11-30 21:55:40 +0100484 starts_with(ref->name, "refs/tags/")) {
Jeff King63154722008-06-26 23:59:50 -0400485 int r;
486 r = s_update_ref("updating tag", ref, 0);
Jeff King5914f2d2011-12-08 03:43:19 -0500487 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
488 r ? '!' : '-',
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700489 TRANSPORT_SUMMARY(_("[tag update]")),
Jeff King5914f2d2011-12-08 03:43:19 -0500490 REFCOL_WIDTH, remote, pretty_ref,
491 r ? _(" (unable to update local ref)") : "");
Jeff King63154722008-06-26 23:59:50 -0400492 return r;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400493 }
494
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000495 current = lookup_commit_reference_gently(ref->old_oid.hash, 1);
496 updated = lookup_commit_reference_gently(ref->new_oid.hash, 1);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400497 if (!current || !updated) {
Nicolas Pitre165f3902007-11-03 01:32:48 -0400498 const char *msg;
499 const char *what;
Jeff King63154722008-06-26 23:59:50 -0400500 int r;
Marc Branchaud0997ada2012-04-16 18:08:50 -0400501 /*
502 * Nicely describe the new ref we're fetching.
503 * Base this on the remote's ref name, as it's
504 * more likely to follow a standard layout.
505 */
506 const char *name = remote_ref ? remote_ref->name : "";
Christian Couder59556542013-11-30 21:55:40 +0100507 if (starts_with(name, "refs/tags/")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400508 msg = "storing tag";
Ævar Arnfjörð Bjarmasonf7b37422011-02-22 23:41:53 +0000509 what = _("[new tag]");
Christian Couder59556542013-11-30 21:55:40 +0100510 } else if (starts_with(name, "refs/heads/")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400511 msg = "storing head";
Ævar Arnfjörð Bjarmasonf7b37422011-02-22 23:41:53 +0000512 what = _("[new branch]");
Marc Branchaud0997ada2012-04-16 18:08:50 -0400513 } else {
514 msg = "storing ref";
515 what = _("[new ref]");
Nicolas Pitre165f3902007-11-03 01:32:48 -0400516 }
517
Jens Lehmanna6801ad2012-04-13 18:25:16 +0200518 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
519 (recurse_submodules != RECURSE_SUBMODULES_ON))
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000520 check_for_new_submodule_commits(ref->new_oid.hash);
Jeff King63154722008-06-26 23:59:50 -0400521 r = s_update_ref(msg, ref, 0);
Jeff King5914f2d2011-12-08 03:43:19 -0500522 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
523 r ? '!' : '*',
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700524 TRANSPORT_SUMMARY(what),
Jeff King5914f2d2011-12-08 03:43:19 -0500525 REFCOL_WIDTH, remote, pretty_ref,
526 r ? _(" (unable to update local ref)") : "");
Jeff King63154722008-06-26 23:59:50 -0400527 return r;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400528 }
529
Junio C Hamanoa20efee2012-08-27 14:46:01 -0700530 if (in_merge_bases(current, updated)) {
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400531 struct strbuf quickref = STRBUF_INIT;
Jeff King63154722008-06-26 23:59:50 -0400532 int r;
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400533 strbuf_add_unique_abbrev(&quickref, current->object.sha1, DEFAULT_ABBREV);
534 strbuf_addstr(&quickref, "..");
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000535 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
Jens Lehmann88a21972011-03-06 23:10:46 +0100536 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
537 (recurse_submodules != RECURSE_SUBMODULES_ON))
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000538 check_for_new_submodule_commits(ref->new_oid.hash);
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300539 r = s_update_ref("fast-forward", ref, 1);
Jeff King5914f2d2011-12-08 03:43:19 -0500540 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
541 r ? '!' : ' ',
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400542 TRANSPORT_SUMMARY_WIDTH, quickref.buf,
Jeff King5914f2d2011-12-08 03:43:19 -0500543 REFCOL_WIDTH, remote, pretty_ref,
544 r ? _(" (unable to update local ref)") : "");
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400545 strbuf_release(&quickref);
Jeff King63154722008-06-26 23:59:50 -0400546 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400547 } else if (force || ref->force) {
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400548 struct strbuf quickref = STRBUF_INIT;
Jeff King63154722008-06-26 23:59:50 -0400549 int r;
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400550 strbuf_add_unique_abbrev(&quickref, current->object.sha1, DEFAULT_ABBREV);
551 strbuf_addstr(&quickref, "...");
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000552 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
Jens Lehmann88a21972011-03-06 23:10:46 +0100553 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
554 (recurse_submodules != RECURSE_SUBMODULES_ON))
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000555 check_for_new_submodule_commits(ref->new_oid.hash);
Jeff King63154722008-06-26 23:59:50 -0400556 r = s_update_ref("forced-update", ref, 1);
Jeff King5914f2d2011-12-08 03:43:19 -0500557 strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
558 r ? '!' : '+',
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400559 TRANSPORT_SUMMARY_WIDTH, quickref.buf,
Jeff King5914f2d2011-12-08 03:43:19 -0500560 REFCOL_WIDTH, remote, pretty_ref,
561 r ? _("unable to update local ref") : _("forced update"));
Jeff Kingbd22d4f2015-09-24 17:07:40 -0400562 strbuf_release(&quickref);
Jeff King63154722008-06-26 23:59:50 -0400563 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400564 } else {
Jeff King5914f2d2011-12-08 03:43:19 -0500565 strbuf_addf(display, "! %-*s %-*s -> %s %s",
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700566 TRANSPORT_SUMMARY(_("[rejected]")),
Jeff King5914f2d2011-12-08 03:43:19 -0500567 REFCOL_WIDTH, remote, pretty_ref,
568 _("(non-fast-forward)"));
Daniel Barkalowb888d612007-09-10 23:03:25 -0400569 return 1;
570 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400571}
572
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700573static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700574{
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700575 struct ref **rm = cb_data;
576 struct ref *ref = *rm;
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700577
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +0700578 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
579 ref = ref->next;
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700580 if (!ref)
581 return -1; /* end of the list */
582 *rm = ref->next;
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000583 hashcpy(sha1, ref->old_oid.hash);
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700584 return 0;
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700585}
586
Andreas Ericsson47abd852009-04-17 10:20:11 +0200587static int store_updated_refs(const char *raw_url, const char *remote_name,
Jeff Kingf3cb1692008-06-27 00:01:41 -0400588 struct ref *ref_map)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400589{
590 FILE *fp;
591 struct commit *commit;
Tom Miller4b3b33a2014-01-02 20:28:51 -0600592 int url_len, i, rc = 0;
Jeff King5914f2d2011-12-08 03:43:19 -0500593 struct strbuf note = STRBUF_INIT;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400594 const char *what, *kind;
595 struct ref *rm;
Nguyễn Thái Ngọc Duydcf69262014-11-30 15:24:27 +0700596 char *url;
Jeff Kingf9327292015-08-10 05:38:57 -0400597 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
Jeff King900f2812013-05-11 18:15:59 +0200598 int want_status;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400599
André Goddard Rosad6617c72007-11-22 20:22:23 -0200600 fp = fopen(filename, "a");
601 if (!fp)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000602 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
Andreas Ericsson47abd852009-04-17 10:20:11 +0200603
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100604 if (raw_url)
605 url = transport_anonymize_url(raw_url);
606 else
607 url = xstrdup("foreign");
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700608
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700609 rm = ref_map;
Junio C Hamano3f7d11c2011-10-17 21:37:12 -0700610 if (check_everything_connected(iterate_ref_map, 0, &rm)) {
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800611 rc = error(_("%s did not send all necessary objects\n"), url);
612 goto abort;
613 }
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700614
Joey Hess96890f42011-12-26 12:16:56 -0400615 /*
Jeff King900f2812013-05-11 18:15:59 +0200616 * We do a pass for each fetch_head_status type in their enum order, so
617 * merged entries are written before not-for-merge. That lets readers
618 * use FETCH_HEAD as a refname to refer to the ref to be merged.
Joey Hess96890f42011-12-26 12:16:56 -0400619 */
Jeff King900f2812013-05-11 18:15:59 +0200620 for (want_status = FETCH_HEAD_MERGE;
621 want_status <= FETCH_HEAD_IGNORE;
622 want_status++) {
Joey Hess96890f42011-12-26 12:16:56 -0400623 for (rm = ref_map; rm; rm = rm->next) {
624 struct ref *ref = NULL;
Jeff King900f2812013-05-11 18:15:59 +0200625 const char *merge_status_marker = "";
Daniel Barkalowb888d612007-09-10 23:03:25 -0400626
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +0700627 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
628 if (want_status == FETCH_HEAD_MERGE)
629 warning(_("reject %s because shallow roots are not allowed to be updated"),
630 rm->peer_ref ? rm->peer_ref->name : rm->name);
631 continue;
632 }
633
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000634 commit = lookup_commit_reference_gently(rm->old_oid.hash, 1);
Joey Hess96890f42011-12-26 12:16:56 -0400635 if (!commit)
Jeff King900f2812013-05-11 18:15:59 +0200636 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400637
Jeff King900f2812013-05-11 18:15:59 +0200638 if (rm->fetch_head_status != want_status)
Joey Hess96890f42011-12-26 12:16:56 -0400639 continue;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400640
Joey Hess96890f42011-12-26 12:16:56 -0400641 if (rm->peer_ref) {
Jeff King6f687c22015-09-24 17:08:09 -0400642 ref = alloc_ref(rm->peer_ref->name);
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000643 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
644 oidcpy(&ref->new_oid, &rm->old_oid);
Joey Hess96890f42011-12-26 12:16:56 -0400645 ref->force = rm->peer_ref->force;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400646 }
Joey Hess96890f42011-12-26 12:16:56 -0400647
648
649 if (!strcmp(rm->name, "HEAD")) {
650 kind = "";
651 what = "";
652 }
Christian Couder59556542013-11-30 21:55:40 +0100653 else if (starts_with(rm->name, "refs/heads/")) {
Joey Hess96890f42011-12-26 12:16:56 -0400654 kind = "branch";
655 what = rm->name + 11;
656 }
Christian Couder59556542013-11-30 21:55:40 +0100657 else if (starts_with(rm->name, "refs/tags/")) {
Joey Hess96890f42011-12-26 12:16:56 -0400658 kind = "tag";
659 what = rm->name + 10;
660 }
Christian Couder59556542013-11-30 21:55:40 +0100661 else if (starts_with(rm->name, "refs/remotes/")) {
Joey Hess96890f42011-12-26 12:16:56 -0400662 kind = "remote-tracking branch";
663 what = rm->name + 13;
664 }
665 else {
666 kind = "";
667 what = rm->name;
668 }
669
670 url_len = strlen(url);
671 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
672 ;
673 url_len = i + 1;
674 if (4 < i && !strncmp(".git", url + i - 3, 4))
675 url_len = i - 3;
676
677 strbuf_reset(&note);
678 if (*what) {
679 if (*kind)
680 strbuf_addf(&note, "%s ", kind);
681 strbuf_addf(&note, "'%s' of ", what);
682 }
Jeff King900f2812013-05-11 18:15:59 +0200683 switch (rm->fetch_head_status) {
684 case FETCH_HEAD_NOT_FOR_MERGE:
685 merge_status_marker = "not-for-merge";
686 /* fall-through */
687 case FETCH_HEAD_MERGE:
688 fprintf(fp, "%s\t%s\t%s",
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000689 oid_to_hex(&rm->old_oid),
Jeff King900f2812013-05-11 18:15:59 +0200690 merge_status_marker,
691 note.buf);
692 for (i = 0; i < url_len; ++i)
693 if ('\n' == url[i])
694 fputs("\\n", fp);
695 else
696 fputc(url[i], fp);
697 fputc('\n', fp);
698 break;
699 default:
700 /* do not write anything to FETCH_HEAD */
701 break;
702 }
Joey Hess96890f42011-12-26 12:16:56 -0400703
704 strbuf_reset(&note);
705 if (ref) {
Marc Branchaud6da618d2012-04-16 18:08:49 -0400706 rc |= update_local_ref(ref, what, rm, &note);
Joey Hess96890f42011-12-26 12:16:56 -0400707 free(ref);
708 } else
709 strbuf_addf(&note, "* %-*s %-*s -> FETCH_HEAD",
710 TRANSPORT_SUMMARY_WIDTH,
711 *kind ? kind : "branch",
712 REFCOL_WIDTH,
713 *what ? what : "HEAD");
714 if (note.len) {
715 if (verbosity >= 0 && !shown_url) {
716 fprintf(stderr, _("From %.*s\n"),
717 url_len, url);
718 shown_url = 1;
719 }
720 if (verbosity >= 0)
721 fprintf(stderr, " %s\n", note.buf);
722 }
Nicolas Pitre165f3902007-11-03 01:32:48 -0400723 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400724 }
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800725
Jeff Kingfa250752009-05-25 06:40:54 -0400726 if (rc & STORE_REF_ERROR_DF_CONFLICT)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000727 error(_("some local refs could not be updated; try running\n"
Jeff Kingf3cb1692008-06-27 00:01:41 -0400728 " 'git remote prune %s' to remove any old, conflicting "
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000729 "branches"), remote_name);
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800730
731 abort:
Jeff King5914f2d2011-12-08 03:43:19 -0500732 strbuf_release(&note);
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800733 free(url);
734 fclose(fp);
Dmitry V. Levinefb98b42008-05-28 19:29:36 +0400735 return rc;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400736}
737
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500738/*
739 * We would want to bypass the object transfer altogether if
Johan Herlandd9eb0202009-07-10 01:52:30 +0200740 * everything we are going to fetch already exists and is connected
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500741 * locally.
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500742 */
743static int quickfetch(struct ref *ref_map)
744{
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700745 struct ref *rm = ref_map;
746
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500747 /*
748 * If we are deepening a shallow clone we already have these
749 * objects reachable. Running rev-list here will return with
750 * a good (0) exit status and we'll bypass the fetch that we
751 * really need to perform. Claiming failure now will ensure
752 * we perform the network exchange to deepen our history.
753 */
754 if (depth)
755 return -1;
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700756 return check_everything_connected(iterate_ref_map, 1, &rm);
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500757}
758
Daniel Barkalowb888d612007-09-10 23:03:25 -0400759static int fetch_refs(struct transport *transport, struct ref *ref_map)
760{
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500761 int ret = quickfetch(ref_map);
762 if (ret)
763 ret = transport_fetch_refs(transport, ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400764 if (!ret)
Jeff Kingf3cb1692008-06-27 00:01:41 -0400765 ret |= store_updated_refs(transport->url,
766 transport->remote->name,
767 ref_map);
Shawn O. Pearce1788c392007-09-14 03:31:23 -0400768 transport_unlock_pack(transport);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400769 return ret;
770}
771
Tom Miller4b3b33a2014-01-02 20:28:51 -0600772static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
773 const char *raw_url)
Jay Soffianf360d842009-11-10 09:15:47 +0100774{
Tom Miller4b3b33a2014-01-02 20:28:51 -0600775 int url_len, i, result = 0;
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200776 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
Tom Miller4b3b33a2014-01-02 20:28:51 -0600777 char *url;
Jay Soffianf360d842009-11-10 09:15:47 +0100778 const char *dangling_msg = dry_run
Nguyễn Thái Ngọc Duy18986d52012-04-23 19:30:25 +0700779 ? _(" (%s will become dangling)")
780 : _(" (%s has become dangling)");
Jay Soffianf360d842009-11-10 09:15:47 +0100781
Tom Miller4b3b33a2014-01-02 20:28:51 -0600782 if (raw_url)
783 url = transport_anonymize_url(raw_url);
784 else
785 url = xstrdup("foreign");
786
787 url_len = strlen(url);
788 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
789 ;
790
791 url_len = i + 1;
792 if (4 < i && !strncmp(".git", url + i - 3, 4))
793 url_len = i - 3;
794
Michael Haggertya087b432015-06-22 16:02:59 +0200795 if (!dry_run) {
796 struct string_list refnames = STRING_LIST_INIT_NODUP;
797
798 for (ref = stale_refs; ref; ref = ref->next)
799 string_list_append(&refnames, ref->name);
800
801 result = delete_refs(&refnames);
802 string_list_clear(&refnames, 0);
803 }
804
805 if (verbosity >= 0) {
806 for (ref = stale_refs; ref; ref = ref->next) {
807 if (!shown_url) {
808 fprintf(stderr, _("From %.*s\n"), url_len, url);
809 shown_url = 1;
810 }
Jay Soffianf360d842009-11-10 09:15:47 +0100811 fprintf(stderr, " x %-*s %-*s -> %s\n",
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700812 TRANSPORT_SUMMARY(_("[deleted]")),
Ævar Arnfjörð Bjarmason502681c2011-02-22 23:41:52 +0000813 REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
Jay Soffianf360d842009-11-10 09:15:47 +0100814 warn_dangling_symref(stderr, dangling_msg, ref->name);
815 }
816 }
Michael Haggertya087b432015-06-22 16:02:59 +0200817
Tom Miller4b3b33a2014-01-02 20:28:51 -0600818 free(url);
Jay Soffianf360d842009-11-10 09:15:47 +0100819 free_refs(stale_refs);
820 return result;
821}
822
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200823static void check_not_current_branch(struct ref *ref_map)
824{
825 struct branch *current_branch = branch_get(NULL);
826
827 if (is_bare_repository() || !current_branch)
828 return;
829
830 for (; ref_map; ref_map = ref_map->next)
831 if (ref_map->peer_ref && !strcmp(current_branch->refname,
832 ref_map->peer_ref->name))
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000833 die(_("Refusing to fetch into current branch %s "
834 "of non-bare repository"), current_branch->refname);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200835}
836
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800837static int truncate_fetch_head(void)
838{
Jeff Kingf9327292015-08-10 05:38:57 -0400839 const char *filename = git_path_fetch_head();
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800840 FILE *fp = fopen(filename, "w");
841
842 if (!fp)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000843 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800844 fclose(fp);
845 return 0;
846}
847
Junio C Hamanodb5723c2013-08-07 14:43:20 -0700848static void set_option(struct transport *transport, const char *name, const char *value)
849{
850 int r = transport_set_option(transport, name, value);
851 if (r < 0)
852 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
853 name, value, transport->url);
854 if (r > 0)
855 warning(_("Option \"%s\" is ignored for %s\n"),
856 name, transport->url);
857}
858
Ramsay Jones0f73f8b2013-08-28 19:56:17 +0100859static struct transport *prepare_transport(struct remote *remote)
Junio C Hamanodb5723c2013-08-07 14:43:20 -0700860{
861 struct transport *transport;
862 transport = transport_get(remote, NULL);
863 transport_set_verbosity(transport, verbosity, progress);
864 if (upload_pack)
865 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
866 if (keep)
867 set_option(transport, TRANS_OPT_KEEP, "yes");
868 if (depth)
869 set_option(transport, TRANS_OPT_DEPTH, depth);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +0700870 if (update_shallow)
871 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
Junio C Hamanodb5723c2013-08-07 14:43:20 -0700872 return transport;
873}
874
Junio C Hamano069d5032013-08-07 15:14:45 -0700875static void backfill_tags(struct transport *transport, struct ref *ref_map)
876{
Junio C Hamanob26ed432013-08-07 15:47:18 -0700877 if (transport->cannot_reuse) {
878 gsecondary = prepare_transport(transport->remote);
879 transport = gsecondary;
880 }
881
Junio C Hamano069d5032013-08-07 15:14:45 -0700882 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
883 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
884 fetch_refs(transport, ref_map);
Junio C Hamanob26ed432013-08-07 15:47:18 -0700885
886 if (gsecondary) {
887 transport_disconnect(gsecondary);
888 gsecondary = NULL;
889 }
Junio C Hamano069d5032013-08-07 15:14:45 -0700890}
891
Daniel Barkalowb888d612007-09-10 23:03:25 -0400892static int do_fetch(struct transport *transport,
893 struct refspec *refs, int ref_count)
894{
Michael Haggertyb87dbcc2013-05-25 11:08:01 +0200895 struct string_list existing_refs = STRING_LIST_INIT_DUP;
Shawn O. Pearce7f984282008-03-02 21:34:43 -0500896 struct ref *ref_map;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400897 struct ref *rm;
898 int autotags = (transport->remote->fetch_tags == 1);
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200899 int retcode = 0;
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000900
Michael Haggerty0e0b7de2015-05-25 18:38:35 +0000901 for_each_ref(add_existing, &existing_refs);
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000902
Daniel Johnsoned368542010-08-11 18:57:20 -0400903 if (tags == TAGS_DEFAULT) {
904 if (transport->remote->fetch_tags == 2)
905 tags = TAGS_SET;
906 if (transport->remote->fetch_tags == -1)
907 tags = TAGS_UNSET;
908 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400909
Shawn O. Pearce824d5772007-09-19 00:49:31 -0400910 if (!transport->get_refs_list || !transport->fetch)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000911 die(_("Don't know how to fetch from %s"), transport->url);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400912
913 /* if not appending, truncate FETCH_HEAD */
Jay Soffian28a15402009-11-10 09:19:43 +0100914 if (!append && !dry_run) {
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200915 retcode = truncate_fetch_head();
916 if (retcode)
917 goto cleanup;
André Goddard Rosad6617c72007-11-22 20:22:23 -0200918 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400919
920 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200921 if (!update_head_ok)
922 check_not_current_branch(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400923
924 for (rm = ref_map; rm; rm = rm->next) {
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000925 if (rm->peer_ref) {
Michael Haggerty6f64a162013-05-25 11:08:15 +0200926 struct string_list_item *peer_item =
927 string_list_lookup(&existing_refs,
928 rm->peer_ref->name);
Michael Haggerty0e0b7de2015-05-25 18:38:35 +0000929 if (peer_item) {
930 struct object_id *old_oid = peer_item->util;
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000931 oidcpy(&rm->peer_ref->old_oid, old_oid);
Michael Haggerty0e0b7de2015-05-25 18:38:35 +0000932 }
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000933 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400934 }
935
Shawn O. Pearce41fa7d22008-03-03 22:27:40 -0500936 if (tags == TAGS_DEFAULT && autotags)
937 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200938 if (prune) {
Michael Haggerty0838bf42013-10-30 06:33:00 +0100939 /*
940 * We only prune based on refspecs specified
941 * explicitly (via command line or configuration); we
942 * don't care whether --tags was specified.
943 */
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100944 if (ref_count) {
Tom Miller4b3b33a2014-01-02 20:28:51 -0600945 prune_refs(refs, ref_count, ref_map, transport->url);
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100946 } else {
Michael Haggerty0838bf42013-10-30 06:33:00 +0100947 prune_refs(transport->remote->fetch,
948 transport->remote->fetch_refspec_nr,
Tom Miller4b3b33a2014-01-02 20:28:51 -0600949 ref_map,
950 transport->url);
Carlos Martín Nietoe8c1e6c2011-10-15 07:04:26 +0200951 }
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200952 }
Tom Miller10a6cc82014-01-02 20:28:52 -0600953 if (fetch_refs(transport, ref_map)) {
954 free_refs(ref_map);
955 retcode = 1;
956 goto cleanup;
957 }
Shawn O. Pearce7f984282008-03-02 21:34:43 -0500958 free_refs(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400959
960 /* if neither --no-tags nor --tags was specified, do automated tag
961 * following ... */
Kristian Høgsberg83201992007-12-04 02:25:47 -0500962 if (tags == TAGS_DEFAULT && autotags) {
Shawn O. Pearcec50b2b42008-03-02 21:35:00 -0500963 struct ref **tail = &ref_map;
964 ref_map = NULL;
965 find_non_local_tags(transport, &ref_map, &tail);
Junio C Hamano069d5032013-08-07 15:14:45 -0700966 if (ref_map)
967 backfill_tags(transport, ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400968 free_refs(ref_map);
969 }
970
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200971 cleanup:
Michael Haggertyf83918e2013-05-25 11:08:17 +0200972 string_list_clear(&existing_refs, 1);
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200973 return retcode;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400974}
975
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100976static int get_one_remote_for_fetch(struct remote *remote, void *priv)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400977{
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100978 struct string_list *list = priv;
Björn Gustavsson7cc91a22009-11-09 21:11:06 +0100979 if (!remote->skip_default_update)
Julian Phillips1d2f80f2010-06-26 00:41:38 +0100980 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100981 return 0;
982}
983
984struct remote_group_data {
985 const char *name;
986 struct string_list *list;
987};
988
989static int get_remote_group(const char *key, const char *value, void *priv)
990{
991 struct remote_group_data *g = priv;
992
Michael Haggertybc598c32015-07-28 23:08:21 +0200993 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100994 /* split list by white space */
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100995 while (*value) {
Michael Haggerty5f654992015-07-28 23:08:20 +0200996 size_t wordlen = strcspn(value, " \t\n");
997
Michael Haggertye2865422015-07-28 23:08:19 +0200998 if (wordlen >= 1)
Julian Phillips1d2f80f2010-06-26 00:41:38 +0100999 string_list_append(g->list,
Michael Haggertye2865422015-07-28 23:08:19 +02001000 xstrndup(value, wordlen));
1001 value += wordlen + (value[wordlen] != '\0');
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001002 }
1003 }
1004
1005 return 0;
1006}
1007
1008static int add_remote_or_group(const char *name, struct string_list *list)
1009{
1010 int prev_nr = list->nr;
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +00001011 struct remote_group_data g;
1012 g.name = name; g.list = list;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001013
1014 git_config(get_remote_group, &g);
1015 if (list->nr == prev_nr) {
1016 struct remote *remote;
1017 if (!remote_is_configured(name))
1018 return 0;
1019 remote = remote_get(name);
Julian Phillips1d2f80f2010-06-26 00:41:38 +01001020 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001021 }
1022 return 1;
1023}
1024
Jeff King85556d42012-09-01 07:27:35 -04001025static void add_options_to_argv(struct argv_array *argv)
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001026{
1027 if (dry_run)
Jeff King85556d42012-09-01 07:27:35 -04001028 argv_array_push(argv, "--dry-run");
Michael Haggerty90765fa2013-10-30 06:33:04 +01001029 if (prune != -1)
1030 argv_array_push(argv, prune ? "--prune" : "--no-prune");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001031 if (update_head_ok)
Jeff King85556d42012-09-01 07:27:35 -04001032 argv_array_push(argv, "--update-head-ok");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001033 if (force)
Jeff King85556d42012-09-01 07:27:35 -04001034 argv_array_push(argv, "--force");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001035 if (keep)
Jeff King85556d42012-09-01 07:27:35 -04001036 argv_array_push(argv, "--keep");
Jens Lehmannbe254a02010-11-11 00:55:02 +01001037 if (recurse_submodules == RECURSE_SUBMODULES_ON)
Jeff King85556d42012-09-01 07:27:35 -04001038 argv_array_push(argv, "--recurse-submodules");
Jens Lehmann8f0700d2011-03-06 23:11:21 +01001039 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
Jeff King85556d42012-09-01 07:27:35 -04001040 argv_array_push(argv, "--recurse-submodules=on-demand");
Dan Johnson85566462012-09-05 17:22:19 -04001041 if (tags == TAGS_SET)
1042 argv_array_push(argv, "--tags");
1043 else if (tags == TAGS_UNSET)
1044 argv_array_push(argv, "--no-tags");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001045 if (verbosity >= 2)
Jeff King85556d42012-09-01 07:27:35 -04001046 argv_array_push(argv, "-v");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001047 if (verbosity >= 1)
Jeff King85556d42012-09-01 07:27:35 -04001048 argv_array_push(argv, "-v");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001049 else if (verbosity < 0)
Jeff King85556d42012-09-01 07:27:35 -04001050 argv_array_push(argv, "-q");
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001051
1052}
1053
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001054static int fetch_multiple(struct string_list *list)
1055{
1056 int i, result = 0;
Jeff King85556d42012-09-01 07:27:35 -04001057 struct argv_array argv = ARGV_ARRAY_INIT;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001058
Junio C Hamanoe6cc5102010-02-24 11:02:05 -08001059 if (!append && !dry_run) {
1060 int errcode = truncate_fetch_head();
1061 if (errcode)
1062 return errcode;
1063 }
1064
Jeff King85556d42012-09-01 07:27:35 -04001065 argv_array_pushl(&argv, "fetch", "--append", NULL);
1066 add_options_to_argv(&argv);
1067
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001068 for (i = 0; i < list->nr; i++) {
1069 const char *name = list->items[i].string;
Jeff King85556d42012-09-01 07:27:35 -04001070 argv_array_push(&argv, name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001071 if (verbosity >= 0)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001072 printf(_("Fetching %s\n"), name);
Jeff King85556d42012-09-01 07:27:35 -04001073 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001074 error(_("Could not fetch %s"), name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001075 result = 1;
1076 }
Jeff King85556d42012-09-01 07:27:35 -04001077 argv_array_pop(&argv);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001078 }
1079
Jeff King85556d42012-09-01 07:27:35 -04001080 argv_array_clear(&argv);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001081 return result;
1082}
1083
1084static int fetch_one(struct remote *remote, int argc, const char **argv)
1085{
Daniel Barkalowb888d612007-09-10 23:03:25 -04001086 static const char **refs = NULL;
Jim Meyeringd8ead152011-06-08 22:06:33 +02001087 struct refspec *refspec;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001088 int ref_nr = 0;
Alex Riesen7b7f39e2008-04-28 22:23:35 +02001089 int exit_code;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001090
Daniel Barkalowfa685bd2009-03-11 01:47:20 -04001091 if (!remote)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001092 die(_("No remote repository specified. Please, specify either a URL or a\n"
1093 "remote name from which new revisions should be fetched."));
Daniel Barkalowfa685bd2009-03-11 01:47:20 -04001094
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001095 gtransport = prepare_transport(remote);
Michael Schubert737c5a92013-07-13 11:36:24 +02001096
1097 if (prune < 0) {
1098 /* no command line request */
Junio C Hamano20419de2013-09-09 14:50:37 -07001099 if (0 <= gtransport->remote->prune)
1100 prune = gtransport->remote->prune;
Michael Schubert737c5a92013-07-13 11:36:24 +02001101 else if (0 <= fetch_prune_config)
1102 prune = fetch_prune_config;
1103 else
1104 prune = PRUNE_BY_DEFAULT;
1105 }
1106
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001107 if (argc > 0) {
Daniel Barkalowb888d612007-09-10 23:03:25 -04001108 int j = 0;
Elia Pintobf7e6452014-01-29 08:54:16 -08001109 int i;
Kristian Høgsberg83201992007-12-04 02:25:47 -05001110 refs = xcalloc(argc + 1, sizeof(const char *));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001111 for (i = 0; i < argc; i++) {
Daniel Barkalowb888d612007-09-10 23:03:25 -04001112 if (!strcmp(argv[i], "tag")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -04001113 i++;
Kevin Ballardf53423b2008-04-05 14:28:53 -04001114 if (i >= argc)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001115 die(_("You need to specify a tag name."));
Jeff Kingb2724c82014-06-19 17:26:56 -04001116 refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1117 argv[i], argv[i]);
Daniel Barkalowb888d612007-09-10 23:03:25 -04001118 } else
1119 refs[j++] = argv[i];
Daniel Barkalowb888d612007-09-10 23:03:25 -04001120 }
1121 refs[j] = NULL;
1122 ref_nr = j;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001123 }
1124
Jeff King57b235a2009-01-22 01:03:08 -05001125 sigchain_push_common(unlock_pack_on_signal);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -04001126 atexit(unlock_pack);
Jim Meyeringd8ead152011-06-08 22:06:33 +02001127 refspec = parse_fetch_refspec(ref_nr, refs);
Junio C Hamanoaf234452013-08-07 15:38:45 -07001128 exit_code = do_fetch(gtransport, refspec, ref_nr);
Carlos Martín Nieto5caf1972011-10-08 00:51:06 +02001129 free_refspec(ref_nr, refspec);
Junio C Hamanoaf234452013-08-07 15:38:45 -07001130 transport_disconnect(gtransport);
1131 gtransport = NULL;
Alex Riesen7b7f39e2008-04-28 22:23:35 +02001132 return exit_code;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001133}
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001134
1135int cmd_fetch(int argc, const char **argv, const char *prefix)
1136{
1137 int i;
Thiago Farina183113a2010-07-04 16:46:19 -03001138 struct string_list list = STRING_LIST_INIT_NODUP;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001139 struct remote *remote;
1140 int result = 0;
Nguyễn Thái Ngọc Duy19910062014-08-16 08:19:27 +07001141 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001142
Jeff Kingbbc30f92011-02-24 09:30:19 -05001143 packet_trace_identity("fetch");
1144
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001145 /* Record the command line for the reflog */
1146 strbuf_addstr(&default_rla, "fetch");
1147 for (i = 1; i < argc; i++)
1148 strbuf_addf(&default_rla, " %s", argv[i]);
1149
Michael Schubert737c5a92013-07-13 11:36:24 +02001150 git_config(git_fetch_config, NULL);
1151
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001152 argc = parse_options(argc, argv, prefix,
1153 builtin_fetch_options, builtin_fetch_usage, 0);
1154
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +07001155 if (unshallow) {
1156 if (depth)
1157 die(_("--depth and --unshallow cannot be used together"));
1158 else if (!is_repository_shallow())
1159 die(_("--unshallow on a complete repository does not make sense"));
Jeff King2805bb52015-09-24 17:07:07 -04001160 else
1161 depth = xstrfmt("%d", INFINITE_DEPTH);
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +07001162 }
1163
Nguyễn Thái Ngọc Duy5594bca2013-12-05 10:31:11 +07001164 /* no need to be strict, transport_set_option() will validate it again */
1165 if (depth && atoi(depth) < 1)
1166 die(_("depth %s is not a positive number"), depth);
1167
Jens Lehmann18322ba2011-09-09 20:22:03 +02001168 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1169 if (recurse_submodules_default) {
1170 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
1171 set_config_fetch_recurse_submodules(arg);
1172 }
1173 gitmodules_config();
1174 git_config(submodule_config, NULL);
1175 }
1176
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001177 if (all) {
1178 if (argc == 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001179 die(_("fetch --all does not take a repository argument"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001180 else if (argc > 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001181 die(_("fetch --all does not make sense with refspecs"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001182 (void) for_each_remote(get_one_remote_for_fetch, &list);
1183 result = fetch_multiple(&list);
1184 } else if (argc == 0) {
1185 /* No arguments -- use default remote */
1186 remote = remote_get(NULL);
1187 result = fetch_one(remote, argc, argv);
Björn Gustavsson16679e32009-11-09 21:10:32 +01001188 } else if (multiple) {
1189 /* All arguments are assumed to be remotes or groups */
1190 for (i = 0; i < argc; i++)
1191 if (!add_remote_or_group(argv[i], &list))
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001192 die(_("No such remote or remote group: %s"), argv[i]);
Björn Gustavsson16679e32009-11-09 21:10:32 +01001193 result = fetch_multiple(&list);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001194 } else {
1195 /* Single remote or group */
1196 (void) add_remote_or_group(argv[0], &list);
1197 if (list.nr > 1) {
1198 /* More than one remote */
1199 if (argc > 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001200 die(_("Fetching a group and specifying refspecs does not make sense"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001201 result = fetch_multiple(&list);
1202 } else {
1203 /* Zero or one remotes */
1204 remote = remote_get(argv[0]);
1205 result = fetch_one(remote, argc-1, argv+1);
1206 }
1207 }
1208
Jens Lehmannbe254a02010-11-11 00:55:02 +01001209 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
Jeff King85556d42012-09-01 07:27:35 -04001210 struct argv_array options = ARGV_ARRAY_INIT;
1211
1212 add_options_to_argv(&options);
Jens Lehmann50d89ad2012-09-01 17:27:06 +02001213 result = fetch_populated_submodules(&options,
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001214 submodule_prefix,
Jens Lehmann8f0700d2011-03-06 23:11:21 +01001215 recurse_submodules,
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001216 verbosity < 0);
Jeff King85556d42012-09-01 07:27:35 -04001217 argv_array_clear(&options);
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001218 }
1219
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001220 /* All names were strdup()ed or strndup()ed */
1221 list.strdup_strings = 1;
1222 string_list_clear(&list, 0);
1223
Nguyễn Thái Ngọc Duy19910062014-08-16 08:19:27 +07001224 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
Nguyễn Thái Ngọc Duy6fceed32014-08-16 08:19:28 +07001225 if (verbosity < 0)
1226 argv_array_push(&argv_gc_auto, "--quiet");
Nguyễn Thái Ngọc Duy19910062014-08-16 08:19:27 +07001227 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1228 argv_array_clear(&argv_gc_auto);
Jeff King131b8fc2013-01-26 17:40:38 -05001229
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001230 return result;
1231}