]> source.dussan.org Git - jgit.git/commitdiff
Consider working tree changes when stashing newly added files 57/11757/3
authorRobin Rosenberg <robin.rosenberg@dewire.com>
Tue, 9 Apr 2013 19:22:18 +0000 (21:22 +0200)
committerRobin Rosenberg <robin.rosenberg@dewire.com>
Tue, 9 Apr 2013 19:28:15 +0000 (21:28 +0200)
Bug: 402396
Change-Id: I50ff707c0c9abcab3f98eea21aaa6e824f7af63a

org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StashCreateCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/StashCreateCommand.java

index f7eba5814f5613b7d78fd280893e9f60b2c9eac3..544d591fe9764784730e96803383eb4b99263712 100644 (file)
@@ -155,6 +155,18 @@ public class StashCreateCommandTest extends RepositoryTestCase {
                }
        }
 
+       private List<DiffEntry> diffIndexAgainstWorking(final RevCommit commit)
+                       throws IOException {
+               TreeWalk walk = createTreeWalk();
+               try {
+                       walk.addTree(commit.getParent(1).getTree());
+                       walk.addTree(commit.getTree());
+                       return DiffEntry.scan(walk);
+               } finally {
+                       walk.release();
+               }
+       }
+
        @Test
        public void noLocalChanges() throws Exception {
                assertNull(git.stashCreate().call());
@@ -194,6 +206,26 @@ public class StashCreateCommandTest extends RepositoryTestCase {
                assertEquals("file2.txt", diffs.get(0).getNewPath());
        }
 
+       @Test
+       public void newFileInIndexThenModifiedInWorkTree() throws Exception {
+               writeTrashFile("file", "content");
+               git.add().addFilepattern("file").call();
+               writeTrashFile("file", "content2");
+               RevCommit stashedWorkTree = Git.wrap(db).stashCreate().call();
+               validateStashedCommit(stashedWorkTree);
+               RevWalk walk = new RevWalk(db);
+               RevCommit stashedIndex = stashedWorkTree.getParent(1);
+               walk.parseBody(stashedIndex);
+               walk.parseBody(stashedIndex.getTree());
+               walk.parseBody(stashedIndex.getParent(0));
+               List<DiffEntry> workTreeStashAgainstWorkTree = diffWorkingAgainstHead(stashedWorkTree);
+               assertEquals(1, workTreeStashAgainstWorkTree.size());
+               List<DiffEntry> workIndexAgainstWorkTree = diffIndexAgainstHead(stashedWorkTree);
+               assertEquals(1, workIndexAgainstWorkTree.size());
+               List<DiffEntry> indexStashAgainstWorkTree = diffIndexAgainstWorking(stashedWorkTree);
+               assertEquals(1, indexStashAgainstWorkTree.size());
+       }
+
        @Test
        public void indexDelete() throws Exception {
                git.rm().addFilepattern("file.txt").call();
index 375dee05c5633b606be511ad7bdd1ccfb6d426fd..ba56c1a7123af061664f427bf6bfad1f6f3d958d 100644 (file)
@@ -248,13 +248,15 @@ public class StashCreateCommand extends GitCommand<RevCommit> {
                                                        DirCacheIterator.class);
                                        WorkingTreeIterator wtIter = treeWalk.getTree(2,
                                                        WorkingTreeIterator.class);
-                                       if (headIter != null && indexIter != null && wtIter != null) {
-                                               if (!indexIter.getDirCacheEntry().isMerged())
-                                                       throw new UnmergedPathsException(
-                                                                       new UnmergedPathException(
-                                                                                       indexIter.getDirCacheEntry()));
-                                               if (wtIter.idEqual(indexIter)
-                                                               || wtIter.idEqual(headIter))
+                                       if (indexIter != null
+                                                       && !indexIter.getDirCacheEntry().isMerged())
+                                               throw new UnmergedPathsException(
+                                                               new UnmergedPathException(
+                                                                               indexIter.getDirCacheEntry()));
+                                       if (wtIter != null) {
+                                               if (indexIter != null && wtIter.idEqual(indexIter)
+                                                               || headIter != null
+                                                               && wtIter.idEqual(headIter))
                                                        continue;
                                                treeWalk.getObjectId(id, 0);
                                                final DirCacheEntry entry = new DirCacheEntry(
@@ -271,14 +273,12 @@ public class StashCreateCommand extends GitCommand<RevCommit> {
                                                        in.close();
                                                }
                                                wtEdits.add(new PathEdit(entry) {
-
                                                        public void apply(DirCacheEntry ent) {
                                                                ent.copyMetaData(entry);
                                                        }
                                                });
-                                       } else if (indexIter == null)
-                                               wtDeletes.add(treeWalk.getPathString());
-                                       else if (wtIter == null && headIter != null)
+                                       }
+                                       if (wtIter == null && headIter != null)
                                                wtDeletes.add(treeWalk.getPathString());
                                } while (treeWalk.next());