]> source.dussan.org Git - jgit.git/commitdiff
ResetCommand: Use DirCacheBuilder in resetIndex 88/8388/2
authorRobin Stocker <robin@nibor.org>
Sun, 28 Oct 2012 14:57:43 +0000 (15:57 +0100)
committerChris Aniszczyk <zx@twitter.com>
Thu, 1 Nov 2012 17:47:10 +0000 (10:47 -0700)
With bug 391855, DirCacheEditor's PathEdit will be applied for each
stage. For an unmerged path, this would result in 3 equal entries for
the same path.

By using a DirCacheBuilder, the code is simpler and does not have the
above problem with unmerged paths.

Bug: 391860
Change-Id: I785deeaeb8474f8c7a7fbc9ef00d3131fac87e41
Signed-off-by: Chris Aniszczyk <zx@twitter.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java

index 3b0e7bd935e42e32beaa011b935287a5b8fa9285..dd6fc67c7b1457c84ada4c949dccc6fe26badd49 100644 (file)
@@ -256,6 +256,36 @@ public class ResetCommandTest extends RepositoryTestCase {
                assertEquals(bEntry.getLastModified(), mixedBEntry.getLastModified());
        }
 
+       @Test
+       public void testMixedResetWithUnmerged() throws Exception {
+               git = new Git(db);
+
+               String file = "a.txt";
+               writeTrashFile(file, "data");
+               String file2 = "b.txt";
+               writeTrashFile(file2, "data");
+
+               git.add().addFilepattern(file).addFilepattern(file2).call();
+               git.commit().setMessage("commit").call();
+
+               DirCache index = db.lockDirCache();
+               DirCacheBuilder builder = index.builder();
+               builder.add(createEntry(file, FileMode.REGULAR_FILE, 1, ""));
+               builder.add(createEntry(file, FileMode.REGULAR_FILE, 2, ""));
+               builder.add(createEntry(file, FileMode.REGULAR_FILE, 3, ""));
+               assertTrue(builder.commit());
+
+               assertEquals("[a.txt, mode:100644, stage:1]"
+                               + "[a.txt, mode:100644, stage:2]"
+                               + "[a.txt, mode:100644, stage:3]",
+                               indexState(0));
+
+               git.reset().setMode(ResetType.MIXED).call();
+
+               assertEquals("[a.txt, mode:100644]" + "[b.txt, mode:100644]",
+                               indexState(0));
+       }
+
        @Test
        public void testPathsReset() throws Exception {
                setupRepository();
index 7f5eb0244c7d0ac1450fd44aa909747a2ab204d3..36c3334b82e6a81b87d4f5fb3bec4afbcc118ebb 100644 (file)
@@ -54,9 +54,6 @@ import org.eclipse.jgit.dircache.DirCache;
 import org.eclipse.jgit.dircache.DirCacheBuildIterator;
 import org.eclipse.jgit.dircache.DirCacheBuilder;
 import org.eclipse.jgit.dircache.DirCacheCheckout;
-import org.eclipse.jgit.dircache.DirCacheEditor;
-import org.eclipse.jgit.dircache.DirCacheEditor.DeletePath;
-import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
 import org.eclipse.jgit.dircache.DirCacheEntry;
 import org.eclipse.jgit.dircache.DirCacheIterator;
 import org.eclipse.jgit.internal.JGitText;
@@ -313,7 +310,7 @@ public class ResetCommand extends GitCommand<Ref> {
                DirCache dc = repo.lockDirCache();
                TreeWalk walk = null;
                try {
-                       DirCacheEditor editor = dc.editor();
+                       DirCacheBuilder builder = dc.builder();
 
                        walk = new TreeWalk(repo);
                        walk.addTree(commit.getTree());
@@ -324,7 +321,7 @@ public class ResetCommand extends GitCommand<Ref> {
                                AbstractTreeIterator cIter = walk.getTree(0,
                                                AbstractTreeIterator.class);
                                if (cIter == null) {
-                                       editor.add(new DeletePath(walk.getPathString()));
+                                       // Not in commit, don't add to new index
                                        continue;
                                }
 
@@ -340,16 +337,10 @@ public class ResetCommand extends GitCommand<Ref> {
                                        entry.setLength(indexEntry.getLength());
                                }
 
-                               editor.add(new PathEdit(entry) {
-
-                                       @Override
-                                       public void apply(DirCacheEntry ent) {
-                                               ent.copyMetaData(entry);
-                                       }
-                               });
+                               builder.add(entry);
                        }
 
-                       editor.commit();
+                       builder.commit();
                } finally {
                        dc.unlock();
                        if (walk != null)