summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.pgm/src
diff options
context:
space:
mode:
authorAndrey Loskutov <loskutov@gmx.de>2016-01-03 16:15:34 +0100
committerAndrey Loskutov <loskutov@gmx.de>2016-01-06 17:27:15 -0500
commitfb5056c2c5e96b815abe568af588157083c66197 (patch)
tree9b23312d5405f590f26d95925e42babba59787b4 /org.eclipse.jgit.pgm/src
parent24468f09e3fb991ea5a6af293f84c7fe37e78b95 (diff)
downloadjgit-fb5056c2c5e96b815abe568af588157083c66197.tar.gz
jgit-fb5056c2c5e96b815abe568af588157083c66197.zip
branch command: print help if requested, even if arguments are wrong
git branch -d -h reports an error (because of missing -d option value) but does not print the help as expected. To fix this, CmdLineParser must catch, print but do not propagate exceptions if help is requested. Bug: 484951 Change-Id: I51265ebe295f22da540792c6a1980b8bdb295a02 Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
Diffstat (limited to 'org.eclipse.jgit.pgm/src')
-rw-r--r--org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/TextBuiltin.java12
-rw-r--r--org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/opt/CmdLineParser.java29
2 files changed, 37 insertions, 4 deletions
diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/TextBuiltin.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/TextBuiltin.java
index c4c5d64a0e..0dc549c7d7 100644
--- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/TextBuiltin.java
+++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/TextBuiltin.java
@@ -277,8 +277,16 @@ public abstract class TextBuiltin {
}
/**
- * @return the resource bundle that will be passed to args4j for purpose
- * of string localization
+ * @return error writer, typically this is standard error.
+ * @since 4.2
+ */
+ public ThrowingPrintWriter getErrorWriter() {
+ return errw;
+ }
+
+ /**
+ * @return the resource bundle that will be passed to args4j for purpose of
+ * string localization
*/
protected ResourceBundle getResourceBundle() {
return CLIText.get().resourceBundle();
diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/opt/CmdLineParser.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/opt/CmdLineParser.java
index a1e39502c7..b531ba65a4 100644
--- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/opt/CmdLineParser.java
+++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/opt/CmdLineParser.java
@@ -43,6 +43,7 @@
package org.eclipse.jgit.pgm.opt;
+import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Field;
import java.util.ArrayList;
@@ -53,6 +54,7 @@ import java.util.ResourceBundle;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.pgm.Die;
import org.eclipse.jgit.pgm.TextBuiltin;
import org.eclipse.jgit.pgm.internal.CLIText;
import org.eclipse.jgit.revwalk.RevCommit;
@@ -95,6 +97,8 @@ public class CmdLineParser extends org.kohsuke.args4j.CmdLineParser {
private boolean seenHelp;
+ private TextBuiltin cmd;
+
/**
* Creates a new command line owner that parses arguments/options and set
* them into the given object.
@@ -126,8 +130,12 @@ public class CmdLineParser extends org.kohsuke.args4j.CmdLineParser {
*/
public CmdLineParser(final Object bean, Repository repo) {
super(bean);
- if (repo == null && bean instanceof TextBuiltin)
- repo = ((TextBuiltin) bean).getRepository();
+ if (bean instanceof TextBuiltin) {
+ cmd = (TextBuiltin) bean;
+ }
+ if (repo == null && cmd != null) {
+ repo = cmd.getRepository();
+ }
this.db = repo;
}
@@ -167,6 +175,11 @@ public class CmdLineParser extends org.kohsuke.args4j.CmdLineParser {
try {
super.parseArgument(tmp.toArray(new String[tmp.size()]));
+ } catch (Die e) {
+ if (!seenHelp) {
+ throw e;
+ }
+ printToErrorWriter(CLIText.fatalError(e.getMessage()));
} finally {
// reset "required" options to defaults for correct command printout
if (backup != null && !backup.isEmpty()) {
@@ -176,6 +189,18 @@ public class CmdLineParser extends org.kohsuke.args4j.CmdLineParser {
}
}
+ private void printToErrorWriter(String error) {
+ if (cmd == null) {
+ System.err.println(error);
+ } else {
+ try {
+ cmd.getErrorWriter().println(error);
+ } catch (IOException e1) {
+ System.err.println(error);
+ }
+ }
+ }
+
private List<OptionHandler> unsetRequiredOptions() {
List<OptionHandler> options = getOptions();
List<OptionHandler> backup = new ArrayList<>(options);