diff options
author | Kevin Sawicki <kevin@github.com> | 2012-05-21 15:00:23 -0700 |
---|---|---|
committer | Kevin Sawicki <kevin@github.com> | 2012-05-28 14:01:14 -0700 |
commit | dac66672df0535f61a13273524d46e1e0012ca69 (patch) | |
tree | 640a1d4ece6f1a41741d503e9f8adc2c0383393c /org.eclipse.jgit.test | |
parent | 24a0f47e32ab7cdf20c2201d7100599ea057f8a3 (diff) | |
download | jgit-dac66672df0535f61a13273524d46e1e0012ca69.tar.gz jgit-dac66672df0535f61a13273524d46e1e0012ca69.zip |
Update smudged entries when writing index
Overload DirCache.lock to take a repository that is
used for updating smudged index entries with information
from the repository's working tree.
New unit tests are also added for updating smudged index
entries on reset, checkout, and commit.
Change-Id: I88689f26000e4e57e77931e5ace7c804d92af1b6
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(); |