Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Main.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.io.PrintWriter;
  47. import java.lang.reflect.InvocationTargetException;
  48. import java.net.MalformedURLException;
  49. import java.net.URL;
  50. import java.text.MessageFormat;
  51. import java.util.ArrayList;
  52. import java.util.List;
  53. import org.eclipse.jgit.awtui.AwtAuthenticator;
  54. import org.eclipse.jgit.awtui.AwtSshSessionFactory;
  55. import org.eclipse.jgit.errors.TransportException;
  56. import org.eclipse.jgit.lib.RepositoryBuilder;
  57. import org.eclipse.jgit.pgm.opt.CmdLineParser;
  58. import org.eclipse.jgit.pgm.opt.SubcommandHandler;
  59. import org.eclipse.jgit.util.CachedAuthenticator;
  60. import org.kohsuke.args4j.Argument;
  61. import org.kohsuke.args4j.CmdLineException;
  62. import org.kohsuke.args4j.ExampleMode;
  63. import org.kohsuke.args4j.Option;
  64. /** Command line entry point. */
  65. public class Main {
  66. @Option(name = "--help", usage = "usage_displayThisHelpText", aliases = { "-h" })
  67. private boolean help;
  68. @Option(name = "--show-stack-trace", usage = "usage_displayThejavaStackTraceOnExceptions")
  69. private boolean showStackTrace;
  70. @Option(name = "--git-dir", metaVar = "metaVar_gitDir", usage = "usage_setTheGitRepositoryToOperateOn")
  71. private File gitdir;
  72. @Argument(index = 0, metaVar = "metaVar_command", required = true, handler = SubcommandHandler.class)
  73. private TextBuiltin subcommand;
  74. @Argument(index = 1, metaVar = "metaVar_arg")
  75. private List<String> arguments = new ArrayList<String>();
  76. /**
  77. * Execute the command line.
  78. *
  79. * @param argv
  80. * arguments.
  81. */
  82. public static void main(final String[] argv) {
  83. final Main me = new Main();
  84. try {
  85. if (!installConsole()) {
  86. AwtAuthenticator.install();
  87. AwtSshSessionFactory.install();
  88. }
  89. configureHttpProxy();
  90. me.execute(argv);
  91. } catch (Die err) {
  92. System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage()));
  93. if (me.showStackTrace)
  94. err.printStackTrace();
  95. System.exit(128);
  96. } catch (Exception err) {
  97. if (!me.showStackTrace && err.getCause() != null
  98. && err instanceof TransportException)
  99. System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getCause().getMessage()));
  100. if (err.getClass().getName().startsWith("org.eclipse.jgit.errors.")) {
  101. System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage()));
  102. if (me.showStackTrace)
  103. err.printStackTrace();
  104. System.exit(128);
  105. }
  106. err.printStackTrace();
  107. System.exit(1);
  108. }
  109. }
  110. private void execute(final String[] argv) throws Exception {
  111. final CmdLineParser clp = new CmdLineParser(this);
  112. PrintWriter writer = new PrintWriter(System.err);
  113. try {
  114. clp.parseArgument(argv);
  115. } catch (CmdLineException err) {
  116. if (argv.length > 0 && !help) {
  117. writer.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage()));
  118. writer.flush();
  119. System.exit(1);
  120. }
  121. }
  122. if (argv.length == 0 || help) {
  123. final String ex = clp.printExample(ExampleMode.ALL, CLIText.get().resourceBundle());
  124. writer.println("jgit" + ex + " command [ARG ...]");
  125. if (help) {
  126. writer.println();
  127. clp.printUsage(writer, CLIText.get().resourceBundle());
  128. writer.println();
  129. } else if (subcommand == null) {
  130. writer.println();
  131. writer.println(CLIText.get().mostCommonlyUsedCommandsAre);
  132. final CommandRef[] common = CommandCatalog.common();
  133. int width = 0;
  134. for (final CommandRef c : common)
  135. width = Math.max(width, c.getName().length());
  136. width += 2;
  137. for (final CommandRef c : common) {
  138. writer.print(' ');
  139. writer.print(c.getName());
  140. for (int i = c.getName().length(); i < width; i++)
  141. writer.print(' ');
  142. writer.print(CLIText.get().resourceBundle().getString(c.getUsage()));
  143. writer.println();
  144. }
  145. writer.println();
  146. }
  147. writer.flush();
  148. System.exit(1);
  149. }
  150. final TextBuiltin cmd = subcommand;
  151. if (cmd.requiresRepository()) {
  152. RepositoryBuilder rb = new RepositoryBuilder() //
  153. .setGitDir(gitdir) //
  154. .readEnvironment() //
  155. .findGitDir();
  156. if (rb.getGitDir() == null) {
  157. writer.println(CLIText.get().cantFindGitDirectory);
  158. writer.flush();
  159. System.exit(1);
  160. }
  161. cmd.init(rb.build(), null);
  162. } else {
  163. cmd.init(null, gitdir);
  164. }
  165. try {
  166. cmd.execute(arguments.toArray(new String[arguments.size()]));
  167. } finally {
  168. if (cmd.out != null)
  169. cmd.out.flush();
  170. }
  171. }
  172. private static boolean installConsole() {
  173. try {
  174. install("org.eclipse.jgit.console.ConsoleAuthenticator");
  175. install("org.eclipse.jgit.console.ConsoleSshSessionFactory");
  176. return true;
  177. } catch (ClassNotFoundException e) {
  178. return false;
  179. } catch (NoClassDefFoundError e) {
  180. return false;
  181. } catch (UnsupportedClassVersionError e) {
  182. return false;
  183. } catch (IllegalArgumentException e) {
  184. throw new RuntimeException(CLIText.get().cannotSetupConsole, e);
  185. } catch (SecurityException e) {
  186. throw new RuntimeException(CLIText.get().cannotSetupConsole, e);
  187. } catch (IllegalAccessException e) {
  188. throw new RuntimeException(CLIText.get().cannotSetupConsole, e);
  189. } catch (InvocationTargetException e) {
  190. throw new RuntimeException(CLIText.get().cannotSetupConsole, e);
  191. } catch (NoSuchMethodException e) {
  192. throw new RuntimeException(CLIText.get().cannotSetupConsole, e);
  193. }
  194. }
  195. private static void install(final String name)
  196. throws IllegalAccessException, InvocationTargetException,
  197. NoSuchMethodException, ClassNotFoundException {
  198. try {
  199. Class.forName(name).getMethod("install").invoke(null);
  200. } catch (InvocationTargetException e) {
  201. if (e.getCause() instanceof RuntimeException)
  202. throw (RuntimeException) e.getCause();
  203. if (e.getCause() instanceof Error)
  204. throw (Error) e.getCause();
  205. throw e;
  206. }
  207. }
  208. /**
  209. * Configure the JRE's standard HTTP based on <code>http_proxy</code>.
  210. * <p>
  211. * The popular libcurl library honors the <code>http_proxy</code>
  212. * environment variable as a means of specifying an HTTP proxy for requests
  213. * made behind a firewall. This is not natively recognized by the JRE, so
  214. * this method can be used by command line utilities to configure the JRE
  215. * before the first request is sent.
  216. *
  217. * @throws MalformedURLException
  218. * the value in <code>http_proxy</code> is unsupportable.
  219. */
  220. private static void configureHttpProxy() throws MalformedURLException {
  221. final String s = System.getenv("http_proxy");
  222. if (s == null || s.equals(""))
  223. return;
  224. final URL u = new URL((s.indexOf("://") == -1) ? "http://" + s : s);
  225. if (!"http".equals(u.getProtocol()))
  226. throw new MalformedURLException(MessageFormat.format(CLIText.get().invalidHttpProxyOnlyHttpSupported, s));
  227. final String proxyHost = u.getHost();
  228. final int proxyPort = u.getPort();
  229. System.setProperty("http.proxyHost", proxyHost);
  230. if (proxyPort > 0)
  231. System.setProperty("http.proxyPort", String.valueOf(proxyPort));
  232. final String userpass = u.getUserInfo();
  233. if (userpass != null && userpass.contains(":")) {
  234. final int c = userpass.indexOf(':');
  235. final String user = userpass.substring(0, c);
  236. final String pass = userpass.substring(c + 1);
  237. CachedAuthenticator
  238. .add(new CachedAuthenticator.CachedAuthentication(
  239. proxyHost, proxyPort, user, pass));
  240. }
  241. }
  242. }