Browse Source

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 <spearce@spearce.org>
stable-0.10
Shawn O. Pearce 14 years ago
parent
commit
0b51a45688
  1. 72
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Main.java

72
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Main.java

@ -45,6 +45,7 @@
package org.eclipse.jgit.pgm; package org.eclipse.jgit.pgm;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
@ -56,6 +57,7 @@ import java.util.List;
import org.eclipse.jgit.awtui.AwtAuthenticator; import org.eclipse.jgit.awtui.AwtAuthenticator;
import org.eclipse.jgit.awtui.AwtSshSessionFactory; import org.eclipse.jgit.awtui.AwtSshSessionFactory;
import org.eclipse.jgit.errors.TransportException; import org.eclipse.jgit.errors.TransportException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryBuilder; import org.eclipse.jgit.lib.RepositoryBuilder;
import org.eclipse.jgit.pgm.opt.CmdLineParser; import org.eclipse.jgit.pgm.opt.CmdLineParser;
import org.eclipse.jgit.pgm.opt.SubcommandHandler; import org.eclipse.jgit.pgm.opt.SubcommandHandler;
@ -74,7 +76,7 @@ public class Main {
private boolean showStackTrace; private boolean showStackTrace;
@Option(name = "--git-dir", metaVar = "metaVar_gitDir", usage = "usage_setTheGitRepositoryToOperateOn") @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) @Argument(index = 0, metaVar = "metaVar_command", required = true, handler = SubcommandHandler.class)
private TextBuiltin subcommand; private TextBuiltin subcommand;
@ -89,27 +91,46 @@ public class Main {
* arguments. * arguments.
*/ */
public static void main(final String[] argv) { 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 { try {
if (!installConsole()) { if (!installConsole()) {
AwtAuthenticator.install(); AwtAuthenticator.install();
AwtSshSessionFactory.install(); AwtSshSessionFactory.install();
} }
configureHttpProxy(); configureHttpProxy();
me.execute(argv); execute(argv);
} catch (Die err) { } catch (Die err) {
System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage())); System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage()));
if (me.showStackTrace) if (showStackTrace)
err.printStackTrace(); err.printStackTrace();
System.exit(128); System.exit(128);
} catch (Exception err) { } catch (Exception err) {
if (!me.showStackTrace && err.getCause() != null if (!showStackTrace && err.getCause() != null
&& err instanceof TransportException) && err instanceof TransportException)
System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getCause().getMessage())); System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getCause().getMessage()));
if (err.getClass().getName().startsWith("org.eclipse.jgit.errors.")) { if (err.getClass().getName().startsWith("org.eclipse.jgit.errors.")) {
System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage())); System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage()));
if (me.showStackTrace) if (showStackTrace)
err.printStackTrace(); err.printStackTrace();
System.exit(128); System.exit(128);
} }
@ -162,21 +183,10 @@ public class Main {
} }
final TextBuiltin cmd = subcommand; final TextBuiltin cmd = subcommand;
if (cmd.requiresRepository()) { if (cmd.requiresRepository())
RepositoryBuilder rb = new RepositoryBuilder() // cmd.init(openGitDir(gitdir), null);
.setGitDir(gitdir) // else
.readEnvironment() // cmd.init(null, gitdir != null ? new File(gitdir) : null);
.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);
}
try { try {
cmd.execute(arguments.toArray(new String[arguments.size()])); cmd.execute(arguments.toArray(new String[arguments.size()]));
} finally { } 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() { private static boolean installConsole() {
try { try {
install("org.eclipse.jgit.console.ConsoleAuthenticator"); install("org.eclipse.jgit.console.ConsoleAuthenticator");

Loading…
Cancel
Save