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.

PullCommandWithRebaseTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright (C) 2011, Mathias Kinzler <mathias.kinzler@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.api;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertNull;
  48. import static org.junit.Assert.assertTrue;
  49. import java.io.ByteArrayOutputStream;
  50. import java.io.File;
  51. import java.io.FileInputStream;
  52. import java.io.FileOutputStream;
  53. import java.io.IOException;
  54. import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
  55. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  56. import org.eclipse.jgit.api.RebaseResult.Status;
  57. import org.eclipse.jgit.junit.RepositoryTestCase;
  58. import org.eclipse.jgit.lib.ConfigConstants;
  59. import org.eclipse.jgit.lib.Constants;
  60. import org.eclipse.jgit.lib.ObjectId;
  61. import org.eclipse.jgit.lib.Repository;
  62. import org.eclipse.jgit.lib.RepositoryState;
  63. import org.eclipse.jgit.lib.StoredConfig;
  64. import org.eclipse.jgit.merge.MergeStrategy;
  65. import org.eclipse.jgit.revwalk.RevCommit;
  66. import org.eclipse.jgit.revwalk.RevWalk;
  67. import org.eclipse.jgit.transport.RefSpec;
  68. import org.eclipse.jgit.transport.RemoteConfig;
  69. import org.eclipse.jgit.transport.URIish;
  70. import org.junit.Before;
  71. import org.junit.Test;
  72. public class PullCommandWithRebaseTest extends RepositoryTestCase {
  73. /** Second Test repository */
  74. protected Repository dbTarget;
  75. private Git source;
  76. private Git target;
  77. private File sourceFile;
  78. private File targetFile;
  79. @Test
  80. public void testPullFastForward() throws Exception {
  81. PullResult res = target.pull().call();
  82. // nothing to update since we don't have different data yet
  83. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  84. assertEquals(Status.UP_TO_DATE, res.getRebaseResult().getStatus());
  85. assertFileContentsEqual(targetFile, "Hello world");
  86. // change the source file
  87. writeToFile(sourceFile, "Another change");
  88. source.add().addFilepattern("SomeFile.txt").call();
  89. source.commit().setMessage("Some change in remote").call();
  90. res = target.pull().call();
  91. assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  92. assertEquals(Status.FAST_FORWARD, res.getRebaseResult().getStatus());
  93. assertFileContentsEqual(targetFile, "Another change");
  94. assertEquals(RepositoryState.SAFE, target.getRepository()
  95. .getRepositoryState());
  96. res = target.pull().call();
  97. assertEquals(Status.UP_TO_DATE, res.getRebaseResult().getStatus());
  98. }
  99. @Test
  100. public void testPullFastForwardWithBranchInSource() throws Exception {
  101. PullResult res = target.pull().call();
  102. // nothing to update since we don't have different data yet
  103. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  104. assertEquals(Status.UP_TO_DATE, res.getRebaseResult().getStatus());
  105. assertFileContentsEqual(targetFile, "Hello world");
  106. // change the source file
  107. writeToFile(sourceFile, "Another change\n\n\n\nFoo");
  108. source.add().addFilepattern("SomeFile.txt").call();
  109. RevCommit initialCommit = source.commit()
  110. .setMessage("Some change in remote").call();
  111. // modify the source file in a branch
  112. createBranch(initialCommit, "refs/heads/side");
  113. checkoutBranch("refs/heads/side");
  114. writeToFile(sourceFile, "Another change\n\n\n\nBoo");
  115. source.add().addFilepattern("SomeFile.txt").call();
  116. RevCommit sideCommit = source.commit()
  117. .setMessage("Some change in remote").call();
  118. // modify the source file on master
  119. checkoutBranch("refs/heads/master");
  120. writeToFile(sourceFile, "More change\n\n\n\nFoo");
  121. source.add().addFilepattern("SomeFile.txt").call();
  122. source.commit().setMessage("Some change in remote").call();
  123. // merge side into master
  124. MergeResult result = source.merge().include(sideCommit.getId())
  125. .setStrategy(MergeStrategy.RESOLVE).call();
  126. assertEquals(MergeStatus.MERGED, result.getMergeStatus());
  127. }
  128. @Test
  129. public void testPullConflict() throws Exception {
  130. PullResult res = target.pull().call();
  131. // nothing to update since we don't have different data yet
  132. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  133. assertEquals(Status.UP_TO_DATE, res.getRebaseResult().getStatus());
  134. assertFileContentsEqual(targetFile, "Hello world");
  135. // change the source file
  136. writeToFile(sourceFile, "Source change");
  137. source.add().addFilepattern("SomeFile.txt").call();
  138. source.commit().setMessage("Source change in remote").call();
  139. // change the target file
  140. writeToFile(targetFile, "Target change");
  141. target.add().addFilepattern("SomeFile.txt").call();
  142. target.commit().setMessage("Target change in local").call();
  143. res = target.pull().call();
  144. String remoteUri = target
  145. .getRepository()
  146. .getConfig()
  147. .getString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin",
  148. ConfigConstants.CONFIG_KEY_URL);
  149. assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  150. assertEquals(Status.STOPPED, res.getRebaseResult().getStatus());
  151. String result = "<<<<<<< Upstream, based on branch 'master' of "
  152. + remoteUri
  153. + "\nSource change\n=======\nTarget change\n>>>>>>> 42453fd Target change in local\n";
  154. assertFileContentsEqual(targetFile, result);
  155. assertEquals(RepositoryState.REBASING_INTERACTIVE, target
  156. .getRepository().getRepositoryState());
  157. }
  158. @Test
  159. public void testPullLocalConflict() throws Exception {
  160. target.branchCreate().setName("basedOnMaster").setStartPoint(
  161. "refs/heads/master").setUpstreamMode(SetupUpstreamMode.NOTRACK)
  162. .call();
  163. StoredConfig config = target.getRepository().getConfig();
  164. config.setString("branch", "basedOnMaster", "remote", ".");
  165. config.setString("branch", "basedOnMaster", "merge",
  166. "refs/heads/master");
  167. config.setBoolean("branch", "basedOnMaster", "rebase", true);
  168. config.save();
  169. target.getRepository().updateRef(Constants.HEAD).link(
  170. "refs/heads/basedOnMaster");
  171. PullResult res = target.pull().call();
  172. // nothing to update since we don't have different data yet
  173. assertNull(res.getFetchResult());
  174. assertEquals(Status.UP_TO_DATE, res.getRebaseResult().getStatus());
  175. assertFileContentsEqual(targetFile, "Hello world");
  176. // change the file in master
  177. target.getRepository().updateRef(Constants.HEAD).link(
  178. "refs/heads/master");
  179. writeToFile(targetFile, "Master change");
  180. target.add().addFilepattern("SomeFile.txt").call();
  181. target.commit().setMessage("Source change in master").call();
  182. // change the file in slave
  183. target.getRepository().updateRef(Constants.HEAD).link(
  184. "refs/heads/basedOnMaster");
  185. writeToFile(targetFile, "Slave change");
  186. target.add().addFilepattern("SomeFile.txt").call();
  187. target.commit().setMessage("Source change in based on master").call();
  188. res = target.pull().call();
  189. assertNull(res.getFetchResult());
  190. assertEquals(Status.STOPPED, res.getRebaseResult().getStatus());
  191. String result = "<<<<<<< Upstream, based on branch 'master' of local repository\n"
  192. + "Master change\n=======\nSlave change\n>>>>>>> 4049c9e Source change in based on master\n";
  193. assertFileContentsEqual(targetFile, result);
  194. assertEquals(RepositoryState.REBASING_INTERACTIVE, target
  195. .getRepository().getRepositoryState());
  196. }
  197. @Test
  198. public void testPullFastForwardWithLocalCommitAndRebaseFlagSet() throws Exception {
  199. final String SOURCE_COMMIT_MESSAGE = "Source commit message for rebase flag test";
  200. final String TARGET_COMMIT_MESSAGE = "Target commit message for rebase flag test";
  201. assertFalse(SOURCE_COMMIT_MESSAGE.equals(TARGET_COMMIT_MESSAGE));
  202. final String SOURCE_FILE_CONTENTS = "Source change";
  203. final String NEW_FILE_CONTENTS = "New file from target";
  204. // make sure the config for target says we should pull with merge
  205. // we will override this later with the setRebase method
  206. StoredConfig targetConfig = dbTarget.getConfig();
  207. targetConfig.setBoolean("branch", "master", "rebase", false);
  208. targetConfig.save();
  209. // create commit in source
  210. writeToFile(sourceFile, SOURCE_FILE_CONTENTS);
  211. source.add().addFilepattern(sourceFile.getName()).call();
  212. source.commit().setMessage(SOURCE_COMMIT_MESSAGE).call();
  213. // create commit in target, not conflicting with the new commit in source
  214. File newFile = new File(dbTarget.getWorkTree().getPath() + "/newFile.txt");
  215. writeToFile(newFile, NEW_FILE_CONTENTS);
  216. target.add().addFilepattern(newFile.getName()).call();
  217. target.commit().setMessage(TARGET_COMMIT_MESSAGE).call();
  218. // verify that rebase is set to false in the config
  219. assertFalse(targetConfig.getBoolean("branch", "master", "rebase", true));
  220. // pull with rebase - local commit in target should be on top
  221. PullResult pullResult = target.pull().setRebase(true).call();
  222. // make sure pull is considered successful
  223. assertTrue(pullResult.isSuccessful());
  224. // verify rebase result is ok
  225. RebaseResult rebaseResult = pullResult.getRebaseResult();
  226. assertNotNull(rebaseResult);
  227. assertNull(rebaseResult.getFailingPaths());
  228. assertEquals(Status.OK, rebaseResult.getStatus());
  229. // Get the HEAD and HEAD~1 commits
  230. Repository targetRepo = target.getRepository();
  231. RevWalk revWalk = new RevWalk(targetRepo);
  232. ObjectId headId = targetRepo.resolve(Constants.HEAD);
  233. RevCommit root = revWalk.parseCommit(headId);
  234. revWalk.markStart(root);
  235. // HEAD
  236. RevCommit head = revWalk.next();
  237. // HEAD~1
  238. RevCommit beforeHead = revWalk.next();
  239. // verify the commit message on the HEAD commit
  240. assertEquals(TARGET_COMMIT_MESSAGE, head.getFullMessage());
  241. // verify the commit just before HEAD
  242. assertEquals(SOURCE_COMMIT_MESSAGE, beforeHead.getFullMessage());
  243. // verify file states
  244. assertFileContentsEqual(sourceFile, SOURCE_FILE_CONTENTS);
  245. assertFileContentsEqual(newFile, NEW_FILE_CONTENTS);
  246. // verify repository state
  247. assertEquals(RepositoryState.SAFE, target
  248. .getRepository().getRepositoryState());
  249. }
  250. @Override
  251. @Before
  252. public void setUp() throws Exception {
  253. super.setUp();
  254. dbTarget = createWorkRepository();
  255. source = new Git(db);
  256. target = new Git(dbTarget);
  257. // put some file in the source repo
  258. sourceFile = new File(db.getWorkTree(), "SomeFile.txt");
  259. writeToFile(sourceFile, "Hello world");
  260. // and commit it
  261. source.add().addFilepattern("SomeFile.txt").call();
  262. source.commit().setMessage("Initial commit for source").call();
  263. // configure the target repo to connect to the source via "origin"
  264. StoredConfig targetConfig = dbTarget.getConfig();
  265. targetConfig.setString("branch", "master", "remote", "origin");
  266. targetConfig
  267. .setString("branch", "master", "merge", "refs/heads/master");
  268. RemoteConfig config = new RemoteConfig(targetConfig, "origin");
  269. config
  270. .addURI(new URIish(source.getRepository().getWorkTree()
  271. .getAbsolutePath()));
  272. config.addFetchRefSpec(new RefSpec(
  273. "+refs/heads/*:refs/remotes/origin/*"));
  274. config.update(targetConfig);
  275. targetConfig.save();
  276. targetFile = new File(dbTarget.getWorkTree(), "SomeFile.txt");
  277. // make sure we have the same content
  278. target.pull().call();
  279. target.checkout().setStartPoint("refs/remotes/origin/master").setName(
  280. "master").call();
  281. targetConfig
  282. .setString("branch", "master", "merge", "refs/heads/master");
  283. targetConfig.setBoolean("branch", "master", "rebase", true);
  284. targetConfig.save();
  285. assertFileContentsEqual(targetFile, "Hello world");
  286. }
  287. private static void writeToFile(File actFile, String string)
  288. throws IOException {
  289. FileOutputStream fos = null;
  290. try {
  291. fos = new FileOutputStream(actFile);
  292. fos.write(string.getBytes("UTF-8"));
  293. fos.close();
  294. } finally {
  295. if (fos != null)
  296. fos.close();
  297. }
  298. }
  299. private static void assertFileContentsEqual(File actFile, String string)
  300. throws IOException {
  301. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  302. FileInputStream fis = null;
  303. byte[] buffer = new byte[100];
  304. try {
  305. fis = new FileInputStream(actFile);
  306. int read = fis.read(buffer);
  307. while (read > 0) {
  308. bos.write(buffer, 0, read);
  309. read = fis.read(buffer);
  310. }
  311. String content = new String(bos.toByteArray(), "UTF-8");
  312. assertEquals(string, content);
  313. } finally {
  314. if (fis != null)
  315. fis.close();
  316. }
  317. }
  318. }