]> source.dussan.org Git - jgit.git/commitdiff
Don't raise checkout conflict for file missing in working tree 29/22529/2
authorChristian Halstrick <christian.halstrick@sap.com>
Tue, 25 Feb 2014 16:29:45 +0000 (17:29 +0100)
committerChristian Halstrick <christian.halstrick@sap.com>
Wed, 26 Feb 2014 14:35:05 +0000 (15:35 +0100)
During a checkout we want to prevent to overwrite unsaved local file
content. Jgit was therefore checking whether the file to overwrite is
dirty or missing and would raise a conflict if this was the case. That
was wrong. It should only check if the file is dirty. It's ok to
"overwrite" a missing/non-existing file.

Change-Id: I63c3a94f663c87f09170fdf8b1b1bf4ed5246fc5
Signed-off-by: Christian Halstrick <christian.halstrick@sap.com>
org.eclipse.jgit.pgm.test/META-INF/MANIFEST.MF
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java

index 1a812bf691de32153762b8ecea2de41a4887d962..403c2f4638a71c4ee77f8b29ed87db995944c7ad 100644 (file)
@@ -18,6 +18,7 @@ Import-Package: org.eclipse.jgit.api;version="[3.3.0,3.4.0)",
  org.eclipse.jgit.revwalk;version="[3.3.0,3.4.0)",
  org.eclipse.jgit.storage.file;version="[3.3.0,3.4.0)",
  org.eclipse.jgit.transport;version="[3.3.0,3.4.0)",
+ org.eclipse.jgit.treewalk;version="[3.3.0,3.4.0)",
  org.eclipse.jgit.util;version="[3.3.0,3.4.0)",
  org.eclipse.jgit.util.io;version="[3.3.0,3.4.0)",
  org.hamcrest.core;bundle-version="[1.1.0,2.0.0)",
index a6ea48c0eaefdfee462cd00ca4dd8d31794a1638..8012893f92319f9f18097bbfcd0b9ab63e097222 100644 (file)
@@ -46,6 +46,10 @@ import java.io.File;
 
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.lib.CLIRepositoryTestCase;
+import org.eclipse.jgit.lib.FileMode;
+import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.treewalk.FileTreeIterator;
+import org.eclipse.jgit.treewalk.FileTreeIterator.FileEntry;
 import org.eclipse.jgit.util.FileUtils;
 import org.junit.Assert;
 import org.junit.Test;
@@ -133,6 +137,52 @@ public class CheckoutTest extends CLIRepositoryTestCase {
                Assert.assertEquals("\ta", execute[1]);
        }
 
+       /**
+        * Steps:
+        * <ol>
+        * <li>Add file 'a' and 'b'
+        * <li>Commit
+        * <li>Create branch '1'
+        * <li>modify file 'a'
+        * <li>Commit
+        * <li>Delete file 'a' in the working tree
+        * <li>Checkout branch '1'
+        * </ol>
+        * The working tree should contain 'a' with FileMode.REGULAR_FILE after the
+        * checkout.
+        *
+        * @throws Exception
+        */
+       @Test
+       public void testCheckoutWithMissingWorkingTreeFile() throws Exception {
+               Git git = new Git(db);
+               File fileA = writeTrashFile("a", "Hello world a");
+               writeTrashFile("b", "Hello world b");
+               git.add().addFilepattern(".").call();
+               git.commit().setMessage("add files a & b").call();
+               Ref branch_1 = git.branchCreate().setName("branch_1").call();
+               writeTrashFile("a", "b");
+               git.add().addFilepattern("a").call();
+               git.commit().setMessage("modify file a").call();
+
+               FileEntry entry = new FileTreeIterator.FileEntry(new File(
+                               db.getWorkTree(), "a"), db.getFS());
+               assertEquals(FileMode.REGULAR_FILE, entry.getMode());
+
+               FileUtils.delete(fileA);
+
+               git.checkout().setName(branch_1.getName()).call();
+
+               entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
+                               db.getFS());
+               assertEquals(FileMode.REGULAR_FILE, entry.getMode());
+               assertEquals("Hello world a", read(fileA));
+       }
+
+       static private void assertEquals(Object expected, Object actual) {
+               Assert.assertEquals(expected, actual);
+       }
+
        static private void assertEquals(String expected, String[] actual) {
                // if there is more than one line, ignore last one if empty
                Assert.assertEquals(
index 79fe8d6f567d9b17d809ce0413e13d3dcc7bf506..f8be5006e33915ef3c94735c92a4de4be38729ba 100644 (file)
@@ -909,9 +909,9 @@ public class DirCacheCheckout {
                                                // file content
                                                update(name, mId, mMode);
                                        } else if (dce != null
-                                                       && (f == null || f.isModified(dce, true,
+                                                       && (f != null && f.isModified(dce, true,
                                                                        this.walk.getObjectReader()))) {
-                                               // File doesn't exist or is dirty
+                                               // File exists and is dirty
                                                // Head and Index don't contain a submodule
                                                // Head contains the same as Index. Merge differs
                                                // Something in Merge
@@ -919,7 +919,7 @@ public class DirCacheCheckout {
                                                // but the file is dirty. Report a conflict
                                                conflict(name, dce, h, m);
                                        } else {
-                                               // File exists and is clean
+                                               // File doesn't exist or is clean
                                                // Head and Index don't contain a submodule
                                                // Head contains the same as Index. Merge differs
                                                // Something in Merge