|
|
@ -43,19 +43,29 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.eclipse.jgit.junit; |
|
|
|
package org.eclipse.jgit.junit; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static java.nio.charset.StandardCharsets.UTF_8; |
|
|
|
import static org.junit.Assert.assertEquals; |
|
|
|
import static org.junit.Assert.assertEquals; |
|
|
|
import static org.junit.Assert.assertFalse; |
|
|
|
import static org.junit.Assert.assertFalse; |
|
|
|
import static org.junit.Assert.assertNull; |
|
|
|
import static org.junit.Assert.assertNull; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertSame; |
|
|
|
import static org.junit.Assert.assertTrue; |
|
|
|
import static org.junit.Assert.assertTrue; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Date; |
|
|
|
import java.util.regex.Pattern; |
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
|
|
|
|
import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
|
|
|
import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
|
|
|
import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
|
|
|
import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
|
|
|
import org.eclipse.jgit.lib.ObjectId; |
|
|
|
import org.eclipse.jgit.lib.ObjectId; |
|
|
|
|
|
|
|
import org.eclipse.jgit.lib.AnyObjectId; |
|
|
|
|
|
|
|
import org.eclipse.jgit.lib.Constants; |
|
|
|
|
|
|
|
import org.eclipse.jgit.lib.ObjectLoader; |
|
|
|
|
|
|
|
import org.eclipse.jgit.lib.PersonIdent; |
|
|
|
import org.eclipse.jgit.lib.Ref; |
|
|
|
import org.eclipse.jgit.lib.Ref; |
|
|
|
|
|
|
|
import org.eclipse.jgit.revwalk.RevBlob; |
|
|
|
import org.eclipse.jgit.revwalk.RevCommit; |
|
|
|
import org.eclipse.jgit.revwalk.RevCommit; |
|
|
|
|
|
|
|
import org.eclipse.jgit.revwalk.RevObject; |
|
|
|
import org.eclipse.jgit.revwalk.RevWalk; |
|
|
|
import org.eclipse.jgit.revwalk.RevWalk; |
|
|
|
|
|
|
|
import org.eclipse.jgit.treewalk.TreeWalk; |
|
|
|
import org.junit.After; |
|
|
|
import org.junit.After; |
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
@ -76,7 +86,7 @@ public class TestRepositoryTest { |
|
|
|
@After |
|
|
|
@After |
|
|
|
public void tearDown() { |
|
|
|
public void tearDown() { |
|
|
|
rw.close(); |
|
|
|
rw.close(); |
|
|
|
tr.getRepository().close(); |
|
|
|
repo.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
@ -168,4 +178,98 @@ public class TestRepositoryTest { |
|
|
|
assertEquals(detached, head.getObjectId()); |
|
|
|
assertEquals(detached, head.getObjectId()); |
|
|
|
assertFalse(head.isSymbolic()); |
|
|
|
assertFalse(head.isSymbolic()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void amendRef() throws Exception { |
|
|
|
|
|
|
|
RevCommit root = tr.commit() |
|
|
|
|
|
|
|
.add("todelete", "to be deleted") |
|
|
|
|
|
|
|
.create(); |
|
|
|
|
|
|
|
RevCommit orig = tr.commit().parent(root) |
|
|
|
|
|
|
|
.rm("todelete") |
|
|
|
|
|
|
|
.add("foo", "foo contents") |
|
|
|
|
|
|
|
.add("bar", "bar contents") |
|
|
|
|
|
|
|
.add("dir/baz", "baz contents") |
|
|
|
|
|
|
|
.create(); |
|
|
|
|
|
|
|
rw.parseBody(orig); |
|
|
|
|
|
|
|
tr.branch("master").update(orig); |
|
|
|
|
|
|
|
assertEquals("foo contents", blobAsString(orig, "foo")); |
|
|
|
|
|
|
|
assertEquals("bar contents", blobAsString(orig, "bar")); |
|
|
|
|
|
|
|
assertEquals("baz contents", blobAsString(orig, "dir/baz")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RevCommit amended = tr.amendRef("master") |
|
|
|
|
|
|
|
.tick(3) |
|
|
|
|
|
|
|
.add("bar", "fixed bar contents") |
|
|
|
|
|
|
|
.create(); |
|
|
|
|
|
|
|
assertEquals(amended, repo.getRef("refs/heads/master").getObjectId()); |
|
|
|
|
|
|
|
rw.parseBody(amended); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(1, amended.getParentCount()); |
|
|
|
|
|
|
|
assertEquals(root, amended.getParent(0)); |
|
|
|
|
|
|
|
assertEquals(orig.getFullMessage(), amended.getFullMessage()); |
|
|
|
|
|
|
|
assertEquals(orig.getAuthorIdent(), amended.getAuthorIdent()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Committer name/email is the same, but time was incremented.
|
|
|
|
|
|
|
|
assertEquals(new PersonIdent(orig.getCommitterIdent(), new Date(0)), |
|
|
|
|
|
|
|
new PersonIdent(amended.getCommitterIdent(), new Date(0))); |
|
|
|
|
|
|
|
assertTrue(orig.getCommitTime() < amended.getCommitTime()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals("foo contents", blobAsString(amended, "foo")); |
|
|
|
|
|
|
|
assertEquals("fixed bar contents", blobAsString(amended, "bar")); |
|
|
|
|
|
|
|
assertEquals("baz contents", blobAsString(amended, "dir/baz")); |
|
|
|
|
|
|
|
assertNull(TreeWalk.forPath(repo, "todelete", amended.getTree())); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void amendHead() throws Exception { |
|
|
|
|
|
|
|
repo.updateRef("HEAD").link("refs/heads/master"); |
|
|
|
|
|
|
|
RevCommit root = tr.commit() |
|
|
|
|
|
|
|
.add("foo", "foo contents") |
|
|
|
|
|
|
|
.create(); |
|
|
|
|
|
|
|
RevCommit orig = tr.commit().parent(root) |
|
|
|
|
|
|
|
.message("original message") |
|
|
|
|
|
|
|
.add("bar", "bar contents") |
|
|
|
|
|
|
|
.create(); |
|
|
|
|
|
|
|
tr.branch("master").update(orig); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RevCommit amended = tr.amendRef("HEAD") |
|
|
|
|
|
|
|
.add("foo", "fixed foo contents") |
|
|
|
|
|
|
|
.create(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ref head = repo.getRef(Constants.HEAD); |
|
|
|
|
|
|
|
assertEquals(amended, head.getObjectId()); |
|
|
|
|
|
|
|
assertTrue(head.isSymbolic()); |
|
|
|
|
|
|
|
assertEquals("refs/heads/master", head.getTarget().getName()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rw.parseBody(amended); |
|
|
|
|
|
|
|
assertEquals("original message", amended.getFullMessage()); |
|
|
|
|
|
|
|
assertEquals("fixed foo contents", blobAsString(amended, "foo")); |
|
|
|
|
|
|
|
assertEquals("bar contents", blobAsString(amended, "bar")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void amendCommit() throws Exception { |
|
|
|
|
|
|
|
RevCommit root = tr.commit() |
|
|
|
|
|
|
|
.add("foo", "foo contents") |
|
|
|
|
|
|
|
.create(); |
|
|
|
|
|
|
|
RevCommit orig = tr.commit().parent(root) |
|
|
|
|
|
|
|
.message("original message") |
|
|
|
|
|
|
|
.add("bar", "bar contents") |
|
|
|
|
|
|
|
.create(); |
|
|
|
|
|
|
|
RevCommit amended = tr.amend(orig.copy()) |
|
|
|
|
|
|
|
.add("foo", "fixed foo contents") |
|
|
|
|
|
|
|
.create(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rw.parseBody(amended); |
|
|
|
|
|
|
|
assertEquals("original message", amended.getFullMessage()); |
|
|
|
|
|
|
|
assertEquals("fixed foo contents", blobAsString(amended, "foo")); |
|
|
|
|
|
|
|
assertEquals("bar contents", blobAsString(amended, "bar")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String blobAsString(AnyObjectId treeish, String path) |
|
|
|
|
|
|
|
throws Exception { |
|
|
|
|
|
|
|
RevObject obj = tr.get(rw.parseTree(treeish), path); |
|
|
|
|
|
|
|
assertSame(RevBlob.class, obj.getClass()); |
|
|
|
|
|
|
|
ObjectLoader loader = rw.getObjectReader().open(obj); |
|
|
|
|
|
|
|
return new String(loader.getCachedBytes(), UTF_8); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|