Browse Source

Add all paths option to CheckoutCommand

This will perform the equivalent  of running a
'git checkout -- .' at the root of a repository

Change-Id: I3e2dd563700999bc063effdd3640499c8ed08136
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-2.0
Kevin Sawicki 13 years ago committed by Matthias Sohn
parent
commit
10c0b34b88
  1. 10
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PathCheckoutCommandTest.java
  2. 25
      org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java

10
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PathCheckoutCommandTest.java

@ -233,4 +233,14 @@ public class PathCheckoutCommandTest extends RepositoryTestCase {
assertEquals(0, status.getRemoved().size());
assertEquals(0, status.getUntracked().size());
}
@Test
public void testCheckoutRepository() throws Exception {
CheckoutCommand co = git.checkout();
File test = writeTrashFile(FILE1, "");
File test2 = writeTrashFile(FILE2, "");
co.setStartPoint("HEAD~2").setAllPaths(true).call();
assertEquals("1", read(test));
assertEquals("a", read(test2));
}
}

25
org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java

@ -102,6 +102,8 @@ public class CheckoutCommand extends GitCommand<Ref> {
private List<String> paths;
private boolean checkoutAllPaths;
/**
* @param repo
*/
@ -126,7 +128,7 @@ public class CheckoutCommand extends GitCommand<Ref> {
checkCallable();
processOptions();
try {
if (!paths.isEmpty()) {
if (checkoutAllPaths || !paths.isEmpty()) {
checkoutPaths();
status = CheckoutResult.OK_RESULT;
setCallable(false);
@ -234,6 +236,22 @@ public class CheckoutCommand extends GitCommand<Ref> {
return this;
}
/**
* Set whether to checkout all paths
* <p>
* This options should be used when you want to do a path checkout on the
* entire repository and so calling {@link #addPath(String)} is not possible
* since empty paths are not allowed.
*
* @param all
* true to checkout all paths, false otherwise
* @return {@code this}
*/
public CheckoutCommand setAllPaths(boolean all) {
checkoutAllPaths = all;
return this;
}
/**
* Checkout paths into index and working directory
*
@ -249,7 +267,8 @@ public class CheckoutCommand extends GitCommand<Ref> {
DirCacheEditor editor = dc.editor();
TreeWalk startWalk = new TreeWalk(revWalk.getObjectReader());
startWalk.setRecursive(true);
startWalk.setFilter(PathFilterGroup.createFromStrings(paths));
if (!checkoutAllPaths)
startWalk.setFilter(PathFilterGroup.createFromStrings(paths));
boolean checkoutIndex = startCommit == null && startPoint == null;
if (!checkoutIndex)
startWalk.addTree(revWalk.parseCommit(getStartPoint())
@ -310,7 +329,7 @@ public class CheckoutCommand extends GitCommand<Ref> {
}
private void processOptions() throws InvalidRefNameException {
if (paths.isEmpty()
if ((!checkoutAllPaths && paths.isEmpty())
&& (name == null || !Repository
.isValidRefName(Constants.R_HEADS + name)))
throw new InvalidRefNameException(MessageFormat.format(JGitText

Loading…
Cancel
Save