Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Remote.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.pgm;
  44. import java.io.IOException;
  45. import java.io.StringWriter;
  46. import java.text.MessageFormat;
  47. import java.util.ArrayList;
  48. import java.util.List;
  49. import org.eclipse.jgit.api.Git;
  50. import org.eclipse.jgit.api.RemoteAddCommand;
  51. import org.eclipse.jgit.api.RemoteListCommand;
  52. import org.eclipse.jgit.api.RemoteRemoveCommand;
  53. import org.eclipse.jgit.api.RemoteSetUrlCommand;
  54. import org.eclipse.jgit.api.errors.JGitInternalException;
  55. import org.eclipse.jgit.pgm.internal.CLIText;
  56. import org.eclipse.jgit.pgm.opt.CmdLineParser;
  57. import org.eclipse.jgit.transport.RemoteConfig;
  58. import org.eclipse.jgit.transport.URIish;
  59. import org.eclipse.jgit.util.io.ThrowingPrintWriter;
  60. import org.kohsuke.args4j.Argument;
  61. import org.kohsuke.args4j.Option;
  62. @Command(common = false, usage = "usage_Remote")
  63. class Remote extends TextBuiltin {
  64. @Option(name = "--verbose", aliases = { "-v" }, usage = "usage_beVerbose")
  65. private boolean verbose = false;
  66. @Option(name = "--prune", aliases = {
  67. "-p" }, usage = "usage_pruneStaleTrackingRefs")
  68. private boolean prune;
  69. @Option(name = "--push", usage = "usage_pushUrls")
  70. private boolean push;
  71. @Argument(index = 0, metaVar = "metaVar_command")
  72. private String command;
  73. @Argument(index = 1, metaVar = "metaVar_remoteName")
  74. private String name;
  75. @Argument(index = 2, metaVar = "metaVar_uriish")
  76. private String uri;
  77. @Override
  78. protected void run() throws Exception {
  79. try (Git git = new Git(db)) {
  80. if (command == null) {
  81. RemoteListCommand cmd = git.remoteList();
  82. List<RemoteConfig> remotes = cmd.call();
  83. print(remotes);
  84. } else if ("add".equals(command)) { //$NON-NLS-1$
  85. RemoteAddCommand cmd = git.remoteAdd();
  86. cmd.setName(name);
  87. cmd.setUri(new URIish(uri));
  88. cmd.call();
  89. } else if ("remove".equals(command) || "rm".equals(command)) { //$NON-NLS-1$ //$NON-NLS-2$
  90. RemoteRemoveCommand cmd = git.remoteRemove();
  91. cmd.setName(name);
  92. cmd.call();
  93. } else if ("set-url".equals(command)) { //$NON-NLS-1$
  94. RemoteSetUrlCommand cmd = git.remoteSetUrl();
  95. cmd.setName(name);
  96. cmd.setUri(new URIish(uri));
  97. cmd.setPush(push);
  98. cmd.call();
  99. } else if ("update".equals(command)) { //$NON-NLS-1$
  100. // reuse fetch command for basic implementation of remote update
  101. Fetch fetch = new Fetch();
  102. fetch.init(db, gitdir);
  103. // redirect the output stream
  104. StringWriter osw = new StringWriter();
  105. fetch.outw = new ThrowingPrintWriter(osw);
  106. // redirect the error stream
  107. StringWriter esw = new StringWriter();
  108. fetch.errw = new ThrowingPrintWriter(esw);
  109. List<String> fetchArgs = new ArrayList<>();
  110. if (verbose) {
  111. fetchArgs.add("--verbose"); //$NON-NLS-1$
  112. }
  113. if (prune) {
  114. fetchArgs.add("--prune"); //$NON-NLS-1$
  115. }
  116. if (name != null) {
  117. fetchArgs.add(name);
  118. }
  119. fetch.execute(fetchArgs.toArray(new String[fetchArgs.size()]));
  120. // flush the streams
  121. fetch.outw.flush();
  122. fetch.errw.flush();
  123. outw.println(osw.toString());
  124. errw.println(esw.toString());
  125. } else {
  126. throw new JGitInternalException(MessageFormat
  127. .format(CLIText.get().unknownSubcommand, command));
  128. }
  129. }
  130. }
  131. @Override
  132. public void printUsageAndExit(final String message, final CmdLineParser clp)
  133. throws IOException {
  134. errw.println(message);
  135. errw.println("jgit remote [--verbose (-v)] [--help (-h)]"); //$NON-NLS-1$
  136. errw.println("jgit remote add name uri-ish [--help (-h)]"); //$NON-NLS-1$
  137. errw.println("jgit remote remove name [--help (-h)]"); //$NON-NLS-1$
  138. errw.println("jgit remote rm name [--help (-h)]"); //$NON-NLS-1$
  139. errw.println(
  140. "jgit remote [--verbose (-v)] update [name] [--prune (-p)] [--help (-h)]"); //$NON-NLS-1$
  141. errw.println("jgit remote set-url name uri-ish [--push] [--help (-h)]"); //$NON-NLS-1$
  142. errw.println();
  143. clp.printUsage(errw, getResourceBundle());
  144. errw.println();
  145. errw.flush();
  146. throw die(true);
  147. }
  148. private void print(List<RemoteConfig> remotes) throws IOException {
  149. for (RemoteConfig remote : remotes) {
  150. String remoteName = remote.getName();
  151. if (verbose) {
  152. List<URIish> fetchURIs = remote.getURIs();
  153. List<URIish> pushURIs = remote.getPushURIs();
  154. String fetchURI = ""; //$NON-NLS-1$
  155. if (!fetchURIs.isEmpty()) {
  156. fetchURI = fetchURIs.get(0).toString();
  157. } else if (!pushURIs.isEmpty()) {
  158. fetchURI = pushURIs.get(0).toString();
  159. }
  160. String pushURI = ""; //$NON-NLS-1$
  161. if (!pushURIs.isEmpty()) {
  162. pushURI = pushURIs.get(0).toString();
  163. } else if (!fetchURIs.isEmpty()) {
  164. pushURI = fetchURIs.get(0).toString();
  165. }
  166. outw.println(
  167. String.format("%s\t%s (fetch)", remoteName, fetchURI)); //$NON-NLS-1$
  168. outw.println(
  169. String.format("%s\t%s (push)", remoteName, pushURI)); //$NON-NLS-1$
  170. } else {
  171. outw.println(remoteName);
  172. }
  173. }
  174. }
  175. }