Browse Source

CLI status should support --untracked-files

A special options handler is added to properly handle the short -u alias
of the option.

The "normal" mode is not supported by this patch, because this mode of
listing untracked files is not
supported by the org.eclipse.jgit.lib.IndexDiff class. This mode is not
necessary for my use case. It can be added later if anyone really needs
it.

The StatusTest is updated to cover all possible combinations of the
--porcelain and --untracked-files options.

Bug: 459319
Change-Id: I305ac95739cfed0c16735e0987844e57fa27e236
Signed-off-by: Kaloyan Raev <kaloyan.r@zend.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-4.0
Kaloyan Raev 10 years ago committed by Matthias Sohn
parent
commit
9041fbc958
  1. 624
      org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/StatusTest.java
  2. 2
      org.eclipse.jgit.pgm/META-INF/MANIFEST.MF
  3. 1
      org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties
  4. 23
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Status.java
  5. 114
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/opt/UntrackedFilesHandler.java
  6. 3
      pom.xml

624
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/StatusTest.java

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2012, 2013 François Rey <eclipse.org_@_francois_._rey_._name> * Copyright (C) 2012, 2015 François Rey <eclipse.org_@_francois_._rey_._name>
* and other copyright owners as documented in the project's IP log. * and other copyright owners as documented in the project's IP log.
* *
* This program and the accompanying materials are made available * This program and the accompanying materials are made available
@ -43,17 +43,144 @@
package org.eclipse.jgit.pgm; package org.eclipse.jgit.pgm;
import java.io.IOException;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.CLIRepositoryTestCase; import org.eclipse.jgit.lib.CLIRepositoryTestCase;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Test; import org.junit.Test;
public class StatusTest extends CLIRepositoryTestCase { public class StatusTest extends CLIRepositoryTestCase {
@Test
public void testStatusDefault() throws Exception {
executeTest("git status", false, true);
}
@Test
public void testStatusU() throws Exception {
executeTest("git status -u", false, true);
}
@Test
public void testStatusUno() throws Exception {
executeTest("git status -uno", false, false);
}
@Test
public void testStatusUall() throws Exception {
executeTest("git status -uall", false, true);
}
@Test
public void testStatusUntrackedFiles() throws Exception {
executeTest("git status --untracked-files", false, true);
}
@Test
public void testStatusUntrackedFilesNo() throws Exception {
executeTest("git status --untracked-files=no", false, false);
}
@Test
public void testStatusUntrackedFilesAll() throws Exception {
executeTest("git status --untracked-files=all", false, true);
}
@Test @Test
public void testStatus() throws Exception { public void testStatusPorcelain() throws Exception {
executeTest("git status --porcelain", true, true);
}
@Test
public void testStatusPorcelainU() throws Exception {
executeTest("git status --porcelain -u", true, true);
}
@Test
public void testStatusPorcelainUno() throws Exception {
executeTest("git status --porcelain -uno", true, false);
}
@Test
public void testStatusPorcelainUall() throws Exception {
executeTest("git status --porcelain -uall", true, true);
}
@Test
public void testStatusPorcelainUntrackedFiles() throws Exception {
executeTest("git status --porcelain --untracked-files", true, true);
}
@Test
public void testStatusPorcelainUntrackedFilesNo() throws Exception {
executeTest("git status --porcelain --untracked-files=no", true, false);
}
@Test
public void testStatusPorcelainUntrackedFilesAll() throws Exception {
executeTest("git status --porcelain --untracked-files=all", true, true);
}
/**
* Executes the test sequence.
*
* @param command
* full git command and parameters to be used
* @param porcelain
* indicates that porcelain format is expected in the output
* @param untrackedFiles
* indicates that untracked files are expected in the output
*
* @throws Exception
* if error during test execution
*/
private void executeTest(String command, boolean porcelain,
boolean untrackedFiles) throws Exception {
Git git = new Git(db); Git git = new Git(db);
// Write all files // Write all files
writeAllFiles();
// Test untracked
assertUntrackedFiles(command, porcelain, untrackedFiles);
// Add to index
addFilesToIndex(git);
// Test staged count
assertStagedFiles(command, porcelain, untrackedFiles);
// Commit
makeInitialCommit(git);
assertAfterInitialCommit(command, porcelain, untrackedFiles);
// Make some changes and stage them
makeSomeChangesAndStageThem(git);
// Test staged/not-staged status
assertStagedStatus(command, porcelain, untrackedFiles);
// Create unmerged file
createUnmergedFile(git);
// Commit pending changes
commitPendingChanges(git);
assertUntracked(command, porcelain, untrackedFiles, "master");
// Checkout new branch
checkoutTestBranch(git);
// Test branch status
assertUntracked(command, porcelain, untrackedFiles, "test");
// Commit change and checkout master again
RevCommit testBranch = commitChangesInTestBranch(git);
assertUntracked(command, porcelain, untrackedFiles, "test");
checkoutMasterBranch(git);
// Change the same file and commit
changeUnmergedFileAndCommit(git);
assertUntracked(command, porcelain, untrackedFiles, "master");
// Merge test branch into master
mergeTestBranchInMaster(git, testBranch);
// Test unmerged status
assertUntrackedAndUnmerged(command, porcelain, untrackedFiles, "master");
// Test detached head
detachHead(git);
assertUntrackedAndUnmerged(command, porcelain, untrackedFiles, null);
}
private void writeAllFiles() throws IOException {
writeTrashFile("tracked", "tracked"); writeTrashFile("tracked", "tracked");
writeTrashFile("stagedNew", "stagedNew"); writeTrashFile("stagedNew", "stagedNew");
writeTrashFile("stagedModified", "stagedModified"); writeTrashFile("stagedModified", "stagedModified");
@ -61,8 +188,100 @@ public class StatusTest extends CLIRepositoryTestCase {
writeTrashFile("trackedModified", "trackedModified"); writeTrashFile("trackedModified", "trackedModified");
writeTrashFile("trackedDeleted", "trackedDeleted"); writeTrashFile("trackedDeleted", "trackedDeleted");
writeTrashFile("untracked", "untracked"); writeTrashFile("untracked", "untracked");
// Test untracked }
assertArrayOfLinesEquals(new String[] { // git status output
private void addFilesToIndex(Git git) throws GitAPIException {
git.add().addFilepattern("tracked").call();
git.add().addFilepattern("stagedModified").call();
git.add().addFilepattern("stagedDeleted").call();
git.add().addFilepattern("trackedModified").call();
git.add().addFilepattern("trackedDeleted").call();
}
private void makeInitialCommit(Git git) throws GitAPIException {
git.commit().setMessage("initial commit").call();
}
private void makeSomeChangesAndStageThem(Git git) throws IOException,
GitAPIException {
writeTrashFile("stagedModified", "stagedModified modified");
deleteTrashFile("stagedDeleted");
writeTrashFile("trackedModified", "trackedModified modified");
deleteTrashFile("trackedDeleted");
git.add().addFilepattern("stagedModified").call();
git.rm().addFilepattern("stagedDeleted").call();
git.add().addFilepattern("stagedNew").call();
}
private void createUnmergedFile(Git git) throws IOException,
GitAPIException {
writeTrashFile("unmerged", "unmerged");
git.add().addFilepattern("unmerged").call();
}
private void commitPendingChanges(Git git) throws GitAPIException {
git.add().addFilepattern("trackedModified").call();
git.rm().addFilepattern("trackedDeleted").call();
git.commit().setMessage("commit before branching").call();
}
private void checkoutTestBranch(Git git) throws GitAPIException {
git.checkout().setCreateBranch(true).setName("test").call();
}
private RevCommit commitChangesInTestBranch(Git git) throws IOException,
GitAPIException {
writeTrashFile("unmerged", "changed in test branch");
git.add().addFilepattern("unmerged").call();
return git.commit()
.setMessage("changed unmerged in test branch").call();
}
private void checkoutMasterBranch(Git git) throws GitAPIException {
git.checkout().setName("master").call();
}
private void changeUnmergedFileAndCommit(Git git) throws IOException,
GitAPIException {
writeTrashFile("unmerged", "changed in master branch");
git.add().addFilepattern("unmerged").call();
git.commit().setMessage("changed unmerged in master branch").call();
}
private void mergeTestBranchInMaster(Git git, RevCommit aCommit)
throws GitAPIException {
git.merge().include(aCommit.getId()).call();
}
private void detachHead(Git git) throws IOException, GitAPIException {
String commitId = db.getRef(Constants.MASTER).getObjectId().name();
git.checkout().setName(commitId).call();
}
private void assertUntrackedFiles(String command, boolean porcelain,
boolean untrackedFiles) throws Exception {
String[] output = new String[0];
if (porcelain) {
if (untrackedFiles) {
output = new String[] { //
"?? stagedDeleted", //
"?? stagedModified", //
"?? stagedNew", //
"?? tracked", //
"?? trackedDeleted", //
"?? trackedModified", //
"?? untracked", //
"" //
};
} else {
output = new String[] { //
"" //
};
}
} else {
if (untrackedFiles) {
output = new String[] { //
"On branch master", // "On branch master", //
"Untracked files:", // "Untracked files:", //
"",// "",//
@ -74,15 +293,47 @@ public class StatusTest extends CLIRepositoryTestCase {
"\ttrackedModified", // "\ttrackedModified", //
"\tuntracked", // "\tuntracked", //
"" // "" //
}, execute("git status")); // };
// Add to index } else {
git.add().addFilepattern("tracked").call(); output = new String[] { //
git.add().addFilepattern("stagedModified").call(); "On branch master", //
git.add().addFilepattern("stagedDeleted").call(); "" //
git.add().addFilepattern("trackedModified").call(); };
git.add().addFilepattern("trackedDeleted").call(); }
// Test staged count }
assertArrayOfLinesEquals(new String[] { // git status output
assertArrayOfLinesEquals(output, execute(command));
}
private void assertStagedFiles(String command, boolean porcelain,
boolean untrackedFiles) throws Exception {
String[] output = new String[0];
if (porcelain) {
if (untrackedFiles) {
output = new String[] { //
"A stagedDeleted", //
"A stagedModified", //
"A tracked", //
"A trackedDeleted", //
"A trackedModified", //
"?? stagedNew", //
"?? untracked", //
"" //
};
} else {
output = new String[] { //
"A stagedDeleted", //
"A stagedModified", //
"A tracked", //
"A trackedDeleted", //
"A trackedModified", //
"" //
};
}
} else {
if (untrackedFiles) {
output = new String[] { //
"On branch master", // "On branch master", //
"Changes to be committed:", // "Changes to be committed:", //
"", // "", //
@ -97,28 +348,90 @@ public class StatusTest extends CLIRepositoryTestCase {
"\tstagedNew", // "\tstagedNew", //
"\tuntracked", // "\tuntracked", //
"" // "" //
}, execute("git status")); // };
// Commit } else {
git.commit().setMessage("initial commit") output = new String[] { //
.call(); "On branch master", //
assertArrayOfLinesEquals(new String[] { // git status output "Changes to be committed:", //
"", //
"\tnew file: stagedDeleted", //
"\tnew file: stagedModified", //
"\tnew file: tracked", //
"\tnew file: trackedDeleted", //
"\tnew file: trackedModified", //
"" //
};
}
}
assertArrayOfLinesEquals(output, execute(command));
}
private void assertAfterInitialCommit(String command, boolean porcelain,
boolean untrackedFiles) throws Exception {
String[] output = new String[0];
if (porcelain) {
if (untrackedFiles) {
output = new String[] { //
"?? stagedNew", //
"?? untracked", //
"" //
};
} else {
output = new String[] { //
"" //
};
}
} else {
if (untrackedFiles) {
output = new String[] { //
"On branch master", // "On branch master", //
"Untracked files:", // "Untracked files:", //
"", // "", //
"\tstagedNew", // "\tstagedNew", //
"\tuntracked", // "\tuntracked", //
"" // "" //
}, execute("git status")); // };
// Make some changes and stage them } else {
writeTrashFile("stagedModified", "stagedModified modified"); output = new String[] { //
deleteTrashFile("stagedDeleted"); "On branch master", //
writeTrashFile("trackedModified", "trackedModified modified"); "" //
deleteTrashFile("trackedDeleted"); };
git.add().addFilepattern("stagedModified").call(); }
git.rm().addFilepattern("stagedDeleted").call(); }
git.add().addFilepattern("stagedNew").call();
// Test staged/not-staged status assertArrayOfLinesEquals(output, execute(command));
assertArrayOfLinesEquals(new String[] { // git status output }
private void assertStagedStatus(String command, boolean porcelain,
boolean untrackedFiles) throws Exception {
String[] output = new String[0];
if (porcelain) {
if (untrackedFiles) {
output = new String[] { //
"D stagedDeleted", //
"M stagedModified", //
"A stagedNew", //
" D trackedDeleted", //
" M trackedModified", //
"?? untracked", //
"" //
};
} else {
output = new String[] { //
"D stagedDeleted", //
"M stagedModified", //
"A stagedNew", //
" D trackedDeleted", //
" M trackedModified", //
"" //
};
}
} else {
if (untrackedFiles) {
output = new String[] { //
"On branch master", // "On branch master", //
"Changes to be committed:", // "Changes to be committed:", //
"", // "", //
@ -135,60 +448,89 @@ public class StatusTest extends CLIRepositoryTestCase {
"", // "", //
"\tuntracked", // "\tuntracked", //
"" // "" //
}, execute("git status")); // };
// Create unmerged file } else {
writeTrashFile("unmerged", "unmerged"); output = new String[] { //
git.add().addFilepattern("unmerged").call();
// Commit pending changes
git.add().addFilepattern("trackedModified").call();
git.rm().addFilepattern("trackedDeleted").call();
git.commit().setMessage("commit before branching").call();
assertArrayOfLinesEquals(new String[] { // git status output
"On branch master", // "On branch master", //
"Untracked files:", // "Changes to be committed:", //
"", // "", //
"\tuntracked", // "\tdeleted: stagedDeleted", //
"" // "\tmodified: stagedModified", //
}, execute("git status")); // "\tnew file: stagedNew", //
// Checkout new branch
git.checkout().setCreateBranch(true).setName("test").call();
// Test branch status
assertArrayOfLinesEquals(new String[] { // git status output
"On branch test", //
"Untracked files:", //
"", // "", //
"\tuntracked", // "Changes not staged for commit:", //
"" //
}, execute("git status")); //
// Commit change and checkout master again
writeTrashFile("unmerged", "changed in test branch");
git.add().addFilepattern("unmerged").call();
RevCommit testBranch = git.commit()
.setMessage("changed unmerged in test branch").call();
assertArrayOfLinesEquals(new String[] { // git status output
"On branch test", //
"Untracked files:", //
"", // "", //
"\tuntracked", // "\tdeleted: trackedDeleted", //
"\tmodified: trackedModified", //
"", //
};
}
}
assertArrayOfLinesEquals(output, execute(command));
}
private void assertUntracked(String command,
boolean porcelain,
boolean untrackedFiles, String branch) throws Exception {
String[] output = new String[0];
String branchHeader = "On branch " + branch;
if (porcelain) {
if (untrackedFiles) {
output = new String[] { //
"?? untracked", //
"" // "" //
}, execute("git status")); // };
git.checkout().setName("master").call(); } else {
// Change the same file and commit output = new String[] { //
writeTrashFile("unmerged", "changed in master branch"); "" //
git.add().addFilepattern("unmerged").call(); };
git.commit().setMessage("changed unmerged in master branch").call(); }
assertArrayOfLinesEquals(new String[] { // git status output } else {
"On branch master", // if (untrackedFiles) {
output = new String[] { //
branchHeader, //
"Untracked files:", // "Untracked files:", //
"", // "", //
"\tuntracked", // "\tuntracked", //
"" // "" //
}, execute("git status")); // };
// Merge test branch into master } else {
git.merge().include(testBranch.getId()).call(); output = new String[] { //
// Test unmerged status branchHeader, //
assertArrayOfLinesEquals(new String[] { // git status output "" //
"On branch master", // };
}
}
assertArrayOfLinesEquals(output, execute(command));
}
private void assertUntrackedAndUnmerged(String command, boolean porcelain,
boolean untrackedFiles, String branch) throws Exception {
String[] output = new String[0];
String branchHeader = (branch == null) //
? "Not currently on any branch." //
: "On branch " + branch;
if (porcelain) {
if (untrackedFiles) {
output = new String[] { //
"UU unmerged", //
"?? untracked", //
"" //
};
} else {
output = new String[] { //
"UU unmerged", //
"" //
};
}
} else {
if (untrackedFiles) {
output = new String[] { //
branchHeader, //
"Unmerged paths:", // "Unmerged paths:", //
"", // "", //
"\tboth modified: unmerged", // "\tboth modified: unmerged", //
@ -197,138 +539,18 @@ public class StatusTest extends CLIRepositoryTestCase {
"", // "", //
"\tuntracked", // "\tuntracked", //
"" // "" //
}, execute("git status")); // };
// Test detached head } else {
String commitId = db.getRef(Constants.MASTER).getObjectId().name(); output = new String[] { //
git.checkout().setName(commitId).call(); branchHeader, //
assertArrayOfLinesEquals(new String[] { // git status output
"Not currently on any branch.", //
"Unmerged paths:", // "Unmerged paths:", //
"", // "", //
"\tboth modified: unmerged", // "\tboth modified: unmerged", //
"", //
"Untracked files:", //
"", //
"\tuntracked", //
"" // "" //
}, execute("git status")); // };
}
} }
@Test assertArrayOfLinesEquals(output, execute(command));
public void testStatusPorcelain() throws Exception {
Git git = new Git(db);
// Write all files
writeTrashFile("tracked", "tracked");
writeTrashFile("stagedNew", "stagedNew");
writeTrashFile("stagedModified", "stagedModified");
writeTrashFile("stagedDeleted", "stagedDeleted");
writeTrashFile("trackedModified", "trackedModified");
writeTrashFile("trackedDeleted", "trackedDeleted");
writeTrashFile("untracked", "untracked");
// Test untracked
assertArrayOfLinesEquals(new String[] { // git status output
"?? stagedDeleted", //
"?? stagedModified", //
"?? stagedNew", //
"?? tracked", //
"?? trackedDeleted", //
"?? trackedModified", //
"?? untracked", //
"" //
}, execute("git status --porcelain")); //
// Add to index
git.add().addFilepattern("tracked").call();
git.add().addFilepattern("stagedModified").call();
git.add().addFilepattern("stagedDeleted").call();
git.add().addFilepattern("trackedModified").call();
git.add().addFilepattern("trackedDeleted").call();
// Test staged count
assertArrayOfLinesEquals(new String[] { // git status output
"A stagedDeleted", //
"A stagedModified", //
"A tracked", //
"A trackedDeleted", //
"A trackedModified", //
"?? stagedNew", //
"?? untracked", //
"" //
}, execute("git status --porcelain")); //
// Commit
git.commit().setMessage("initial commit").call();
assertArrayOfLinesEquals(new String[] { // git status output
"?? stagedNew", //
"?? untracked", //
"" //
}, execute("git status --porcelain")); //
// Make some changes and stage them
writeTrashFile("stagedModified", "stagedModified modified");
deleteTrashFile("stagedDeleted");
writeTrashFile("trackedModified", "trackedModified modified");
deleteTrashFile("trackedDeleted");
git.add().addFilepattern("stagedModified").call();
git.rm().addFilepattern("stagedDeleted").call();
git.add().addFilepattern("stagedNew").call();
// Test staged/not-staged status
assertArrayOfLinesEquals(new String[] { // git status output
"D stagedDeleted", //
"M stagedModified", //
"A stagedNew", //
" D trackedDeleted", //
" M trackedModified", //
"?? untracked", //
"" //
}, execute("git status --porcelain")); //
// Create unmerged file
writeTrashFile("unmerged", "unmerged");
git.add().addFilepattern("unmerged").call();
// Commit pending changes
git.add().addFilepattern("trackedModified").call();
git.rm().addFilepattern("trackedDeleted").call();
git.commit().setMessage("commit before branching").call();
assertArrayOfLinesEquals(new String[] { // git status output
"?? untracked", //
"" //
}, execute("git status --porcelain")); //
// Checkout new branch
git.checkout().setCreateBranch(true).setName("test").call();
// Test branch status
assertArrayOfLinesEquals(new String[] { // git status output
"?? untracked", //
"" //
}, execute("git status --porcelain")); //
// Commit change and checkout master again
writeTrashFile("unmerged", "changed in test branch");
git.add().addFilepattern("unmerged").call();
RevCommit testBranch = git.commit()
.setMessage("changed unmerged in test branch").call();
assertArrayOfLinesEquals(new String[] { // git status output
"?? untracked", //
"" //
}, execute("git status --porcelain")); //
git.checkout().setName("master").call();
// Change the same file and commit
writeTrashFile("unmerged", "changed in master branch");
git.add().addFilepattern("unmerged").call();
git.commit().setMessage("changed unmerged in master branch").call();
assertArrayOfLinesEquals(new String[] { // git status output
"?? untracked", //
"" //
}, execute("git status --porcelain")); //
// Merge test branch into master
git.merge().include(testBranch.getId()).call();
// Test unmerged status
assertArrayOfLinesEquals(new String[] { // git status output
"UU unmerged", //
"?? untracked", //
"" //
}, execute("git status --porcelain")); //
// Test detached head
String commitId = db.getRef(Constants.MASTER).getObjectId().name();
git.checkout().setName(commitId).call();
assertArrayOfLinesEquals(new String[] { // git status output
"UU unmerged", //
"?? untracked", //
"" //
}, execute("git status --porcelain")); //
} }
} }

2
org.eclipse.jgit.pgm/META-INF/MANIFEST.MF

@ -36,7 +36,7 @@ Import-Package: org.apache.commons.compress.archivers;version="[1.3,2.0)",
org.eclipse.jgit.util;version="[4.0.0,4.1.0)", org.eclipse.jgit.util;version="[4.0.0,4.1.0)",
org.eclipse.jgit.util.io;version="[4.0.0,4.1.0)", org.eclipse.jgit.util.io;version="[4.0.0,4.1.0)",
org.kohsuke.args4j;version="[2.0.12,2.1.0)", org.kohsuke.args4j;version="[2.0.12,2.1.0)",
org.kohsuke.args4j.spi;version="[2.0.12,2.1.0)" org.kohsuke.args4j.spi;version="[2.0.15,2.1.0)"
Bundle-ActivationPolicy: lazy Bundle-ActivationPolicy: lazy
Export-Package: org.eclipse.jgit.console;version="4.0.0", Export-Package: org.eclipse.jgit.console;version="4.0.0",
org.eclipse.jgit.pgm;version="4.0.0"; org.eclipse.jgit.pgm;version="4.0.0";

1
org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties

@ -346,6 +346,7 @@ usage_symbolicVersionForTheProject=Symbolic version for the project
usage_tags=fetch all tags usage_tags=fetch all tags
usage_notags=do not fetch tags usage_notags=do not fetch tags
usage_tagMessage=tag message usage_tagMessage=tag message
usage_untrackedFilesMode=show untracked files
usage_updateRemoteRefsFromAnotherRepository=Update remote refs from another repository usage_updateRemoteRefsFromAnotherRepository=Update remote refs from another repository
usage_useNameInsteadOfOriginToTrackUpstream=use <name> instead of 'origin' to track upstream usage_useNameInsteadOfOriginToTrackUpstream=use <name> instead of 'origin' to track upstream
usage_checkoutBranchAfterClone=checkout named branch instead of remotes's HEAD usage_checkoutBranchAfterClone=checkout named branch instead of remotes's HEAD

23
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Status.java

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2011, 2013 François Rey <eclipse.org_@_francois_._rey_._name> * Copyright (C) 2011, 2015 François Rey <eclipse.org_@_francois_._rey_._name>
* and other copyright owners as documented in the project's IP log. * and other copyright owners as documented in the project's IP log.
* *
* This program and the accompanying materials are made available * This program and the accompanying materials are made available
@ -61,6 +61,11 @@ import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.pgm.internal.CLIText; import org.eclipse.jgit.pgm.internal.CLIText;
import org.kohsuke.args4j.Option; import org.kohsuke.args4j.Option;
import org.eclipse.jgit.pgm.opt.UntrackedFilesHandler;
/**
* Status command
*/
@Command(usage = "usage_Status", common = true) @Command(usage = "usage_Status", common = true)
class Status extends TextBuiltin { class Status extends TextBuiltin {
@ -75,6 +80,9 @@ class Status extends TextBuiltin {
@Option(name = "--porcelain", usage = "usage_machineReadableOutput") @Option(name = "--porcelain", usage = "usage_machineReadableOutput")
protected boolean porcelain; protected boolean porcelain;
@Option(name = "--untracked-files", aliases = { "-u", "-uno", "-uall" }, usage = "usage_untrackedFilesMode", handler = UntrackedFilesHandler.class)
protected String untrackedFilesMode = "all"; // default value //$NON-NLS-1$
@Option(name = "--", metaVar = "metaVar_path", multiValued = true) @Option(name = "--", metaVar = "metaVar_path", multiValued = true)
protected List<String> filterPaths; protected List<String> filterPaths;
@ -174,10 +182,13 @@ class Status extends TextBuiltin {
} }
// untracked are always at the end of the list // untracked are always at the end of the list
TreeSet<String> untracked = new TreeSet<String>(status.getUntracked()); if ("all".equals(untrackedFilesMode)) { //$NON-NLS-1$
TreeSet<String> untracked = new TreeSet<String>(
status.getUntracked());
for (String path : untracked) for (String path : untracked)
printPorcelainLine('?', '?', path); printPorcelainLine('?', '?', path);
} }
}
private void printPorcelainLine(char x, char y, String path) private void printPorcelainLine(char x, char y, String path)
throws IOException { throws IOException {
@ -240,7 +251,7 @@ class Status extends TextBuiltin {
firstHeader = false; firstHeader = false;
} }
int nbUntracked = untracked.size(); int nbUntracked = untracked.size();
if (nbUntracked > 0) { if (nbUntracked > 0 && ("all".equals(untrackedFilesMode))) { //$NON-NLS-1$
if (!firstHeader) if (!firstHeader)
printSectionHeader(""); //$NON-NLS-1$ printSectionHeader(""); //$NON-NLS-1$
printSectionHeader(CLIText.get().untrackedFiles); printSectionHeader(CLIText.get().untrackedFiles);
@ -250,12 +261,14 @@ class Status extends TextBuiltin {
protected void printSectionHeader(String pattern, Object... arguments) protected void printSectionHeader(String pattern, Object... arguments)
throws IOException { throws IOException {
outw.println(CLIText.formatLine(MessageFormat if (!porcelain) {
.format(pattern, arguments))); outw.println(CLIText.formatLine(MessageFormat.format(pattern,
arguments)));
if (!pattern.equals("")) //$NON-NLS-1$ if (!pattern.equals("")) //$NON-NLS-1$
outw.println(CLIText.formatLine("")); //$NON-NLS-1$ outw.println(CLIText.formatLine("")); //$NON-NLS-1$
outw.flush(); outw.flush();
} }
}
protected int printList(Collection<String> list) throws IOException { protected int printList(Collection<String> list) throws IOException {
if (!list.isEmpty()) { if (!list.isEmpty()) {

114
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/opt/UntrackedFilesHandler.java

@ -0,0 +1,114 @@
/*
* Copyright (C) 2015 Zend Technologies Ltd. and others
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.pgm.opt;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
import org.kohsuke.args4j.spi.StringOptionHandler;
/**
* Special handler for the <code>--untracked-files</code> option of the
* <code>status</code> command.
*
* The following rules apply:
* <ul>
* <li>If no mode is given, i.e. just <code>--untracked-files</code> is passed,
* then it is the same as <code>--untracked-files=all</code></li>
* <li>If the <code>-u</code> alias is passed then it is the same as
* <code>--untracked-files</code></li>
* <li>If the <code>-uno</code> alias is passed then it is the same as
* <code>--untracked-files=no</code></li>
* <li>If the <code>-uall</code> alias is passed then it is the same as
* <code>--untracked-files=all</code></li>
* </ul>
*
* @since 4.0
*/
public class UntrackedFilesHandler extends StringOptionHandler {
/**
* @param parser
* The parser to which this handler belongs to.
* @param option
* The annotation.
* @param setter
* Object to be used for setting value.
*/
public UntrackedFilesHandler(CmdLineParser parser, OptionDef option,
Setter<? super String> setter) {
super(parser, option, setter);
}
@Override
public int parseArguments(Parameters params) throws CmdLineException {
String alias = params.getParameter(-1);
if ("-u".equals(alias)) { //$NON-NLS-1$
setter.addValue("all"); //$NON-NLS-1$
return 0;
} else if ("-uno".equals(alias)) { //$NON-NLS-1$
setter.addValue("no"); //$NON-NLS-1$
return 0;
} else if ("-uall".equals(alias)) { //$NON-NLS-1$
setter.addValue("all"); //$NON-NLS-1$
return 0;
} else if (params.size() == 0) {
setter.addValue("all"); //$NON-NLS-1$
return 0;
} else if (params.size() == 1) {
String mode = params.getParameter(0);
if ("no".equals(mode) || "all".equals(mode)) { //$NON-NLS-1$ //$NON-NLS-2$
setter.addValue(mode);
} else {
throw new CmdLineException(owner, String.format(
"Invalid untracked files mode '%s'", mode)); //$NON-NLS-1$
}
return 1;
} else {
return super.parseArguments(params);
}
}
}

3
pom.xml

@ -181,8 +181,7 @@
<javaewah-version>0.7.9</javaewah-version> <javaewah-version>0.7.9</javaewah-version>
<junit-version>4.11</junit-version> <junit-version>4.11</junit-version>
<test-fork-count>1C</test-fork-count> <test-fork-count>1C</test-fork-count>
<!-- TODO: update Maven dependency for args4j to 2.0.21 as soon as available on Maven Central --> <args4j-version>2.0.15</args4j-version>
<args4j-version>2.0.12</args4j-version>
<commons-compress-version>1.6</commons-compress-version> <commons-compress-version>1.6</commons-compress-version>
<osgi-core-version>4.3.1</osgi-core-version> <osgi-core-version>4.3.1</osgi-core-version>
<servlet-api-version>2.5</servlet-api-version> <servlet-api-version>2.5</servlet-api-version>

Loading…
Cancel
Save