blob: 3d978eb58ec5cc1e2629db10562319c666bc031e [file] [log] [blame]
Daniel Barkalowb888d612007-09-10 23:03:25 -04001/*
2 * "git fetch"
3 */
4#include "cache.h"
5#include "refs.h"
6#include "commit.h"
7#include "builtin.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01008#include "string-list.h"
Daniel Barkalowb888d612007-09-10 23:03:25 -04009#include "remote.h"
10#include "transport.h"
Shawn O. Pearce4191c352007-11-11 02:29:47 -050011#include "run-command.h"
Kristian Høgsberg83201992007-12-04 02:25:47 -050012#include "parse-options.h"
Jeff King4a16d072009-01-22 01:02:35 -050013#include "sigchain.h"
Michael Lukashovf1863d02010-02-16 23:42:52 +000014#include "transport.h"
Jens Lehmann7dce19d2010-11-12 13:54:52 +010015#include "submodule.h"
Junio C Hamanof96400c2011-09-02 16:33:22 -070016#include "connected.h"
Jeff King85556d42012-09-01 07:27:35 -040017#include "argv-array.h"
Daniel Barkalowb888d612007-09-10 23:03:25 -040018
Kristian Høgsberg83201992007-12-04 02:25:47 -050019static const char * const builtin_fetch_usage[] = {
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070020 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
21 N_("git fetch [<options>] <group>"),
22 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
23 N_("git fetch --all [<options>]"),
Kristian Høgsberg83201992007-12-04 02:25:47 -050024 NULL
25};
Daniel Barkalowb888d612007-09-10 23:03:25 -040026
Kristian Høgsberg83201992007-12-04 02:25:47 -050027enum {
28 TAGS_UNSET = 0,
29 TAGS_DEFAULT = 1,
30 TAGS_SET = 2
31};
32
Michael Schubert737c5a92013-07-13 11:36:24 +020033static int fetch_prune_config = -1; /* unspecified */
34static int prune = -1; /* unspecified */
35#define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
36
37static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity;
Clemens Buchacher01fdc212012-02-13 21:17:15 +010038static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +070039static int tags = TAGS_DEFAULT, unshallow;
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;
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -040047
Jens Lehmann8f0700d2011-03-06 23:11:21 +010048static int option_parse_recurse_submodules(const struct option *opt,
49 const char *arg, int unset)
50{
51 if (unset) {
52 recurse_submodules = RECURSE_SUBMODULES_OFF;
53 } else {
54 if (arg)
55 recurse_submodules = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
56 else
57 recurse_submodules = RECURSE_SUBMODULES_ON;
58 }
59 return 0;
60}
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -040061
Michael Schubert737c5a92013-07-13 11:36:24 +020062static int git_fetch_config(const char *k, const char *v, void *cb)
63{
64 if (!strcmp(k, "fetch.prune")) {
65 fetch_prune_config = git_config_bool(k, v);
66 return 0;
67 }
68 return 0;
69}
70
Kristian Høgsberg83201992007-12-04 02:25:47 -050071static struct option builtin_fetch_options[] = {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +010072 OPT__VERBOSITY(&verbosity),
Stefan Bellerd5d09d42013-08-03 13:51:19 +020073 OPT_BOOL(0, "all", &all,
74 N_("fetch from all remotes")),
75 OPT_BOOL('a', "append", &append,
76 N_("append to .git/FETCH_HEAD instead of overwriting")),
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070077 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
78 N_("path to upload pack on remote end")),
79 OPT__FORCE(&force, N_("force overwrite of local branch")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +020080 OPT_BOOL('m', "multiple", &multiple,
81 N_("fetch from multiple remotes")),
Kristian Høgsberg83201992007-12-04 02:25:47 -050082 OPT_SET_INT('t', "tags", &tags,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070083 N_("fetch all tags and associated objects"), TAGS_SET),
Johannes Schindeline7951292008-03-13 08:13:15 +010084 OPT_SET_INT('n', NULL, &tags,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070085 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
Stefan Bellerd5d09d42013-08-03 13:51:19 +020086 OPT_BOOL('p', "prune", &prune,
87 N_("prune remote-tracking branches no longer on remote")),
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070088 { OPTION_CALLBACK, 0, "recurse-submodules", NULL, N_("on-demand"),
89 N_("control recursive fetching of submodules"),
Jens Lehmann8f0700d2011-03-06 23:11:21 +010090 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
Stefan Bellerd5d09d42013-08-03 13:51:19 +020091 OPT_BOOL(0, "dry-run", &dry_run,
92 N_("dry run")),
93 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
94 OPT_BOOL('u', "update-head-ok", &update_head_ok,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +070095 N_("allow updating of HEAD ref")),
96 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
97 OPT_STRING(0, "depth", &depth, N_("depth"),
98 N_("deepen history of shallow clone")),
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +070099 { OPTION_SET_INT, 0, "unshallow", &unshallow, NULL,
100 N_("convert to a complete repository"),
101 PARSE_OPT_NONEG | PARSE_OPT_NOARG, NULL, 1 },
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700102 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
103 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
Jens Lehmann88a21972011-03-06 23:10:46 +0100104 { OPTION_STRING, 0, "recurse-submodules-default",
105 &recurse_submodules_default, NULL,
Nguyễn Thái Ngọc Duy719aced2012-08-20 19:32:09 +0700106 N_("default mode for recursion"), PARSE_OPT_HIDDEN },
Kristian Høgsberg83201992007-12-04 02:25:47 -0500107 OPT_END()
108};
109
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400110static void unlock_pack(void)
111{
Junio C Hamanoaf234452013-08-07 15:38:45 -0700112 if (gtransport)
113 transport_unlock_pack(gtransport);
Junio C Hamanob26ed432013-08-07 15:47:18 -0700114 if (gsecondary)
115 transport_unlock_pack(gsecondary);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400116}
117
118static void unlock_pack_on_signal(int signo)
119{
120 unlock_pack();
Jeff King4a16d072009-01-22 01:02:35 -0500121 sigchain_pop(signo);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400122 raise(signo);
123}
Daniel Barkalowb888d612007-09-10 23:03:25 -0400124
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400125static void add_merge_config(struct ref **head,
Daniel Barkalow45773702007-10-29 21:05:40 -0400126 const struct ref *remote_refs,
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400127 struct branch *branch,
128 struct ref ***tail)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400129{
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400130 int i;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400131
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400132 for (i = 0; i < branch->merge_nr; i++) {
133 struct ref *rm, **old_tail = *tail;
134 struct refspec refspec;
135
136 for (rm = *head; rm; rm = rm->next) {
137 if (branch_merge_matches(branch, i, rm->name)) {
Jeff King900f2812013-05-11 18:15:59 +0200138 rm->fetch_head_status = FETCH_HEAD_MERGE;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400139 break;
140 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400141 }
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400142 if (rm)
143 continue;
144
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700145 /*
Matthieu Moy8b3f3f82010-11-02 16:31:23 +0100146 * Not fetched to a remote-tracking branch? We need to fetch
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400147 * it anyway to allow this branch's "branch.$name.merge"
Heikki Orsila05207a22008-09-09 13:28:30 +0300148 * to be honored by 'git pull', but we do not have to
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700149 * fail if branch.$name.merge is misconfigured to point
150 * at a nonexisting branch. If we were indeed called by
Heikki Orsila05207a22008-09-09 13:28:30 +0300151 * 'git pull', it will notice the misconfiguration because
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700152 * there is no entry in the resulting FETCH_HEAD marked
153 * for merging.
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400154 */
Andreas Gruenbacher8da61a22010-03-12 23:27:33 +0100155 memset(&refspec, 0, sizeof(refspec));
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400156 refspec.src = branch->merge[i]->src;
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700157 get_fetch_map(remote_refs, &refspec, tail, 1);
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400158 for (rm = *old_tail; rm; rm = rm->next)
Jeff King900f2812013-05-11 18:15:59 +0200159 rm->fetch_head_status = FETCH_HEAD_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400160 }
161}
162
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100163static int add_existing(const char *refname, const unsigned char *sha1,
164 int flag, void *cbdata)
165{
166 struct string_list *list = (struct string_list *)cbdata;
167 struct string_list_item *item = string_list_insert(list, refname);
168 item->util = xmalloc(20);
169 hashcpy(item->util, sha1);
170 return 0;
171}
172
173static int will_fetch(struct ref **head, const unsigned char *sha1)
174{
175 struct ref *rm = *head;
176 while (rm) {
177 if (!hashcmp(rm->old_sha1, sha1))
178 return 1;
179 rm = rm->next;
180 }
181 return 0;
182}
183
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500184static void find_non_local_tags(struct transport *transport,
185 struct ref **head,
Michael Haggertya0fbb5a2013-10-30 06:32:55 +0100186 struct ref ***tail)
187{
188 struct string_list existing_refs = STRING_LIST_INIT_DUP;
189 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
190 const struct ref *ref;
191 struct string_list_item *item = NULL;
192
193 for_each_ref(add_existing, &existing_refs);
194 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
195 if (prefixcmp(ref->name, "refs/tags/"))
196 continue;
197
198 /*
199 * The peeled ref always follows the matching base
200 * ref, so if we see a peeled ref that we don't want
201 * to fetch then we can mark the ref entry in the list
202 * as one to ignore by setting util to NULL.
203 */
204 if (!suffixcmp(ref->name, "^{}")) {
205 if (item && !has_sha1_file(ref->old_sha1) &&
206 !will_fetch(head, ref->old_sha1) &&
207 !has_sha1_file(item->util) &&
208 !will_fetch(head, item->util))
209 item->util = NULL;
210 item = NULL;
211 continue;
212 }
213
214 /*
215 * If item is non-NULL here, then we previously saw a
216 * ref not followed by a peeled reference, so we need
217 * to check if it is a lightweight tag that we want to
218 * fetch.
219 */
220 if (item && !has_sha1_file(item->util) &&
221 !will_fetch(head, item->util))
222 item->util = NULL;
223
224 item = NULL;
225
226 /* skip duplicates and refs that we already have */
227 if (string_list_has_string(&remote_refs, ref->name) ||
228 string_list_has_string(&existing_refs, ref->name))
229 continue;
230
231 item = string_list_insert(&remote_refs, ref->name);
232 item->util = (void *)ref->old_sha1;
233 }
234 string_list_clear(&existing_refs, 1);
235
236 /*
237 * We may have a final lightweight tag that needs to be
238 * checked to see if it needs fetching.
239 */
240 if (item && !has_sha1_file(item->util) &&
241 !will_fetch(head, item->util))
242 item->util = NULL;
243
244 /*
245 * For all the tags in the remote_refs string list,
246 * add them to the list of refs to be fetched
247 */
248 for_each_string_list_item(item, &remote_refs) {
249 /* Unless we have already decided to ignore this item... */
250 if (item->util)
251 {
252 struct ref *rm = alloc_ref(item->string);
253 rm->peer_ref = alloc_ref(item->string);
254 hashcpy(rm->old_sha1, item->util);
255 **tail = rm;
256 *tail = &rm->next;
257 }
258 }
259
260 string_list_clear(&remote_refs, 0);
261}
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500262
Daniel Barkalowb888d612007-09-10 23:03:25 -0400263static struct ref *get_ref_map(struct transport *transport,
Michael Haggertyf137a452013-10-23 17:50:38 +0200264 struct refspec *refspecs, int refspec_count,
265 int tags, int *autotags)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400266{
267 int i;
268 struct ref *rm;
269 struct ref *ref_map = NULL;
270 struct ref **tail = &ref_map;
271
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100272 /* opportunistically-updated references: */
273 struct ref *orefs = NULL, **oref_tail = &orefs;
274
Daniel Barkalow45773702007-10-29 21:05:40 -0400275 const struct ref *remote_refs = transport_get_remote_refs(transport);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400276
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100277 if (refspec_count) {
Michael Haggertyf137a452013-10-23 17:50:38 +0200278 for (i = 0; i < refspec_count; i++) {
279 get_fetch_map(remote_refs, &refspecs[i], &tail, 0);
280 if (refspecs[i].dst && refspecs[i].dst[0])
Daniel Barkalowb888d612007-09-10 23:03:25 -0400281 *autotags = 1;
282 }
Michael Haggerty0281c932013-10-30 06:32:58 +0100283 /* Merge everything on the command line (but not --tags) */
Daniel Barkalowb888d612007-09-10 23:03:25 -0400284 for (rm = ref_map; rm; rm = rm->next)
Jeff King900f2812013-05-11 18:15:59 +0200285 rm->fetch_head_status = FETCH_HEAD_MERGE;
Michael Haggerty0281c932013-10-30 06:32:58 +0100286
287 /*
288 * For any refs that we happen to be fetching via
289 * command-line arguments, the destination ref might
290 * have been missing or have been different than the
291 * remote-tracking ref that would be derived from the
292 * configured refspec. In these cases, we want to
293 * take the opportunity to update their configured
294 * remote-tracking reference. However, we do not want
295 * to mention these entries in FETCH_HEAD at all, as
296 * they would simply be duplicates of existing
297 * entries, so we set them FETCH_HEAD_IGNORE below.
298 *
299 * We compute these entries now, based only on the
300 * refspecs specified on the command line. But we add
301 * them to the list following the refspecs resulting
302 * from the tags option so that one of the latter,
303 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
304 * by ref_remove_duplicates() in favor of one of these
305 * opportunistic entries with FETCH_HEAD_IGNORE.
306 */
307 for (i = 0; i < transport->remote->fetch_refspec_nr; i++)
308 get_fetch_map(ref_map, &transport->remote->fetch[i],
309 &oref_tail, 1);
310
Daniel Barkalowe0aaa292008-04-17 19:32:35 -0400311 if (tags == TAGS_SET)
312 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400313 } else {
314 /* Use the defaults */
315 struct remote *remote = transport->remote;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400316 struct branch *branch = branch_get(NULL);
317 int has_merge = branch_has_merge_config(branch);
Brandon Casey3ee17572010-08-25 12:52:56 -0500318 if (remote &&
319 (remote->fetch_refspec_nr ||
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500320 /* Note: has_merge implies non-NULL branch->remote_name */
Brandon Casey3ee17572010-08-25 12:52:56 -0500321 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400322 for (i = 0; i < remote->fetch_refspec_nr; i++) {
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700323 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400324 if (remote->fetch[i].dst &&
325 remote->fetch[i].dst[0])
326 *autotags = 1;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400327 if (!i && !has_merge && ref_map &&
Daniel Barkalowcfb8f892007-09-28 19:34:17 -0400328 !remote->fetch[0].pattern)
Jeff King900f2812013-05-11 18:15:59 +0200329 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400330 }
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100331 /*
332 * if the remote we're fetching from is the same
333 * as given in branch.<name>.remote, we add the
334 * ref given in branch.<name>.merge, too.
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500335 *
336 * Note: has_merge implies non-NULL branch->remote_name
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100337 */
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700338 if (has_merge &&
339 !strcmp(branch->remote_name, remote->name))
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400340 add_merge_config(&ref_map, remote_refs, branch, &tail);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400341 } else {
342 ref_map = get_remote_ref(remote_refs, "HEAD");
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700343 if (!ref_map)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000344 die(_("Couldn't find remote ref HEAD"));
Jeff King900f2812013-05-11 18:15:59 +0200345 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
Shawn O. Pearce5aaf7f22008-03-02 21:34:51 -0500346 tail = &ref_map->next;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400347 }
348 }
Michael Haggerty0281c932013-10-30 06:32:58 +0100349
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100350 if (tags == TAGS_SET)
351 /* also fetch all tags */
352 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
353 else if (tags == TAGS_DEFAULT && *autotags)
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500354 find_non_local_tags(transport, &ref_map, &tail);
Michael Haggerty0281c932013-10-30 06:32:58 +0100355
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100356 /* Now append any refs to be updated opportunistically: */
357 *tail = orefs;
358 for (rm = orefs; rm; rm = rm->next) {
359 rm->fetch_head_status = FETCH_HEAD_IGNORE;
360 tail = &rm->next;
361 }
362
Michael Haggertyb9afe662013-10-30 06:33:09 +0100363 return ref_remove_duplicates(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400364}
365
Jeff Kingfa250752009-05-25 06:40:54 -0400366#define STORE_REF_ERROR_OTHER 1
367#define STORE_REF_ERROR_DF_CONFLICT 2
368
Daniel Barkalowb888d612007-09-10 23:03:25 -0400369static int s_update_ref(const char *action,
370 struct ref *ref,
371 int check_old)
372{
373 char msg[1024];
374 char *rla = getenv("GIT_REFLOG_ACTION");
375 static struct ref_lock *lock;
376
Jay Soffian28a15402009-11-10 09:19:43 +0100377 if (dry_run)
378 return 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400379 if (!rla)
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -0500380 rla = default_rla.buf;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400381 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
382 lock = lock_any_ref_for_update(ref->name,
Brad King9bbb0fa2013-08-30 14:12:00 -0400383 check_old ? ref->old_sha1 : NULL,
384 0, NULL);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400385 if (!lock)
Jeff Kingfa250752009-05-25 06:40:54 -0400386 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
387 STORE_REF_ERROR_OTHER;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400388 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
Jeff Kingfa250752009-05-25 06:40:54 -0400389 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
390 STORE_REF_ERROR_OTHER;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400391 return 0;
392}
393
Pierre Habouzit9ef42722007-11-04 15:05:45 -0800394#define REFCOL_WIDTH 10
Nicolas Pitre165f3902007-11-03 01:32:48 -0400395
Daniel Barkalowb888d612007-09-10 23:03:25 -0400396static int update_local_ref(struct ref *ref,
Nicolas Pitre165f3902007-11-03 01:32:48 -0400397 const char *remote,
Marc Branchaud6da618d2012-04-16 18:08:49 -0400398 const struct ref *remote_ref,
Jeff King5914f2d2011-12-08 03:43:19 -0500399 struct strbuf *display)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400400{
Daniel Barkalowb888d612007-09-10 23:03:25 -0400401 struct commit *current = NULL, *updated;
402 enum object_type type;
403 struct branch *current_branch = branch_get(NULL);
Felipe Contreras4577e482009-05-14 00:22:04 +0300404 const char *pretty_ref = prettify_refname(ref->name);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400405
406 type = sha1_object_info(ref->new_sha1, NULL);
407 if (type < 0)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000408 die(_("object %s not found"), sha1_to_hex(ref->new_sha1));
Daniel Barkalowb888d612007-09-10 23:03:25 -0400409
Daniel Barkalowb888d612007-09-10 23:03:25 -0400410 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100411 if (verbosity > 0)
Jeff King5914f2d2011-12-08 03:43:19 -0500412 strbuf_addf(display, "= %-*s %-*s -> %s",
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700413 TRANSPORT_SUMMARY(_("[up to date]")),
414 REFCOL_WIDTH, remote, pretty_ref);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400415 return 0;
416 }
417
Shawn O. Pearceb3abdd92007-09-16 02:31:26 -0400418 if (current_branch &&
419 !strcmp(ref->name, current_branch->name) &&
Daniel Barkalowb888d612007-09-10 23:03:25 -0400420 !(update_head_ok || is_bare_repository()) &&
421 !is_null_sha1(ref->old_sha1)) {
422 /*
423 * If this is the head, and it's not okay to update
424 * the head, and the old value of the head isn't empty...
425 */
Jeff King5914f2d2011-12-08 03:43:19 -0500426 strbuf_addf(display,
427 _("! %-*s %-*s -> %s (can't fetch in current branch)"),
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700428 TRANSPORT_SUMMARY(_("[rejected]")),
Jeff King5914f2d2011-12-08 03:43:19 -0500429 REFCOL_WIDTH, remote, pretty_ref);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400430 return 1;
431 }
432
433 if (!is_null_sha1(ref->old_sha1) &&
434 !prefixcmp(ref->name, "refs/tags/")) {
Jeff King63154722008-06-26 23:59:50 -0400435 int r;
436 r = s_update_ref("updating tag", ref, 0);
Jeff King5914f2d2011-12-08 03:43:19 -0500437 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
438 r ? '!' : '-',
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700439 TRANSPORT_SUMMARY(_("[tag update]")),
Jeff King5914f2d2011-12-08 03:43:19 -0500440 REFCOL_WIDTH, remote, pretty_ref,
441 r ? _(" (unable to update local ref)") : "");
Jeff King63154722008-06-26 23:59:50 -0400442 return r;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400443 }
444
Shawn O. Pearcecfa5b2b2007-10-19 00:54:59 -0400445 current = lookup_commit_reference_gently(ref->old_sha1, 1);
446 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400447 if (!current || !updated) {
Nicolas Pitre165f3902007-11-03 01:32:48 -0400448 const char *msg;
449 const char *what;
Jeff King63154722008-06-26 23:59:50 -0400450 int r;
Marc Branchaud0997ada2012-04-16 18:08:50 -0400451 /*
452 * Nicely describe the new ref we're fetching.
453 * Base this on the remote's ref name, as it's
454 * more likely to follow a standard layout.
455 */
456 const char *name = remote_ref ? remote_ref->name : "";
457 if (!prefixcmp(name, "refs/tags/")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400458 msg = "storing tag";
Ævar Arnfjörð Bjarmasonf7b37422011-02-22 23:41:53 +0000459 what = _("[new tag]");
Marc Branchaud0997ada2012-04-16 18:08:50 -0400460 } else if (!prefixcmp(name, "refs/heads/")) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400461 msg = "storing head";
Ævar Arnfjörð Bjarmasonf7b37422011-02-22 23:41:53 +0000462 what = _("[new branch]");
Marc Branchaud0997ada2012-04-16 18:08:50 -0400463 } else {
464 msg = "storing ref";
465 what = _("[new ref]");
Nicolas Pitre165f3902007-11-03 01:32:48 -0400466 }
467
Jens Lehmanna6801ad2012-04-13 18:25:16 +0200468 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
469 (recurse_submodules != RECURSE_SUBMODULES_ON))
470 check_for_new_submodule_commits(ref->new_sha1);
Jeff King63154722008-06-26 23:59:50 -0400471 r = s_update_ref(msg, ref, 0);
Jeff King5914f2d2011-12-08 03:43:19 -0500472 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
473 r ? '!' : '*',
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700474 TRANSPORT_SUMMARY(what),
Jeff King5914f2d2011-12-08 03:43:19 -0500475 REFCOL_WIDTH, remote, pretty_ref,
476 r ? _(" (unable to update local ref)") : "");
Jeff King63154722008-06-26 23:59:50 -0400477 return r;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400478 }
479
Junio C Hamanoa20efee2012-08-27 14:46:01 -0700480 if (in_merge_bases(current, updated)) {
Nicolas Pitre165f3902007-11-03 01:32:48 -0400481 char quickref[83];
Jeff King63154722008-06-26 23:59:50 -0400482 int r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400483 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
484 strcat(quickref, "..");
485 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
Jens Lehmann88a21972011-03-06 23:10:46 +0100486 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
487 (recurse_submodules != RECURSE_SUBMODULES_ON))
488 check_for_new_submodule_commits(ref->new_sha1);
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300489 r = s_update_ref("fast-forward", ref, 1);
Jeff King5914f2d2011-12-08 03:43:19 -0500490 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
491 r ? '!' : ' ',
492 TRANSPORT_SUMMARY_WIDTH, quickref,
493 REFCOL_WIDTH, remote, pretty_ref,
494 r ? _(" (unable to update local ref)") : "");
Jeff King63154722008-06-26 23:59:50 -0400495 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400496 } else if (force || ref->force) {
497 char quickref[84];
Jeff King63154722008-06-26 23:59:50 -0400498 int r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400499 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
500 strcat(quickref, "...");
501 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
Jens Lehmann88a21972011-03-06 23:10:46 +0100502 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
503 (recurse_submodules != RECURSE_SUBMODULES_ON))
504 check_for_new_submodule_commits(ref->new_sha1);
Jeff King63154722008-06-26 23:59:50 -0400505 r = s_update_ref("forced-update", ref, 1);
Jeff King5914f2d2011-12-08 03:43:19 -0500506 strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
507 r ? '!' : '+',
508 TRANSPORT_SUMMARY_WIDTH, quickref,
509 REFCOL_WIDTH, remote, pretty_ref,
510 r ? _("unable to update local ref") : _("forced update"));
Jeff King63154722008-06-26 23:59:50 -0400511 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400512 } else {
Jeff King5914f2d2011-12-08 03:43:19 -0500513 strbuf_addf(display, "! %-*s %-*s -> %s %s",
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700514 TRANSPORT_SUMMARY(_("[rejected]")),
Jeff King5914f2d2011-12-08 03:43:19 -0500515 REFCOL_WIDTH, remote, pretty_ref,
516 _("(non-fast-forward)"));
Daniel Barkalowb888d612007-09-10 23:03:25 -0400517 return 1;
518 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400519}
520
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700521static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700522{
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700523 struct ref **rm = cb_data;
524 struct ref *ref = *rm;
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700525
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700526 if (!ref)
527 return -1; /* end of the list */
528 *rm = ref->next;
529 hashcpy(sha1, ref->old_sha1);
530 return 0;
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700531}
532
Andreas Ericsson47abd852009-04-17 10:20:11 +0200533static int store_updated_refs(const char *raw_url, const char *remote_name,
Jeff Kingf3cb1692008-06-27 00:01:41 -0400534 struct ref *ref_map)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400535{
536 FILE *fp;
537 struct commit *commit;
Jeff King5914f2d2011-12-08 03:43:19 -0500538 int url_len, i, shown_url = 0, rc = 0;
539 struct strbuf note = STRBUF_INIT;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400540 const char *what, *kind;
541 struct ref *rm;
Jay Soffian28a15402009-11-10 09:19:43 +0100542 char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
Jeff King900f2812013-05-11 18:15:59 +0200543 int want_status;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400544
André Goddard Rosad6617c72007-11-22 20:22:23 -0200545 fp = fopen(filename, "a");
546 if (!fp)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000547 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
Andreas Ericsson47abd852009-04-17 10:20:11 +0200548
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100549 if (raw_url)
550 url = transport_anonymize_url(raw_url);
551 else
552 url = xstrdup("foreign");
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700553
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700554 rm = ref_map;
Junio C Hamano3f7d11c2011-10-17 21:37:12 -0700555 if (check_everything_connected(iterate_ref_map, 0, &rm)) {
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800556 rc = error(_("%s did not send all necessary objects\n"), url);
557 goto abort;
558 }
Junio C Hamano6b67e0d2011-09-01 15:43:35 -0700559
Joey Hess96890f42011-12-26 12:16:56 -0400560 /*
Jeff King900f2812013-05-11 18:15:59 +0200561 * We do a pass for each fetch_head_status type in their enum order, so
562 * merged entries are written before not-for-merge. That lets readers
563 * use FETCH_HEAD as a refname to refer to the ref to be merged.
Joey Hess96890f42011-12-26 12:16:56 -0400564 */
Jeff King900f2812013-05-11 18:15:59 +0200565 for (want_status = FETCH_HEAD_MERGE;
566 want_status <= FETCH_HEAD_IGNORE;
567 want_status++) {
Joey Hess96890f42011-12-26 12:16:56 -0400568 for (rm = ref_map; rm; rm = rm->next) {
569 struct ref *ref = NULL;
Jeff King900f2812013-05-11 18:15:59 +0200570 const char *merge_status_marker = "";
Daniel Barkalowb888d612007-09-10 23:03:25 -0400571
Joey Hess96890f42011-12-26 12:16:56 -0400572 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
573 if (!commit)
Jeff King900f2812013-05-11 18:15:59 +0200574 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400575
Jeff King900f2812013-05-11 18:15:59 +0200576 if (rm->fetch_head_status != want_status)
Joey Hess96890f42011-12-26 12:16:56 -0400577 continue;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400578
Joey Hess96890f42011-12-26 12:16:56 -0400579 if (rm->peer_ref) {
580 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
581 strcpy(ref->name, rm->peer_ref->name);
582 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
583 hashcpy(ref->new_sha1, rm->old_sha1);
584 ref->force = rm->peer_ref->force;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400585 }
Joey Hess96890f42011-12-26 12:16:56 -0400586
587
588 if (!strcmp(rm->name, "HEAD")) {
589 kind = "";
590 what = "";
591 }
592 else if (!prefixcmp(rm->name, "refs/heads/")) {
593 kind = "branch";
594 what = rm->name + 11;
595 }
596 else if (!prefixcmp(rm->name, "refs/tags/")) {
597 kind = "tag";
598 what = rm->name + 10;
599 }
600 else if (!prefixcmp(rm->name, "refs/remotes/")) {
601 kind = "remote-tracking branch";
602 what = rm->name + 13;
603 }
604 else {
605 kind = "";
606 what = rm->name;
607 }
608
609 url_len = strlen(url);
610 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
611 ;
612 url_len = i + 1;
613 if (4 < i && !strncmp(".git", url + i - 3, 4))
614 url_len = i - 3;
615
616 strbuf_reset(&note);
617 if (*what) {
618 if (*kind)
619 strbuf_addf(&note, "%s ", kind);
620 strbuf_addf(&note, "'%s' of ", what);
621 }
Jeff King900f2812013-05-11 18:15:59 +0200622 switch (rm->fetch_head_status) {
623 case FETCH_HEAD_NOT_FOR_MERGE:
624 merge_status_marker = "not-for-merge";
625 /* fall-through */
626 case FETCH_HEAD_MERGE:
627 fprintf(fp, "%s\t%s\t%s",
628 sha1_to_hex(rm->old_sha1),
629 merge_status_marker,
630 note.buf);
631 for (i = 0; i < url_len; ++i)
632 if ('\n' == url[i])
633 fputs("\\n", fp);
634 else
635 fputc(url[i], fp);
636 fputc('\n', fp);
637 break;
638 default:
639 /* do not write anything to FETCH_HEAD */
640 break;
641 }
Joey Hess96890f42011-12-26 12:16:56 -0400642
643 strbuf_reset(&note);
644 if (ref) {
Marc Branchaud6da618d2012-04-16 18:08:49 -0400645 rc |= update_local_ref(ref, what, rm, &note);
Joey Hess96890f42011-12-26 12:16:56 -0400646 free(ref);
647 } else
648 strbuf_addf(&note, "* %-*s %-*s -> FETCH_HEAD",
649 TRANSPORT_SUMMARY_WIDTH,
650 *kind ? kind : "branch",
651 REFCOL_WIDTH,
652 *what ? what : "HEAD");
653 if (note.len) {
654 if (verbosity >= 0 && !shown_url) {
655 fprintf(stderr, _("From %.*s\n"),
656 url_len, url);
657 shown_url = 1;
658 }
659 if (verbosity >= 0)
660 fprintf(stderr, " %s\n", note.buf);
661 }
Nicolas Pitre165f3902007-11-03 01:32:48 -0400662 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400663 }
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800664
Jeff Kingfa250752009-05-25 06:40:54 -0400665 if (rc & STORE_REF_ERROR_DF_CONFLICT)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000666 error(_("some local refs could not be updated; try running\n"
Jeff Kingf3cb1692008-06-27 00:01:41 -0400667 " 'git remote prune %s' to remove any old, conflicting "
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000668 "branches"), remote_name);
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800669
670 abort:
Jeff King5914f2d2011-12-08 03:43:19 -0500671 strbuf_release(&note);
Tay Ray Chuan9516a592011-10-07 15:40:22 +0800672 free(url);
673 fclose(fp);
Dmitry V. Levinefb98b42008-05-28 19:29:36 +0400674 return rc;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400675}
676
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500677/*
678 * We would want to bypass the object transfer altogether if
Johan Herlandd9eb0202009-07-10 01:52:30 +0200679 * everything we are going to fetch already exists and is connected
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500680 * locally.
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500681 */
682static int quickfetch(struct ref *ref_map)
683{
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700684 struct ref *rm = ref_map;
685
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500686 /*
687 * If we are deepening a shallow clone we already have these
688 * objects reachable. Running rev-list here will return with
689 * a good (0) exit status and we'll bypass the fetch that we
690 * really need to perform. Claiming failure now will ensure
691 * we perform the network exchange to deepen our history.
692 */
693 if (depth)
694 return -1;
Junio C Hamanof0e278b2011-09-02 16:22:47 -0700695 return check_everything_connected(iterate_ref_map, 1, &rm);
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500696}
697
Daniel Barkalowb888d612007-09-10 23:03:25 -0400698static int fetch_refs(struct transport *transport, struct ref *ref_map)
699{
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500700 int ret = quickfetch(ref_map);
701 if (ret)
702 ret = transport_fetch_refs(transport, ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400703 if (!ret)
Jeff Kingf3cb1692008-06-27 00:01:41 -0400704 ret |= store_updated_refs(transport->url,
705 transport->remote->name,
706 ref_map);
Shawn O. Pearce1788c392007-09-14 03:31:23 -0400707 transport_unlock_pack(transport);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400708 return ret;
709}
710
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200711static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map)
Jay Soffianf360d842009-11-10 09:15:47 +0100712{
713 int result = 0;
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200714 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
Jay Soffianf360d842009-11-10 09:15:47 +0100715 const char *dangling_msg = dry_run
Nguyễn Thái Ngọc Duy18986d52012-04-23 19:30:25 +0700716 ? _(" (%s will become dangling)")
717 : _(" (%s has become dangling)");
Jay Soffianf360d842009-11-10 09:15:47 +0100718
719 for (ref = stale_refs; ref; ref = ref->next) {
720 if (!dry_run)
721 result |= delete_ref(ref->name, NULL, 0);
722 if (verbosity >= 0) {
723 fprintf(stderr, " x %-*s %-*s -> %s\n",
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +0700724 TRANSPORT_SUMMARY(_("[deleted]")),
Ævar Arnfjörð Bjarmason502681c2011-02-22 23:41:52 +0000725 REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
Jay Soffianf360d842009-11-10 09:15:47 +0100726 warn_dangling_symref(stderr, dangling_msg, ref->name);
727 }
728 }
729 free_refs(stale_refs);
730 return result;
731}
732
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200733static void check_not_current_branch(struct ref *ref_map)
734{
735 struct branch *current_branch = branch_get(NULL);
736
737 if (is_bare_repository() || !current_branch)
738 return;
739
740 for (; ref_map; ref_map = ref_map->next)
741 if (ref_map->peer_ref && !strcmp(current_branch->refname,
742 ref_map->peer_ref->name))
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000743 die(_("Refusing to fetch into current branch %s "
744 "of non-bare repository"), current_branch->refname);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200745}
746
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800747static int truncate_fetch_head(void)
748{
749 char *filename = git_path("FETCH_HEAD");
750 FILE *fp = fopen(filename, "w");
751
752 if (!fp)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000753 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800754 fclose(fp);
755 return 0;
756}
757
Junio C Hamanodb5723c2013-08-07 14:43:20 -0700758static void set_option(struct transport *transport, const char *name, const char *value)
759{
760 int r = transport_set_option(transport, name, value);
761 if (r < 0)
762 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
763 name, value, transport->url);
764 if (r > 0)
765 warning(_("Option \"%s\" is ignored for %s\n"),
766 name, transport->url);
767}
768
Ramsay Jones0f73f8b2013-08-28 19:56:17 +0100769static struct transport *prepare_transport(struct remote *remote)
Junio C Hamanodb5723c2013-08-07 14:43:20 -0700770{
771 struct transport *transport;
772 transport = transport_get(remote, NULL);
773 transport_set_verbosity(transport, verbosity, progress);
774 if (upload_pack)
775 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
776 if (keep)
777 set_option(transport, TRANS_OPT_KEEP, "yes");
778 if (depth)
779 set_option(transport, TRANS_OPT_DEPTH, depth);
780 return transport;
781}
782
Junio C Hamano069d5032013-08-07 15:14:45 -0700783static void backfill_tags(struct transport *transport, struct ref *ref_map)
784{
Junio C Hamanob26ed432013-08-07 15:47:18 -0700785 if (transport->cannot_reuse) {
786 gsecondary = prepare_transport(transport->remote);
787 transport = gsecondary;
788 }
789
Junio C Hamano069d5032013-08-07 15:14:45 -0700790 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
791 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
792 fetch_refs(transport, ref_map);
Junio C Hamanob26ed432013-08-07 15:47:18 -0700793
794 if (gsecondary) {
795 transport_disconnect(gsecondary);
796 gsecondary = NULL;
797 }
Junio C Hamano069d5032013-08-07 15:14:45 -0700798}
799
Daniel Barkalowb888d612007-09-10 23:03:25 -0400800static int do_fetch(struct transport *transport,
801 struct refspec *refs, int ref_count)
802{
Michael Haggertyb87dbcc2013-05-25 11:08:01 +0200803 struct string_list existing_refs = STRING_LIST_INIT_DUP;
Shawn O. Pearce7f984282008-03-02 21:34:43 -0500804 struct ref *ref_map;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400805 struct ref *rm;
806 int autotags = (transport->remote->fetch_tags == 1);
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200807 int retcode = 0;
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000808
809 for_each_ref(add_existing, &existing_refs);
810
Daniel Johnsoned368542010-08-11 18:57:20 -0400811 if (tags == TAGS_DEFAULT) {
812 if (transport->remote->fetch_tags == 2)
813 tags = TAGS_SET;
814 if (transport->remote->fetch_tags == -1)
815 tags = TAGS_UNSET;
816 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400817
Shawn O. Pearce824d5772007-09-19 00:49:31 -0400818 if (!transport->get_refs_list || !transport->fetch)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000819 die(_("Don't know how to fetch from %s"), transport->url);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400820
821 /* if not appending, truncate FETCH_HEAD */
Jay Soffian28a15402009-11-10 09:19:43 +0100822 if (!append && !dry_run) {
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200823 retcode = truncate_fetch_head();
824 if (retcode)
825 goto cleanup;
André Goddard Rosad6617c72007-11-22 20:22:23 -0200826 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400827
828 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200829 if (!update_head_ok)
830 check_not_current_branch(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400831
832 for (rm = ref_map; rm; rm = rm->next) {
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000833 if (rm->peer_ref) {
Michael Haggerty6f64a162013-05-25 11:08:15 +0200834 struct string_list_item *peer_item =
835 string_list_lookup(&existing_refs,
836 rm->peer_ref->name);
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000837 if (peer_item)
838 hashcpy(rm->peer_ref->old_sha1,
839 peer_item->util);
840 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400841 }
842
Shawn O. Pearce41fa7d22008-03-03 22:27:40 -0500843 if (tags == TAGS_DEFAULT && autotags)
844 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
Daniel Barkalowb888d612007-09-10 23:03:25 -0400845 if (fetch_refs(transport, ref_map)) {
846 free_refs(ref_map);
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200847 retcode = 1;
848 goto cleanup;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400849 }
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200850 if (prune) {
Michael Haggerty0838bf42013-10-30 06:33:00 +0100851 /*
852 * We only prune based on refspecs specified
853 * explicitly (via command line or configuration); we
854 * don't care whether --tags was specified.
855 */
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100856 if (ref_count) {
Michael Haggerty0838bf42013-10-30 06:33:00 +0100857 prune_refs(refs, ref_count, ref_map);
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100858 } else {
Michael Haggerty0838bf42013-10-30 06:33:00 +0100859 prune_refs(transport->remote->fetch,
860 transport->remote->fetch_refspec_nr,
861 ref_map);
Carlos Martín Nietoe8c1e6c2011-10-15 07:04:26 +0200862 }
Carlos Martín Nietoed43de62011-10-15 07:04:25 +0200863 }
Shawn O. Pearce7f984282008-03-02 21:34:43 -0500864 free_refs(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400865
866 /* if neither --no-tags nor --tags was specified, do automated tag
867 * following ... */
Kristian Høgsberg83201992007-12-04 02:25:47 -0500868 if (tags == TAGS_DEFAULT && autotags) {
Shawn O. Pearcec50b2b42008-03-02 21:35:00 -0500869 struct ref **tail = &ref_map;
870 ref_map = NULL;
871 find_non_local_tags(transport, &ref_map, &tail);
Junio C Hamano069d5032013-08-07 15:14:45 -0700872 if (ref_map)
873 backfill_tags(transport, ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400874 free_refs(ref_map);
875 }
876
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200877 cleanup:
Michael Haggertyf83918e2013-05-25 11:08:17 +0200878 string_list_clear(&existing_refs, 1);
Michael Haggerty5b87d8d2013-05-25 11:08:16 +0200879 return retcode;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400880}
881
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100882static int get_one_remote_for_fetch(struct remote *remote, void *priv)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400883{
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100884 struct string_list *list = priv;
Björn Gustavsson7cc91a22009-11-09 21:11:06 +0100885 if (!remote->skip_default_update)
Julian Phillips1d2f80f2010-06-26 00:41:38 +0100886 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100887 return 0;
888}
889
890struct remote_group_data {
891 const char *name;
892 struct string_list *list;
893};
894
895static int get_remote_group(const char *key, const char *value, void *priv)
896{
897 struct remote_group_data *g = priv;
898
899 if (!prefixcmp(key, "remotes.") &&
900 !strcmp(key + 8, g->name)) {
901 /* split list by white space */
902 int space = strcspn(value, " \t\n");
903 while (*value) {
904 if (space > 1) {
Julian Phillips1d2f80f2010-06-26 00:41:38 +0100905 string_list_append(g->list,
906 xstrndup(value, space));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100907 }
908 value += space + (value[space] != '\0');
909 space = strcspn(value, " \t\n");
910 }
911 }
912
913 return 0;
914}
915
916static int add_remote_or_group(const char *name, struct string_list *list)
917{
918 int prev_nr = list->nr;
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000919 struct remote_group_data g;
920 g.name = name; g.list = list;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100921
922 git_config(get_remote_group, &g);
923 if (list->nr == prev_nr) {
924 struct remote *remote;
925 if (!remote_is_configured(name))
926 return 0;
927 remote = remote_get(name);
Julian Phillips1d2f80f2010-06-26 00:41:38 +0100928 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100929 }
930 return 1;
931}
932
Jeff King85556d42012-09-01 07:27:35 -0400933static void add_options_to_argv(struct argv_array *argv)
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100934{
935 if (dry_run)
Jeff King85556d42012-09-01 07:27:35 -0400936 argv_array_push(argv, "--dry-run");
Michael Haggerty90765fa2013-10-30 06:33:04 +0100937 if (prune != -1)
938 argv_array_push(argv, prune ? "--prune" : "--no-prune");
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100939 if (update_head_ok)
Jeff King85556d42012-09-01 07:27:35 -0400940 argv_array_push(argv, "--update-head-ok");
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100941 if (force)
Jeff King85556d42012-09-01 07:27:35 -0400942 argv_array_push(argv, "--force");
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100943 if (keep)
Jeff King85556d42012-09-01 07:27:35 -0400944 argv_array_push(argv, "--keep");
Jens Lehmannbe254a02010-11-11 00:55:02 +0100945 if (recurse_submodules == RECURSE_SUBMODULES_ON)
Jeff King85556d42012-09-01 07:27:35 -0400946 argv_array_push(argv, "--recurse-submodules");
Jens Lehmann8f0700d2011-03-06 23:11:21 +0100947 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
Jeff King85556d42012-09-01 07:27:35 -0400948 argv_array_push(argv, "--recurse-submodules=on-demand");
Dan Johnson85566462012-09-05 17:22:19 -0400949 if (tags == TAGS_SET)
950 argv_array_push(argv, "--tags");
951 else if (tags == TAGS_UNSET)
952 argv_array_push(argv, "--no-tags");
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100953 if (verbosity >= 2)
Jeff King85556d42012-09-01 07:27:35 -0400954 argv_array_push(argv, "-v");
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100955 if (verbosity >= 1)
Jeff King85556d42012-09-01 07:27:35 -0400956 argv_array_push(argv, "-v");
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100957 else if (verbosity < 0)
Jeff King85556d42012-09-01 07:27:35 -0400958 argv_array_push(argv, "-q");
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100959
960}
961
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100962static int fetch_multiple(struct string_list *list)
963{
964 int i, result = 0;
Jeff King85556d42012-09-01 07:27:35 -0400965 struct argv_array argv = ARGV_ARRAY_INIT;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100966
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800967 if (!append && !dry_run) {
968 int errcode = truncate_fetch_head();
969 if (errcode)
970 return errcode;
971 }
972
Jeff King85556d42012-09-01 07:27:35 -0400973 argv_array_pushl(&argv, "fetch", "--append", NULL);
974 add_options_to_argv(&argv);
975
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100976 for (i = 0; i < list->nr; i++) {
977 const char *name = list->items[i].string;
Jeff King85556d42012-09-01 07:27:35 -0400978 argv_array_push(&argv, name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100979 if (verbosity >= 0)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000980 printf(_("Fetching %s\n"), name);
Jeff King85556d42012-09-01 07:27:35 -0400981 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +0000982 error(_("Could not fetch %s"), name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100983 result = 1;
984 }
Jeff King85556d42012-09-01 07:27:35 -0400985 argv_array_pop(&argv);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100986 }
987
Jeff King85556d42012-09-01 07:27:35 -0400988 argv_array_clear(&argv);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100989 return result;
990}
991
992static int fetch_one(struct remote *remote, int argc, const char **argv)
993{
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -0500994 int i;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400995 static const char **refs = NULL;
Jim Meyeringd8ead152011-06-08 22:06:33 +0200996 struct refspec *refspec;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400997 int ref_nr = 0;
Alex Riesen7b7f39e2008-04-28 22:23:35 +0200998 int exit_code;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400999
Daniel Barkalowfa685bd2009-03-11 01:47:20 -04001000 if (!remote)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001001 die(_("No remote repository specified. Please, specify either a URL or a\n"
1002 "remote name from which new revisions should be fetched."));
Daniel Barkalowfa685bd2009-03-11 01:47:20 -04001003
Junio C Hamanodb5723c2013-08-07 14:43:20 -07001004 gtransport = prepare_transport(remote);
Michael Schubert737c5a92013-07-13 11:36:24 +02001005
1006 if (prune < 0) {
1007 /* no command line request */
Junio C Hamano20419de2013-09-09 14:50:37 -07001008 if (0 <= gtransport->remote->prune)
1009 prune = gtransport->remote->prune;
Michael Schubert737c5a92013-07-13 11:36:24 +02001010 else if (0 <= fetch_prune_config)
1011 prune = fetch_prune_config;
1012 else
1013 prune = PRUNE_BY_DEFAULT;
1014 }
1015
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001016 if (argc > 0) {
Daniel Barkalowb888d612007-09-10 23:03:25 -04001017 int j = 0;
Kristian Høgsberg83201992007-12-04 02:25:47 -05001018 refs = xcalloc(argc + 1, sizeof(const char *));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001019 for (i = 0; i < argc; i++) {
Daniel Barkalowb888d612007-09-10 23:03:25 -04001020 if (!strcmp(argv[i], "tag")) {
1021 char *ref;
1022 i++;
Kevin Ballardf53423b2008-04-05 14:28:53 -04001023 if (i >= argc)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001024 die(_("You need to specify a tag name."));
Daniel Barkalowb888d612007-09-10 23:03:25 -04001025 ref = xmalloc(strlen(argv[i]) * 2 + 22);
1026 strcpy(ref, "refs/tags/");
1027 strcat(ref, argv[i]);
1028 strcat(ref, ":refs/tags/");
1029 strcat(ref, argv[i]);
1030 refs[j++] = ref;
1031 } else
1032 refs[j++] = argv[i];
Daniel Barkalowb888d612007-09-10 23:03:25 -04001033 }
1034 refs[j] = NULL;
1035 ref_nr = j;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001036 }
1037
Jeff King57b235a2009-01-22 01:03:08 -05001038 sigchain_push_common(unlock_pack_on_signal);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -04001039 atexit(unlock_pack);
Jim Meyeringd8ead152011-06-08 22:06:33 +02001040 refspec = parse_fetch_refspec(ref_nr, refs);
Junio C Hamanoaf234452013-08-07 15:38:45 -07001041 exit_code = do_fetch(gtransport, refspec, ref_nr);
Carlos Martín Nieto5caf1972011-10-08 00:51:06 +02001042 free_refspec(ref_nr, refspec);
Junio C Hamanoaf234452013-08-07 15:38:45 -07001043 transport_disconnect(gtransport);
1044 gtransport = NULL;
Alex Riesen7b7f39e2008-04-28 22:23:35 +02001045 return exit_code;
Daniel Barkalowb888d612007-09-10 23:03:25 -04001046}
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001047
1048int cmd_fetch(int argc, const char **argv, const char *prefix)
1049{
1050 int i;
Thiago Farina183113a2010-07-04 16:46:19 -03001051 struct string_list list = STRING_LIST_INIT_NODUP;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001052 struct remote *remote;
1053 int result = 0;
Jeff King131b8fc2013-01-26 17:40:38 -05001054 static const char *argv_gc_auto[] = {
1055 "gc", "--auto", NULL,
1056 };
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001057
Jeff Kingbbc30f92011-02-24 09:30:19 -05001058 packet_trace_identity("fetch");
1059
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001060 /* Record the command line for the reflog */
1061 strbuf_addstr(&default_rla, "fetch");
1062 for (i = 1; i < argc; i++)
1063 strbuf_addf(&default_rla, " %s", argv[i]);
1064
Michael Schubert737c5a92013-07-13 11:36:24 +02001065 git_config(git_fetch_config, NULL);
1066
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001067 argc = parse_options(argc, argv, prefix,
1068 builtin_fetch_options, builtin_fetch_usage, 0);
1069
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +07001070 if (unshallow) {
1071 if (depth)
1072 die(_("--depth and --unshallow cannot be used together"));
1073 else if (!is_repository_shallow())
1074 die(_("--unshallow on a complete repository does not make sense"));
1075 else {
1076 static char inf_depth[12];
1077 sprintf(inf_depth, "%d", INFINITE_DEPTH);
1078 depth = inf_depth;
1079 }
1080 }
1081
Jens Lehmann18322ba2011-09-09 20:22:03 +02001082 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1083 if (recurse_submodules_default) {
1084 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
1085 set_config_fetch_recurse_submodules(arg);
1086 }
1087 gitmodules_config();
1088 git_config(submodule_config, NULL);
1089 }
1090
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001091 if (all) {
1092 if (argc == 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001093 die(_("fetch --all does not take a repository argument"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001094 else if (argc > 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001095 die(_("fetch --all does not make sense with refspecs"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001096 (void) for_each_remote(get_one_remote_for_fetch, &list);
1097 result = fetch_multiple(&list);
1098 } else if (argc == 0) {
1099 /* No arguments -- use default remote */
1100 remote = remote_get(NULL);
1101 result = fetch_one(remote, argc, argv);
Björn Gustavsson16679e32009-11-09 21:10:32 +01001102 } else if (multiple) {
1103 /* All arguments are assumed to be remotes or groups */
1104 for (i = 0; i < argc; i++)
1105 if (!add_remote_or_group(argv[i], &list))
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001106 die(_("No such remote or remote group: %s"), argv[i]);
Björn Gustavsson16679e32009-11-09 21:10:32 +01001107 result = fetch_multiple(&list);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001108 } else {
1109 /* Single remote or group */
1110 (void) add_remote_or_group(argv[0], &list);
1111 if (list.nr > 1) {
1112 /* More than one remote */
1113 if (argc > 1)
Ævar Arnfjörð Bjarmasonbd4a51f2011-02-22 23:41:51 +00001114 die(_("Fetching a group and specifying refspecs does not make sense"));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001115 result = fetch_multiple(&list);
1116 } else {
1117 /* Zero or one remotes */
1118 remote = remote_get(argv[0]);
1119 result = fetch_one(remote, argc-1, argv+1);
1120 }
1121 }
1122
Jens Lehmannbe254a02010-11-11 00:55:02 +01001123 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
Jeff King85556d42012-09-01 07:27:35 -04001124 struct argv_array options = ARGV_ARRAY_INIT;
1125
1126 add_options_to_argv(&options);
Jens Lehmann50d89ad2012-09-01 17:27:06 +02001127 result = fetch_populated_submodules(&options,
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001128 submodule_prefix,
Jens Lehmann8f0700d2011-03-06 23:11:21 +01001129 recurse_submodules,
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001130 verbosity < 0);
Jeff King85556d42012-09-01 07:27:35 -04001131 argv_array_clear(&options);
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001132 }
1133
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001134 /* All names were strdup()ed or strndup()ed */
1135 list.strdup_strings = 1;
1136 string_list_clear(&list, 0);
1137
Jeff King131b8fc2013-01-26 17:40:38 -05001138 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
1139
Björn Gustavsson9c4a0362009-11-09 21:09:56 +01001140 return result;
1141}