Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

PullCommand.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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.merge.MergeStrategy;
  68. import org.eclipse.jgit.transport.FetchResult;
  69. /**
  70. * The Pull command
  71. *
  72. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-pull.html"
  73. * >Git documentation about Pull</a>
  74. */
  75. public class PullCommand extends TransportCommand<PullCommand, PullResult> {
  76. private final static String DOT = "."; //$NON-NLS-1$
  77. private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
  78. private PullRebaseMode pullRebaseMode = PullRebaseMode.USE_CONFIG;
  79. private String remote;
  80. private String remoteBranchName;
  81. private MergeStrategy strategy = MergeStrategy.RECURSIVE;
  82. private enum PullRebaseMode {
  83. USE_CONFIG,
  84. REBASE,
  85. NO_REBASE
  86. }
  87. /**
  88. * @param repo
  89. */
  90. protected PullCommand(Repository repo) {
  91. super(repo);
  92. }
  93. /**
  94. * @param monitor
  95. * a progress monitor
  96. * @return this instance
  97. */
  98. public PullCommand setProgressMonitor(ProgressMonitor monitor) {
  99. this.monitor = monitor;
  100. return this;
  101. }
  102. /**
  103. * Set if rebase should be used after fetching. If set to true, rebase is
  104. * used instead of merge. This is equivalent to --rebase on the command
  105. * line.
  106. * <p>
  107. * If set to false, merge is used after fetching, overriding the
  108. * configuration file. This is equivalent to --no-rebase on the command
  109. * line.
  110. * <p>
  111. * This setting overrides the settings in the configuration file. By
  112. * default, the setting in the repository configuration file is used.
  113. * <p>
  114. * A branch can be configured to use rebase by default. See
  115. * branch.[name].rebase and branch.autosetuprebase.
  116. *
  117. * @param useRebase
  118. * @return {@code this}
  119. */
  120. public PullCommand setRebase(boolean useRebase) {
  121. checkCallable();
  122. pullRebaseMode = useRebase ? PullRebaseMode.REBASE : PullRebaseMode.NO_REBASE;
  123. return this;
  124. }
  125. /**
  126. * Executes the {@code Pull} command with all the options and parameters
  127. * collected by the setter methods (e.g.
  128. * {@link #setProgressMonitor(ProgressMonitor)}) of this class. Each
  129. * instance of this class should only be used for one invocation of the
  130. * command. Don't call this method twice on an instance.
  131. *
  132. * @return the result of the pull
  133. * @throws WrongRepositoryStateException
  134. * @throws InvalidConfigurationException
  135. * @throws DetachedHeadException
  136. * @throws InvalidRemoteException
  137. * @throws CanceledException
  138. * @throws RefNotFoundException
  139. * @throws NoHeadException
  140. * @throws org.eclipse.jgit.api.errors.TransportException
  141. * @throws GitAPIException
  142. */
  143. public PullResult call() throws GitAPIException,
  144. WrongRepositoryStateException, InvalidConfigurationException,
  145. DetachedHeadException, InvalidRemoteException, CanceledException,
  146. RefNotFoundException, NoHeadException,
  147. org.eclipse.jgit.api.errors.TransportException {
  148. checkCallable();
  149. monitor.beginTask(JGitText.get().pullTaskName, 2);
  150. String branchName;
  151. try {
  152. String fullBranch = repo.getFullBranch();
  153. if (fullBranch == null)
  154. throw new NoHeadException(
  155. JGitText.get().pullOnRepoWithoutHEADCurrentlyNotSupported);
  156. if (!fullBranch.startsWith(Constants.R_HEADS)) {
  157. // we can not pull if HEAD is detached and branch is not
  158. // specified explicitly
  159. throw new DetachedHeadException();
  160. }
  161. branchName = fullBranch.substring(Constants.R_HEADS.length());
  162. } catch (IOException e) {
  163. throw new JGitInternalException(
  164. JGitText.get().exceptionCaughtDuringExecutionOfPullCommand,
  165. e);
  166. }
  167. if (!repo.getRepositoryState().equals(RepositoryState.SAFE))
  168. throw new WrongRepositoryStateException(MessageFormat.format(
  169. JGitText.get().cannotPullOnARepoWithState, repo
  170. .getRepositoryState().name()));
  171. Config repoConfig = repo.getConfig();
  172. if (remote == null) {
  173. // get the configured remote for the currently checked out branch
  174. // stored in configuration key branch.<branch name>.remote
  175. remote = repoConfig.getString(
  176. ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
  177. ConfigConstants.CONFIG_KEY_REMOTE);
  178. }
  179. if (remote == null)
  180. // fall back to default remote
  181. remote = Constants.DEFAULT_REMOTE_NAME;
  182. if (remoteBranchName == null)
  183. // get the name of the branch in the remote repository
  184. // stored in configuration key branch.<branch name>.merge
  185. remoteBranchName = repoConfig.getString(
  186. ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
  187. ConfigConstants.CONFIG_KEY_MERGE);
  188. // determines whether rebase should be used after fetching
  189. boolean doRebase = false;
  190. switch (pullRebaseMode) {
  191. case REBASE:
  192. doRebase = true;
  193. break;
  194. case NO_REBASE:
  195. doRebase = false;
  196. break;
  197. case USE_CONFIG:
  198. default:
  199. // check if the branch is configured for pull-rebase
  200. doRebase = repoConfig.getBoolean(
  201. ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
  202. ConfigConstants.CONFIG_KEY_REBASE, false);
  203. break;
  204. }
  205. if (remoteBranchName == null)
  206. remoteBranchName = branchName;
  207. final boolean isRemote = !remote.equals("."); //$NON-NLS-1$
  208. String remoteUri;
  209. FetchResult fetchRes;
  210. if (isRemote) {
  211. remoteUri = repoConfig.getString(
  212. ConfigConstants.CONFIG_REMOTE_SECTION, remote,
  213. ConfigConstants.CONFIG_KEY_URL);
  214. if (remoteUri == null) {
  215. String missingKey = ConfigConstants.CONFIG_REMOTE_SECTION + DOT
  216. + remote + DOT + ConfigConstants.CONFIG_KEY_URL;
  217. throw new InvalidConfigurationException(MessageFormat.format(
  218. JGitText.get().missingConfigurationForKey, missingKey));
  219. }
  220. if (monitor.isCancelled())
  221. throw new CanceledException(MessageFormat.format(
  222. JGitText.get().operationCanceled,
  223. JGitText.get().pullTaskName));
  224. FetchCommand fetch = new FetchCommand(repo);
  225. fetch.setRemote(remote);
  226. fetch.setProgressMonitor(monitor);
  227. configure(fetch);
  228. fetchRes = fetch.call();
  229. } else {
  230. // we can skip the fetch altogether
  231. remoteUri = "local repository";
  232. fetchRes = null;
  233. }
  234. monitor.update(1);
  235. if (monitor.isCancelled())
  236. throw new CanceledException(MessageFormat.format(
  237. JGitText.get().operationCanceled,
  238. JGitText.get().pullTaskName));
  239. // we check the updates to see which of the updated branches
  240. // corresponds
  241. // to the remote branch name
  242. AnyObjectId commitToMerge;
  243. if (isRemote) {
  244. Ref r = null;
  245. if (fetchRes != null) {
  246. r = fetchRes.getAdvertisedRef(remoteBranchName);
  247. if (r == null)
  248. r = fetchRes.getAdvertisedRef(Constants.R_HEADS
  249. + remoteBranchName);
  250. }
  251. if (r == null)
  252. throw new JGitInternalException(MessageFormat.format(JGitText
  253. .get().couldNotGetAdvertisedRef, remoteBranchName));
  254. else
  255. commitToMerge = r.getObjectId();
  256. } else {
  257. try {
  258. commitToMerge = repo.resolve(remoteBranchName);
  259. if (commitToMerge == null)
  260. throw new RefNotFoundException(MessageFormat.format(
  261. JGitText.get().refNotResolved, remoteBranchName));
  262. } catch (IOException e) {
  263. throw new JGitInternalException(
  264. JGitText.get().exceptionCaughtDuringExecutionOfPullCommand,
  265. e);
  266. }
  267. }
  268. String upstreamName = "branch \'"
  269. + Repository.shortenRefName(remoteBranchName) + "\' of "
  270. + remoteUri;
  271. PullResult result;
  272. if (doRebase) {
  273. RebaseCommand rebase = new RebaseCommand(repo);
  274. RebaseResult rebaseRes = rebase.setUpstream(commitToMerge)
  275. .setUpstreamName(upstreamName).setProgressMonitor(monitor)
  276. .setOperation(Operation.BEGIN).setStrategy(strategy)
  277. .call();
  278. result = new PullResult(fetchRes, remote, rebaseRes);
  279. } else {
  280. MergeCommand merge = new MergeCommand(repo);
  281. merge.include(upstreamName, commitToMerge);
  282. merge.setStrategy(strategy);
  283. MergeResult mergeRes = merge.call();
  284. monitor.update(1);
  285. result = new PullResult(fetchRes, remote, mergeRes);
  286. }
  287. monitor.endTask();
  288. return result;
  289. }
  290. /**
  291. * The remote (uri or name) to be used for the pull operation. If no remote
  292. * is set, the branch's configuration will be used. If the branch
  293. * configuration is missing the default value of
  294. * <code>Constants.DEFAULT_REMOTE_NAME</code> will be used.
  295. *
  296. * @see Constants#DEFAULT_REMOTE_NAME
  297. * @param remote
  298. * @return {@code this}
  299. * @since 3.3
  300. */
  301. public PullCommand setRemote(String remote) {
  302. checkCallable();
  303. this.remote = remote;
  304. return this;
  305. }
  306. /**
  307. * The remote branch name to be used for the pull operation. If no
  308. * remoteBranchName is set, the branch's configuration will be used. If the
  309. * branch configuration is missing the remote branch with the same name as
  310. * the current branch is used.
  311. *
  312. * @param remoteBranchName
  313. * @return {@code this}
  314. * @since 3.3
  315. */
  316. public PullCommand setRemoteBranchName(String remoteBranchName) {
  317. checkCallable();
  318. this.remoteBranchName = remoteBranchName;
  319. return this;
  320. }
  321. /**
  322. * @return the remote used for the pull operation if it was set explicitly
  323. * @since 3.3
  324. */
  325. public String getRemote() {
  326. return remote;
  327. }
  328. /**
  329. * @return the remote branch name used for the pull operation if it was set
  330. * explicitly
  331. * @since 3.3
  332. */
  333. public String getRemoteBranchName() {
  334. return remoteBranchName;
  335. }
  336. /**
  337. * @param strategy
  338. * The merge strategy to use during this pull operation.
  339. * @return {@code this}
  340. * @since 3.4
  341. */
  342. public PullCommand setStrategy(MergeStrategy strategy) {
  343. this.strategy = strategy;
  344. return this;
  345. }
  346. }