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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.pgm;
  11. import java.io.IOException;
  12. import java.io.StringWriter;
  13. import java.text.MessageFormat;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import org.eclipse.jgit.api.Git;
  17. import org.eclipse.jgit.api.RemoteAddCommand;
  18. import org.eclipse.jgit.api.RemoteListCommand;
  19. import org.eclipse.jgit.api.RemoteRemoveCommand;
  20. import org.eclipse.jgit.api.RemoteSetUrlCommand;
  21. import org.eclipse.jgit.api.RemoteSetUrlCommand.UriType;
  22. import org.eclipse.jgit.api.errors.JGitInternalException;
  23. import org.eclipse.jgit.pgm.internal.CLIText;
  24. import org.eclipse.jgit.pgm.opt.CmdLineParser;
  25. import org.eclipse.jgit.transport.RemoteConfig;
  26. import org.eclipse.jgit.transport.URIish;
  27. import org.eclipse.jgit.util.io.ThrowingPrintWriter;
  28. import org.kohsuke.args4j.Argument;
  29. import org.kohsuke.args4j.Option;
  30. @Command(common = false, usage = "usage_Remote")
  31. class Remote extends TextBuiltin {
  32. @Option(name = "--verbose", aliases = { "-v" }, usage = "usage_beVerbose")
  33. private boolean verbose = false;
  34. @Option(name = "--prune", aliases = {
  35. "-p" }, usage = "usage_pruneStaleTrackingRefs")
  36. private boolean prune;
  37. @Option(name = "--push", usage = "usage_pushUrls")
  38. private boolean push;
  39. @Argument(index = 0, metaVar = "metaVar_command")
  40. private String command;
  41. @Argument(index = 1, metaVar = "metaVar_remoteName")
  42. private String name;
  43. @Argument(index = 2, metaVar = "metaVar_uriish")
  44. private String uri;
  45. /** {@inheritDoc} */
  46. @Override
  47. protected void run() {
  48. try (Git git = new Git(db)) {
  49. if (command == null) {
  50. RemoteListCommand cmd = git.remoteList();
  51. List<RemoteConfig> remotes = cmd.call();
  52. print(remotes);
  53. return;
  54. }
  55. switch (command) {
  56. case "add": //$NON-NLS-1$
  57. RemoteAddCommand add = git.remoteAdd();
  58. add.setName(name);
  59. add.setUri(new URIish(uri));
  60. add.call();
  61. break;
  62. case "remove": //$NON-NLS-1$
  63. case "rm": //$NON-NLS-1$
  64. RemoteRemoveCommand rm = git.remoteRemove();
  65. rm.setRemoteName(name);
  66. rm.call();
  67. break;
  68. case "set-url": //$NON-NLS-1$
  69. RemoteSetUrlCommand remoteSetUrl = git.remoteSetUrl();
  70. remoteSetUrl.setRemoteName(name);
  71. remoteSetUrl.setRemoteUri(new URIish(uri));
  72. remoteSetUrl.setUriType(push ? UriType.PUSH : UriType.FETCH);
  73. remoteSetUrl.call();
  74. break;
  75. case "update": //$NON-NLS-1$
  76. Fetch fetch = new Fetch();
  77. fetch.init(db, gitdir);
  78. StringWriter osw = new StringWriter();
  79. fetch.outw = new ThrowingPrintWriter(osw);
  80. StringWriter esw = new StringWriter();
  81. fetch.errw = new ThrowingPrintWriter(esw);
  82. List<String> fetchArgs = new ArrayList<>();
  83. if (verbose) {
  84. fetchArgs.add("--verbose"); //$NON-NLS-1$
  85. }
  86. if (prune) {
  87. fetchArgs.add("--prune"); //$NON-NLS-1$
  88. }
  89. if (name != null) {
  90. fetchArgs.add(name);
  91. }
  92. fetch.execute(fetchArgs.toArray(new String[0]));
  93. fetch.outw.flush();
  94. fetch.errw.flush();
  95. outw.println(osw.toString());
  96. errw.println(esw.toString());
  97. break;
  98. default:
  99. throw new JGitInternalException(MessageFormat
  100. .format(CLIText.get().unknownSubcommand, command));
  101. }
  102. } catch (Exception e) {
  103. throw die(e.getMessage(), e);
  104. }
  105. }
  106. /** {@inheritDoc} */
  107. @Override
  108. public void printUsage(String message, CmdLineParser clp)
  109. throws IOException {
  110. errw.println(message);
  111. errw.println("jgit remote [--verbose (-v)] [--help (-h)]"); //$NON-NLS-1$
  112. errw.println("jgit remote add name uri-ish [--help (-h)]"); //$NON-NLS-1$
  113. errw.println("jgit remote remove name [--help (-h)]"); //$NON-NLS-1$
  114. errw.println("jgit remote rm name [--help (-h)]"); //$NON-NLS-1$
  115. errw.println(
  116. "jgit remote [--verbose (-v)] update [name] [--prune (-p)] [--help (-h)]"); //$NON-NLS-1$
  117. errw.println("jgit remote set-url name uri-ish [--push] [--help (-h)]"); //$NON-NLS-1$
  118. errw.println();
  119. clp.printUsage(errw, getResourceBundle());
  120. errw.println();
  121. errw.flush();
  122. }
  123. private void print(List<RemoteConfig> remotes) throws IOException {
  124. for (RemoteConfig remote : remotes) {
  125. String remoteName = remote.getName();
  126. if (verbose) {
  127. List<URIish> fetchURIs = remote.getURIs();
  128. List<URIish> pushURIs = remote.getPushURIs();
  129. String fetchURI = ""; //$NON-NLS-1$
  130. if (!fetchURIs.isEmpty()) {
  131. fetchURI = fetchURIs.get(0).toString();
  132. } else if (!pushURIs.isEmpty()) {
  133. fetchURI = pushURIs.get(0).toString();
  134. }
  135. String pushURI = ""; //$NON-NLS-1$
  136. if (!pushURIs.isEmpty()) {
  137. pushURI = pushURIs.get(0).toString();
  138. } else if (!fetchURIs.isEmpty()) {
  139. pushURI = fetchURIs.get(0).toString();
  140. }
  141. outw.println(
  142. String.format("%s\t%s (fetch)", remoteName, fetchURI)); //$NON-NLS-1$
  143. outw.println(
  144. String.format("%s\t%s (push)", remoteName, pushURI)); //$NON-NLS-1$
  145. } else {
  146. outw.println(remoteName);
  147. }
  148. }
  149. }
  150. }