From 0b51a4568835e95fe2fc88f9458d4a7368a9c691 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Sat, 16 Oct 2010 21:33:06 -0700 Subject: [PATCH] Allow pgm Main to be extended 3rd party packages that use repository types other than FileRepository may wish to extend our pgm package and implement their own resolution scheme for repository "names" that are passed in by the --git-dir command line option. Make that possible by allowing the package to extend the Main class and override the lookup. This is primarily useful when developing new storage implementations and trying to experiment with the results, without linking all of it into the core JGit package. Change-Id: Id30e168da16341e5da43365688a63aa30c7b7e2c Signed-off-by: Shawn O. Pearce --- .../src/org/eclipse/jgit/pgm/Main.java | 72 +++++++++++++------ 1 file 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 ab11062cc..ca3960e97 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: + * + *
+	 * class ExtMain {
+	 * 	public static void main(String[] argv) {
+	 * 		new ExtMain().run(argv);
+	 * 	}
+	 * }
+	 * 
+ * + * @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");