]> source.dussan.org Git - jgit.git/commitdiff
Command line: implement checkout -- <path> 86/23086/3
authorMatthias Sohn <matthias.sohn@sap.com>
Mon, 10 Feb 2014 07:10:31 +0000 (08:10 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Mon, 12 May 2014 21:12:48 +0000 (17:12 -0400)
Change-Id: I3d261e6541fe0fcaa210587d2aa226e153071351
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Checkout.java

index a328baec2780ed11229dcf5766155e6e06af09ce..50ddfe04d8c1439470be37dd2603b71692bbce2b 100644 (file)
@@ -81,6 +81,10 @@ public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase {
                return JGitTestUtil.writeTrashFile(db, name, data);
        }
 
+       protected String read(final File file) throws IOException {
+               return JGitTestUtil.read(file);
+       }
+
        protected void deleteTrashFile(final String name) throws IOException {
                JGitTestUtil.deleteTrashFile(db, name);
        }
index 8012893f92319f9f18097bbfcd0b9ab63e097222..2c1f59f250a7d59d01c68c9aeb4351a6a807e704 100644 (file)
@@ -42,6 +42,8 @@
  */
 package org.eclipse.jgit.pgm;
 
+import static org.junit.Assert.assertArrayEquals;
+
 import java.io.File;
 
 import org.eclipse.jgit.api.Git;
@@ -191,4 +193,26 @@ public class CheckoutTest extends CLIRepositoryTestCase {
                                                : actual.length);
                Assert.assertEquals(expected, actual[0]);
        }
+
+       @Test
+       public void testCheckoutPath() throws Exception {
+               Git git = new Git(db);
+               writeTrashFile("a", "Hello world a");
+               git.add().addFilepattern(".").call();
+               git.commit().setMessage("commit file a").call();
+               git.branchCreate().setName("branch_1").call();
+               git.checkout().setName("branch_1").call();
+               File b = writeTrashFile("b", "Hello world b");
+               git.add().addFilepattern("b").call();
+               git.commit().setMessage("commit file b").call();
+               File a = writeTrashFile("a", "New Hello world a");
+               git.add().addFilepattern(".").call();
+               git.commit().setMessage("modified a").call();
+               assertArrayEquals(new String[] { "" },
+                               execute("git checkout HEAD~2 -- a"));
+               assertEquals("Hello world a", read(a));
+               assertArrayEquals(new String[] { "* branch_1", "  master", "" },
+                               execute("git branch"));
+               assertEquals("Hello world b", read(b));
+       }
 }
index 2f35ecbabd93086510e8dc78da560ffe622a86d6..8f911fd9245df02a3469c7b71fe6ddc17ad6bc7d 100644 (file)
@@ -45,6 +45,8 @@
 package org.eclipse.jgit.pgm;
 
 import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.eclipse.jgit.api.CheckoutCommand;
 import org.eclipse.jgit.api.Git;
@@ -58,6 +60,7 @@ import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.pgm.internal.CLIText;
 import org.kohsuke.args4j.Argument;
 import org.kohsuke.args4j.Option;
+import org.kohsuke.args4j.spi.StopOptionHandler;
 
 @Command(common = true, usage = "usage_checkout")
 class Checkout extends TextBuiltin {
@@ -68,9 +71,13 @@ class Checkout extends TextBuiltin {
        @Option(name = "--force", aliases = { "-f" }, usage = "usage_forceCheckout")
        private boolean force = false;
 
-       @Argument(required = true, metaVar = "metaVar_name", usage = "usage_checkout")
+       @Argument(required = true, index = 0, metaVar = "metaVar_name", usage = "usage_checkout")
        private String name;
 
+       @Argument(index = 1)
+       @Option(name = "--", metaVar = "metaVar_paths", multiValued = true, handler = StopOptionHandler.class)
+       private List<String> paths = new ArrayList<String>();
+
        @Override
        protected void run() throws Exception {
                if (createBranch) {
@@ -80,9 +87,15 @@ class Checkout extends TextBuiltin {
                }
 
                CheckoutCommand command = new Git(db).checkout();
-               command.setCreateBranch(createBranch);
-               command.setName(name);
-               command.setForce(force);
+               if (paths.size() > 0) {
+                       command.setStartPoint(name);
+                       for (String path : paths)
+                               command.addPath(path);
+               } else {
+                       command.setCreateBranch(createBranch);
+                       command.setName(name);
+                       command.setForce(force);
+               }
                try {
                        String oldBranch = db.getBranch();
                        Ref ref = command.call();