Convert merge-base program to args4j
[egit/zawir.git] / org.spearce.jgit.pgm / src / org / spearce / jgit / pgm / Main.java
blobc069989fdea75fd2082c845f5dbd90ad8dbebd7b
1 /*
2 * Copyright (C) 2006, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
22 * written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org.spearce.jgit.pgm;
41 import java.io.File;
42 import java.util.ArrayList;
43 import java.util.List;
45 import org.kohsuke.args4j.Argument;
46 import org.kohsuke.args4j.CmdLineException;
47 import org.kohsuke.args4j.ExampleMode;
48 import org.kohsuke.args4j.Option;
49 import org.spearce.jgit.awtui.AwtAuthenticator;
50 import org.spearce.jgit.errors.TransportException;
51 import org.spearce.jgit.lib.Repository;
52 import org.spearce.jgit.pgm.opt.CmdLineParser;
53 import org.spearce.jgit.pgm.opt.SubcommandHandler;
54 import org.spearce.jgit.util.HttpSupport;
56 /** Command line entry point. */
57 public class Main {
58 @Option(name = "--help", usage = "display this help text", aliases = { "-h" })
59 private boolean help;
61 @Option(name = "--show-stack-trace", usage = "display the Java stack trace on exceptions")
62 private boolean showStackTrace;
64 @Option(name = "--git-dir", metaVar = "GIT_DIR", usage = "set the git repository to operate on")
65 private File gitdir;
67 @Argument(index = 0, metaVar = "command", required = true, handler = SubcommandHandler.class)
68 private TextBuiltin subcommand;
70 @Argument(index = 1, metaVar = "ARG")
71 private List<String> arguments = new ArrayList<String>();
73 /**
74 * Execute the command line.
76 * @param argv
77 * arguments.
79 public static void main(final String[] argv) {
80 final Main me = new Main();
81 try {
82 AwtAuthenticator.install();
83 HttpSupport.configureHttpProxy();
84 me.execute(argv);
85 } catch (Die err) {
86 System.err.println("fatal: " + err.getMessage());
87 if (me.showStackTrace)
88 err.printStackTrace();
89 System.exit(128);
90 } catch (Exception err) {
91 if (!me.showStackTrace && err.getCause() != null
92 && err instanceof TransportException)
93 System.err.println("fatal: " + err.getCause().getMessage());
95 if (err.getClass().getName().startsWith("org.spearce.jgit.errors.")) {
96 System.err.println("fatal: " + err.getMessage());
97 if (me.showStackTrace)
98 err.printStackTrace();
99 System.exit(128);
101 err.printStackTrace();
102 System.exit(1);
106 private void execute(final String[] argv) throws Exception {
107 final CmdLineParser clp = new CmdLineParser(this);
108 try {
109 clp.parseArgument(argv);
110 } catch (CmdLineException err) {
111 if (argv.length > 0 && !help) {
112 System.err.println("fatal: " + err.getMessage());
113 System.exit(1);
117 if (argv.length == 0 || help) {
118 final String ex = clp.printExample(ExampleMode.ALL);
119 System.err.println("jgit" + ex + " command [ARG ...]");
120 if (help) {
121 System.err.println();
122 clp.printUsage(System.err);
123 System.err.println();
125 System.exit(1);
128 if (gitdir == null)
129 gitdir = findGitDir();
130 if (gitdir == null || !gitdir.isDirectory()) {
131 System.err.println("error: can't find git directory");
132 System.exit(1);
135 final TextBuiltin cmd = subcommand;
136 cmd.init(new Repository(gitdir));
137 try {
138 cmd.execute(arguments.toArray(new String[arguments.size()]));
139 } finally {
140 if (cmd.out != null)
141 cmd.out.flush();
145 private static File findGitDir() {
146 File current = new File(".").getAbsoluteFile();
147 while (current != null) {
148 final File gitDir = new File(current, ".git");
149 if (gitDir.isDirectory())
150 return gitDir;
151 current = current.getParentFile();
153 return null;