2 * Copyright (C) 2006, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
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
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
;
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. */
58 @Option(name
= "--help", usage
= "display this help text", aliases
= { "-h" })
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")
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
>();
74 * Execute the command line.
79 public static void main(final String
[] argv
) {
80 final Main me
= new Main();
82 AwtAuthenticator
.install();
83 HttpSupport
.configureHttpProxy();
86 System
.err
.println("fatal: " + err
.getMessage());
87 if (me
.showStackTrace
)
88 err
.printStackTrace();
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();
101 err
.printStackTrace();
106 private void execute(final String
[] argv
) throws Exception
{
107 final CmdLineParser clp
= new CmdLineParser(this);
109 clp
.parseArgument(argv
);
110 } catch (CmdLineException err
) {
111 if (argv
.length
> 0 && !help
) {
112 System
.err
.println("fatal: " + err
.getMessage());
117 if (argv
.length
== 0 || help
) {
118 final String ex
= clp
.printExample(ExampleMode
.ALL
);
119 System
.err
.println("jgit" + ex
+ " command [ARG ...]");
121 System
.err
.println();
122 clp
.printUsage(System
.err
);
123 System
.err
.println();
129 gitdir
= findGitDir();
130 if (gitdir
== null || !gitdir
.isDirectory()) {
131 System
.err
.println("error: can't find git directory");
135 final TextBuiltin cmd
= subcommand
;
136 cmd
.init(new Repository(gitdir
));
138 cmd
.execute(arguments
.toArray(new String
[arguments
.size()]));
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())
151 current
= current
.getParentFile();