diff options
Diffstat (limited to 'org.eclipse.jgit.pgm')
-rw-r--r-- | org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Main.java | 72 |
1 files changed, 51 insertions, 21 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 ab11062cc2..ca3960e97b 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 @@ -45,6 +45,7 @@ package org.eclipse.jgit.pgm; import java.io.File; +import java.io.IOException; import java.io.PrintWriter; import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; @@ -56,6 +57,7 @@ import java.util.List; import org.eclipse.jgit.awtui.AwtAuthenticator; import org.eclipse.jgit.awtui.AwtSshSessionFactory; import org.eclipse.jgit.errors.TransportException; +import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryBuilder; import org.eclipse.jgit.pgm.opt.CmdLineParser; import org.eclipse.jgit.pgm.opt.SubcommandHandler; @@ -74,7 +76,7 @@ public class Main { private boolean showStackTrace; @Option(name = "--git-dir", metaVar = "metaVar_gitDir", usage = "usage_setTheGitRepositoryToOperateOn") - private File gitdir; + private String gitdir; @Argument(index = 0, metaVar = "metaVar_command", required = true, handler = SubcommandHandler.class) private TextBuiltin subcommand; @@ -89,27 +91,46 @@ public class Main { * arguments. */ public static void main(final String[] argv) { - final Main me = new Main(); + new Main().run(argv); + } + + /** + * Parse the command line and execute the requested action. + * + * Subclasses should allocate themselves and then invoke this method: + * + * <pre> + * class ExtMain { + * public static void main(String[] argv) { + * new ExtMain().run(argv); + * } + * } + * </pre> + * + * @param argv + * arguments. + */ + protected void run(final String[] argv) { try { if (!installConsole()) { AwtAuthenticator.install(); AwtSshSessionFactory.install(); } configureHttpProxy(); - me.execute(argv); + execute(argv); } catch (Die err) { System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage())); - if (me.showStackTrace) + if (showStackTrace) err.printStackTrace(); System.exit(128); } catch (Exception err) { - if (!me.showStackTrace && err.getCause() != null + if (!showStackTrace && err.getCause() != null && err instanceof TransportException) System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getCause().getMessage())); if (err.getClass().getName().startsWith("org.eclipse.jgit.errors.")) { System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage())); - if (me.showStackTrace) + if (showStackTrace) err.printStackTrace(); System.exit(128); } @@ -162,21 +183,10 @@ public class Main { } final TextBuiltin cmd = subcommand; - if (cmd.requiresRepository()) { - RepositoryBuilder rb = new RepositoryBuilder() // - .setGitDir(gitdir) // - .readEnvironment() // - .findGitDir(); - if (rb.getGitDir() == null) { - writer.println(CLIText.get().cantFindGitDirectory); - writer.flush(); - System.exit(1); - } - - cmd.init(rb.build(), null); - } else { - cmd.init(null, gitdir); - } + if (cmd.requiresRepository()) + cmd.init(openGitDir(gitdir), null); + else + cmd.init(null, gitdir != null ? new File(gitdir) : null); try { cmd.execute(arguments.toArray(new String[arguments.size()])); } finally { @@ -185,6 +195,26 @@ public class Main { } } + /** + * Evaluate the {@code --git-dir} option and open the repository. + * + * @param gitdir + * the {@code --git-dir} option given on the command line. May be + * null if it was not supplied. + * @return the repository to operate on. + * @throws IOException + * the repository cannot be opened. + */ + protected Repository openGitDir(String gitdir) throws IOException { + RepositoryBuilder rb = new RepositoryBuilder() // + .setGitDir(gitdir != null ? new File(gitdir) : null) // + .readEnvironment() // + .findGitDir(); + if (rb.getGitDir() == null) + throw new Die(CLIText.get().cantFindGitDirectory); + return rb.build(); + } + private static boolean installConsole() { try { install("org.eclipse.jgit.console.ConsoleAuthenticator"); |