blob: 636b47ffb713f6698c10815f008947853f4dff5d [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
Jay Soffian28a15402009-11-10 09:19:43 +010033static int all, append, dry_run, force, keep, multiple, prune, update_head_ok, verbosity;
Clemens Buchacher01fdc212012-02-13 21:17:15 +010034static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +070035static int tags = TAGS_DEFAULT, unshallow;
Shawn O. Pearce4191c352007-11-11 02:29:47 -050036static const char *depth;
Kristian Høgsberg83201992007-12-04 02:25:47 -050037static const char *upload_pack;
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -050038static struct strbuf default_rla = STRBUF_INIT;
Junio C Hamanoaf234452013-08-07 15:38:45 -070039static struct transport *gtransport;
Jens Lehmann7dce19d2010-11-12 13:54:52 +010040static const char *submodule_prefix = "";
Jens Lehmann88a21972011-03-06 23:10:46 +010041static const char *recurse_submodules_default;
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -040042
Jens Lehmann8f0700d2011-03-06 23:11:21 +010043static int option_parse_recurse_submodules(const struct option *opt,
44 const char *arg, int unset)
45{
46 if (unset) {
47 recurse_submodules = RECURSE_SUBMODULES_OFF;
48 } else {
49 if (arg)
50 recurse_submodules = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
51 else
52 recurse_submodules = RECURSE_SUBMODULES_ON;
53 }
54 return 0;
55}
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -040056
Kristian Høgsberg83201992007-12-04 02:25:47 -050057static struct option builtin_fetch_options[] = {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +010058 OPT__VERBOSITY(&verbosity),
Björn Gustavsson9c4a0362009-11-09 21:09:56 +010059 OPT_BOOLEAN(0, "all", &all,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070060 N_("fetch from all remotes")),
Kristian Høgsberg83201992007-12-04 02:25:47 -050061 OPT_BOOLEAN('a', "append", &append,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070062 N_("append to .git/FETCH_HEAD instead of overwriting")),
63 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
64 N_("path to upload pack on remote end")),
65 OPT__FORCE(&force, N_("force overwrite of local branch")),
Björn Gustavsson16679e32009-11-09 21:10:32 +010066 OPT_BOOLEAN('m', "multiple", &multiple,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070067 N_("fetch from multiple remotes")),
Kristian Høgsberg83201992007-12-04 02:25:47 -050068 OPT_SET_INT('t', "tags", &tags,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070069 N_("fetch all tags and associated objects"), TAGS_SET),
Johannes Schindeline7951292008-03-13 08:13:15 +010070 OPT_SET_INT('n', NULL, &tags,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070071 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
Jay Soffianf360d842009-11-10 09:15:47 +010072 OPT_BOOLEAN('p', "prune", &prune,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070073 N_("prune remote-tracking branches no longer on remote")),
74 { OPTION_CALLBACK, 0, "recurse-submodules", NULL, N_("on-demand"),
75 N_("control recursive fetching of submodules"),
Jens Lehmann8f0700d2011-03-06 23:11:21 +010076 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
Jay Soffian28a15402009-11-10 09:19:43 +010077 OPT_BOOLEAN(0, "dry-run", &dry_run,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070078 N_("dry run")),
79 OPT_BOOLEAN('k', "keep", &keep, N_("keep downloaded pack")),
Kristian Høgsberg83201992007-12-04 02:25:47 -050080 OPT_BOOLEAN('u', "update-head-ok", &update_head_ok,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070081 N_("allow updating of HEAD ref")),
82 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
83 OPT_STRING(0, "depth", &depth, N_("depth"),
84 N_("deepen history of shallow clone")),
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +070085 { OPTION_SET_INT, 0, "unshallow", &unshallow, NULL,
86 N_("convert to a complete repository"),
87 PARSE_OPT_NONEG | PARSE_OPT_NOARG, NULL, 1 },
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070088 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
89 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
Jens Lehmann88a21972011-03-06 23:10:46 +010090 { OPTION_STRING, 0, "recurse-submodules-default",
91 &recurse_submodules_default, NULL,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070092 N_("default mode for recursion"), PARSE_OPT_HIDDEN },
Kristian Høgsberg83201992007-12-04 02:25:47 -050093 OPT_END()
94};
95
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -040096static void unlock_pack(void)
97{
Junio C Hamanoaf234452013-08-07 15:38:45 -070098 if (gtransport)
99 transport_unlock_pack(gtransport);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400100}
101
102static void unlock_pack_on_signal(int signo)
103{
104 unlock_pack();
Jeff King4a16d072009-01-22 01:02:35 -0500105 sigchain_pop(signo);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400106 raise(signo);
107}
Daniel Barkalowb888d612007-09-10 23:03:25 -0400108
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400109static void add_merge_config(struct ref **head,
Daniel Barkalow45773702007-10-29 21:05:40 -0400110 const struct ref *remote_refs,
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400111 struct branch *branch,
112 struct ref ***tail)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400113{
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400114 int i;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400115
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400116 for (i = 0; i < branch->merge_nr; i++) {
117 struct ref *rm, **old_tail = *tail;
118 struct refspec refspec;
119
120 for (rm = *head; rm; rm = rm->next) {
121 if (branch_merge_matches(branch, i, rm->name)) {
Jeff King900f2812013-05-11 18:15:59 +0200122 rm->fetch_head_status = FETCH_HEAD_MERGE;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400123 break;
124 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400125 }
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400126 if (rm)
127 continue;
128
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700129 /*
Matthieu Moy8b3f3f82010-11-02 16:31:23 +0100130 * Not fetched to a remote-tracking branch? We need to fetch
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400131 * it anyway to allow this branch's "branch.$name.merge"
Heikki Orsila05207a22008-09-09 13:28:30 +0300132 * to be honored by 'git pull', but we do not have to
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700133 * fail if branch.$name.merge is misconfigured to point
134 * at a nonexisting branch. If we were indeed called by
Heikki Orsila05207a22008-09-09 13:28:30 +0300135 * 'git pull', it will notice the misconfiguration because
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700136 * there is no entry in the resulting FETCH_HEAD marked
137 * for merging.
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400138 */
Andreas Gruenbacher8da61a22010-03-12 23:27:33 +0100139 memset(&refspec, 0, sizeof(refspec));
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400140 refspec.src = branch->merge[i]->src;
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700141 get_fetch_map(remote_refs, &refspec, tail, 1);
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400142 for (rm = *old_tail; rm; rm = rm->next)
Jeff King900f2812013-05-11 18:15:59 +0200143 rm->fetch_head_status = FETCH_HEAD_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400144 }
145}
146
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500147static void find_non_local_tags(struct transport *transport,
148 struct ref **head,
149 struct ref ***tail);
150
Daniel Barkalowb888d612007-09-10 23:03:25 -0400151static struct ref *get_ref_map(struct transport *transport,
152 struct refspec *refs, int ref_count, int tags,
153 int *autotags)
154{
155 int i;
156 struct ref *rm;
157 struct ref *ref_map = NULL;
158 struct ref **tail = &ref_map;
159
Daniel Barkalow45773702007-10-29 21:05:40 -0400160 const struct ref *remote_refs = transport_get_remote_refs(transport);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400161
Kristian Høgsberg83201992007-12-04 02:25:47 -0500162 if (ref_count || tags == TAGS_SET) {
Jeff Kingf2690482013-05-11 18:16:52 +0200163 struct ref **old_tail;
164
Daniel Barkalowb888d612007-09-10 23:03:25 -0400165 for (i = 0; i < ref_count; i++) {
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700166 get_fetch_map(remote_refs, &refs[i], &tail, 0);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400167 if (refs[i].dst && refs[i].dst[0])
168 *autotags = 1;
169 }
170 /* Merge everything on the command line, but not --tags */
171 for (rm = ref_map; rm; rm = rm->next)
Jeff King900f2812013-05-11 18:15:59 +0200172 rm->fetch_head_status = FETCH_HEAD_MERGE;
Daniel Barkalowe0aaa292008-04-17 19:32:35 -0400173 if (tags == TAGS_SET)
174 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
Jeff Kingf2690482013-05-11 18:16:52 +0200175
176 /*
177 * For any refs that we happen to be fetching via command-line
178 * arguments, take the opportunity to update their configured
179 * counterparts. However, we do not want to mention these
180 * entries in FETCH_HEAD at all, as they would simply be
181 * duplicates of existing entries.
182 */
183 old_tail = tail;
184 for (i = 0; i < transport->remote->fetch_refspec_nr; i++)
185 get_fetch_map(ref_map, &transport->remote->fetch[i],
John Keeping823c6d52013-05-27 17:33:09 +0100186 &tail, 1);
Jeff Kingf2690482013-05-11 18:16:52 +0200187 for (rm = *old_tail; rm; rm = rm->next)
188 rm->fetch_head_status = FETCH_HEAD_IGNORE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400189 } else {
190 /* Use the defaults */
191 struct remote *remote = transport->remote;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400192 struct branch *branch = branch_get(NULL);
193 int has_merge = branch_has_merge_config(branch);
Brandon Casey3ee17572010-08-25 12:52:56 -0500194 if (remote &&
195 (remote->fetch_refspec_nr ||
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500196 /* Note: has_merge implies non-NULL branch->remote_name */
Brandon Casey3ee17572010-08-25 12:52:56 -0500197 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400198 for (i = 0; i < remote->fetch_refspec_nr; i++) {
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700199 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400200 if (remote->fetch[i].dst &&
201 remote->fetch[i].dst[0])
202 *autotags = 1;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400203 if (!i && !has_merge && ref_map &&
Daniel Barkalowcfb8f892007-09-28 19:34:17 -0400204 !remote->fetch[0].pattern)
Jeff King900f2812013-05-11 18:15:59 +0200205 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400206 }
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100207 /*
208 * if the remote we're fetching from is the same
209 * as given in branch.<name>.remote, we add the
210 * ref given in branch.<name>.merge, too.
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500211 *
212 * Note: has_merge implies non-NULL branch->remote_name
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100213 */
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700214 if (has_merge &&
215 !strcmp(branch->remote_name, remote->name))
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400216 add_merge_config(&ref_map, remote_refs, branch, &tail);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400217 } else {
218 ref_map = get_remote_ref(remote_refs, "HEAD");
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700219 if (!ref_map)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000220 die(_("Couldn't find remote ref HEAD"));
Jeff King900f2812013-05-11 18:15:59 +0200221 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
Shawn O. Pearce5aaf7f22008-03-02 21:34:51 -0500222 tail = &ref_map->next;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400223 }
224 }
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500225 if (tags == TAGS_DEFAULT && *autotags)
226 find_non_local_tags(transport, &ref_map, &tail);
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400227 ref_remove_duplicates(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400228
229 return ref_map;
230}
231
Jeff Kingfa250752009-05-25 06:40:54 -0400232#define STORE_REF_ERROR_OTHER 1
233#define STORE_REF_ERROR_DF_CONFLICT 2
234
Daniel Barkalowb888d612007-09-10 23:03:25 -0400235static int s_update_ref(const char *action,
236 struct ref *ref,
237 int check_old)
238{
239 char msg[1024];
240 char *rla = getenv("GIT_REFLOG_ACTION");
241 static struct ref_lock *lock;
242
Jay Soffian28a15402009-11-10 09:19:43 +0100243 if (dry_run)
244 return 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400245 if (!rla)
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -0500246 rla = default_rla.buf;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400247 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
248 lock = lock_any_ref_for_update(ref->name,
249 check_old ? ref->old_sha1 : NULL, 0);
250 if (!lock)
Jeff Kingfa250752009-05-25 06:40:54 -0400251 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
252 STORE_REF_ERROR_OTHER;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400253 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
Jeff Kingfa250752009-05-25 06:40:54 -0400254 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
255 STORE_REF_ERROR_OTHER;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400256 return 0;
257}
258
Pierre Habouzit9ef42722007-11-04 15:05:45 -0800259#define REFCOL_WIDTH 10
Nicolas Pitre165f3902007-11-03 01:32:48 -0400260
Daniel Barkalowb888d612007-09-10 23:03:25 -0400261static int update_local_ref(struct ref *ref,
Nicolas Pitre165f3902007-11-03 01:32:48 -0400262 const char *remote,
Marc Branchaud6da618d2012-04-16 18:08:49 -0400263 const struct ref *remote_ref,
Jeff King5914f2d2011-12-08 03:43:19 -0500264 struct strbuf *display)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400265{
Daniel Barkalowb888d612007-09-10 23:03:25 -0400266 struct commit *current = NULL, *updated;
267 enum object_type type;
268 struct branch *current_branch = branch_get(NULL);
Felipe Contreras4577e482009-05-14 00:22:04 +0300269 const char *pretty_ref = prettify_refname(ref->name);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400270
Daniel Barkalowb888d612007-09-10 23:03:25 -0400271 type = sha1_object_info(ref->new_sha1, NULL);
272 if (type < 0)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000273 die(_("object %s not found"), sha1_to_hex(ref->new_sha1));
Daniel Barkalowb888d612007-09-10 23:03:25 -0400274
Daniel Barkalowb888d612007-09-10 23:03:25 -0400275 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100276 if (verbosity > 0)
Jeff King5914f2d2011-12-08 03:43:19 -0500277 strbuf_addf(display, "= %-*s %-*s -> %s",
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700278 TRANSPORT_SUMMARY(_("[up to date]")),
279 REFCOL_WIDTH, remote, pretty_ref);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400280 return 0;
281 }
282
Shawn O. Pearceb3abdd92007-09-16 02:31:26 -0400283 if (current_branch &&
284 !strcmp(ref->name, current_branch->name) &&
Daniel Barkalowb888d612007-09-10 23:03:25 -0400285 !(update_head_ok || is_bare_repository()) &&
286 !is_null_sha1(ref->old_sha1)) {
287 /*
288 * If this is the head, and it's not okay to update
289 * the head, and the old value of the head isn't empty...
290 */
Jeff King5914f2d2011-12-08 03:43:19 -0500291 strbuf_addf(display,
292 _("! %-*s %-*s -> %s (can't fetch in current branch)"),
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700293 TRANSPORT_SUMMARY(_("[rejected]")),
Jeff King5914f2d2011-12-08 03:43:19 -0500294 REFCOL_WIDTH, remote, pretty_ref);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400295 return 1;
296 }
297
298 if (!is_null_sha1(ref->old_sha1) &&
299 !prefixcmp(ref->name, "refs/tags/")) {
Jeff King63154722008-06-26 23:59:50 -0400300 int r;
301 r = s_update_ref("updating tag", ref, 0);
Jeff King5914f2d2011-12-08 03:43:19 -0500302 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
303 r ? '!' : '-',
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700304 TRANSPORT_SUMMARY(_("[tag update]")),
Jeff King5914f2d2011-12-08 03:43:19 -0500305 REFCOL_WIDTH, remote, pretty_ref,
306 r ? _(" (unable to update local ref)") : "");
Jeff King63154722008-06-26 23:59:50 -0400307 return r;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400308 }
309
Shawn O. Pearcecfa5b2b2007-10-19 00:54:59 -0400310 current = lookup_commit_reference_gently(ref->old_sha1, 1);
311 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400312 if (!current || !updated) {
Nicolas Pitre165f3902007-11-03 01:32:48 -0400313 const char *msg;
314 const char *what;
Jeff King63154722008-06-26 23:59:50 -0400315 int r;
Marc Branchaud0997ada2012-04-16 18:08:50 -0400316 /*
317 * Nicely describe the new ref we're fetching.
318 * Base this on the remote's ref name, as it's
319 * more likely to follow a standard layout.
320 */
321 const char *name = remote_ref ? remote_ref->name : "";
322 if (!prefixcmp(name, "refs/tags/")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400323 msg = "storing tag";
Ævar Arnfjörð Bjarmasonf7b37422011-02-22 23:41:53 +0000324 what = _("[new tag]");
Marc Branchaud0997ada2012-04-16 18:08:50 -0400325 } else if (!prefixcmp(name, "refs/heads/")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400326 msg = "storing head";
Ævar Arnfjörð Bjarmasonf7b37422011-02-22 23:41:53 +0000327 what = _("[new branch]");
Marc Branchaud0997ada2012-04-16 18:08:50 -0400328 } else {
329 msg = "storing ref";
330 what = _("[new ref]");
Nicolas Pitre165f3902007-11-03 01:32:48 -0400331 }
332
Jens Lehmanna6801ad2012-04-13 18:25:16 +0200333 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
334 (recurse_submodules != RECURSE_SUBMODULES_ON))
335 check_for_new_submodule_commits(ref->new_sha1);
Jeff King63154722008-06-26 23:59:50 -0400336 r = s_update_ref(msg, ref, 0);
Jeff King5914f2d2011-12-08 03:43:19 -0500337 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
338 r ? '!' : '*',
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700339 TRANSPORT_SUMMARY(what),
Jeff King5914f2d2011-12-08 03:43:19 -0500340 REFCOL_WIDTH, remote, pretty_ref,
341 r ? _(" (unable to update local ref)") : "");
Jeff King63154722008-06-26 23:59:50 -0400342 return r;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400343 }
344
Junio C Hamanoa20efee2012-08-27 14:46:01 -0700345 if (in_merge_bases(current, updated)) {
Nicolas Pitre165f3902007-11-03 01:32:48 -0400346 char quickref[83];
Jeff King63154722008-06-26 23:59:50 -0400347 int r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400348 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
349 strcat(quickref, "..");
350 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
Jens Lehmann88a21972011-03-06 23:10:46 +0100351 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
352 (recurse_submodules != RECURSE_SUBMODULES_ON))
353 check_for_new_submodule_commits(ref->new_sha1);
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300354 r = s_update_ref("fast-forward", ref, 1);
Jeff King5914f2d2011-12-08 03:43:19 -0500355 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
356 r ? '!' : ' ',
357 TRANSPORT_SUMMARY_WIDTH, quickref,
358 REFCOL_WIDTH, remote, pretty_ref,
359 r ? _(" (unable to update local ref)") : "");
Jeff King63154722008-06-26 23:59:50 -0400360 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400361 } else if (force || ref->force) {
362 char quickref[84];
Jeff King63154722008-06-26 23:59:50 -0400363 int r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400364 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
365 strcat(quickref, "...");
366 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
Jens Lehmann88a21972011-03-06 23:10:46 +0100367 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
368 (recurse_submodules != RECURSE_SUBMODULES_ON))
369 check_for_new_submodule_commits(ref->new_sha1);
Jeff King63154722008-06-26 23:59:50 -0400370 r = s_update_ref("forced-update", ref, 1);
Jeff King5914f2d2011-12-08 03:43:19 -0500371 strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
372 r ? '!' : '+',
373 TRANSPORT_SUMMARY_WIDTH, quickref,
374 REFCOL_WIDTH, remote, pretty_ref,
375 r ? _("unable to update local ref") : _("forced update"));
Jeff King63154722008-06-26 23:59:50 -0400376 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400377 } else {
Jeff King5914f2d2011-12-08 03:43:19 -0500378 strbuf_addf(display, "! %-*s %-*s -> %s %s",
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700379 TRANSPORT_SUMMARY(_("[rejected]")),
Jeff King5914f2d2011-12-08 03:43:19 -0500380 REFCOL_WIDTH, remote, pretty_ref,
381 _("(non-fast-forward)"));
Daniel Barkalowb888d612007-09-10 23:03:25 -0400382 return 1;
383 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400384}
385
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700386static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700387{
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700388 struct ref **rm = cb_data;
389 struct ref *ref = *rm;
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700390
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700391 if (!ref)
392 return -1; /* end of the list */
393 *rm = ref->next;
394 hashcpy(sha1, ref->old_sha1);
395 return 0;
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700396}
397
Andreas Ericsson47abd852009-04-17 10:20:11 +0200398static int store_updated_refs(const char *raw_url, const char *remote_name,
Jeff Kingf3cb1692008-06-27 00:01:41 -0400399 struct ref *ref_map)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400400{
401 FILE *fp;
402 struct commit *commit;
Jeff King5914f2d2011-12-08 03:43:19 -0500403 int url_len, i, shown_url = 0, rc = 0;
404 struct strbuf note = STRBUF_INIT;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400405 const char *what, *kind;
406 struct ref *rm;
Jay Soffian28a15402009-11-10 09:19:43 +0100407 char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
Jeff King900f2812013-05-11 18:15:59 +0200408 int want_status;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400409
André Goddard Rosad6617c72007-11-22 20:22:23 -0200410 fp = fopen(filename, "a");
411 if (!fp)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000412 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
Andreas Ericsson47abd852009-04-17 10:20:11 +0200413
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100414 if (raw_url)
415 url = transport_anonymize_url(raw_url);
416 else
417 url = xstrdup("foreign");
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700418
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700419 rm = ref_map;
Junio C Hamano3f7d11c2011-10-17 21:37:12 -0700420 if (check_everything_connected(iterate_ref_map, 0, &rm)) {
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800421 rc = error(_("%s did not send all necessary objects\n"), url);
422 goto abort;
423 }
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700424
Joey Hess96890f42011-12-26 12:16:56 -0400425 /*
Jeff King900f2812013-05-11 18:15:59 +0200426 * We do a pass for each fetch_head_status type in their enum order, so
427 * merged entries are written before not-for-merge. That lets readers
428 * use FETCH_HEAD as a refname to refer to the ref to be merged.
Joey Hess96890f42011-12-26 12:16:56 -0400429 */
Jeff King900f2812013-05-11 18:15:59 +0200430 for (want_status = FETCH_HEAD_MERGE;
431 want_status <= FETCH_HEAD_IGNORE;
432 want_status++) {
Joey Hess96890f42011-12-26 12:16:56 -0400433 for (rm = ref_map; rm; rm = rm->next) {
434 struct ref *ref = NULL;
Jeff King900f2812013-05-11 18:15:59 +0200435 const char *merge_status_marker = "";
Daniel Barkalowb888d612007-09-10 23:03:25 -0400436
Joey Hess96890f42011-12-26 12:16:56 -0400437 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
438 if (!commit)
Jeff King900f2812013-05-11 18:15:59 +0200439 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400440
Jeff King900f2812013-05-11 18:15:59 +0200441 if (rm->fetch_head_status != want_status)
Joey Hess96890f42011-12-26 12:16:56 -0400442 continue;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400443
Joey Hess96890f42011-12-26 12:16:56 -0400444 if (rm->peer_ref) {
445 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
446 strcpy(ref->name, rm->peer_ref->name);
447 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
448 hashcpy(ref->new_sha1, rm->old_sha1);
449 ref->force = rm->peer_ref->force;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400450 }
Joey Hess96890f42011-12-26 12:16:56 -0400451
452
453 if (!strcmp(rm->name, "HEAD")) {
454 kind = "";
455 what = "";
456 }
457 else if (!prefixcmp(rm->name, "refs/heads/")) {
458 kind = "branch";
459 what = rm->name + 11;
460 }
461 else if (!prefixcmp(rm->name, "refs/tags/")) {
462 kind = "tag";
463 what = rm->name + 10;
464 }
465 else if (!prefixcmp(rm->name, "refs/remotes/")) {
466 kind = "remote-tracking branch";
467 what = rm->name + 13;
468 }
469 else {
470 kind = "";
471 what = rm->name;
472 }
473
474 url_len = strlen(url);
475 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
476 ;
477 url_len = i + 1;
478 if (4 < i && !strncmp(".git", url + i - 3, 4))
479 url_len = i - 3;
480
481 strbuf_reset(&note);
482 if (*what) {
483 if (*kind)
484 strbuf_addf(&note, "%s ", kind);
485 strbuf_addf(&note, "'%s' of ", what);
486 }
Jeff King900f2812013-05-11 18:15:59 +0200487 switch (rm->fetch_head_status) {
488 case FETCH_HEAD_NOT_FOR_MERGE:
489 merge_status_marker = "not-for-merge";
490 /* fall-through */
491 case FETCH_HEAD_MERGE:
492 fprintf(fp, "%s\t%s\t%s",
493 sha1_to_hex(rm->old_sha1),
494 merge_status_marker,
495 note.buf);
496 for (i = 0; i < url_len; ++i)
497 if ('\n' == url[i])
498 fputs("\\n", fp);
499 else
500 fputc(url[i], fp);
501 fputc('\n', fp);
502 break;
503 default:
504 /* do not write anything to FETCH_HEAD */
505 break;
506 }
Joey Hess96890f42011-12-26 12:16:56 -0400507
508 strbuf_reset(&note);
509 if (ref) {
Marc Branchaud6da618d2012-04-16 18:08:49 -0400510 rc |= update_local_ref(ref, what, rm, &note);
Joey Hess96890f42011-12-26 12:16:56 -0400511 free(ref);
512 } else
513 strbuf_addf(&note, "* %-*s %-*s -> FETCH_HEAD",
514 TRANSPORT_SUMMARY_WIDTH,
515 *kind ? kind : "branch",
516 REFCOL_WIDTH,
517 *what ? what : "HEAD");
518 if (note.len) {
519 if (verbosity >= 0 && !shown_url) {
520 fprintf(stderr, _("From %.*s\n"),
521 url_len, url);
522 shown_url = 1;
523 }
524 if (verbosity >= 0)
525 fprintf(stderr, " %s\n", note.buf);
526 }
Nicolas Pitre165f3902007-11-03 01:32:48 -0400527 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400528 }
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800529
Jeff Kingfa250752009-05-25 06:40:54 -0400530 if (rc & STORE_REF_ERROR_DF_CONFLICT)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000531 error(_("some local refs could not be updated; try running\n"
Jeff Kingf3cb1692008-06-27 00:01:41 -0400532 " 'git remote prune %s' to remove any old, conflicting "
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000533 "branches"), remote_name);
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800534
535 abort:
Jeff King5914f2d2011-12-08 03:43:19 -0500536 strbuf_release(&note);
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800537 free(url);
538 fclose(fp);
Dmitry V. Levinefb98b42008-05-28 19:29:36 +0400539 return rc;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400540}
541
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500542/*
543 * We would want to bypass the object transfer altogether if
Johan Herlandd9eb0202009-07-10 01:52:30 +0200544 * everything we are going to fetch already exists and is connected
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500545 * locally.
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500546 */
547static int quickfetch(struct ref *ref_map)
548{
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700549 struct ref *rm = ref_map;
550
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500551 /*
552 * If we are deepening a shallow clone we already have these
553 * objects reachable. Running rev-list here will return with
554 * a good (0) exit status and we'll bypass the fetch that we
555 * really need to perform. Claiming failure now will ensure
556 * we perform the network exchange to deepen our history.
557 */
558 if (depth)
559 return -1;
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700560 return check_everything_connected(iterate_ref_map, 1, &rm);
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500561}
562
Daniel Barkalowb888d612007-09-10 23:03:25 -0400563static int fetch_refs(struct transport *transport, struct ref *ref_map)
564{
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500565 int ret = quickfetch(ref_map);
566 if (ret)
567 ret = transport_fetch_refs(transport, ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400568 if (!ret)
Jeff Kingf3cb1692008-06-27 00:01:41 -0400569 ret |= store_updated_refs(transport->url,
570 transport->remote->name,
571 ref_map);
Shawn O. Pearce1788c392007-09-14 03:31:23 -0400572 transport_unlock_pack(transport);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400573 return ret;
574}
575
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200576static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map)
Jay Soffianf360d842009-11-10 09:15:47 +0100577{
578 int result = 0;
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200579 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
Jay Soffianf360d842009-11-10 09:15:47 +0100580 const char *dangling_msg = dry_run
Nguyễn Thái Ngọc Duy18986d52012-04-23 19:30:25 +0700581 ? _(" (%s will become dangling)")
582 : _(" (%s has become dangling)");
Jay Soffianf360d842009-11-10 09:15:47 +0100583
584 for (ref = stale_refs; ref; ref = ref->next) {
585 if (!dry_run)
586 result |= delete_ref(ref->name, NULL, 0);
587 if (verbosity >= 0) {
588 fprintf(stderr, " x %-*s %-*s -> %s\n",
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700589 TRANSPORT_SUMMARY(_("[deleted]")),
Ævar Arnfjörð Bjarmason502681c2011-02-22 23:41:52 +0000590 REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
Jay Soffianf360d842009-11-10 09:15:47 +0100591 warn_dangling_symref(stderr, dangling_msg, ref->name);
592 }
593 }
594 free_refs(stale_refs);
595 return result;
596}
597
Daniel Barkalowb888d612007-09-10 23:03:25 -0400598static int add_existing(const char *refname, const unsigned char *sha1,
599 int flag, void *cbdata)
600{
Johannes Schindelinc455c872008-07-21 19:03:49 +0100601 struct string_list *list = (struct string_list *)cbdata;
Julian Phillips78a395d2010-06-26 00:41:35 +0100602 struct string_list_item *item = string_list_insert(list, refname);
Michael Haggertyf83918e2013-05-25 11:08:17 +0200603 item->util = xmalloc(20);
604 hashcpy(item->util, sha1);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400605 return 0;
606}
607
Shawn O. Pearcecf7f9292008-03-02 21:35:33 -0500608static int will_fetch(struct ref **head, const unsigned char *sha1)
609{
610 struct ref *rm = *head;
611 while (rm) {
612 if (!hashcmp(rm->old_sha1, sha1))
613 return 1;
614 rm = rm->next;
615 }
616 return 0;
617}
618
Shawn O. Pearcec50b2b42008-03-02 21:35:00 -0500619static void find_non_local_tags(struct transport *transport,
620 struct ref **head,
621 struct ref ***tail)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400622{
Michael Haggertyb87dbcc2013-05-25 11:08:01 +0200623 struct string_list existing_refs = STRING_LIST_INIT_DUP;
Thiago Farina183113a2010-07-04 16:46:19 -0300624 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
Daniel Barkalow45773702007-10-29 21:05:40 -0400625 const struct ref *ref;
Julian Phillipse984c542009-09-17 08:33:19 +0100626 struct string_list_item *item = NULL;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400627
628 for_each_ref(add_existing, &existing_refs);
629 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
Nguyễn Thái Ngọc Duy97ba6422012-01-05 19:39:40 +0700630 if (prefixcmp(ref->name, "refs/tags/"))
Daniel Barkalowb888d612007-09-10 23:03:25 -0400631 continue;
632
Julian Phillipse984c542009-09-17 08:33:19 +0100633 /*
634 * The peeled ref always follows the matching base
635 * ref, so if we see a peeled ref that we don't want
636 * to fetch then we can mark the ref entry in the list
637 * as one to ignore by setting util to NULL.
638 */
Andreas Gruenbacheraac1d7b2010-03-13 18:17:04 +0100639 if (!suffixcmp(ref->name, "^{}")) {
Julian Phillipse984c542009-09-17 08:33:19 +0100640 if (item && !has_sha1_file(ref->old_sha1) &&
641 !will_fetch(head, ref->old_sha1) &&
642 !has_sha1_file(item->util) &&
643 !will_fetch(head, item->util))
644 item->util = NULL;
645 item = NULL;
646 continue;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400647 }
648
Julian Phillipse984c542009-09-17 08:33:19 +0100649 /*
650 * If item is non-NULL here, then we previously saw a
651 * ref not followed by a peeled reference, so we need
652 * to check if it is a lightweight tag that we want to
653 * fetch.
654 */
655 if (item && !has_sha1_file(item->util) &&
656 !will_fetch(head, item->util))
657 item->util = NULL;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400658
Julian Phillipse984c542009-09-17 08:33:19 +0100659 item = NULL;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400660
Julian Phillipse984c542009-09-17 08:33:19 +0100661 /* skip duplicates and refs that we already have */
662 if (string_list_has_string(&remote_refs, ref->name) ||
663 string_list_has_string(&existing_refs, ref->name))
664 continue;
665
Julian Phillips78a395d2010-06-26 00:41:35 +0100666 item = string_list_insert(&remote_refs, ref->name);
Julian Phillipse984c542009-09-17 08:33:19 +0100667 item->util = (void *)ref->old_sha1;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400668 }
Michael Haggertyf83918e2013-05-25 11:08:17 +0200669 string_list_clear(&existing_refs, 1);
Julian Phillipse984c542009-09-17 08:33:19 +0100670
671 /*
672 * We may have a final lightweight tag that needs to be
673 * checked to see if it needs fetching.
674 */
675 if (item && !has_sha1_file(item->util) &&
676 !will_fetch(head, item->util))
677 item->util = NULL;
678
679 /*
Alex Riesen8a57c6e2010-07-03 14:41:54 +0200680 * For all the tags in the remote_refs string list,
681 * add them to the list of refs to be fetched
Julian Phillipse984c542009-09-17 08:33:19 +0100682 */
Alex Riesen8a57c6e2010-07-03 14:41:54 +0200683 for_each_string_list_item(item, &remote_refs) {
684 /* Unless we have already decided to ignore this item... */
685 if (item->util)
686 {
687 struct ref *rm = alloc_ref(item->string);
688 rm->peer_ref = alloc_ref(item->string);
689 hashcpy(rm->old_sha1, item->util);
690 **tail = rm;
691 *tail = &rm->next;
692 }
693 }
Julian Phillipse984c542009-09-17 08:33:19 +0100694
695 string_list_clear(&remote_refs, 0);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400696}
697
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200698static void check_not_current_branch(struct ref *ref_map)
699{
700 struct branch *current_branch = branch_get(NULL);
701
702 if (is_bare_repository() || !current_branch)
703 return;
704
705 for (; ref_map; ref_map = ref_map->next)
706 if (ref_map->peer_ref && !strcmp(current_branch->refname,
707 ref_map->peer_ref->name))
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000708 die(_("Refusing to fetch into current branch %s "
709 "of non-bare repository"), current_branch->refname);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200710}
711
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800712static int truncate_fetch_head(void)
713{
714 char *filename = git_path("FETCH_HEAD");
715 FILE *fp = fopen(filename, "w");
716
717 if (!fp)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000718 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800719 fclose(fp);
720 return 0;
721}
722
Daniel Barkalowb888d612007-09-10 23:03:25 -0400723static int do_fetch(struct transport *transport,
724 struct refspec *refs, int ref_count)
725{
Michael Haggertyb87dbcc2013-05-25 11:08:01 +0200726 struct string_list existing_refs = STRING_LIST_INIT_DUP;
Shawn O. Pearce7f984282008-03-02 21:34:43 -0500727 struct ref *ref_map;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400728 struct ref *rm;
729 int autotags = (transport->remote->fetch_tags == 1);
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200730 int retcode = 0;
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000731
732 for_each_ref(add_existing, &existing_refs);
733
Daniel Johnsoned368542010-08-11 18:57:20 -0400734 if (tags == TAGS_DEFAULT) {
735 if (transport->remote->fetch_tags == 2)
736 tags = TAGS_SET;
737 if (transport->remote->fetch_tags == -1)
738 tags = TAGS_UNSET;
739 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400740
Shawn O. Pearce824d5772007-09-19 00:49:31 -0400741 if (!transport->get_refs_list || !transport->fetch)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000742 die(_("Don't know how to fetch from %s"), transport->url);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400743
744 /* if not appending, truncate FETCH_HEAD */
Jay Soffian28a15402009-11-10 09:19:43 +0100745 if (!append && !dry_run) {
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200746 retcode = truncate_fetch_head();
747 if (retcode)
748 goto cleanup;
André Goddard Rosad6617c72007-11-22 20:22:23 -0200749 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400750
751 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200752 if (!update_head_ok)
753 check_not_current_branch(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400754
755 for (rm = ref_map; rm; rm = rm->next) {
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000756 if (rm->peer_ref) {
Michael Haggerty6f64a162013-05-25 11:08:15 +0200757 struct string_list_item *peer_item =
758 string_list_lookup(&existing_refs,
759 rm->peer_ref->name);
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000760 if (peer_item)
761 hashcpy(rm->peer_ref->old_sha1,
762 peer_item->util);
763 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400764 }
765
Shawn O. Pearce41fa7d22008-03-03 22:27:40 -0500766 if (tags == TAGS_DEFAULT && autotags)
767 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
Daniel Barkalowb888d612007-09-10 23:03:25 -0400768 if (fetch_refs(transport, ref_map)) {
769 free_refs(ref_map);
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200770 retcode = 1;
771 goto cleanup;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400772 }
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200773 if (prune) {
Carlos Martín Nietoe8c1e6c2011-10-15 07:04:26 +0200774 /* If --tags was specified, pretend the user gave us the canonical tags refspec */
775 if (tags == TAGS_SET) {
776 const char *tags_str = "refs/tags/*:refs/tags/*";
777 struct refspec *tags_refspec, *refspec;
778
779 /* Copy the refspec and add the tags to it */
780 refspec = xcalloc(ref_count + 1, sizeof(struct refspec));
781 tags_refspec = parse_fetch_refspec(1, &tags_str);
782 memcpy(refspec, refs, ref_count * sizeof(struct refspec));
783 memcpy(&refspec[ref_count], tags_refspec, sizeof(struct refspec));
784 ref_count++;
785
786 prune_refs(refspec, ref_count, ref_map);
787
788 ref_count--;
789 /* The rest of the strings belong to fetch_one */
790 free_refspec(1, tags_refspec);
791 free(refspec);
792 } else if (ref_count) {
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200793 prune_refs(refs, ref_count, ref_map);
Carlos Martín Nietoe8c1e6c2011-10-15 07:04:26 +0200794 } else {
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200795 prune_refs(transport->remote->fetch, transport->remote->fetch_refspec_nr, ref_map);
Carlos Martín Nietoe8c1e6c2011-10-15 07:04:26 +0200796 }
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200797 }
Shawn O. Pearce7f984282008-03-02 21:34:43 -0500798 free_refs(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400799
800 /* if neither --no-tags nor --tags was specified, do automated tag
801 * following ... */
Kristian Høgsberg83201992007-12-04 02:25:47 -0500802 if (tags == TAGS_DEFAULT && autotags) {
Shawn O. Pearcec50b2b42008-03-02 21:35:00 -0500803 struct ref **tail = &ref_map;
804 ref_map = NULL;
805 find_non_local_tags(transport, &ref_map, &tail);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400806 if (ref_map) {
Shawn O. Pearce41fa7d22008-03-03 22:27:40 -0500807 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400808 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
809 fetch_refs(transport, ref_map);
810 }
811 free_refs(ref_map);
812 }
813
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200814 cleanup:
Michael Haggertyf83918e2013-05-25 11:08:17 +0200815 string_list_clear(&existing_refs, 1);
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200816 return retcode;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400817}
818
Shawn O. Pearceab865e62007-09-18 04:54:57 -0400819static void set_option(const char *name, const char *value)
820{
Junio C Hamanoaf234452013-08-07 15:38:45 -0700821 int r = transport_set_option(gtransport, name, value);
Shawn O. Pearceab865e62007-09-18 04:54:57 -0400822 if (r < 0)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000823 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
Junio C Hamanoaf234452013-08-07 15:38:45 -0700824 name, value, gtransport->url);
Shawn O. Pearceab865e62007-09-18 04:54:57 -0400825 if (r > 0)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000826 warning(_("Option \"%s\" is ignored for %s\n"),
Junio C Hamanoaf234452013-08-07 15:38:45 -0700827 name, gtransport->url);
Shawn O. Pearceab865e62007-09-18 04:54:57 -0400828}
829
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100830static int get_one_remote_for_fetch(struct remote *remote, void *priv)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400831{
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100832 struct string_list *list = priv;
Björn Gustavsson7cc91a22009-11-09 21:11:06 +0100833 if (!remote->skip_default_update)
Julian Phillips1d2f80f2010-06-26 00:41:38 +0100834 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100835 return 0;
836}
837
838struct remote_group_data {
839 const char *name;
840 struct string_list *list;
841};
842
843static int get_remote_group(const char *key, const char *value, void *priv)
844{
845 struct remote_group_data *g = priv;
846
847 if (!prefixcmp(key, "remotes.") &&
848 !strcmp(key + 8, g->name)) {
849 /* split list by white space */
850 int space = strcspn(value, " \t\n");
851 while (*value) {
852 if (space > 1) {
Julian Phillips1d2f80f2010-06-26 00:41:38 +0100853 string_list_append(g->list,
854 xstrndup(value, space));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100855 }
856 value += space + (value[space] != '\0');
857 space = strcspn(value, " \t\n");
858 }
859 }
860
861 return 0;
862}
863
864static int add_remote_or_group(const char *name, struct string_list *list)
865{
866 int prev_nr = list->nr;
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000867 struct remote_group_data g;
868 g.name = name; g.list = list;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100869
870 git_config(get_remote_group, &g);
871 if (list->nr == prev_nr) {
872 struct remote *remote;
873 if (!remote_is_configured(name))
874 return 0;
875 remote = remote_get(name);
Julian Phillips1d2f80f2010-06-26 00:41:38 +0100876 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100877 }
878 return 1;
879}
880
Jeff King85556d42012-09-01 07:27:35 -0400881static void add_options_to_argv(struct argv_array *argv)
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100882{
883 if (dry_run)
Jeff King85556d42012-09-01 07:27:35 -0400884 argv_array_push(argv, "--dry-run");
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100885 if (prune)
Jeff King85556d42012-09-01 07:27:35 -0400886 argv_array_push(argv, "--prune");
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100887 if (update_head_ok)
Jeff King85556d42012-09-01 07:27:35 -0400888 argv_array_push(argv, "--update-head-ok");
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100889 if (force)
Jeff King85556d42012-09-01 07:27:35 -0400890 argv_array_push(argv, "--force");
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100891 if (keep)
Jeff King85556d42012-09-01 07:27:35 -0400892 argv_array_push(argv, "--keep");
Jens Lehmannbe254a02010-11-11 00:55:02 +0100893 if (recurse_submodules == RECURSE_SUBMODULES_ON)
Jeff King85556d42012-09-01 07:27:35 -0400894 argv_array_push(argv, "--recurse-submodules");
Jens Lehmann8f0700d2011-03-06 23:11:21 +0100895 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
Jeff King85556d42012-09-01 07:27:35 -0400896 argv_array_push(argv, "--recurse-submodules=on-demand");
Dan Johnson85566462012-09-05 17:22:19 -0400897 if (tags == TAGS_SET)
898 argv_array_push(argv, "--tags");
899 else if (tags == TAGS_UNSET)
900 argv_array_push(argv, "--no-tags");
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100901 if (verbosity >= 2)
Jeff King85556d42012-09-01 07:27:35 -0400902 argv_array_push(argv, "-v");
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100903 if (verbosity >= 1)
Jeff King85556d42012-09-01 07:27:35 -0400904 argv_array_push(argv, "-v");
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100905 else if (verbosity < 0)
Jeff King85556d42012-09-01 07:27:35 -0400906 argv_array_push(argv, "-q");
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100907
908}
909
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100910static int fetch_multiple(struct string_list *list)
911{
912 int i, result = 0;
Jeff King85556d42012-09-01 07:27:35 -0400913 struct argv_array argv = ARGV_ARRAY_INIT;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100914
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800915 if (!append && !dry_run) {
916 int errcode = truncate_fetch_head();
917 if (errcode)
918 return errcode;
919 }
920
Jeff King85556d42012-09-01 07:27:35 -0400921 argv_array_pushl(&argv, "fetch", "--append", NULL);
922 add_options_to_argv(&argv);
923
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100924 for (i = 0; i < list->nr; i++) {
925 const char *name = list->items[i].string;
Jeff King85556d42012-09-01 07:27:35 -0400926 argv_array_push(&argv, name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100927 if (verbosity >= 0)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000928 printf(_("Fetching %s\n"), name);
Jeff King85556d42012-09-01 07:27:35 -0400929 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000930 error(_("Could not fetch %s"), name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100931 result = 1;
932 }
Jeff King85556d42012-09-01 07:27:35 -0400933 argv_array_pop(&argv);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100934 }
935
Jeff King85556d42012-09-01 07:27:35 -0400936 argv_array_clear(&argv);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100937 return result;
938}
939
940static int fetch_one(struct remote *remote, int argc, const char **argv)
941{
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -0500942 int i;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400943 static const char **refs = NULL;
Jim Meyeringd8ead152011-06-08 22:06:33 +0200944 struct refspec *refspec;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400945 int ref_nr = 0;
Alex Riesen7b7f39e2008-04-28 22:23:35 +0200946 int exit_code;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400947
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400948 if (!remote)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000949 die(_("No remote repository specified. Please, specify either a URL or a\n"
950 "remote name from which new revisions should be fetched."));
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400951
Junio C Hamanoaf234452013-08-07 15:38:45 -0700952 gtransport = transport_get(remote, NULL);
953 transport_set_verbosity(gtransport, verbosity, progress);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400954 if (upload_pack)
Shawn O. Pearceab865e62007-09-18 04:54:57 -0400955 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400956 if (keep)
Shawn O. Pearceab865e62007-09-18 04:54:57 -0400957 set_option(TRANS_OPT_KEEP, "yes");
958 if (depth)
959 set_option(TRANS_OPT_DEPTH, depth);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400960
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100961 if (argc > 0) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400962 int j = 0;
Kristian Høgsberg83201992007-12-04 02:25:47 -0500963 refs = xcalloc(argc + 1, sizeof(const char *));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100964 for (i = 0; i < argc; i++) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400965 if (!strcmp(argv[i], "tag")) {
966 char *ref;
967 i++;
Kevin Ballardf53423b2008-04-05 14:28:53 -0400968 if (i >= argc)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000969 die(_("You need to specify a tag name."));
Daniel Barkalowb888d612007-09-10 23:03:25 -0400970 ref = xmalloc(strlen(argv[i]) * 2 + 22);
971 strcpy(ref, "refs/tags/");
972 strcat(ref, argv[i]);
973 strcat(ref, ":refs/tags/");
974 strcat(ref, argv[i]);
975 refs[j++] = ref;
976 } else
977 refs[j++] = argv[i];
Daniel Barkalowb888d612007-09-10 23:03:25 -0400978 }
979 refs[j] = NULL;
980 ref_nr = j;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400981 }
982
Jeff King57b235a2009-01-22 01:03:08 -0500983 sigchain_push_common(unlock_pack_on_signal);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400984 atexit(unlock_pack);
Jim Meyeringd8ead152011-06-08 22:06:33 +0200985 refspec = parse_fetch_refspec(ref_nr, refs);
Junio C Hamanoaf234452013-08-07 15:38:45 -0700986 exit_code = do_fetch(gtransport, refspec, ref_nr);
Carlos Martín Nieto5caf1972011-10-08 00:51:06 +0200987 free_refspec(ref_nr, refspec);
Junio C Hamanoaf234452013-08-07 15:38:45 -0700988 transport_disconnect(gtransport);
989 gtransport = NULL;
Alex Riesen7b7f39e2008-04-28 22:23:35 +0200990 return exit_code;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400991}
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100992
993int cmd_fetch(int argc, const char **argv, const char *prefix)
994{
995 int i;
Thiago Farina183113a2010-07-04 16:46:19 -0300996 struct string_list list = STRING_LIST_INIT_NODUP;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100997 struct remote *remote;
998 int result = 0;
Jeff King131b8fc2013-01-26 17:40:38 -0500999 static const char *argv_gc_auto[] = {
1000 "gc", "--auto", NULL,
1001 };
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001002
Jeff Kingbbc30f92011-02-24 09:30:19 -05001003 packet_trace_identity("fetch");
1004
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001005 /* Record the command line for the reflog */
1006 strbuf_addstr(&default_rla, "fetch");
1007 for (i = 1; i < argc; i++)
1008 strbuf_addf(&default_rla, " %s", argv[i]);
1009
1010 argc = parse_options(argc, argv, prefix,
1011 builtin_fetch_options, builtin_fetch_usage, 0);
1012
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +07001013 if (unshallow) {
1014 if (depth)
1015 die(_("--depth and --unshallow cannot be used together"));
1016 else if (!is_repository_shallow())
1017 die(_("--unshallow on a complete repository does not make sense"));
1018 else {
1019 static char inf_depth[12];
1020 sprintf(inf_depth, "%d", INFINITE_DEPTH);
1021 depth = inf_depth;
1022 }
1023 }
1024
Jens Lehmann18322ba2011-09-09 20:22:03 +02001025 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1026 if (recurse_submodules_default) {
1027 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
1028 set_config_fetch_recurse_submodules(arg);
1029 }
1030 gitmodules_config();
1031 git_config(submodule_config, NULL);
1032 }
1033
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001034 if (all) {
1035 if (argc == 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001036 die(_("fetch --all does not take a repository argument"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001037 else if (argc > 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001038 die(_("fetch --all does not make sense with refspecs"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001039 (void) for_each_remote(get_one_remote_for_fetch, &list);
1040 result = fetch_multiple(&list);
1041 } else if (argc == 0) {
1042 /* No arguments -- use default remote */
1043 remote = remote_get(NULL);
1044 result = fetch_one(remote, argc, argv);
Björn Gustavsson16679e32009-11-09 21:10:32 +01001045 } else if (multiple) {
1046 /* All arguments are assumed to be remotes or groups */
1047 for (i = 0; i < argc; i++)
1048 if (!add_remote_or_group(argv[i], &list))
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001049 die(_("No such remote or remote group: %s"), argv[i]);
Björn Gustavsson16679e32009-11-09 21:10:32 +01001050 result = fetch_multiple(&list);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001051 } else {
1052 /* Single remote or group */
1053 (void) add_remote_or_group(argv[0], &list);
1054 if (list.nr > 1) {
1055 /* More than one remote */
1056 if (argc > 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001057 die(_("Fetching a group and specifying refspecs does not make sense"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001058 result = fetch_multiple(&list);
1059 } else {
1060 /* Zero or one remotes */
1061 remote = remote_get(argv[0]);
1062 result = fetch_one(remote, argc-1, argv+1);
1063 }
1064 }
1065
Jens Lehmannbe254a02010-11-11 00:55:02 +01001066 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
Jeff King85556d42012-09-01 07:27:35 -04001067 struct argv_array options = ARGV_ARRAY_INIT;
1068
1069 add_options_to_argv(&options);
Jens Lehmann50d89ad2012-09-01 17:27:06 +02001070 result = fetch_populated_submodules(&options,
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001071 submodule_prefix,
Jens Lehmann8f0700d2011-03-06 23:11:21 +01001072 recurse_submodules,
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001073 verbosity < 0);
Jeff King85556d42012-09-01 07:27:35 -04001074 argv_array_clear(&options);
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001075 }
1076
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001077 /* All names were strdup()ed or strndup()ed */
1078 list.strdup_strings = 1;
1079 string_list_clear(&list, 0);
1080
Jeff King131b8fc2013-01-26 17:40:38 -05001081 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
1082
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001083 return result;
1084}