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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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.DetachedHeadException;
  50. import org.eclipse.jgit.api.errors.GitAPIException;
  51. import org.eclipse.jgit.api.errors.InvalidConfigurationException;
  52. import org.eclipse.jgit.api.errors.InvalidRemoteException;
  53. import org.eclipse.jgit.api.errors.JGitInternalException;
  54. import org.eclipse.jgit.api.errors.NoHeadException;
  55. import org.eclipse.jgit.api.errors.RefNotFoundException;
  56. import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
  57. import org.eclipse.jgit.internal.JGitText;
  58. import org.eclipse.jgit.lib.AnyObjectId;
  59. import org.eclipse.jgit.lib.Config;
  60. import org.eclipse.jgit.lib.ConfigConstants;
  61. import org.eclipse.jgit.lib.Constants;
  62. import org.eclipse.jgit.lib.NullProgressMonitor;
  63. import org.eclipse.jgit.lib.ProgressMonitor;
  64. import org.eclipse.jgit.lib.Ref;
  65. import org.eclipse.jgit.lib.Repository;
  66. import org.eclipse.jgit.lib.RepositoryState;
  67. import org.eclipse.jgit.transport.FetchResult;
  68. /**
  69. * The Pull command
  70. *
  71. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-pull.html"
  72. * >Git documentation about Pull</a>
  73. */
  74. public class PullCommand extends TransportCommand<PullCommand, PullResult> {
  75. private final static String DOT = "."; //$NON-NLS-1$
  76. private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
  77. private PullRebaseMode pullRebaseMode = PullRebaseMode.USE_CONFIG;
  78. private enum PullRebaseMode {
  79. USE_CONFIG,
  80. REBASE,
  81. NO_REBASE
  82. }
  83. /**
  84. * @param repo
  85. */
  86. protected PullCommand(Repository repo) {
  87. super(repo);
  88. }
  89. /**
  90. * @param monitor
  91. * a progress monitor
  92. * @return this instance
  93. */
  94. public PullCommand setProgressMonitor(ProgressMonitor monitor) {
  95. this.monitor = monitor;
  96. return this;
  97. }
  98. /**
  99. * Set if rebase should be used after fetching. If set to true, rebase is
  100. * used instead of merge. This is equivalent to --rebase on the command line.
  101. * <p/>
  102. * If set to false, merge is used after fetching, overriding the configuration
  103. * file. This is equivalent to --no-rebase on the command line.
  104. * <p/>
  105. * This setting overrides the settings in the configuration file.
  106. * By default, the setting in the repository configuration file is used.
  107. * <p/>
  108. * A branch can be configured to use rebase by default.
  109. * See branch.[name].rebase and branch.autosetuprebase.
  110. *
  111. * @param useRebase
  112. * @return {@code this}
  113. */
  114. public PullCommand setRebase(boolean useRebase) {
  115. checkCallable();
  116. pullRebaseMode = useRebase ? PullRebaseMode.REBASE : PullRebaseMode.NO_REBASE;
  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. * @throws WrongRepositoryStateException
  128. * @throws InvalidConfigurationException
  129. * @throws DetachedHeadException
  130. * @throws InvalidRemoteException
  131. * @throws CanceledException
  132. * @throws RefNotFoundException
  133. * @throws NoHeadException
  134. * @throws org.eclipse.jgit.api.errors.TransportException
  135. * @throws GitAPIException
  136. */
  137. public PullResult call() throws GitAPIException,
  138. WrongRepositoryStateException, InvalidConfigurationException,
  139. DetachedHeadException, InvalidRemoteException, CanceledException,
  140. RefNotFoundException, NoHeadException,
  141. org.eclipse.jgit.api.errors.TransportException {
  142. checkCallable();
  143. monitor.beginTask(JGitText.get().pullTaskName, 2);
  144. String branchName;
  145. try {
  146. String fullBranch = repo.getFullBranch();
  147. if (fullBranch == null)
  148. throw new NoHeadException(
  149. JGitText.get().pullOnRepoWithoutHEADCurrentlyNotSupported);
  150. if (!fullBranch.startsWith(Constants.R_HEADS)) {
  151. // we can not pull if HEAD is detached and branch is not
  152. // specified explicitly
  153. throw new DetachedHeadException();
  154. }
  155. branchName = fullBranch.substring(Constants.R_HEADS.length());
  156. } catch (IOException e) {
  157. throw new JGitInternalException(
  158. JGitText.get().exceptionCaughtDuringExecutionOfPullCommand,
  159. e);
  160. }
  161. if (!repo.getRepositoryState().equals(RepositoryState.SAFE))
  162. throw new WrongRepositoryStateException(MessageFormat.format(
  163. JGitText.get().cannotPullOnARepoWithState, repo
  164. .getRepositoryState().name()));
  165. // get the configured remote for the currently checked out branch
  166. // stored in configuration key branch.<branch name>.remote
  167. Config repoConfig = repo.getConfig();
  168. String remote = repoConfig.getString(
  169. ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
  170. ConfigConstants.CONFIG_KEY_REMOTE);
  171. if (remote == null)
  172. // fall back to default remote
  173. remote = Constants.DEFAULT_REMOTE_NAME;
  174. // get the name of the branch in the remote repository
  175. // stored in configuration key branch.<branch name>.merge
  176. String remoteBranchName = repoConfig.getString(
  177. ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
  178. ConfigConstants.CONFIG_KEY_MERGE);
  179. // determines whether rebase should be used after fetching
  180. boolean doRebase = false;
  181. switch (pullRebaseMode) {
  182. case REBASE:
  183. doRebase = true;
  184. break;
  185. case NO_REBASE:
  186. doRebase = false;
  187. break;
  188. case USE_CONFIG:
  189. default:
  190. // check if the branch is configured for pull-rebase
  191. doRebase = repoConfig.getBoolean(
  192. ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
  193. ConfigConstants.CONFIG_KEY_REBASE, false);
  194. break;
  195. }
  196. if (remoteBranchName == null) {
  197. String missingKey = ConfigConstants.CONFIG_BRANCH_SECTION + DOT
  198. + branchName + DOT + ConfigConstants.CONFIG_KEY_MERGE;
  199. throw new InvalidConfigurationException(MessageFormat.format(
  200. JGitText.get().missingConfigurationForKey, missingKey));
  201. }
  202. final boolean isRemote = !remote.equals("."); //$NON-NLS-1$
  203. String remoteUri;
  204. FetchResult fetchRes;
  205. if (isRemote) {
  206. remoteUri = repoConfig.getString(
  207. ConfigConstants.CONFIG_REMOTE_SECTION, remote,
  208. ConfigConstants.CONFIG_KEY_URL);
  209. if (remoteUri == null) {
  210. String missingKey = ConfigConstants.CONFIG_REMOTE_SECTION + DOT
  211. + remote + DOT + ConfigConstants.CONFIG_KEY_URL;
  212. throw new InvalidConfigurationException(MessageFormat.format(
  213. JGitText.get().missingConfigurationForKey, missingKey));
  214. }
  215. if (monitor.isCancelled())
  216. throw new CanceledException(MessageFormat.format(
  217. JGitText.get().operationCanceled,
  218. JGitText.get().pullTaskName));
  219. FetchCommand fetch = new FetchCommand(repo);
  220. fetch.setRemote(remote);
  221. fetch.setProgressMonitor(monitor);
  222. configure(fetch);
  223. fetchRes = fetch.call();
  224. } else {
  225. // we can skip the fetch altogether
  226. remoteUri = "local repository";
  227. fetchRes = null;
  228. }
  229. monitor.update(1);
  230. if (monitor.isCancelled())
  231. throw new CanceledException(MessageFormat.format(
  232. JGitText.get().operationCanceled,
  233. JGitText.get().pullTaskName));
  234. // we check the updates to see which of the updated branches
  235. // corresponds
  236. // to the remote branch name
  237. AnyObjectId commitToMerge;
  238. if (isRemote) {
  239. Ref r = null;
  240. if (fetchRes != null) {
  241. r = fetchRes.getAdvertisedRef(remoteBranchName);
  242. if (r == null)
  243. r = fetchRes.getAdvertisedRef(Constants.R_HEADS
  244. + remoteBranchName);
  245. }
  246. if (r == null)
  247. throw new JGitInternalException(MessageFormat.format(JGitText
  248. .get().couldNotGetAdvertisedRef, remoteBranchName));
  249. else
  250. commitToMerge = r.getObjectId();
  251. } else {
  252. try {
  253. commitToMerge = repo.resolve(remoteBranchName);
  254. if (commitToMerge == null)
  255. throw new RefNotFoundException(MessageFormat.format(
  256. JGitText.get().refNotResolved, remoteBranchName));
  257. } catch (IOException e) {
  258. throw new JGitInternalException(
  259. JGitText.get().exceptionCaughtDuringExecutionOfPullCommand,
  260. e);
  261. }
  262. }
  263. String upstreamName = "branch \'"
  264. + Repository.shortenRefName(remoteBranchName) + "\' of "
  265. + remoteUri;
  266. PullResult result;
  267. if (doRebase) {
  268. RebaseCommand rebase = new RebaseCommand(repo);
  269. RebaseResult rebaseRes = rebase.setUpstream(commitToMerge)
  270. .setUpstreamName(upstreamName)
  271. .setProgressMonitor(monitor).setOperation(Operation.BEGIN)
  272. .call();
  273. result = new PullResult(fetchRes, remote, rebaseRes);
  274. } else {
  275. MergeCommand merge = new MergeCommand(repo);
  276. merge.include(upstreamName, commitToMerge);
  277. MergeResult mergeRes = merge.call();
  278. monitor.update(1);
  279. result = new PullResult(fetchRes, remote, mergeRes);
  280. }
  281. monitor.endTask();
  282. return result;
  283. }
  284. }