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.

Fetch.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2009, Mykola Nikishov <mn@mn.com.ua>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.pgm;
  46. import java.io.IOException;
  47. import java.text.MessageFormat;
  48. import java.util.List;
  49. import org.eclipse.jgit.api.FetchCommand;
  50. import org.eclipse.jgit.api.Git;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.lib.SubmoduleConfig.FetchRecurseSubmodulesMode;
  53. import org.eclipse.jgit.lib.TextProgressMonitor;
  54. import org.eclipse.jgit.pgm.internal.CLIText;
  55. import org.eclipse.jgit.transport.FetchResult;
  56. import org.eclipse.jgit.transport.RefSpec;
  57. import org.eclipse.jgit.transport.TagOpt;
  58. import org.kohsuke.args4j.Argument;
  59. import org.kohsuke.args4j.Option;
  60. @Command(common = true, usage = "usage_updateRemoteRefsFromAnotherRepository")
  61. class Fetch extends AbstractFetchCommand implements FetchCommand.Callback {
  62. @Option(name = "--timeout", metaVar = "metaVar_seconds", usage = "usage_abortConnectionIfNoActivity")
  63. int timeout = -1;
  64. @Option(name = "--fsck", usage = "usage_performFsckStyleChecksOnReceive")
  65. private Boolean fsck;
  66. @Option(name = "--no-fsck")
  67. void nofsck(@SuppressWarnings("unused") final boolean ignored) {
  68. fsck = Boolean.FALSE;
  69. }
  70. @Option(name = "--prune", usage = "usage_pruneStaleTrackingRefs")
  71. private Boolean prune;
  72. @Option(name = "--dry-run")
  73. private boolean dryRun;
  74. @Option(name = "--thin", usage = "usage_fetchThinPack")
  75. private Boolean thin;
  76. @Option(name = "--no-thin")
  77. void nothin(@SuppressWarnings("unused") final boolean ignored) {
  78. thin = Boolean.FALSE;
  79. }
  80. @Option(name = "--quiet", usage = "usage_quiet")
  81. private Boolean quiet;
  82. @Option(name = "--tags", usage="usage_tags", aliases = { "-t" })
  83. private Boolean tags;
  84. @Option(name = "--no-tags", usage = "usage_notags", aliases = { "-n" })
  85. void notags(@SuppressWarnings("unused")
  86. final boolean ignored) {
  87. tags = Boolean.FALSE;
  88. }
  89. @Option(name = "--force", usage = "usage_forcedFetch", aliases = { "-f" })
  90. private Boolean force;
  91. private FetchRecurseSubmodulesMode recurseSubmodules;
  92. @Option(name = "--recurse-submodules", usage = "usage_recurseSubmodules")
  93. void recurseSubmodules(String mode) {
  94. if (mode == null || mode.isEmpty()) {
  95. recurseSubmodules = FetchRecurseSubmodulesMode.YES;
  96. } else {
  97. for (FetchRecurseSubmodulesMode m : FetchRecurseSubmodulesMode
  98. .values()) {
  99. if (m.matchConfigValue(mode)) {
  100. recurseSubmodules = m;
  101. return;
  102. }
  103. }
  104. throw die(MessageFormat
  105. .format(CLIText.get().invalidRecurseSubmodulesMode, mode));
  106. }
  107. }
  108. @Option(name = "--no-recurse-submodules", usage = "usage_noRecurseSubmodules")
  109. void noRecurseSubmodules(@SuppressWarnings("unused")
  110. final boolean ignored) {
  111. recurseSubmodules = FetchRecurseSubmodulesMode.NO;
  112. }
  113. @Argument(index = 0, metaVar = "metaVar_uriish")
  114. private String remote = Constants.DEFAULT_REMOTE_NAME;
  115. @Argument(index = 1, metaVar = "metaVar_refspec")
  116. private List<RefSpec> toget;
  117. /** {@inheritDoc} */
  118. @Override
  119. protected void run() throws Exception {
  120. try (Git git = new Git(db)) {
  121. FetchCommand fetch = git.fetch();
  122. if (fsck != null)
  123. fetch.setCheckFetchedObjects(fsck.booleanValue());
  124. if (prune != null)
  125. fetch.setRemoveDeletedRefs(prune.booleanValue());
  126. if (toget != null)
  127. fetch.setRefSpecs(toget);
  128. if (tags != null) {
  129. fetch.setTagOpt(tags.booleanValue() ? TagOpt.FETCH_TAGS
  130. : TagOpt.NO_TAGS);
  131. }
  132. if (0 <= timeout)
  133. fetch.setTimeout(timeout);
  134. fetch.setDryRun(dryRun);
  135. fetch.setRemote(remote);
  136. if (thin != null)
  137. fetch.setThin(thin.booleanValue());
  138. if (quiet == null || !quiet.booleanValue())
  139. fetch.setProgressMonitor(new TextProgressMonitor(errw));
  140. fetch.setRecurseSubmodules(recurseSubmodules).setCallback(this);
  141. if (force != null) {
  142. fetch.setForceUpdate(force.booleanValue());
  143. }
  144. FetchResult result = fetch.call();
  145. if (result.getTrackingRefUpdates().isEmpty()
  146. && result.submoduleResults().isEmpty())
  147. return;
  148. showFetchResult(result);
  149. }
  150. }
  151. /** {@inheritDoc} */
  152. @Override
  153. public void fetchingSubmodule(String name) {
  154. try {
  155. outw.println(MessageFormat.format(CLIText.get().fetchingSubmodule,
  156. name));
  157. outw.flush();
  158. } catch (IOException e) {
  159. // ignore
  160. }
  161. }
  162. }