summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.pgm/src/org
diff options
context:
space:
mode:
authorAndrey Loskutov <loskutov@gmx.de>2016-01-03 12:29:55 +0100
committerAndrey Loskutov <loskutov@gmx.de>2016-01-06 17:27:31 -0500
commit7780a4ee31df0e73f9519aaa51fd349a70b18e71 (patch)
tree7fca7d0a26d211ada7b5826fae60699f20fd484a /org.eclipse.jgit.pgm/src/org
parentfb5056c2c5e96b815abe568af588157083c66197 (diff)
downloadjgit-7780a4ee31df0e73f9519aaa51fd349a70b18e71.tar.gz
jgit-7780a4ee31df0e73f9519aaa51fd349a70b18e71.zip
Make sure CLIGitCommand and Main produce (almost) same results
Currently execution of tests in pgm uses CLIGitCommand which re-implements few things from Main. Unfortunately this can results in a different test behavior compared to the real CLI runtime. The change let CLIGitCommand extend Main and only slightly modifies the runtime (stream redirection and undesired exit() termination). Change-Id: I87b7b61d1c84a89e5917610d84409f01be90b70b Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
Diffstat (limited to 'org.eclipse.jgit.pgm/src/org')
-rw-r--r--org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Main.java111
1 files changed, 76 insertions, 35 deletions
diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Main.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Main.java
index e31306b214..d701f22c38 100644
--- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Main.java
+++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Main.java
@@ -90,14 +90,23 @@ public class Main {
@Argument(index = 1, metaVar = "metaVar_arg")
private List<String> arguments = new ArrayList<String>();
+ PrintWriter writer;
+
+ /**
+ *
+ */
+ public Main() {
+ HttpTransport.setConnectionFactory(new HttpClientConnectionFactory());
+ }
+
/**
* Execute the command line.
*
* @param argv
* arguments.
+ * @throws Exception
*/
- public static void main(final String[] argv) {
- HttpTransport.setConnectionFactory(new HttpClientConnectionFactory());
+ public static void main(final String[] argv) throws Exception {
new Main().run(argv);
}
@@ -116,8 +125,10 @@ public class Main {
*
* @param argv
* arguments.
+ * @throws Exception
*/
- protected void run(final String[] argv) {
+ protected void run(final String[] argv) throws Exception {
+ writer = createErrorWriter();
try {
if (!installConsole()) {
AwtAuthenticator.install();
@@ -126,12 +137,14 @@ public class Main {
configureHttpProxy();
execute(argv);
} catch (Die err) {
- if (err.isAborted())
- System.exit(1);
- System.err.println(CLIText.fatalError(err.getMessage()));
- if (showStackTrace)
- err.printStackTrace();
- System.exit(128);
+ if (err.isAborted()) {
+ exit(1, err);
+ }
+ writer.println(CLIText.fatalError(err.getMessage()));
+ if (showStackTrace) {
+ err.printStackTrace(writer);
+ }
+ exit(128, err);
} catch (Exception err) {
// Try to detect errno == EPIPE and exit normally if that happens
// There may be issues with operating system versions and locale,
@@ -139,46 +152,54 @@ public class Main {
// under other circumstances.
if (err.getClass() == IOException.class) {
// Linux, OS X
- if (err.getMessage().equals("Broken pipe")) //$NON-NLS-1$
- System.exit(0);
+ if (err.getMessage().equals("Broken pipe")) { //$NON-NLS-1$
+ exit(0, err);
+ }
// Windows
- if (err.getMessage().equals("The pipe is being closed")) //$NON-NLS-1$
- System.exit(0);
+ if (err.getMessage().equals("The pipe is being closed")) { //$NON-NLS-1$
+ exit(0, err);
+ }
}
if (!showStackTrace && err.getCause() != null
- && err instanceof TransportException)
- System.err.println(CLIText.fatalError(err.getCause().getMessage()));
+ && err instanceof TransportException) {
+ writer.println(CLIText.fatalError(err.getCause().getMessage()));
+ }
if (err.getClass().getName().startsWith("org.eclipse.jgit.errors.")) { //$NON-NLS-1$
- System.err.println(CLIText.fatalError(err.getMessage()));
- if (showStackTrace)
+ writer.println(CLIText.fatalError(err.getMessage()));
+ if (showStackTrace) {
err.printStackTrace();
- System.exit(128);
+ }
+ exit(128, err);
}
err.printStackTrace();
- System.exit(1);
+ exit(1, err);
}
if (System.out.checkError()) {
- System.err.println(CLIText.get().unknownIoErrorStdout);
- System.exit(1);
+ writer.println(CLIText.get().unknownIoErrorStdout);
+ exit(1, null);
}
- if (System.err.checkError()) {
+ if (writer.checkError()) {
// No idea how to present an error here, most likely disk full or
// broken pipe
- System.exit(1);
+ exit(1, null);
}
}
+ PrintWriter createErrorWriter() {
+ return new PrintWriter(System.err);
+ }
+
private void execute(final String[] argv) throws Exception {
final CmdLineParser clp = new SubcommandLineParser(this);
- PrintWriter writer = new PrintWriter(System.err);
+
try {
clp.parseArgument(argv);
} catch (CmdLineException err) {
if (argv.length > 0 && !help && !version) {
writer.println(CLIText.fatalError(err.getMessage()));
writer.flush();
- System.exit(1);
+ exit(1, err);
}
}
@@ -194,22 +215,24 @@ public class Main {
writer.println(CLIText.get().mostCommonlyUsedCommandsAre);
final CommandRef[] common = CommandCatalog.common();
int width = 0;
- for (final CommandRef c : common)
+ for (final CommandRef c : common) {
width = Math.max(width, c.getName().length());
+ }
width += 2;
for (final CommandRef c : common) {
writer.print(' ');
writer.print(c.getName());
- for (int i = c.getName().length(); i < width; i++)
+ for (int i = c.getName().length(); i < width; i++) {
writer.print(' ');
+ }
writer.print(CLIText.get().resourceBundle().getString(c.getUsage()));
writer.println();
}
writer.println();
}
writer.flush();
- System.exit(1);
+ exit(1, null);
}
if (version) {
@@ -218,21 +241,39 @@ public class Main {
}
final TextBuiltin cmd = subcommand;
- if (cmd.requiresRepository())
- cmd.init(openGitDir(gitdir), null);
- else
- cmd.init(null, gitdir);
+ init(cmd);
try {
cmd.execute(arguments.toArray(new String[arguments.size()]));
} finally {
- if (cmd.outw != null)
+ if (cmd.outw != null) {
cmd.outw.flush();
- if (cmd.errw != null)
+ }
+ if (cmd.errw != null) {
cmd.errw.flush();
+ }
+ }
+ }
+
+ void init(final TextBuiltin cmd) throws IOException {
+ if (cmd.requiresRepository()) {
+ cmd.init(openGitDir(gitdir), null);
+ } else {
+ cmd.init(null, gitdir);
}
}
/**
+ * @param status
+ * @param t
+ * can be {@code null}
+ * @throws Exception
+ */
+ void exit(int status, Exception t) throws Exception {
+ writer.flush();
+ System.exit(status);
+ }
+
+ /**
* Evaluate the {@code --git-dir} option and open the repository.
*
* @param aGitdir
@@ -281,7 +322,7 @@ public class Main {
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException, ClassNotFoundException {
try {
- Class.forName(name).getMethod("install").invoke(null); //$NON-NLS-1$
+ Class.forName(name).getMethod("install").invoke(null); //$NON-NLS-1$
} catch (InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException)
throw (RuntimeException) e.getCause();