選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

PullCommand.java 10KB

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