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.

PullCommand.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  3. * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.api;
  45. import java.io.IOException;
  46. import java.text.MessageFormat;
  47. import org.eclipse.jgit.api.RebaseCommand.Operation;
  48. import org.eclipse.jgit.api.errors.CanceledException;
  49. import org.eclipse.jgit.api.errors.CheckoutConflictException;
  50. import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
  51. import org.eclipse.jgit.api.errors.DetachedHeadException;
  52. import org.eclipse.jgit.api.errors.GitAPIException;
  53. import org.eclipse.jgit.api.errors.InvalidConfigurationException;
  54. import org.eclipse.jgit.api.errors.InvalidMergeHeadsException;
  55. import org.eclipse.jgit.api.errors.InvalidRemoteException;
  56. import org.eclipse.jgit.api.errors.JGitInternalException;
  57. import org.eclipse.jgit.api.errors.NoHeadException;
  58. import org.eclipse.jgit.api.errors.NoMessageException;
  59. import org.eclipse.jgit.api.errors.RefNotFoundException;
  60. import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
  61. import org.eclipse.jgit.internal.JGitText;
  62. import org.eclipse.jgit.lib.AnyObjectId;
  63. import org.eclipse.jgit.lib.Config;
  64. import org.eclipse.jgit.lib.ConfigConstants;
  65. import org.eclipse.jgit.lib.Constants;
  66. import org.eclipse.jgit.lib.NullProgressMonitor;
  67. import org.eclipse.jgit.lib.ProgressMonitor;
  68. import org.eclipse.jgit.lib.Ref;
  69. import org.eclipse.jgit.lib.Repository;
  70. import org.eclipse.jgit.lib.RepositoryState;
  71. import org.eclipse.jgit.transport.FetchResult;
  72. /**
  73. * The Pull command
  74. *
  75. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-pull.html"
  76. * >Git documentation about Pull</a>
  77. */
  78. public class PullCommand extends TransportCommand<PullCommand, PullResult> {
  79. private final static String DOT = ".";
  80. private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
  81. /**
  82. * @param repo
  83. */
  84. protected PullCommand(Repository repo) {
  85. super(repo);
  86. }
  87. /**
  88. * @param monitor
  89. * a progress monitor
  90. * @return this instance
  91. */
  92. public PullCommand setProgressMonitor(ProgressMonitor monitor) {
  93. this.monitor = monitor;
  94. return this;
  95. }
  96. /**
  97. * Executes the {@code Pull} command with all the options and parameters
  98. * collected by the setter methods (e.g.
  99. * {@link #setProgressMonitor(ProgressMonitor)}) of this class. Each
  100. * instance of this class should only be used for one invocation of the
  101. * command. Don't call this method twice on an instance.
  102. *
  103. * @return the result of the pull
  104. */
  105. public PullResult call() throws GitAPIException, WrongRepositoryStateException,
  106. InvalidConfigurationException, DetachedHeadException,
  107. InvalidRemoteException, CanceledException, RefNotFoundException,
  108. NoHeadException {
  109. checkCallable();
  110. monitor.beginTask(JGitText.get().pullTaskName, 2);
  111. String branchName;
  112. try {
  113. String fullBranch = repo.getFullBranch();
  114. if (fullBranch == null)
  115. throw new NoHeadException(
  116. JGitText.get().pullOnRepoWithoutHEADCurrentlyNotSupported);
  117. if (!fullBranch.startsWith(Constants.R_HEADS)) {
  118. // we can not pull if HEAD is detached and branch is not
  119. // specified explicitly
  120. throw new DetachedHeadException();
  121. }
  122. branchName = fullBranch.substring(Constants.R_HEADS.length());
  123. } catch (IOException e) {
  124. throw new JGitInternalException(
  125. JGitText.get().exceptionCaughtDuringExecutionOfPullCommand,
  126. e);
  127. }
  128. if (!repo.getRepositoryState().equals(RepositoryState.SAFE))
  129. throw new WrongRepositoryStateException(MessageFormat.format(
  130. JGitText.get().cannotPullOnARepoWithState, repo
  131. .getRepositoryState().name()));
  132. // get the configured remote for the currently checked out branch
  133. // stored in configuration key branch.<branch name>.remote
  134. Config repoConfig = repo.getConfig();
  135. String remote = repoConfig.getString(
  136. ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
  137. ConfigConstants.CONFIG_KEY_REMOTE);
  138. if (remote == null)
  139. // fall back to default remote
  140. remote = Constants.DEFAULT_REMOTE_NAME;
  141. // get the name of the branch in the remote repository
  142. // stored in configuration key branch.<branch name>.merge
  143. String remoteBranchName = repoConfig.getString(
  144. ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
  145. ConfigConstants.CONFIG_KEY_MERGE);
  146. // check if the branch is configured for pull-rebase
  147. boolean doRebase = repoConfig.getBoolean(
  148. ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
  149. ConfigConstants.CONFIG_KEY_REBASE, false);
  150. if (remoteBranchName == null) {
  151. String missingKey = ConfigConstants.CONFIG_BRANCH_SECTION + DOT
  152. + branchName + DOT + ConfigConstants.CONFIG_KEY_MERGE;
  153. throw new InvalidConfigurationException(MessageFormat.format(
  154. JGitText.get().missingConfigurationForKey, missingKey));
  155. }
  156. final boolean isRemote = !remote.equals(".");
  157. String remoteUri;
  158. FetchResult fetchRes;
  159. if (isRemote) {
  160. remoteUri = repoConfig.getString(
  161. ConfigConstants.CONFIG_REMOTE_SECTION, remote,
  162. ConfigConstants.CONFIG_KEY_URL);
  163. if (remoteUri == null) {
  164. String missingKey = ConfigConstants.CONFIG_REMOTE_SECTION + DOT
  165. + remote + DOT + ConfigConstants.CONFIG_KEY_URL;
  166. throw new InvalidConfigurationException(MessageFormat.format(
  167. JGitText.get().missingConfigurationForKey, missingKey));
  168. }
  169. if (monitor.isCancelled())
  170. throw new CanceledException(MessageFormat.format(
  171. JGitText.get().operationCanceled,
  172. JGitText.get().pullTaskName));
  173. FetchCommand fetch = new FetchCommand(repo);
  174. fetch.setRemote(remote);
  175. fetch.setProgressMonitor(monitor);
  176. configure(fetch);
  177. fetchRes = fetch.call();
  178. } else {
  179. // we can skip the fetch altogether
  180. remoteUri = "local repository";
  181. fetchRes = null;
  182. }
  183. monitor.update(1);
  184. if (monitor.isCancelled())
  185. throw new CanceledException(MessageFormat.format(
  186. JGitText.get().operationCanceled,
  187. JGitText.get().pullTaskName));
  188. // we check the updates to see which of the updated branches
  189. // corresponds
  190. // to the remote branch name
  191. AnyObjectId commitToMerge;
  192. if (isRemote) {
  193. Ref r = null;
  194. if (fetchRes != null) {
  195. r = fetchRes.getAdvertisedRef(remoteBranchName);
  196. if (r == null)
  197. r = fetchRes.getAdvertisedRef(Constants.R_HEADS
  198. + remoteBranchName);
  199. }
  200. if (r == null)
  201. throw new JGitInternalException(MessageFormat.format(JGitText
  202. .get().couldNotGetAdvertisedRef, remoteBranchName));
  203. else
  204. commitToMerge = r.getObjectId();
  205. } else {
  206. try {
  207. commitToMerge = repo.resolve(remoteBranchName);
  208. if (commitToMerge == null)
  209. throw new RefNotFoundException(MessageFormat.format(
  210. JGitText.get().refNotResolved, remoteBranchName));
  211. } catch (IOException e) {
  212. throw new JGitInternalException(
  213. JGitText.get().exceptionCaughtDuringExecutionOfPullCommand,
  214. e);
  215. }
  216. }
  217. PullResult result;
  218. if (doRebase) {
  219. RebaseCommand rebase = new RebaseCommand(repo);
  220. try {
  221. RebaseResult rebaseRes = rebase.setUpstream(commitToMerge)
  222. .setProgressMonitor(monitor).setOperation(
  223. Operation.BEGIN).call();
  224. result = new PullResult(fetchRes, remote, rebaseRes);
  225. } catch (NoHeadException e) {
  226. throw new JGitInternalException(e.getMessage(), e);
  227. } catch (RefNotFoundException e) {
  228. throw new JGitInternalException(e.getMessage(), e);
  229. } catch (JGitInternalException e) {
  230. throw new JGitInternalException(e.getMessage(), e);
  231. } catch (GitAPIException e) {
  232. throw new JGitInternalException(e.getMessage(), e);
  233. }
  234. } else {
  235. MergeCommand merge = new MergeCommand(repo);
  236. String name = "branch \'"
  237. + Repository.shortenRefName(remoteBranchName) + "\' of "
  238. + remoteUri;
  239. merge.include(name, commitToMerge);
  240. MergeResult mergeRes;
  241. try {
  242. mergeRes = merge.call();
  243. monitor.update(1);
  244. result = new PullResult(fetchRes, remote, mergeRes);
  245. } catch (NoHeadException e) {
  246. throw new JGitInternalException(e.getMessage(), e);
  247. } catch (ConcurrentRefUpdateException e) {
  248. throw new JGitInternalException(e.getMessage(), e);
  249. } catch (CheckoutConflictException e) {
  250. throw new JGitInternalException(e.getMessage(), e);
  251. } catch (InvalidMergeHeadsException e) {
  252. throw new JGitInternalException(e.getMessage(), e);
  253. } catch (WrongRepositoryStateException e) {
  254. throw new JGitInternalException(e.getMessage(), e);
  255. } catch (NoMessageException e) {
  256. throw new JGitInternalException(e.getMessage(), e);
  257. }
  258. }
  259. monitor.endTask();
  260. return result;
  261. }
  262. }