aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java
diff options
context:
space:
mode:
authorKevin Sawicki <kevin@github.com>2012-05-21 15:00:23 -0700
committerKevin Sawicki <kevin@github.com>2012-05-28 14:01:14 -0700
commitdac66672df0535f61a13273524d46e1e0012ca69 (patch)
tree640a1d4ece6f1a41741d503e9f8adc2c0383393c /org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java
parent24a0f47e32ab7cdf20c2201d7100599ea057f8a3 (diff)
downloadjgit-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/tst/org/eclipse/jgit/api/CommitCommandTest.java')
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java107
1 files changed, 107 insertions, 0 deletions
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());
+ }
}