Browse Source

Allow checkout paths without specifying branch name

JGit CLI should allow to do this: checkout -- <path>

Currently, even if "a" is a valid path in the git repo, jgit CLI can't
checkout it:
$jgit checkout -- a
error: pathspec 'a' did not match any file(s) known to git.

The fix also fixes at same time "unnamed" zombie "[VAL ...]" argument
shown on the command line.

Before fix:
$jgit -h
jgit checkout name [VAL ...] [-- path ... ...] [--force (-f)] [--help
(-h)] [--orphan] [-b]

After fix:
$jgit -h
jgit checkout [name] [-- path ... ...] [--force (-f)] [--help (-h)]
[--orphan] [-b]

Bug: 475765
Change-Id: I2b0e77959a72e4aac68452dc3846adaa745b0831
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
stable-4.3
Andrey Loskutov 9 years ago
parent
commit
c7c2897527
  1. 13
      org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java
  2. 37
      org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java
  3. 7
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Checkout.java

13
org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java

@ -46,6 +46,7 @@ import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
@ -76,6 +77,18 @@ public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase {
return result.toArray(new String[0]);
}
/**
* @param link
* the path of the symbolic link to create
* @param target
* the target of the symbolic link
* @return the path to the symbolic link
* @throws Exception
*/
protected Path writeLink(String link, String target) throws Exception {
return JGitTestUtil.writeLink(db, link, target);
}
protected File writeTrashFile(final String name, final String data)
throws IOException {
return JGitTestUtil.writeTrashFile(db, name, data);

37
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java

@ -44,9 +44,14 @@ package org.eclipse.jgit.pgm;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import org.eclipse.jgit.api.Git;
@ -59,7 +64,9 @@ import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.FileTreeIterator;
import org.eclipse.jgit.treewalk.FileTreeIterator.FileEntry;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FileUtils;
import org.junit.Assume;
import org.junit.Test;
public class CheckoutTest extends CLIRepositoryTestCase {
@ -578,4 +585,34 @@ public class CheckoutTest extends CLIRepositoryTestCase {
execute("git branch"));
assertEquals("Hello world b", read(b));
}
@Test
public void testCheckouSingleFile() throws Exception {
try (Git git = new Git(db)) {
File a = writeTrashFile("a", "file a");
git.add().addFilepattern(".").call();
git.commit().setMessage("commit file a").call();
writeTrashFile("a", "b");
assertEquals("b", read(a));
assertEquals("[]", Arrays.toString(execute("git checkout -- a")));
assertEquals("file a", read(a));
}
}
@Test
public void testCheckoutLink() throws Exception {
Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
try (Git git = new Git(db)) {
Path path = writeLink("a", "link_a");
assertTrue(Files.isSymbolicLink(path));
git.add().addFilepattern(".").call();
git.commit().setMessage("commit link a").call();
deleteTrashFile("a");
writeTrashFile("a", "Hello world a");
assertFalse(Files.isSymbolicLink(path));
assertEquals("[]", Arrays.toString(execute("git checkout -- a")));
assertEquals("link_a", FileUtils.readSymLink(path.toFile()));
assertTrue(Files.isSymbolicLink(path));
}
}
}

7
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Checkout.java

@ -60,7 +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;
import org.kohsuke.args4j.spi.RestOfArgumentsHandler;
@Command(common = true, usage = "usage_checkout")
class Checkout extends TextBuiltin {
@ -74,11 +74,10 @@ class Checkout extends TextBuiltin {
@Option(name = "--orphan", usage = "usage_orphan")
private boolean orphan = false;
@Argument(required = true, index = 0, metaVar = "metaVar_name", usage = "usage_checkout")
@Argument(required = false, index = 0, metaVar = "metaVar_name", usage = "usage_checkout")
private String name;
@Argument(index = 1)
@Option(name = "--", metaVar = "metaVar_paths", multiValued = true, handler = StopOptionHandler.class)
@Option(name = "--", metaVar = "metaVar_paths", multiValued = true, handler = RestOfArgumentsHandler.class)
private List<String> paths = new ArrayList<String>();
@Override

Loading…
Cancel
Save