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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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> and others
  5. *
  6. * This program and the accompanying materials are made available under the
  7. * terms of the Eclipse Distribution License v. 1.0 which is available at
  8. * https://www.eclipse.org/org/documents/edl-v10.php.
  9. *
  10. * SPDX-License-Identifier: BSD-3-Clause
  11. */
  12. package org.eclipse.jgit.pgm;
  13. import java.io.IOException;
  14. import java.text.MessageFormat;
  15. import java.util.List;
  16. import org.eclipse.jgit.api.FetchCommand;
  17. import org.eclipse.jgit.api.Git;
  18. import org.eclipse.jgit.api.errors.GitAPIException;
  19. import org.eclipse.jgit.lib.Constants;
  20. import org.eclipse.jgit.lib.SubmoduleConfig.FetchRecurseSubmodulesMode;
  21. import org.eclipse.jgit.lib.TextProgressMonitor;
  22. import org.eclipse.jgit.pgm.internal.CLIText;
  23. import org.eclipse.jgit.transport.FetchResult;
  24. import org.eclipse.jgit.transport.RefSpec;
  25. import org.eclipse.jgit.transport.TagOpt;
  26. import org.kohsuke.args4j.Argument;
  27. import org.kohsuke.args4j.Option;
  28. @Command(common = true, usage = "usage_updateRemoteRefsFromAnotherRepository")
  29. class Fetch extends AbstractFetchCommand implements FetchCommand.Callback {
  30. @Option(name = "--timeout", metaVar = "metaVar_seconds", usage = "usage_abortConnectionIfNoActivity")
  31. int timeout = -1;
  32. @Option(name = "--fsck", usage = "usage_performFsckStyleChecksOnReceive")
  33. private Boolean fsck;
  34. @Option(name = "--no-fsck")
  35. void nofsck(@SuppressWarnings("unused") final boolean ignored) {
  36. fsck = Boolean.FALSE;
  37. }
  38. @Option(name = "--prune", usage = "usage_pruneStaleTrackingRefs")
  39. private Boolean prune;
  40. @Option(name = "--dry-run")
  41. private boolean dryRun;
  42. @Option(name = "--thin", usage = "usage_fetchThinPack")
  43. private Boolean thin;
  44. @Option(name = "--no-thin")
  45. void nothin(@SuppressWarnings("unused") final boolean ignored) {
  46. thin = Boolean.FALSE;
  47. }
  48. @Option(name = "--quiet", usage = "usage_quiet")
  49. private Boolean quiet;
  50. @Option(name = "--tags", usage="usage_tags", aliases = { "-t" })
  51. private Boolean tags;
  52. @Option(name = "--no-tags", usage = "usage_notags", aliases = { "-n" })
  53. void notags(@SuppressWarnings("unused")
  54. final boolean ignored) {
  55. tags = Boolean.FALSE;
  56. }
  57. @Option(name = "--force", usage = "usage_forcedFetch", aliases = { "-f" })
  58. private Boolean force;
  59. private FetchRecurseSubmodulesMode recurseSubmodules;
  60. @Option(name = "--recurse-submodules", usage = "usage_recurseSubmodules")
  61. void recurseSubmodules(String mode) {
  62. if (mode == null || mode.isEmpty()) {
  63. recurseSubmodules = FetchRecurseSubmodulesMode.YES;
  64. } else {
  65. for (FetchRecurseSubmodulesMode m : FetchRecurseSubmodulesMode
  66. .values()) {
  67. if (m.matchConfigValue(mode)) {
  68. recurseSubmodules = m;
  69. return;
  70. }
  71. }
  72. throw die(MessageFormat
  73. .format(CLIText.get().invalidRecurseSubmodulesMode, mode));
  74. }
  75. }
  76. @Option(name = "--no-recurse-submodules", usage = "usage_noRecurseSubmodules")
  77. void noRecurseSubmodules(@SuppressWarnings("unused")
  78. final boolean ignored) {
  79. recurseSubmodules = FetchRecurseSubmodulesMode.NO;
  80. }
  81. @Argument(index = 0, metaVar = "metaVar_uriish")
  82. private String remote = Constants.DEFAULT_REMOTE_NAME;
  83. @Argument(index = 1, metaVar = "metaVar_refspec")
  84. private List<RefSpec> toget;
  85. /** {@inheritDoc} */
  86. @Override
  87. protected void run() {
  88. try (Git git = new Git(db)) {
  89. FetchCommand fetch = git.fetch();
  90. if (fsck != null) {
  91. fetch.setCheckFetchedObjects(fsck.booleanValue());
  92. }
  93. if (prune != null) {
  94. fetch.setRemoveDeletedRefs(prune.booleanValue());
  95. }
  96. if (toget != null) {
  97. fetch.setRefSpecs(toget);
  98. }
  99. if (tags != null) {
  100. fetch.setTagOpt(tags.booleanValue() ? TagOpt.FETCH_TAGS
  101. : TagOpt.NO_TAGS);
  102. }
  103. if (0 <= timeout) {
  104. fetch.setTimeout(timeout);
  105. }
  106. fetch.setDryRun(dryRun);
  107. fetch.setRemote(remote);
  108. if (thin != null) {
  109. fetch.setThin(thin.booleanValue());
  110. }
  111. if (quiet == null || !quiet.booleanValue()) {
  112. fetch.setProgressMonitor(new TextProgressMonitor(errw));
  113. }
  114. fetch.setRecurseSubmodules(recurseSubmodules).setCallback(this);
  115. if (force != null) {
  116. fetch.setForceUpdate(force.booleanValue());
  117. }
  118. FetchResult result = fetch.call();
  119. if (result.getTrackingRefUpdates().isEmpty()
  120. && result.submoduleResults().isEmpty()) {
  121. return;
  122. }
  123. showFetchResult(result);
  124. } catch (GitAPIException | IOException e) {
  125. throw die(e.getMessage(), e);
  126. }
  127. }
  128. /** {@inheritDoc} */
  129. @Override
  130. public void fetchingSubmodule(String name) {
  131. try {
  132. outw.println(MessageFormat.format(CLIText.get().fetchingSubmodule,
  133. name));
  134. outw.flush();
  135. } catch (IOException e) {
  136. // ignore
  137. }
  138. }
  139. }