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.

Remote.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.RemoteSetUrlCommand.UriType;
  55. import org.eclipse.jgit.api.errors.JGitInternalException;
  56. import org.eclipse.jgit.pgm.internal.CLIText;
  57. import org.eclipse.jgit.pgm.opt.CmdLineParser;
  58. import org.eclipse.jgit.transport.RemoteConfig;
  59. import org.eclipse.jgit.transport.URIish;
  60. import org.eclipse.jgit.util.io.ThrowingPrintWriter;
  61. import org.kohsuke.args4j.Argument;
  62. import org.kohsuke.args4j.Option;
  63. @Command(common = false, usage = "usage_Remote")
  64. class Remote extends TextBuiltin {
  65. @Option(name = "--verbose", aliases = { "-v" }, usage = "usage_beVerbose")
  66. private boolean verbose = false;
  67. @Option(name = "--prune", aliases = {
  68. "-p" }, usage = "usage_pruneStaleTrackingRefs")
  69. private boolean prune;
  70. @Option(name = "--push", usage = "usage_pushUrls")
  71. private boolean push;
  72. @Argument(index = 0, metaVar = "metaVar_command")
  73. private String command;
  74. @Argument(index = 1, metaVar = "metaVar_remoteName")
  75. private String name;
  76. @Argument(index = 2, metaVar = "metaVar_uriish")
  77. private String uri;
  78. /** {@inheritDoc} */
  79. @Override
  80. protected void run() {
  81. try (Git git = new Git(db)) {
  82. if (command == null) {
  83. RemoteListCommand cmd = git.remoteList();
  84. List<RemoteConfig> remotes = cmd.call();
  85. print(remotes);
  86. } else if ("add".equals(command)) { //$NON-NLS-1$
  87. RemoteAddCommand cmd = git.remoteAdd();
  88. cmd.setName(name);
  89. cmd.setUri(new URIish(uri));
  90. cmd.call();
  91. } else if ("remove".equals(command) || "rm".equals(command)) { //$NON-NLS-1$ //$NON-NLS-2$
  92. RemoteRemoveCommand cmd = git.remoteRemove();
  93. cmd.setRemoteName(name);
  94. cmd.call();
  95. } else if ("set-url".equals(command)) { //$NON-NLS-1$
  96. RemoteSetUrlCommand cmd = git.remoteSetUrl();
  97. cmd.setRemoteName(name);
  98. cmd.setRemoteUri(new URIish(uri));
  99. cmd.setUriType(push ? UriType.PUSH : UriType.FETCH);
  100. cmd.call();
  101. } else if ("update".equals(command)) { //$NON-NLS-1$
  102. // reuse fetch command for basic implementation of remote update
  103. Fetch fetch = new Fetch();
  104. fetch.init(db, gitdir);
  105. // redirect the output stream
  106. StringWriter osw = new StringWriter();
  107. fetch.outw = new ThrowingPrintWriter(osw);
  108. // redirect the error stream
  109. StringWriter esw = new StringWriter();
  110. fetch.errw = new ThrowingPrintWriter(esw);
  111. List<String> fetchArgs = new ArrayList<>();
  112. if (verbose) {
  113. fetchArgs.add("--verbose"); //$NON-NLS-1$
  114. }
  115. if (prune) {
  116. fetchArgs.add("--prune"); //$NON-NLS-1$
  117. }
  118. if (name != null) {
  119. fetchArgs.add(name);
  120. }
  121. fetch.execute(fetchArgs.toArray(new String[0]));
  122. // flush the streams
  123. fetch.outw.flush();
  124. fetch.errw.flush();
  125. outw.println(osw.toString());
  126. errw.println(esw.toString());
  127. } else {
  128. throw new JGitInternalException(MessageFormat
  129. .format(CLIText.get().unknownSubcommand, command));
  130. }
  131. } catch (Exception e) {
  132. throw die(e.getMessage(), e);
  133. }
  134. }
  135. /** {@inheritDoc} */
  136. @Override
  137. public void printUsage(String message, CmdLineParser clp)
  138. throws IOException {
  139. errw.println(message);
  140. errw.println("jgit remote [--verbose (-v)] [--help (-h)]"); //$NON-NLS-1$
  141. errw.println("jgit remote add name uri-ish [--help (-h)]"); //$NON-NLS-1$
  142. errw.println("jgit remote remove name [--help (-h)]"); //$NON-NLS-1$
  143. errw.println("jgit remote rm name [--help (-h)]"); //$NON-NLS-1$
  144. errw.println(
  145. "jgit remote [--verbose (-v)] update [name] [--prune (-p)] [--help (-h)]"); //$NON-NLS-1$
  146. errw.println("jgit remote set-url name uri-ish [--push] [--help (-h)]"); //$NON-NLS-1$
  147. errw.println();
  148. clp.printUsage(errw, getResourceBundle());
  149. errw.println();
  150. errw.flush();
  151. }
  152. private void print(List<RemoteConfig> remotes) throws IOException {
  153. for (RemoteConfig remote : remotes) {
  154. String remoteName = remote.getName();
  155. if (verbose) {
  156. List<URIish> fetchURIs = remote.getURIs();
  157. List<URIish> pushURIs = remote.getPushURIs();
  158. String fetchURI = ""; //$NON-NLS-1$
  159. if (!fetchURIs.isEmpty()) {
  160. fetchURI = fetchURIs.get(0).toString();
  161. } else if (!pushURIs.isEmpty()) {
  162. fetchURI = pushURIs.get(0).toString();
  163. }
  164. String pushURI = ""; //$NON-NLS-1$
  165. if (!pushURIs.isEmpty()) {
  166. pushURI = pushURIs.get(0).toString();
  167. } else if (!fetchURIs.isEmpty()) {
  168. pushURI = fetchURIs.get(0).toString();
  169. }
  170. outw.println(
  171. String.format("%s\t%s (fetch)", remoteName, fetchURI)); //$NON-NLS-1$
  172. outw.println(
  173. String.format("%s\t%s (push)", remoteName, pushURI)); //$NON-NLS-1$
  174. } else {
  175. outw.println(remoteName);
  176. }
  177. }
  178. }
  179. }