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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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.JGitText;
  48. import org.eclipse.jgit.api.RebaseCommand.Operation;
  49. import org.eclipse.jgit.api.errors.CanceledException;
  50. import org.eclipse.jgit.api.errors.CheckoutConflictException;
  51. import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
  52. import org.eclipse.jgit.api.errors.DetachedHeadException;
  53. import org.eclipse.jgit.api.errors.GitAPIException;
  54. import org.eclipse.jgit.api.errors.InvalidConfigurationException;
  55. import org.eclipse.jgit.api.errors.InvalidMergeHeadsException;
  56. import org.eclipse.jgit.api.errors.InvalidRemoteException;
  57. import org.eclipse.jgit.api.errors.JGitInternalException;
  58. import org.eclipse.jgit.api.errors.NoHeadException;
  59. import org.eclipse.jgit.api.errors.NoMessageException;
  60. import org.eclipse.jgit.api.errors.RefNotFoundException;
  61. import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
  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.CredentialsProvider;
  72. import org.eclipse.jgit.transport.FetchResult;
  73. /**
  74. * The Pull command
  75. *
  76. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-pull.html"
  77. * >Git documentation about Pull</a>
  78. */
  79. public class PullCommand extends GitCommand<PullResult> {
  80. private int timeout = 0;
  81. private final static String DOT = ".";
  82. private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
  83. private CredentialsProvider credentialsProvider;
  84. /**
  85. * @param repo
  86. */
  87. protected PullCommand(Repository repo) {
  88. super(repo);
  89. }
  90. /**
  91. * @param timeout
  92. * in seconds
  93. * @return this instance
  94. */
  95. public PullCommand setTimeout(int timeout) {
  96. this.timeout = timeout;
  97. return this;
  98. }
  99. /**
  100. * @param monitor
  101. * a progress monitor
  102. * @return this instance
  103. */
  104. public PullCommand setProgressMonitor(ProgressMonitor monitor) {
  105. this.monitor = monitor;
  106. return this;
  107. }
  108. /**
  109. * @param credentialsProvider
  110. * the {@link CredentialsProvider} to use
  111. * @return this instance
  112. */
  113. public PullCommand setCredentialsProvider(
  114. CredentialsProvider credentialsProvider) {
  115. checkCallable();
  116. this.credentialsProvider = credentialsProvider;
  117. return this;
  118. }
  119. /**
  120. * Executes the {@code Pull} command with all the options and parameters
  121. * collected by the setter methods (e.g.
  122. * {@link #setProgressMonitor(ProgressMonitor)}) of this class. Each
  123. * instance of this class should only be used for one invocation of the
  124. * command. Don't call this method twice on an instance.
  125. *
  126. * @return the result of the pull
  127. */
  128. public PullResult call() throws WrongRepositoryStateException,
  129. InvalidConfigurationException, DetachedHeadException,
  130. InvalidRemoteException, CanceledException, RefNotFoundException {
  131. checkCallable();
  132. monitor.beginTask(JGitText.get().pullTaskName, 2);
  133. String branchName;
  134. try {
  135. String fullBranch = repo.getFullBranch();
  136. if (!fullBranch.startsWith(Constants.R_HEADS)) {
  137. // we can not pull if HEAD is detached and branch is not
  138. // specified explicitly
  139. throw new DetachedHeadException();
  140. }
  141. branchName = fullBranch.substring(Constants.R_HEADS.length());
  142. } catch (IOException e) {
  143. throw new JGitInternalException(
  144. JGitText.get().exceptionCaughtDuringExecutionOfPullCommand,
  145. e);
  146. }
  147. if (!repo.getRepositoryState().equals(RepositoryState.SAFE))
  148. throw new WrongRepositoryStateException(MessageFormat.format(
  149. JGitText.get().cannotPullOnARepoWithState, repo
  150. .getRepositoryState().name()));
  151. // get the configured remote for the currently checked out branch
  152. // stored in configuration key branch.<branch name>.remote
  153. Config repoConfig = repo.getConfig();
  154. String remote = repoConfig.getString(
  155. ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
  156. ConfigConstants.CONFIG_KEY_REMOTE);
  157. if (remote == null)
  158. // fall back to default remote
  159. remote = Constants.DEFAULT_REMOTE_NAME;
  160. // get the name of the branch in the remote repository
  161. // stored in configuration key branch.<branch name>.merge
  162. String remoteBranchName = repoConfig.getString(
  163. ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
  164. ConfigConstants.CONFIG_KEY_MERGE);
  165. // check if the branch is configured for pull-rebase
  166. boolean doRebase = repoConfig.getBoolean(
  167. ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
  168. ConfigConstants.CONFIG_KEY_REBASE, false);
  169. if (remoteBranchName == null) {
  170. String missingKey = ConfigConstants.CONFIG_BRANCH_SECTION + DOT
  171. + branchName + DOT + ConfigConstants.CONFIG_KEY_MERGE;
  172. throw new InvalidConfigurationException(MessageFormat.format(
  173. JGitText.get().missingConfigurationForKey, missingKey));
  174. }
  175. final boolean isRemote = !remote.equals(".");
  176. String remoteUri;
  177. FetchResult fetchRes;
  178. if (isRemote) {
  179. remoteUri = repoConfig.getString("remote", remote,
  180. ConfigConstants.CONFIG_KEY_URL);
  181. if (remoteUri == null) {
  182. String missingKey = ConfigConstants.CONFIG_REMOTE_SECTION + DOT
  183. + remote + DOT + ConfigConstants.CONFIG_KEY_URL;
  184. throw new InvalidConfigurationException(MessageFormat.format(
  185. JGitText.get().missingConfigurationForKey, missingKey));
  186. }
  187. if (monitor.isCancelled())
  188. throw new CanceledException(MessageFormat.format(
  189. JGitText.get().operationCanceled,
  190. JGitText.get().pullTaskName));
  191. FetchCommand fetch = new FetchCommand(repo);
  192. fetch.setRemote(remote);
  193. fetch.setProgressMonitor(monitor);
  194. fetch.setTimeout(this.timeout);
  195. fetch.setCredentialsProvider(credentialsProvider);
  196. fetchRes = fetch.call();
  197. } else {
  198. // we can skip the fetch altogether
  199. remoteUri = "local repository";
  200. fetchRes = null;
  201. }
  202. monitor.update(1);
  203. if (monitor.isCancelled())
  204. throw new CanceledException(MessageFormat.format(
  205. JGitText.get().operationCanceled,
  206. JGitText.get().pullTaskName));
  207. // we check the updates to see which of the updated branches
  208. // corresponds
  209. // to the remote branch name
  210. AnyObjectId commitToMerge;
  211. if (isRemote) {
  212. Ref r = null;
  213. if (fetchRes != null) {
  214. r = fetchRes.getAdvertisedRef(remoteBranchName);
  215. if (r == null)
  216. r = fetchRes.getAdvertisedRef(Constants.R_HEADS
  217. + remoteBranchName);
  218. }
  219. if (r == null)
  220. throw new JGitInternalException(MessageFormat.format(JGitText
  221. .get().couldNotGetAdvertisedRef, remoteBranchName));
  222. else
  223. commitToMerge = r.getObjectId();
  224. } else {
  225. try {
  226. commitToMerge = repo.resolve(remoteBranchName);
  227. if (commitToMerge == null)
  228. throw new RefNotFoundException(MessageFormat.format(
  229. JGitText.get().refNotResolved, remoteBranchName));
  230. } catch (IOException e) {
  231. throw new JGitInternalException(
  232. JGitText.get().exceptionCaughtDuringExecutionOfPullCommand,
  233. e);
  234. }
  235. }
  236. PullResult result;
  237. if (doRebase) {
  238. RebaseCommand rebase = new RebaseCommand(repo);
  239. try {
  240. RebaseResult rebaseRes = rebase.setUpstream(commitToMerge)
  241. .setProgressMonitor(monitor).setOperation(
  242. Operation.BEGIN).call();
  243. result = new PullResult(fetchRes, remote, rebaseRes);
  244. } catch (NoHeadException e) {
  245. throw new JGitInternalException(e.getMessage(), e);
  246. } catch (RefNotFoundException e) {
  247. throw new JGitInternalException(e.getMessage(), e);
  248. } catch (JGitInternalException e) {
  249. throw new JGitInternalException(e.getMessage(), e);
  250. } catch (GitAPIException e) {
  251. throw new JGitInternalException(e.getMessage(), e);
  252. }
  253. } else {
  254. MergeCommand merge = new MergeCommand(repo);
  255. merge.include(
  256. "branch \'" + remoteBranchName + "\' of " + remoteUri,
  257. commitToMerge);
  258. MergeResult mergeRes;
  259. try {
  260. mergeRes = merge.call();
  261. monitor.update(1);
  262. result = new PullResult(fetchRes, remote, mergeRes);
  263. } catch (NoHeadException e) {
  264. throw new JGitInternalException(e.getMessage(), e);
  265. } catch (ConcurrentRefUpdateException e) {
  266. throw new JGitInternalException(e.getMessage(), e);
  267. } catch (CheckoutConflictException e) {
  268. throw new JGitInternalException(e.getMessage(), e);
  269. } catch (InvalidMergeHeadsException e) {
  270. throw new JGitInternalException(e.getMessage(), e);
  271. } catch (WrongRepositoryStateException e) {
  272. throw new JGitInternalException(e.getMessage(), e);
  273. } catch (NoMessageException e) {
  274. throw new JGitInternalException(e.getMessage(), e);
  275. }
  276. }
  277. monitor.endTask();
  278. return result;
  279. }
  280. }