Switch jgit library to the EDL (3-clause BSD)
[jgit.git] / org.spearce.jgit / src / org / spearce / jgit / pgm / Main.java
blob44f8a428d8684959318d59d00f1b55686cc73695
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.net.MalformedURLException;
43 import java.net.URL;
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. */
51 public class Main {
52 private static boolean showStackTrace;
54 /**
55 * Execute the command line.
57 * @param argv
58 * arguments.
60 public static void main(final String[] argv) {
61 try {
62 AwtAuthenticator.install();
63 configureHttpProxy();
64 execute(argv);
65 } catch (Die err) {
66 System.err.println("fatal: " + err.getMessage());
67 if (showStackTrace)
68 err.printStackTrace();
69 System.exit(128);
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());
77 if (showStackTrace)
78 err.printStackTrace();
79 System.exit(128);
81 err.printStackTrace();
82 System.exit(1);
86 private static void execute(final String[] argv) throws Exception {
87 int argi = 0;
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("--"))
97 usage();
98 else
99 break;
102 if (argi == argv.length)
103 usage();
104 final TextBuiltin cmd = createCommand(argv[argi++]);
105 cmd.db = new Repository(new File(gitdir));
106 try {
107 cmd.execute(subarray(argv, argi));
108 } finally {
109 if (cmd.out != null)
110 cmd.out.flush();
114 private static String[] subarray(final String[] argv, final int i) {
115 return Arrays.asList(argv).subList(i, argv.length).toArray(
116 new String[0]);
119 private static TextBuiltin createCommand(final String name) {
120 final StringBuilder s = new StringBuilder();
121 s.append(mypackage());
122 s.append('.');
123 boolean upnext = true;
124 for (int i = 0; i < name.length(); i++) {
125 final char c = name.charAt(i);
126 if (c == '-') {
127 upnext = true;
128 continue;
130 if (upnext)
131 s.append(Character.toUpperCase(c));
132 else
133 s.append(c);
134 upnext = false;
136 try {
137 return (TextBuiltin) Class.forName(s.toString()).newInstance();
138 } catch (Exception e) {
139 System.err.println("error: " + name + " is not a jgit command.");
140 System.exit(1);
141 return null;
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 ...");
153 System.exit(1);
156 private static void configureHttpProxy() {
157 final String s = System.getenv("http_proxy");
158 if (s == null || s.equals(""))
159 return;
161 final URL u;
162 try {
163 u = new URL(s);
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);
174 if (proxyPort > 0)
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));