diff options
author | Robin Rosenberg <robin.rosenberg@dewire.com> | 2012-05-30 15:56:30 -0400 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org <gerrit@eclipse.org> | 2012-05-30 15:56:30 -0400 |
commit | 875247e24b3b5cfa97cad9be3ab2766c0506c1c4 (patch) | |
tree | 1e074ad6668f9c1853c49f5f2f2965a4fc3ffef8 /org.eclipse.jgit.test | |
parent | 59d2ef94709ba29395c58fec454bf846f920e99a (diff) | |
parent | dac66672df0535f61a13273524d46e1e0012ca69 (diff) | |
download | jgit-875247e24b3b5cfa97cad9be3ab2766c0506c1c4.tar.gz jgit-875247e24b3b5cfa97cad9be3ab2766c0506c1c4.zip |
Merge "Update smudged entries when writing index" into stable-2.0
Diffstat (limited to 'org.eclipse.jgit.test')
3 files changed, 188 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java index b1cac3a54d..a51a8b4697 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java @@ -61,6 +61,8 @@ import org.eclipse.jgit.api.errors.InvalidRefNameException; import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.RefAlreadyExistsException; import org.eclipse.jgit.api.errors.RefNotFoundException; +import org.eclipse.jgit.dircache.DirCache; +import org.eclipse.jgit.dircache.DirCacheEntry; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.RefUpdate; @@ -236,4 +238,41 @@ public class CheckoutCommandTest extends RepositoryTestCase { assertFalse(head.isSymbolic()); assertSame(head, head.getTarget()); } + + @Test + public void testUpdateSmudgedEntries() throws Exception { + git.branchCreate().setName("test2").call(); + RefUpdate rup = db.updateRef(Constants.HEAD); + rup.link("refs/heads/test2"); + + File file = new File(db.getWorkTree(), "Test.txt"); + long size = file.length(); + long mTime = file.lastModified() - 5000L; + assertTrue(file.setLastModified(mTime)); + + DirCache cache = DirCache.lock(db.getIndexFile(), db.getFS()); + DirCacheEntry entry = cache.getEntry("Test.txt"); + assertNotNull(entry); + entry.setLength(0); + entry.setLastModified(0); + cache.write(); + assertTrue(cache.commit()); + + cache = DirCache.read(db.getIndexFile(), db.getFS()); + entry = cache.getEntry("Test.txt"); + assertNotNull(entry); + assertEquals(0, entry.getLength()); + assertEquals(0, entry.getLastModified()); + + db.getIndexFile().setLastModified( + db.getIndexFile().lastModified() - 5000); + + assertNotNull(git.checkout().setName("test").call()); + + cache = DirCache.read(db.getIndexFile(), db.getFS()); + entry = cache.getEntry("Test.txt"); + assertNotNull(entry); + assertEquals(size, entry.getLength()); + assertEquals(mTime, entry.getLastModified()); + } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java index b9f5882d50..3aec611f44 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java @@ -50,6 +50,7 @@ import java.io.File; import java.util.List; import org.eclipse.jgit.diff.DiffEntry; +import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.FileMode; @@ -258,4 +259,110 @@ public class CommitCommandTest extends RepositoryTestCase { assertEquals(path, subDiff.getNewPath()); assertEquals(path, subDiff.getOldPath()); } + + @Test + public void commitUpdatesSmudgedEntries() throws Exception { + Git git = new Git(db); + + File file1 = writeTrashFile("file1.txt", "content1"); + assertTrue(file1.setLastModified(file1.lastModified() - 5000)); + File file2 = writeTrashFile("file2.txt", "content2"); + assertTrue(file2.setLastModified(file2.lastModified() - 5000)); + File file3 = writeTrashFile("file3.txt", "content3"); + assertTrue(file3.setLastModified(file3.lastModified() - 5000)); + + assertNotNull(git.add().addFilepattern("file1.txt") + .addFilepattern("file2.txt").addFilepattern("file3.txt").call()); + RevCommit commit = git.commit().setMessage("add files").call(); + assertNotNull(commit); + + DirCache cache = DirCache.read(db.getIndexFile(), db.getFS()); + int file1Size = cache.getEntry("file1.txt").getLength(); + int file2Size = cache.getEntry("file2.txt").getLength(); + int file3Size = cache.getEntry("file3.txt").getLength(); + ObjectId file2Id = cache.getEntry("file2.txt").getObjectId(); + ObjectId file3Id = cache.getEntry("file3.txt").getObjectId(); + assertTrue(file1Size > 0); + assertTrue(file2Size > 0); + assertTrue(file3Size > 0); + + // Smudge entries + cache = DirCache.lock(db.getIndexFile(), db.getFS()); + cache.getEntry("file1.txt").setLength(0); + cache.getEntry("file2.txt").setLength(0); + cache.getEntry("file3.txt").setLength(0); + cache.write(); + assertTrue(cache.commit()); + + // Verify entries smudged + cache = DirCache.read(db.getIndexFile(), db.getFS()); + assertEquals(0, cache.getEntry("file1.txt").getLength()); + assertEquals(0, cache.getEntry("file2.txt").getLength()); + assertEquals(0, cache.getEntry("file3.txt").getLength()); + + long indexTime = db.getIndexFile().lastModified(); + db.getIndexFile().setLastModified(indexTime - 5000); + + write(file1, "content4"); + assertTrue(file1.setLastModified(file1.lastModified() + 1000)); + assertNotNull(git.commit().setMessage("edit file").setOnly("file1.txt") + .call()); + + cache = db.readDirCache(); + assertEquals(file1Size, cache.getEntry("file1.txt").getLength()); + assertEquals(file2Size, cache.getEntry("file2.txt").getLength()); + assertEquals(file3Size, cache.getEntry("file3.txt").getLength()); + assertEquals(file2Id, cache.getEntry("file2.txt").getObjectId()); + assertEquals(file3Id, cache.getEntry("file3.txt").getObjectId()); + } + + @Test + public void commitIgnoresSmudgedEntryWithDifferentId() throws Exception { + Git git = new Git(db); + + File file1 = writeTrashFile("file1.txt", "content1"); + assertTrue(file1.setLastModified(file1.lastModified() - 5000)); + File file2 = writeTrashFile("file2.txt", "content2"); + assertTrue(file2.setLastModified(file2.lastModified() - 5000)); + + assertNotNull(git.add().addFilepattern("file1.txt") + .addFilepattern("file2.txt").call()); + RevCommit commit = git.commit().setMessage("add files").call(); + assertNotNull(commit); + + DirCache cache = DirCache.read(db.getIndexFile(), db.getFS()); + int file1Size = cache.getEntry("file1.txt").getLength(); + int file2Size = cache.getEntry("file2.txt").getLength(); + assertTrue(file1Size > 0); + assertTrue(file2Size > 0); + + writeTrashFile("file2.txt", "content3"); + assertNotNull(git.add().addFilepattern("file2.txt").call()); + writeTrashFile("file2.txt", "content4"); + + // Smudge entries + cache = DirCache.lock(db.getIndexFile(), db.getFS()); + cache.getEntry("file1.txt").setLength(0); + cache.getEntry("file2.txt").setLength(0); + cache.write(); + assertTrue(cache.commit()); + + // Verify entries smudged + cache = db.readDirCache(); + assertEquals(0, cache.getEntry("file1.txt").getLength()); + assertEquals(0, cache.getEntry("file2.txt").getLength()); + + long indexTime = db.getIndexFile().lastModified(); + db.getIndexFile().setLastModified(indexTime - 5000); + + write(file1, "content5"); + assertTrue(file1.setLastModified(file1.lastModified() + 1000)); + + assertNotNull(git.commit().setMessage("edit file").setOnly("file1.txt") + .call()); + + cache = db.readDirCache(); + assertEquals(file1Size, cache.getEntry("file1.txt").getLength()); + assertEquals(0, cache.getEntry("file2.txt").getLength()); + } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java index 27c3549be3..3087ca829b 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java @@ -220,6 +220,48 @@ public class ResetCommandTest extends RepositoryTestCase { } @Test + public void testMixedResetRetainsSizeAndModifiedTime() throws Exception { + git = new Git(db); + + writeTrashFile("a.txt", "a").setLastModified( + System.currentTimeMillis() - 60 * 1000); + assertNotNull(git.add().addFilepattern("a.txt").call()); + assertNotNull(git.commit().setMessage("a commit").call()); + + writeTrashFile("b.txt", "b").setLastModified( + System.currentTimeMillis() - 60 * 1000); + assertNotNull(git.add().addFilepattern("b.txt").call()); + RevCommit commit2 = git.commit().setMessage("b commit").call(); + assertNotNull(commit2); + + DirCache cache = db.readDirCache(); + + DirCacheEntry aEntry = cache.getEntry("a.txt"); + assertNotNull(aEntry); + assertTrue(aEntry.getLength() > 0); + assertTrue(aEntry.getLastModified() > 0); + + DirCacheEntry bEntry = cache.getEntry("b.txt"); + assertNotNull(bEntry); + assertTrue(bEntry.getLength() > 0); + assertTrue(bEntry.getLastModified() > 0); + + git.reset().setMode(ResetType.MIXED).setRef(commit2.getName()).call(); + + cache = db.readDirCache(); + + DirCacheEntry mixedAEntry = cache.getEntry("a.txt"); + assertNotNull(mixedAEntry); + assertEquals(aEntry.getLastModified(), mixedAEntry.getLastModified()); + assertEquals(aEntry.getLastModified(), mixedAEntry.getLastModified()); + + DirCacheEntry mixedBEntry = cache.getEntry("b.txt"); + assertNotNull(mixedBEntry); + assertEquals(bEntry.getLastModified(), mixedBEntry.getLastModified()); + assertEquals(bEntry.getLastModified(), mixedBEntry.getLastModified()); + } + + @Test public void testPathsReset() throws Exception { setupRepository(); |