You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Main.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C) 2006, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.pgm;
  45. import java.io.File;
  46. import java.net.MalformedURLException;
  47. import java.net.URL;
  48. import java.util.ArrayList;
  49. import java.util.List;
  50. import org.eclipse.jgit.awtui.AwtAuthenticator;
  51. import org.eclipse.jgit.errors.TransportException;
  52. import org.eclipse.jgit.lib.Repository;
  53. import org.eclipse.jgit.pgm.opt.CmdLineParser;
  54. import org.eclipse.jgit.pgm.opt.SubcommandHandler;
  55. import org.kohsuke.args4j.Argument;
  56. import org.kohsuke.args4j.CmdLineException;
  57. import org.kohsuke.args4j.ExampleMode;
  58. import org.kohsuke.args4j.Option;
  59. /** Command line entry point. */
  60. public class Main {
  61. @Option(name = "--help", usage = "display this help text", aliases = { "-h" })
  62. private boolean help;
  63. @Option(name = "--show-stack-trace", usage = "display the Java stack trace on exceptions")
  64. private boolean showStackTrace;
  65. @Option(name = "--git-dir", metaVar = "GIT_DIR", usage = "set the git repository to operate on")
  66. private File gitdir;
  67. @Argument(index = 0, metaVar = "command", required = true, handler = SubcommandHandler.class)
  68. private TextBuiltin subcommand;
  69. @Argument(index = 1, metaVar = "ARG")
  70. private List<String> arguments = new ArrayList<String>();
  71. /**
  72. * Execute the command line.
  73. *
  74. * @param argv
  75. * arguments.
  76. */
  77. public static void main(final String[] argv) {
  78. final Main me = new Main();
  79. try {
  80. AwtAuthenticator.install();
  81. configureHttpProxy();
  82. me.execute(argv);
  83. } catch (Die err) {
  84. System.err.println("fatal: " + err.getMessage());
  85. if (me.showStackTrace)
  86. err.printStackTrace();
  87. System.exit(128);
  88. } catch (Exception err) {
  89. if (!me.showStackTrace && err.getCause() != null
  90. && err instanceof TransportException)
  91. System.err.println("fatal: " + err.getCause().getMessage());
  92. if (err.getClass().getName().startsWith("org.eclipse.jgit.errors.")) {
  93. System.err.println("fatal: " + err.getMessage());
  94. if (me.showStackTrace)
  95. err.printStackTrace();
  96. System.exit(128);
  97. }
  98. err.printStackTrace();
  99. System.exit(1);
  100. }
  101. }
  102. private void execute(final String[] argv) throws Exception {
  103. final CmdLineParser clp = new CmdLineParser(this);
  104. try {
  105. clp.parseArgument(argv);
  106. } catch (CmdLineException err) {
  107. if (argv.length > 0 && !help) {
  108. System.err.println("fatal: " + err.getMessage());
  109. System.exit(1);
  110. }
  111. }
  112. if (argv.length == 0 || help) {
  113. final String ex = clp.printExample(ExampleMode.ALL);
  114. System.err.println("jgit" + ex + " command [ARG ...]");
  115. if (help) {
  116. System.err.println();
  117. clp.printUsage(System.err);
  118. System.err.println();
  119. } else if (subcommand == null) {
  120. System.err.println();
  121. System.err.println("The most commonly used commands are:");
  122. final CommandRef[] common = CommandCatalog.common();
  123. int width = 0;
  124. for (final CommandRef c : common)
  125. width = Math.max(width, c.getName().length());
  126. width += 2;
  127. for (final CommandRef c : common) {
  128. System.err.print(' ');
  129. System.err.print(c.getName());
  130. for (int i = c.getName().length(); i < width; i++)
  131. System.err.print(' ');
  132. System.err.print(c.getUsage());
  133. System.err.println();
  134. }
  135. System.err.println();
  136. }
  137. System.exit(1);
  138. }
  139. final TextBuiltin cmd = subcommand;
  140. if (cmd.requiresRepository()) {
  141. if (gitdir == null)
  142. gitdir = findGitDir();
  143. if (gitdir == null || !gitdir.isDirectory()) {
  144. System.err.println("error: can't find git directory");
  145. System.exit(1);
  146. }
  147. cmd.init(new Repository(gitdir), gitdir);
  148. } else {
  149. cmd.init(null, gitdir);
  150. }
  151. try {
  152. cmd.execute(arguments.toArray(new String[arguments.size()]));
  153. } finally {
  154. if (cmd.out != null)
  155. cmd.out.flush();
  156. }
  157. }
  158. private static File findGitDir() {
  159. File current = new File(".").getAbsoluteFile();
  160. while (current != null) {
  161. final File gitDir = new File(current, ".git");
  162. if (gitDir.isDirectory())
  163. return gitDir;
  164. current = current.getParentFile();
  165. }
  166. return null;
  167. }
  168. /**
  169. * Configure the JRE's standard HTTP based on <code>http_proxy</code>.
  170. * <p>
  171. * The popular libcurl library honors the <code>http_proxy</code>
  172. * environment variable as a means of specifying an HTTP proxy for requests
  173. * made behind a firewall. This is not natively recognized by the JRE, so
  174. * this method can be used by command line utilities to configure the JRE
  175. * before the first request is sent.
  176. *
  177. * @throws MalformedURLException
  178. * the value in <code>http_proxy</code> is unsupportable.
  179. */
  180. private static void configureHttpProxy() throws MalformedURLException {
  181. final String s = System.getenv("http_proxy");
  182. if (s == null || s.equals(""))
  183. return;
  184. final URL u = new URL((s.indexOf("://") == -1) ? "http://" + s : s);
  185. if (!"http".equals(u.getProtocol()))
  186. throw new MalformedURLException("Invalid http_proxy: " + s
  187. + ": Only http supported.");
  188. final String proxyHost = u.getHost();
  189. final int proxyPort = u.getPort();
  190. System.setProperty("http.proxyHost", proxyHost);
  191. if (proxyPort > 0)
  192. System.setProperty("http.proxyPort", String.valueOf(proxyPort));
  193. final String userpass = u.getUserInfo();
  194. if (userpass != null && userpass.contains(":")) {
  195. final int c = userpass.indexOf(':');
  196. final String user = userpass.substring(0, c);
  197. final String pass = userpass.substring(c + 1);
  198. AwtAuthenticator.add(new AwtAuthenticator.CachedAuthentication(
  199. proxyHost, proxyPort, user, pass));
  200. }
  201. }
  202. }