package org.eclipse.jgit.api;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import java.io.File;
+ import java.util.ArrayList;
import java.util.Collection;
+ import java.util.List;
+import org.eclipse.jgit.junit.JGitTestUtil;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
db.resolve(tagRef.getObjectId().getName()));
}
+ @Test
+ public void testForcedFetch() throws Exception {
+ remoteGit.commit().setMessage("commit").call();
+ remoteGit.commit().setMessage("commit2").call();
+ git.fetch().setRemote("test")
+ .setRefSpecs("refs/heads/master:refs/heads/master").call();
+
+ remoteGit.commit().setAmend(true).setMessage("amended").call();
+ FetchResult res = git.fetch().setRemote("test")
+ .setRefSpecs("refs/heads/master:refs/heads/master").call();
+ assertEquals(RefUpdate.Result.REJECTED,
+ res.getTrackingRefUpdate("refs/heads/master").getResult());
+ res = git.fetch().setRemote("test")
+ .setRefSpecs("refs/heads/master:refs/heads/master")
+ .setForceUpdate(true).call();
+ assertEquals(RefUpdate.Result.FORCED,
+ res.getTrackingRefUpdate("refs/heads/master").getResult());
+ }
+
+ @Test
+ public void fetchAddsBranches() throws Exception {
+ final String branch1 = "b1";
+ final String branch2 = "b2";
+ final String remoteBranch1 = "test/" + branch1;
+ final String remoteBranch2 = "test/" + branch2;
+ remoteGit.commit().setMessage("commit").call();
+ Ref branchRef1 = remoteGit.branchCreate().setName(branch1).call();
+ remoteGit.commit().setMessage("commit").call();
+ Ref branchRef2 = remoteGit.branchCreate().setName(branch2).call();
+
+ String spec = "refs/heads/*:refs/remotes/test/*";
+ git.fetch().setRemote("test").setRefSpecs(spec).call();
+ assertEquals(branchRef1.getObjectId(), db.resolve(remoteBranch1));
+ assertEquals(branchRef2.getObjectId(), db.resolve(remoteBranch2));
+ }
+
+ @Test
+ public void fetchDoesntDeleteBranches() throws Exception {
+ final String branch1 = "b1";
+ final String branch2 = "b2";
+ final String remoteBranch1 = "test/" + branch1;
+ final String remoteBranch2 = "test/" + branch2;
+ remoteGit.commit().setMessage("commit").call();
+ Ref branchRef1 = remoteGit.branchCreate().setName(branch1).call();
+ remoteGit.commit().setMessage("commit").call();
+ Ref branchRef2 = remoteGit.branchCreate().setName(branch2).call();
+
+ String spec = "refs/heads/*:refs/remotes/test/*";
+ git.fetch().setRemote("test").setRefSpecs(spec).call();
+ assertEquals(branchRef1.getObjectId(), db.resolve(remoteBranch1));
+ assertEquals(branchRef2.getObjectId(), db.resolve(remoteBranch2));
+
+ remoteGit.branchDelete().setBranchNames(branch1).call();
+ git.fetch().setRemote("test").setRefSpecs(spec).call();
+ assertEquals(branchRef1.getObjectId(), db.resolve(remoteBranch1));
+ assertEquals(branchRef2.getObjectId(), db.resolve(remoteBranch2));
+ }
+
+ @Test
+ public void fetchUpdatesBranches() throws Exception {
+ final String branch1 = "b1";
+ final String branch2 = "b2";
+ final String remoteBranch1 = "test/" + branch1;
+ final String remoteBranch2 = "test/" + branch2;
+ remoteGit.commit().setMessage("commit").call();
+ Ref branchRef1 = remoteGit.branchCreate().setName(branch1).call();
+ remoteGit.commit().setMessage("commit").call();
+ Ref branchRef2 = remoteGit.branchCreate().setName(branch2).call();
+
+ String spec = "refs/heads/*:refs/remotes/test/*";
+ git.fetch().setRemote("test").setRefSpecs(spec).call();
+ assertEquals(branchRef1.getObjectId(), db.resolve(remoteBranch1));
+ assertEquals(branchRef2.getObjectId(), db.resolve(remoteBranch2));
+
+ remoteGit.commit().setMessage("commit").call();
+ branchRef2 = remoteGit.branchCreate().setName(branch2).setForce(true).call();
+ git.fetch().setRemote("test").setRefSpecs(spec).call();
+ assertEquals(branchRef1.getObjectId(), db.resolve(remoteBranch1));
+ assertEquals(branchRef2.getObjectId(), db.resolve(remoteBranch2));
+ }
+
+ @Test
+ public void fetchPrunesBranches() throws Exception {
+ final String branch1 = "b1";
+ final String branch2 = "b2";
+ final String remoteBranch1 = "test/" + branch1;
+ final String remoteBranch2 = "test/" + branch2;
+ remoteGit.commit().setMessage("commit").call();
+ Ref branchRef1 = remoteGit.branchCreate().setName(branch1).call();
+ remoteGit.commit().setMessage("commit").call();
+ Ref branchRef2 = remoteGit.branchCreate().setName(branch2).call();
+
+ String spec = "refs/heads/*:refs/remotes/test/*";
+ git.fetch().setRemote("test").setRefSpecs(spec).call();
+ assertEquals(branchRef1.getObjectId(), db.resolve(remoteBranch1));
+ assertEquals(branchRef2.getObjectId(), db.resolve(remoteBranch2));
+
+ remoteGit.branchDelete().setBranchNames(branch1).call();
+ git.fetch().setRemote("test").setRefSpecs(spec)
+ .setRemoveDeletedRefs(true).call();
+ assertNull(db.resolve(remoteBranch1));
+ assertEquals(branchRef2.getObjectId(), db.resolve(remoteBranch2));
+ }
+
@Test
public void fetchShouldAutoFollowTag() throws Exception {
remoteGit.commit().setMessage("commit").call();
assertEquals(tagRef2.getObjectId(), db.resolve(tagName));
}
+ @Test
+ public void testFetchWithPruneShouldKeepOriginHead() throws Exception {
+ // Create a commit in the test repo.
+ commitFile("foo", "foo", "master");
+ // Produce a real clone of the git repo
+ Git cloned = Git.cloneRepository()
+ .setDirectory(createTempDirectory("testCloneRepository"))
+ .setURI("file://"
+ + git.getRepository().getWorkTree().getAbsolutePath())
+ .call();
+ assertNotNull(cloned);
+ Repository clonedRepo = cloned.getRepository();
+ addRepoToClose(clonedRepo);
+ ObjectId originMasterId = clonedRepo
+ .resolve("refs/remotes/origin/master");
+ assertNotNull("Should have origin/master", originMasterId);
+ assertNotEquals("origin/master should not be zero ID",
+ ObjectId.zeroId(), originMasterId);
+ // Canonical git creates origin/HEAD; JGit (for now) doesn't. Let's
+ // pretend we did the clone via command-line git.
+ ObjectId originHeadId = clonedRepo.resolve("refs/remotes/origin/HEAD");
+ if (originHeadId == null) {
+ JGitTestUtil.write(
+ new File(clonedRepo.getDirectory(),
+ "refs/remotes/origin/HEAD"),
+ "ref: refs/remotes/origin/master\n");
+ originHeadId = clonedRepo.resolve("refs/remotes/origin/HEAD");
+ }
+ assertEquals("Should have origin/HEAD", originMasterId, originHeadId);
+ FetchResult result = cloned.fetch().setRemote("origin")
+ .setRemoveDeletedRefs(true).call();
+ assertTrue("Fetch after clone should be up-to-date",
+ result.getTrackingRefUpdates().isEmpty());
+ assertEquals("origin/master should still exist", originMasterId,
+ clonedRepo.resolve("refs/remotes/origin/master"));
+ assertEquals("origin/HEAD should be unchanged", originHeadId,
+ clonedRepo.resolve("refs/remotes/origin/HEAD"));
+ }
++
+ @Test
+ public void fetchAddRefsWithDuplicateRefspec() throws Exception {
+ final String branchName = "branch";
+ final String remoteBranchName = "test/" + branchName;
+ remoteGit.commit().setMessage("commit").call();
+ Ref branchRef = remoteGit.branchCreate().setName(branchName).call();
+
+ final String spec1 = "+refs/heads/*:refs/remotes/test/*";
+ final String spec2 = "refs/heads/*:refs/remotes/test/*";
+ final StoredConfig config = db.getConfig();
+ RemoteConfig remoteConfig = new RemoteConfig(config, "test");
+ remoteConfig.addFetchRefSpec(new RefSpec(spec1));
+ remoteConfig.addFetchRefSpec(new RefSpec(spec2));
+ remoteConfig.update(config);
+
+ git.fetch().setRemote("test").setRefSpecs(spec1).call();
+ assertEquals(branchRef.getObjectId(), db.resolve(remoteBranchName));
+ }
+
+ @Test
+ public void fetchPruneRefsWithDuplicateRefspec()
+ throws Exception {
+ final String branchName = "branch";
+ final String remoteBranchName = "test/" + branchName;
+ remoteGit.commit().setMessage("commit").call();
+ Ref branchRef = remoteGit.branchCreate().setName(branchName).call();
+
+ final String spec1 = "+refs/heads/*:refs/remotes/test/*";
+ final String spec2 = "refs/heads/*:refs/remotes/test/*";
+ final StoredConfig config = db.getConfig();
+ RemoteConfig remoteConfig = new RemoteConfig(config, "test");
+ remoteConfig.addFetchRefSpec(new RefSpec(spec1));
+ remoteConfig.addFetchRefSpec(new RefSpec(spec2));
+ remoteConfig.update(config);
+
+ git.fetch().setRemote("test").setRefSpecs(spec1).call();
+ assertEquals(branchRef.getObjectId(), db.resolve(remoteBranchName));
+
+ remoteGit.branchDelete().setBranchNames(branchName).call();
+ git.fetch().setRemote("test").setRefSpecs(spec1)
+ .setRemoveDeletedRefs(true).call();
+ assertNull(db.resolve(remoteBranchName));
+ }
+
+ @Test
+ public void fetchUpdateRefsWithDuplicateRefspec() throws Exception {
+ final String tagName = "foo";
+ remoteGit.commit().setMessage("commit").call();
+ Ref tagRef1 = remoteGit.tag().setName(tagName).call();
+ List<RefSpec> refSpecs = new ArrayList<>();
+ refSpecs.add(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
+ refSpecs.add(new RefSpec("+refs/tags/*:refs/tags/*"));
+ // Updating tags via the RefSpecs and setting TagOpt.FETCH_TAGS (or
+ // AUTO_FOLLOW) will result internally in *two* updates for the same
+ // ref.
+ git.fetch().setRemote("test").setRefSpecs(refSpecs)
+ .setTagOpt(TagOpt.AUTO_FOLLOW).call();
+ assertEquals(tagRef1.getObjectId(), db.resolve(tagName));
+
+ remoteGit.commit().setMessage("commit 2").call();
+ Ref tagRef2 = remoteGit.tag().setName(tagName).setForceUpdate(true)
+ .call();
+ FetchResult result = git.fetch().setRemote("test").setRefSpecs(refSpecs)
+ .setTagOpt(TagOpt.FETCH_TAGS).call();
+ assertEquals(2, result.getTrackingRefUpdates().size());
+ TrackingRefUpdate update = result
+ .getTrackingRefUpdate(Constants.R_TAGS + tagName);
+ assertEquals(RefUpdate.Result.FORCED, update.getResult());
+ assertEquals(tagRef2.getObjectId(), db.resolve(tagName));
+ }
}
+++ /dev/null
--<?xml version="1.0" encoding="UTF-8" standalone="no"?>
--<component id="org.eclipse.jgit" version="2">
- <resource path="src/org/eclipse/jgit/lib/ObjectIdSerializer.java" type="org.eclipse.jgit.lib.ObjectIdSerializer">
- <filter id="1141899266">
- <resource path="META-INF/MANIFEST.MF">
- <filter id="924844039">
-- <message_arguments>
- <message_argument value="4.11"/>
- <message_argument value="5.0"/>
- <message_argument value="readWithoutMarker(InputStream)"/>
- <message_argument value="4.11.1"/>
- <message_argument value="4.11.0"/>
-- </message_arguments>
-- </filter>
- <filter id="1141899266">
- <filter id="924844039">
-- <message_arguments>
- <message_argument value="4.11"/>
- <message_argument value="5.0"/>
- <message_argument value="writeWithoutMarker(OutputStream, AnyObjectId)"/>
- <message_argument value="4.11.2"/>
- <message_argument value="4.11.0"/>
- </message_arguments>
- </filter>
- </resource>
- <resource path="src/org/eclipse/jgit/diff/DiffEntry.java" type="org.eclipse.jgit.diff.DiffEntry">
- <filter id="336658481">
- <message_arguments>
- <message_argument value="org.eclipse.jgit.diff.DiffEntry"/>
- <message_argument value="diffAttribute"/>
- </message_arguments>
- </filter>
- </resource>
- <resource path="src/org/eclipse/jgit/lib/ConfigConstants.java" type="org.eclipse.jgit.lib.ConfigConstants">
- <filter id="336658481">
- <message_arguments>
- <message_argument value="org.eclipse.jgit.lib.ConfigConstants"/>
- <message_argument value="CONFIG_KEY_REQUIRED"/>
- </message_arguments>
- </filter>
- <filter id="336658481">
- <message_arguments>
- <message_argument value="org.eclipse.jgit.lib.ConfigConstants"/>
- <message_argument value="CONFIG_SECTION_LFS"/>
- </message_arguments>
- </filter>
- </resource>
- <resource path="src/org/eclipse/jgit/merge/ResolveMerger.java" type="org.eclipse.jgit.merge.ResolveMerger">
- <filter id="336658481">
- <message_arguments>
- <message_argument value="org.eclipse.jgit.merge.ResolveMerger"/>
- <message_argument value="workingTreeOptions"/>
- </message_arguments>
- </filter>
- </resource>
- <resource path="src/org/eclipse/jgit/storage/pack/PackStatistics.java" type="org.eclipse.jgit.storage.pack.PackStatistics$Accumulator">
- <filter id="336658481">
- <message_arguments>
- <message_argument value="org.eclipse.jgit.storage.pack.PackStatistics.Accumulator"/>
- <message_argument value="advertised"/>
- </message_arguments>
- </filter>
- <filter id="336658481">
- <message_arguments>
- <message_argument value="org.eclipse.jgit.storage.pack.PackStatistics.Accumulator"/>
- <message_argument value="haves"/>
- </message_arguments>
- </filter>
- <filter id="336658481">
- <message_arguments>
- <message_argument value="org.eclipse.jgit.storage.pack.PackStatistics.Accumulator"/>
- <message_argument value="timeNegotiating"/>
- </message_arguments>
- </filter>
- <filter id="336658481">
- <message_arguments>
- <message_argument value="org.eclipse.jgit.storage.pack.PackStatistics.Accumulator"/>
- <message_argument value="wants"/>
-- </message_arguments>
-- </filter>
-- </resource>
--</component>