]> source.dussan.org Git - jgit.git/commitdiff
added resetIndex() to RepositoryTestCase 15/1315/2
authorChristian Halstrick <christian.halstrick@sap.com>
Mon, 16 Aug 2010 16:42:24 +0000 (18:42 +0200)
committerShawn O. Pearce <spearce@spearce.org>
Wed, 18 Aug 2010 23:45:56 +0000 (16:45 -0700)
Added a utility method to set the reset an index to match exactly
some content in the filesystem. This can be used by tests to prepare
commits in the working-tree and set the index in one shot.

[sp: Cleaned up formatting, added getEntryFile(), released inserter.]

Change-Id: If38b1f7cacaaf769f51b14541c5da0c1e24568a5
Signed-off-by: Christian Halstrick <christian.halstrick@sap.com>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RacyGitTests.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryTestCase.java
org.eclipse.jgit/src/org/eclipse/jgit/treewalk/FileTreeIterator.java

index e208b27e6bcb2ff53a06194857b6bfc1d81300f1..89ad252019820d97a4b5f7b3f51f2838e016ec15 100644 (file)
 package org.eclipse.jgit.lib;
 
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.TreeSet;
 
-import org.eclipse.jgit.dircache.DirCacheBuilder;
-import org.eclipse.jgit.dircache.DirCacheEntry;
 import org.eclipse.jgit.treewalk.FileTreeIterator;
 import org.eclipse.jgit.treewalk.FileTreeIteratorWithTimeControl;
 import org.eclipse.jgit.treewalk.NameConflictTreeWalk;
@@ -133,7 +130,7 @@ public class RacyGitTests extends RepositoryTestCase {
                modTimes.add(fsTick(lastFile));
 
                // now add both files to the index. No racy git expected
-               addToIndex(modTimes);
+               resetIndex(new FileTreeIteratorWithTimeControl(db, modTimes));
 
                assertEquals(
                                "[a, mode:100644, time:t0, length:1, sha1:2e65efe2a145dda7ee51d1741299f848e5bf752e]" +
@@ -150,7 +147,7 @@ public class RacyGitTests extends RepositoryTestCase {
                // now update the index the index. 'a' has to be racily clean -- because
                // it's modification time is exactly the same as the previous index file
                // mod time.
-               addToIndex(modTimes);
+               resetIndex(new FileTreeIteratorWithTimeControl(db, modTimes));
 
                db.readDirCache();
                // although racily clean a should not be reported as being dirty
@@ -160,24 +157,6 @@ public class RacyGitTests extends RepositoryTestCase {
                                indexState(SMUDGE|MOD_TIME|LENGTH));
        }
 
-       private void addToIndex(TreeSet<Long> modTimes)
-                       throws FileNotFoundException, IOException {
-               DirCacheBuilder builder = db.lockDirCache().builder();
-               FileTreeIterator fIt = new FileTreeIteratorWithTimeControl(
-                               db, modTimes);
-               DirCacheEntry dce;
-               while (!fIt.eof()) {
-                       dce = new DirCacheEntry(fIt.getEntryPathString());
-                       dce.setFileMode(fIt.getEntryFileMode());
-                       dce.setLastModified(fIt.getEntryLastModified());
-                       dce.setLength((int) fIt.getEntryLength());
-                       dce.setObjectId(fIt.getEntryObjectId());
-                       builder.add(dce);
-                       fIt.next(1);
-               }
-               builder.commit();
-       }
-
        private File addToWorkDir(String path, String content) throws IOException {
                File f = new File(db.getWorkTree(), path);
                FileOutputStream fos = new FileOutputStream(f);
index 963c9d039dbad53b57c80f7caa77a3ade509b214..a98350c8d61439ddeb593bdaf16a67e3f2c97bf0 100644 (file)
@@ -48,6 +48,7 @@ package org.eclipse.jgit.lib;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
@@ -56,9 +57,12 @@ import java.util.Map;
 import java.util.TreeSet;
 
 import org.eclipse.jgit.dircache.DirCache;
+import org.eclipse.jgit.dircache.DirCacheBuilder;
+import org.eclipse.jgit.dircache.DirCacheEntry;
 import org.eclipse.jgit.dircache.DirCacheIterator;
 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
 import org.eclipse.jgit.storage.file.FileRepository;
+import org.eclipse.jgit.treewalk.FileTreeIterator;
 import org.eclipse.jgit.treewalk.NameConflictTreeWalk;
 
 /**
@@ -202,6 +206,46 @@ public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase {
                return sb.toString();
        }
 
+       /**
+        * Resets the index to represent exactly some filesystem content. E.g. the
+        * following call will replace the index with the working tree content:
+        * <p>
+        * <code>resetIndex(new FileSystemIterator(db))</code>
+        * <p>
+        * This method can be used by testcases which first prepare a new commit
+        * somewhere in the filesystem (e.g. in the working-tree) and then want to
+        * have an index which matches their prepared content.
+        *
+        * @param treeItr
+        *            a {@link FileTreeIterator} which determines which files should
+        *            go into the new index
+        * @throws FileNotFoundException
+        * @throws IOException
+        */
+       protected void resetIndex(FileTreeIterator treeItr)
+                       throws FileNotFoundException, IOException {
+               ObjectInserter inserter = db.newObjectInserter();
+               DirCacheBuilder builder = db.lockDirCache().builder();
+               DirCacheEntry dce;
+
+               while (!treeItr.eof()) {
+                       long len = treeItr.getEntryLength();
+
+                       dce = new DirCacheEntry(treeItr.getEntryPathString());
+                       dce.setFileMode(treeItr.getEntryFileMode());
+                       dce.setLastModified(treeItr.getEntryLastModified());
+                       dce.setLength((int) len);
+                       FileInputStream in = new FileInputStream(treeItr.getEntryFile());
+                       dce.setObjectId(inserter.insert(Constants.OBJ_BLOB, len, in));
+                       in.close();
+                       builder.add(dce);
+                       treeItr.next(1);
+               }
+               builder.commit();
+               inserter.flush();
+               inserter.release();
+       }
+
        /**
         * Helper method to map arbitrary objects to user-defined names. This can be
         * used create short names for objects to produce small and stable debug
index 2d032ab835c502ee1186bacfa192403b7876d86b..09dd50063fdacdd043773c92060bd935a0f9fe06 100644 (file)
@@ -211,4 +211,13 @@ public class FileTreeIterator extends WorkingTreeIterator {
        public File getDirectory() {
                return directory;
        }
+
+       /**
+        * @return The location of the working file. This is the same as {@code new
+        *         File(getDirectory(), getEntryPath())} but may be faster by
+        *         reusing an internal File instance.
+        */
+       public File getEntryFile() {
+               return ((FileEntry) current()).getFile();
+       }
 }