View Javadoc
1   /*
2    * Copyright (C) 2010, 2013 Chris Aniszczyk <caniszczyk@gmail.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  package org.eclipse.jgit.api;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertNull;
47  
48  import java.util.Collection;
49  
50  import org.eclipse.jgit.junit.RepositoryTestCase;
51  import org.eclipse.jgit.lib.Constants;
52  import org.eclipse.jgit.lib.ObjectId;
53  import org.eclipse.jgit.lib.Ref;
54  import org.eclipse.jgit.lib.RefUpdate;
55  import org.eclipse.jgit.lib.Repository;
56  import org.eclipse.jgit.lib.StoredConfig;
57  import org.eclipse.jgit.revwalk.RevCommit;
58  import org.eclipse.jgit.transport.FetchResult;
59  import org.eclipse.jgit.transport.RefSpec;
60  import org.eclipse.jgit.transport.RemoteConfig;
61  import org.eclipse.jgit.transport.TagOpt;
62  import org.eclipse.jgit.transport.TrackingRefUpdate;
63  import org.eclipse.jgit.transport.URIish;
64  import org.junit.Before;
65  import org.junit.Test;
66  
67  public class FetchCommandTest extends RepositoryTestCase {
68  
69  	private Git git;
70  	private Git remoteGit;
71  
72  	@Before
73  	public void setupRemoteRepository() throws Exception {
74  		git = new Git(db);
75  
76  		// create other repository
77  		Repository remoteRepository = createWorkRepository();
78  		remoteGit = new Git(remoteRepository);
79  
80  		// setup the first repository to fetch from the second repository
81  		final StoredConfig config = db.getConfig();
82  		RemoteConfig remoteConfig = new RemoteConfig(config, "test");
83  		URIish uri = new URIish(remoteRepository.getDirectory().toURI().toURL());
84  		remoteConfig.addURI(uri);
85  		remoteConfig.update(config);
86  		config.save();
87  	}
88  
89  	@Test
90  	public void testFetch() throws Exception {
91  
92  		// create some refs via commits and tag
93  		RevCommit commit = remoteGit.commit().setMessage("initial commit").call();
94  		Ref tagRef = remoteGit.tag().setName("tag").call();
95  
96  		RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x");
97  		git.fetch().setRemote("test").setRefSpecs(spec)
98  				.call();
99  
100 		assertEquals(commit.getId(),
101 				db.resolve(commit.getId().getName() + "^{commit}"));
102 		assertEquals(tagRef.getObjectId(),
103 				db.resolve(tagRef.getObjectId().getName()));
104 	}
105 
106 	@Test
107 	public void fetchShouldAutoFollowTag() throws Exception {
108 		remoteGit.commit().setMessage("commit").call();
109 		Ref tagRef = remoteGit.tag().setName("foo").call();
110 
111 		RefSpec spec = new RefSpec("refs/heads/*:refs/remotes/origin/*");
112 		git.fetch().setRemote("test").setRefSpecs(spec)
113 				.setTagOpt(TagOpt.AUTO_FOLLOW).call();
114 
115 		assertEquals(tagRef.getObjectId(), db.resolve("foo"));
116 	}
117 
118 	@Test
119 	public void fetchShouldAutoFollowTagForFetchedObjects() throws Exception {
120 		remoteGit.commit().setMessage("commit").call();
121 		Ref tagRef = remoteGit.tag().setName("foo").call();
122 		remoteGit.commit().setMessage("commit2").call();
123 		RefSpec spec = new RefSpec("refs/heads/*:refs/remotes/origin/*");
124 		git.fetch().setRemote("test").setRefSpecs(spec)
125 				.setTagOpt(TagOpt.AUTO_FOLLOW).call();
126 		assertEquals(tagRef.getObjectId(), db.resolve("foo"));
127 	}
128 
129 	@Test
130 	public void fetchShouldNotFetchTagsFromOtherBranches() throws Exception {
131 		remoteGit.commit().setMessage("commit").call();
132 		remoteGit.checkout().setName("other").setCreateBranch(true).call();
133 		remoteGit.commit().setMessage("commit2").call();
134 		remoteGit.tag().setName("foo").call();
135 		RefSpec spec = new RefSpec(
136 				"refs/heads/master:refs/remotes/origin/master");
137 		git.fetch().setRemote("test").setRefSpecs(spec)
138 				.setTagOpt(TagOpt.AUTO_FOLLOW).call();
139 		assertNull(db.resolve("foo"));
140 	}
141 
142 	@Test
143 	public void fetchWithUpdatedTagShouldNotTryToUpdateLocal() throws Exception {
144 		final String tagName = "foo";
145 		remoteGit.commit().setMessage("commit").call();
146 		Ref tagRef = remoteGit.tag().setName(tagName).call();
147 		ObjectId originalId = tagRef.getObjectId();
148 
149 		RefSpec spec = new RefSpec("refs/heads/*:refs/remotes/origin/*");
150 		git.fetch().setRemote("test").setRefSpecs(spec)
151 				.setTagOpt(TagOpt.AUTO_FOLLOW).call();
152 		assertEquals(originalId, db.resolve(tagName));
153 
154 		remoteGit.commit().setMessage("commit 2").call();
155 		remoteGit.tag().setName(tagName).setForceUpdate(true).call();
156 
157 		FetchResult result = git.fetch().setRemote("test").setRefSpecs(spec)
158 				.setTagOpt(TagOpt.AUTO_FOLLOW).call();
159 
160 		Collection<TrackingRefUpdate> refUpdates = result
161 				.getTrackingRefUpdates();
162 		assertEquals(1, refUpdates.size());
163 		TrackingRefUpdate update = refUpdates.iterator().next();
164 		assertEquals("refs/heads/master", update.getRemoteName());
165 
166 		assertEquals(originalId, db.resolve(tagName));
167 	}
168 
169 	@Test
170 	public void fetchWithExplicitTagsShouldUpdateLocal() throws Exception {
171 		final String tagName = "foo";
172 		remoteGit.commit().setMessage("commit").call();
173 		Ref tagRef1 = remoteGit.tag().setName(tagName).call();
174 
175 		RefSpec spec = new RefSpec("refs/heads/*:refs/remotes/origin/*");
176 		git.fetch().setRemote("test").setRefSpecs(spec)
177 				.setTagOpt(TagOpt.AUTO_FOLLOW).call();
178 		assertEquals(tagRef1.getObjectId(), db.resolve(tagName));
179 
180 		remoteGit.commit().setMessage("commit 2").call();
181 		Ref tagRef2 = remoteGit.tag().setName(tagName).setForceUpdate(true)
182 				.call();
183 
184 		FetchResult result = git.fetch().setRemote("test").setRefSpecs(spec)
185 				.setTagOpt(TagOpt.FETCH_TAGS).call();
186 		TrackingRefUpdate update = result.getTrackingRefUpdate(Constants.R_TAGS
187 				+ tagName);
188 		assertEquals(RefUpdate.Result.FORCED, update.getResult());
189 		assertEquals(tagRef2.getObjectId(), db.resolve(tagName));
190 	}
191 }