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
.net
.MalformedURLException
;
44 import java
.util
.Arrays
;
46 import org
.spearce
.jgit
.awtui
.AwtAuthenticator
;
47 import org
.spearce
.jgit
.errors
.TransportException
;
48 import org
.spearce
.jgit
.lib
.Repository
;
50 /** Command line entry point. */
52 private static boolean showStackTrace
;
55 * Execute the command line.
60 public static void main(final String
[] argv
) {
62 AwtAuthenticator
.install();
66 System
.err
.println("fatal: " + err
.getMessage());
68 err
.printStackTrace();
70 } catch (Exception err
) {
71 if (!showStackTrace
&& err
.getCause() != null
72 && err
instanceof TransportException
)
73 System
.err
.println("fatal: " + err
.getCause().getMessage());
75 if (err
.getClass().getName().startsWith("org.spearce.jgit.errors.")) {
76 System
.err
.println("fatal: " + err
.getMessage());
78 err
.printStackTrace();
81 err
.printStackTrace();
86 private static void execute(final String
[] argv
) throws Exception
{
88 String gitdir
= ".git";
90 for (; argi
< argv
.length
; argi
++) {
91 final String arg
= argv
[argi
];
92 if (arg
.startsWith("--git-dir="))
93 gitdir
= arg
.substring("--git-dir=".length());
94 else if (arg
.equals("--show-stack-trace"))
95 showStackTrace
= true;
96 else if (arg
.startsWith("--"))
102 if (argi
== argv
.length
)
104 final TextBuiltin cmd
= createCommand(argv
[argi
++]);
105 cmd
.db
= new Repository(new File(gitdir
));
107 cmd
.execute(subarray(argv
, argi
));
114 private static String
[] subarray(final String
[] argv
, final int i
) {
115 return Arrays
.asList(argv
).subList(i
, argv
.length
).toArray(
119 private static TextBuiltin
createCommand(final String name
) {
120 final StringBuilder s
= new StringBuilder();
121 s
.append(mypackage());
123 boolean upnext
= true;
124 for (int i
= 0; i
< name
.length(); i
++) {
125 final char c
= name
.charAt(i
);
131 s
.append(Character
.toUpperCase(c
));
137 return (TextBuiltin
) Class
.forName(s
.toString()).newInstance();
138 } catch (Exception e
) {
139 System
.err
.println("error: " + name
+ " is not a jgit command.");
145 private static String
mypackage() {
146 final String p
= Main
.class.getName();
147 final int dot
= p
.lastIndexOf('.');
148 return p
.substring(0, dot
);
151 private static void usage() {
152 System
.err
.println("jgit [--git-dir=path] cmd ...");
156 private static void configureHttpProxy() {
157 final String s
= System
.getenv("http_proxy");
158 if (s
== null || s
.equals(""))
164 } catch (MalformedURLException e
) {
165 throw new Die("Invalid http_proxy: " + s
+ ": " + e
.getMessage());
167 if (!"http".equals(u
.getProtocol()))
168 throw new Die("Invalid http_proxy: " + s
+ ": Only http supported.");
170 final String proxyHost
= u
.getHost();
171 final int proxyPort
= u
.getPort();
173 System
.setProperty("http.proxyHost", proxyHost
);
175 System
.setProperty("http.proxyPort", String
.valueOf(proxyPort
));
177 final String userpass
= u
.getUserInfo();
178 if (userpass
!= null && userpass
.contains(":")) {
179 final int c
= userpass
.indexOf(':');
180 final String user
= userpass
.substring(0, c
);
181 final String pass
= userpass
.substring(c
+ 1);
182 AwtAuthenticator
.add(new AwtAuthenticator
.CachedAuthentication(
183 proxyHost
, proxyPort
, user
, pass
));