1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
77 Repository remoteRepository = createWorkRepository();
78 remoteGit = new Git(remoteRepository);
79
80
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
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 }