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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Copyright (C) 2011, Mathias Kinzler <mathias.kinzler@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.api;
  11. import static java.nio.charset.StandardCharsets.UTF_8;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertFalse;
  14. import static org.junit.Assert.assertNotNull;
  15. import static org.junit.Assert.assertNull;
  16. import static org.junit.Assert.assertTrue;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
  23. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  24. import org.eclipse.jgit.api.RebaseResult.Status;
  25. import org.eclipse.jgit.junit.RepositoryTestCase;
  26. import org.eclipse.jgit.lib.ConfigConstants;
  27. import org.eclipse.jgit.lib.Constants;
  28. import org.eclipse.jgit.lib.ObjectId;
  29. import org.eclipse.jgit.lib.Ref;
  30. import org.eclipse.jgit.lib.Repository;
  31. import org.eclipse.jgit.lib.RepositoryState;
  32. import org.eclipse.jgit.lib.StoredConfig;
  33. import org.eclipse.jgit.merge.MergeStrategy;
  34. import org.eclipse.jgit.revwalk.RevCommit;
  35. import org.eclipse.jgit.revwalk.RevWalk;
  36. import org.eclipse.jgit.transport.RefSpec;
  37. import org.eclipse.jgit.transport.RemoteConfig;
  38. import org.eclipse.jgit.transport.URIish;
  39. import org.junit.Before;
  40. import org.junit.Test;
  41. public class PullCommandWithRebaseTest extends RepositoryTestCase {
  42. /** Second Test repository */
  43. protected Repository dbTarget;
  44. private Git source;
  45. private Git target;
  46. private File sourceFile;
  47. private File targetFile;
  48. @Test
  49. public void testPullFastForward() throws Exception {
  50. PullResult res = target.pull().call();
  51. // nothing to update since we don't have different data yet
  52. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  53. assertEquals(Status.UP_TO_DATE, res.getRebaseResult().getStatus());
  54. assertFileContentsEqual(targetFile, "Hello world");
  55. // change the source file
  56. writeToFile(sourceFile, "Another change");
  57. source.add().addFilepattern("SomeFile.txt").call();
  58. source.commit().setMessage("Some change in remote").call();
  59. res = target.pull().call();
  60. assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  61. assertEquals(Status.FAST_FORWARD, res.getRebaseResult().getStatus());
  62. assertFileContentsEqual(targetFile, "Another change");
  63. assertEquals(RepositoryState.SAFE, target.getRepository()
  64. .getRepositoryState());
  65. res = target.pull().call();
  66. assertEquals(Status.UP_TO_DATE, res.getRebaseResult().getStatus());
  67. }
  68. @Test
  69. public void testPullFastForwardWithBranchInSource() throws Exception {
  70. PullResult res = target.pull().call();
  71. // nothing to update since we don't have different data yet
  72. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  73. assertEquals(Status.UP_TO_DATE, res.getRebaseResult().getStatus());
  74. assertFileContentsEqual(targetFile, "Hello world");
  75. // change the source file
  76. writeToFile(sourceFile, "Another change\n\n\n\nFoo");
  77. source.add().addFilepattern("SomeFile.txt").call();
  78. RevCommit initialCommit = source.commit()
  79. .setMessage("Some change in remote").call();
  80. // modify the source file in a branch
  81. createBranch(initialCommit, "refs/heads/side");
  82. checkoutBranch("refs/heads/side");
  83. writeToFile(sourceFile, "Another change\n\n\n\nBoo");
  84. source.add().addFilepattern("SomeFile.txt").call();
  85. RevCommit sideCommit = source.commit()
  86. .setMessage("Some change in remote").call();
  87. // modify the source file on master
  88. checkoutBranch("refs/heads/master");
  89. writeToFile(sourceFile, "More change\n\n\n\nFoo");
  90. source.add().addFilepattern("SomeFile.txt").call();
  91. source.commit().setMessage("Some change in remote").call();
  92. // merge side into master
  93. MergeResult result = source.merge().include(sideCommit.getId())
  94. .setStrategy(MergeStrategy.RESOLVE).call();
  95. assertEquals(MergeStatus.MERGED, result.getMergeStatus());
  96. }
  97. @Test
  98. public void testPullFastForwardDetachedHead() throws Exception {
  99. Repository repository = source.getRepository();
  100. writeToFile(sourceFile, "2nd commit");
  101. source.add().addFilepattern("SomeFile.txt").call();
  102. source.commit().setMessage("2nd commit").call();
  103. try (RevWalk revWalk = new RevWalk(repository)) {
  104. // git checkout HEAD^
  105. String initialBranch = repository.getBranch();
  106. Ref initialRef = repository.findRef(Constants.HEAD);
  107. RevCommit initialCommit = revWalk
  108. .parseCommit(initialRef.getObjectId());
  109. assertEquals("this test need linear history", 1,
  110. initialCommit.getParentCount());
  111. source.checkout().setName(initialCommit.getParent(0).getName())
  112. .call();
  113. assertFalse("expected detached HEAD",
  114. repository.getFullBranch().startsWith(Constants.R_HEADS));
  115. // change and commit another file
  116. File otherFile = new File(sourceFile.getParentFile(),
  117. System.currentTimeMillis() + ".tst");
  118. writeToFile(otherFile, "other 2nd commit");
  119. source.add().addFilepattern(otherFile.getName()).call();
  120. RevCommit newCommit = source.commit().setMessage("other 2nd commit")
  121. .call();
  122. // git pull --rebase initialBranch
  123. source.pull().setRebase(true).setRemote(".")
  124. .setRemoteBranchName(initialBranch)
  125. .call();
  126. assertEquals(RepositoryState.SAFE,
  127. source.getRepository().getRepositoryState());
  128. Ref head = source.getRepository().findRef(Constants.HEAD);
  129. RevCommit headCommit = revWalk.parseCommit(head.getObjectId());
  130. // HEAD^ == initialCommit, no merge commit
  131. assertEquals(1, headCommit.getParentCount());
  132. assertEquals(initialCommit, headCommit.getParent(0));
  133. // both contributions for both commits are available
  134. assertFileContentsEqual(sourceFile, "2nd commit");
  135. assertFileContentsEqual(otherFile, "other 2nd commit");
  136. // HEAD has same message as rebased commit
  137. assertEquals(newCommit.getShortMessage(),
  138. headCommit.getShortMessage());
  139. }
  140. }
  141. @Test
  142. public void testPullConflict() throws Exception {
  143. PullResult res = target.pull().call();
  144. // nothing to update since we don't have different data yet
  145. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  146. assertEquals(Status.UP_TO_DATE, res.getRebaseResult().getStatus());
  147. assertFileContentsEqual(targetFile, "Hello world");
  148. // change the source file
  149. writeToFile(sourceFile, "Source change");
  150. source.add().addFilepattern("SomeFile.txt").call();
  151. source.commit().setMessage("Source change in remote").call();
  152. // change the target file
  153. writeToFile(targetFile, "Target change");
  154. target.add().addFilepattern("SomeFile.txt").call();
  155. target.commit().setMessage("Target change in local").call();
  156. res = target.pull().call();
  157. String remoteUri = target
  158. .getRepository()
  159. .getConfig()
  160. .getString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin",
  161. ConfigConstants.CONFIG_KEY_URL);
  162. assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  163. assertEquals(Status.STOPPED, res.getRebaseResult().getStatus());
  164. String result = "<<<<<<< Upstream, based on branch 'master' of "
  165. + remoteUri
  166. + "\nSource change\n=======\nTarget change\n>>>>>>> 42453fd Target change in local\n";
  167. assertFileContentsEqual(targetFile, result);
  168. assertEquals(RepositoryState.REBASING_MERGE, target
  169. .getRepository().getRepositoryState());
  170. }
  171. @Test
  172. public void testPullLocalConflict() throws Exception {
  173. target.branchCreate().setName("basedOnMaster").setStartPoint(
  174. "refs/heads/master").setUpstreamMode(SetupUpstreamMode.NOTRACK)
  175. .call();
  176. StoredConfig config = target.getRepository().getConfig();
  177. config.setString("branch", "basedOnMaster", "remote", ".");
  178. config.setString("branch", "basedOnMaster", "merge",
  179. "refs/heads/master");
  180. config.setBoolean("branch", "basedOnMaster", "rebase", true);
  181. config.save();
  182. target.getRepository().updateRef(Constants.HEAD).link(
  183. "refs/heads/basedOnMaster");
  184. PullResult res = target.pull().call();
  185. // nothing to update since we don't have different data yet
  186. assertNull(res.getFetchResult());
  187. assertEquals(Status.UP_TO_DATE, res.getRebaseResult().getStatus());
  188. assertFileContentsEqual(targetFile, "Hello world");
  189. // change the file in master
  190. target.getRepository().updateRef(Constants.HEAD).link(
  191. "refs/heads/master");
  192. writeToFile(targetFile, "Master change");
  193. target.add().addFilepattern("SomeFile.txt").call();
  194. target.commit().setMessage("Source change in master").call();
  195. // change the file in slave
  196. target.getRepository().updateRef(Constants.HEAD).link(
  197. "refs/heads/basedOnMaster");
  198. writeToFile(targetFile, "Slave change");
  199. target.add().addFilepattern("SomeFile.txt").call();
  200. target.commit().setMessage("Source change in based on master").call();
  201. res = target.pull().call();
  202. assertNull(res.getFetchResult());
  203. assertEquals(Status.STOPPED, res.getRebaseResult().getStatus());
  204. String result = "<<<<<<< Upstream, based on branch 'master' of local repository\n"
  205. + "Master change\n=======\nSlave change\n>>>>>>> 4049c9e Source change in based on master\n";
  206. assertFileContentsEqual(targetFile, result);
  207. assertEquals(RepositoryState.REBASING_MERGE, target
  208. .getRepository().getRepositoryState());
  209. }
  210. @Test
  211. public void testPullFastForwardWithLocalCommitAndRebaseFlagSet() throws Exception {
  212. final String SOURCE_COMMIT_MESSAGE = "Source commit message for rebase flag test";
  213. final String TARGET_COMMIT_MESSAGE = "Target commit message for rebase flag test";
  214. assertFalse(SOURCE_COMMIT_MESSAGE.equals(TARGET_COMMIT_MESSAGE));
  215. final String SOURCE_FILE_CONTENTS = "Source change";
  216. final String NEW_FILE_CONTENTS = "New file from target";
  217. // make sure the config for target says we should pull with merge
  218. // we will override this later with the setRebase method
  219. StoredConfig targetConfig = dbTarget.getConfig();
  220. targetConfig.setBoolean("branch", "master", "rebase", false);
  221. targetConfig.save();
  222. // create commit in source
  223. writeToFile(sourceFile, SOURCE_FILE_CONTENTS);
  224. source.add().addFilepattern(sourceFile.getName()).call();
  225. source.commit().setMessage(SOURCE_COMMIT_MESSAGE).call();
  226. // create commit in target, not conflicting with the new commit in source
  227. File newFile = new File(dbTarget.getWorkTree().getPath() + "/newFile.txt");
  228. writeToFile(newFile, NEW_FILE_CONTENTS);
  229. target.add().addFilepattern(newFile.getName()).call();
  230. target.commit().setMessage(TARGET_COMMIT_MESSAGE).call();
  231. // verify that rebase is set to false in the config
  232. assertFalse(targetConfig.getBoolean("branch", "master", "rebase", true));
  233. // pull with rebase - local commit in target should be on top
  234. PullResult pullResult = target.pull().setRebase(true).call();
  235. // make sure pull is considered successful
  236. assertTrue(pullResult.isSuccessful());
  237. // verify rebase result is ok
  238. RebaseResult rebaseResult = pullResult.getRebaseResult();
  239. assertNotNull(rebaseResult);
  240. assertNull(rebaseResult.getFailingPaths());
  241. assertEquals(Status.OK, rebaseResult.getStatus());
  242. // Get the HEAD and HEAD~1 commits
  243. Repository targetRepo = target.getRepository();
  244. try (RevWalk revWalk = new RevWalk(targetRepo)) {
  245. ObjectId headId = targetRepo.resolve(Constants.HEAD);
  246. RevCommit root = revWalk.parseCommit(headId);
  247. revWalk.markStart(root);
  248. // HEAD
  249. RevCommit head = revWalk.next();
  250. // HEAD~1
  251. RevCommit beforeHead = revWalk.next();
  252. // verify the commit message on the HEAD commit
  253. assertEquals(TARGET_COMMIT_MESSAGE, head.getFullMessage());
  254. // verify the commit just before HEAD
  255. assertEquals(SOURCE_COMMIT_MESSAGE, beforeHead.getFullMessage());
  256. // verify file states
  257. assertFileContentsEqual(sourceFile, SOURCE_FILE_CONTENTS);
  258. assertFileContentsEqual(newFile, NEW_FILE_CONTENTS);
  259. // verify repository state
  260. assertEquals(RepositoryState.SAFE, target
  261. .getRepository().getRepositoryState());
  262. }
  263. }
  264. @Override
  265. @Before
  266. public void setUp() throws Exception {
  267. super.setUp();
  268. dbTarget = createWorkRepository();
  269. source = new Git(db);
  270. target = new Git(dbTarget);
  271. // put some file in the source repo
  272. sourceFile = new File(db.getWorkTree(), "SomeFile.txt");
  273. writeToFile(sourceFile, "Hello world");
  274. // and commit it
  275. source.add().addFilepattern("SomeFile.txt").call();
  276. source.commit().setMessage("Initial commit for source").call();
  277. // configure the target repo to connect to the source via "origin"
  278. StoredConfig targetConfig = dbTarget.getConfig();
  279. targetConfig.setString("branch", "master", "remote", "origin");
  280. targetConfig
  281. .setString("branch", "master", "merge", "refs/heads/master");
  282. RemoteConfig config = new RemoteConfig(targetConfig, "origin");
  283. config
  284. .addURI(new URIish(source.getRepository().getWorkTree()
  285. .getAbsolutePath()));
  286. config.addFetchRefSpec(new RefSpec(
  287. "+refs/heads/*:refs/remotes/origin/*"));
  288. config.update(targetConfig);
  289. targetConfig.save();
  290. targetFile = new File(dbTarget.getWorkTree(), "SomeFile.txt");
  291. // make sure we have the same content
  292. target.pull().call();
  293. target.checkout().setStartPoint("refs/remotes/origin/master").setName(
  294. "master").call();
  295. targetConfig
  296. .setString("branch", "master", "merge", "refs/heads/master");
  297. targetConfig.setBoolean("branch", "master", "rebase", true);
  298. targetConfig.save();
  299. assertFileContentsEqual(targetFile, "Hello world");
  300. }
  301. private static void writeToFile(File actFile, String string)
  302. throws IOException {
  303. try (FileOutputStream fos = new FileOutputStream(actFile)) {
  304. fos.write(string.getBytes(UTF_8));
  305. }
  306. }
  307. private static void assertFileContentsEqual(File actFile, String string)
  308. throws IOException {
  309. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  310. byte[] buffer = new byte[100];
  311. try (FileInputStream fis = new FileInputStream(actFile)) {
  312. int read = fis.read(buffer);
  313. while (read > 0) {
  314. bos.write(buffer, 0, read);
  315. read = fis.read(buffer);
  316. }
  317. String content = new String(bos.toByteArray(), UTF_8);
  318. assertEquals(string, content);
  319. }
  320. }
  321. }