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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.assertNull;
  47. import static org.junit.Assert.assertTrue;
  48. import java.io.ByteArrayOutputStream;
  49. import java.io.File;
  50. import java.io.FileInputStream;
  51. import java.io.FileOutputStream;
  52. import java.io.IOException;
  53. import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
  54. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  55. import org.eclipse.jgit.api.RebaseResult.Status;
  56. import org.eclipse.jgit.lib.ConfigConstants;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.lib.RepositoryState;
  59. import org.eclipse.jgit.lib.RepositoryTestCase;
  60. import org.eclipse.jgit.lib.StoredConfig;
  61. import org.eclipse.jgit.merge.MergeStrategy;
  62. import org.eclipse.jgit.revwalk.RevCommit;
  63. import org.eclipse.jgit.storage.file.FileRepository;
  64. import org.eclipse.jgit.transport.RefSpec;
  65. import org.eclipse.jgit.transport.RemoteConfig;
  66. import org.eclipse.jgit.transport.URIish;
  67. import org.junit.Before;
  68. import org.junit.Test;
  69. public class PullCommandWithRebaseTest extends RepositoryTestCase {
  70. /** Second Test repository */
  71. protected FileRepository dbTarget;
  72. private Git source;
  73. private Git target;
  74. private File sourceFile;
  75. private File targetFile;
  76. @Test
  77. public void testPullFastForward() throws Exception {
  78. PullResult res = target.pull().call();
  79. // nothing to update since we don't have different data yet
  80. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  81. assertEquals(Status.UP_TO_DATE, res.getRebaseResult().getStatus());
  82. assertFileContentsEqual(targetFile, "Hello world");
  83. // change the source file
  84. writeToFile(sourceFile, "Another change");
  85. source.add().addFilepattern("SomeFile.txt").call();
  86. source.commit().setMessage("Some change in remote").call();
  87. res = target.pull().call();
  88. assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  89. assertEquals(Status.FAST_FORWARD, res.getRebaseResult().getStatus());
  90. assertFileContentsEqual(targetFile, "Another change");
  91. assertEquals(RepositoryState.SAFE, target.getRepository()
  92. .getRepositoryState());
  93. res = target.pull().call();
  94. assertEquals(Status.UP_TO_DATE, res.getRebaseResult().getStatus());
  95. }
  96. @Test
  97. public void testPullFastForwardWithBranchInSource() throws Exception {
  98. PullResult res = target.pull().call();
  99. // nothing to update since we don't have different data yet
  100. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  101. assertEquals(Status.UP_TO_DATE, res.getRebaseResult().getStatus());
  102. assertFileContentsEqual(targetFile, "Hello world");
  103. // change the source file
  104. writeToFile(sourceFile, "Another change\n\n\n\nFoo");
  105. source.add().addFilepattern("SomeFile.txt").call();
  106. RevCommit initialCommit = source.commit()
  107. .setMessage("Some change in remote").call();
  108. // modify the source file in a branch
  109. createBranch(initialCommit, "refs/heads/side");
  110. checkoutBranch("refs/heads/side");
  111. writeToFile(sourceFile, "Another change\n\n\n\nBoo");
  112. source.add().addFilepattern("SomeFile.txt").call();
  113. RevCommit sideCommit = source.commit()
  114. .setMessage("Some change in remote").call();
  115. // modify the source file on master
  116. checkoutBranch("refs/heads/master");
  117. writeToFile(sourceFile, "More change\n\n\n\nFoo");
  118. source.add().addFilepattern("SomeFile.txt").call();
  119. source.commit().setMessage("Some change in remote").call();
  120. // merge side into master
  121. MergeResult result = source.merge().include(sideCommit.getId())
  122. .setStrategy(MergeStrategy.RESOLVE).call();
  123. assertEquals(MergeStatus.MERGED, result.getMergeStatus());
  124. }
  125. @Test
  126. public void testPullConflict() throws Exception {
  127. PullResult res = target.pull().call();
  128. // nothing to update since we don't have different data yet
  129. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  130. assertTrue(res.getRebaseResult().getStatus().equals(Status.UP_TO_DATE));
  131. assertFileContentsEqual(targetFile, "Hello world");
  132. // change the source file
  133. writeToFile(sourceFile, "Source change");
  134. source.add().addFilepattern("SomeFile.txt").call();
  135. source.commit().setMessage("Source change in remote").call();
  136. // change the target file
  137. writeToFile(targetFile, "Target change");
  138. target.add().addFilepattern("SomeFile.txt").call();
  139. target.commit().setMessage("Target change in local").call();
  140. res = target.pull().call();
  141. String remoteUri = target
  142. .getRepository()
  143. .getConfig()
  144. .getString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin",
  145. ConfigConstants.CONFIG_KEY_URL);
  146. assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  147. assertTrue(res.getRebaseResult().getStatus().equals(Status.STOPPED));
  148. String result = "<<<<<<< Upstream, based on branch 'master' of "
  149. + remoteUri
  150. + "\nSource change\n=======\nTarget change\n>>>>>>> 42453fd Target change in local\n";
  151. assertFileContentsEqual(targetFile, result);
  152. assertEquals(RepositoryState.REBASING_INTERACTIVE, target
  153. .getRepository().getRepositoryState());
  154. }
  155. @Test
  156. public void testPullLocalConflict() throws Exception {
  157. target.branchCreate().setName("basedOnMaster").setStartPoint(
  158. "refs/heads/master").setUpstreamMode(SetupUpstreamMode.NOTRACK)
  159. .call();
  160. StoredConfig config = target.getRepository().getConfig();
  161. config.setString("branch", "basedOnMaster", "remote", ".");
  162. config.setString("branch", "basedOnMaster", "merge",
  163. "refs/heads/master");
  164. config.setBoolean("branch", "basedOnMaster", "rebase", true);
  165. config.save();
  166. target.getRepository().updateRef(Constants.HEAD).link(
  167. "refs/heads/basedOnMaster");
  168. PullResult res = target.pull().call();
  169. // nothing to update since we don't have different data yet
  170. assertNull(res.getFetchResult());
  171. assertEquals(Status.UP_TO_DATE, res.getRebaseResult().getStatus());
  172. assertFileContentsEqual(targetFile, "Hello world");
  173. // change the file in master
  174. target.getRepository().updateRef(Constants.HEAD).link(
  175. "refs/heads/master");
  176. writeToFile(targetFile, "Master change");
  177. target.add().addFilepattern("SomeFile.txt").call();
  178. target.commit().setMessage("Source change in master").call();
  179. // change the file in slave
  180. target.getRepository().updateRef(Constants.HEAD).link(
  181. "refs/heads/basedOnMaster");
  182. writeToFile(targetFile, "Slave change");
  183. target.add().addFilepattern("SomeFile.txt").call();
  184. target.commit().setMessage("Source change in based on master").call();
  185. res = target.pull().call();
  186. assertNull(res.getFetchResult());
  187. assertEquals(Status.STOPPED, res.getRebaseResult().getStatus());
  188. String result = "<<<<<<< Upstream, based on branch 'master' of local repository\n"
  189. + "Master change\n=======\nSlave change\n>>>>>>> 4049c9e Source change in based on master\n";
  190. assertFileContentsEqual(targetFile, result);
  191. assertEquals(RepositoryState.REBASING_INTERACTIVE, target
  192. .getRepository().getRepositoryState());
  193. }
  194. @Override
  195. @Before
  196. public void setUp() throws Exception {
  197. super.setUp();
  198. dbTarget = createWorkRepository();
  199. source = new Git(db);
  200. target = new Git(dbTarget);
  201. // put some file in the source repo
  202. sourceFile = new File(db.getWorkTree(), "SomeFile.txt");
  203. writeToFile(sourceFile, "Hello world");
  204. // and commit it
  205. source.add().addFilepattern("SomeFile.txt").call();
  206. source.commit().setMessage("Initial commit for source").call();
  207. // configure the target repo to connect to the source via "origin"
  208. StoredConfig targetConfig = dbTarget.getConfig();
  209. targetConfig.setString("branch", "master", "remote", "origin");
  210. targetConfig
  211. .setString("branch", "master", "merge", "refs/heads/master");
  212. RemoteConfig config = new RemoteConfig(targetConfig, "origin");
  213. config
  214. .addURI(new URIish(source.getRepository().getWorkTree()
  215. .getPath()));
  216. config.addFetchRefSpec(new RefSpec(
  217. "+refs/heads/*:refs/remotes/origin/*"));
  218. config.update(targetConfig);
  219. targetConfig.save();
  220. targetFile = new File(dbTarget.getWorkTree(), "SomeFile.txt");
  221. // make sure we have the same content
  222. target.pull().call();
  223. target.checkout().setStartPoint("refs/remotes/origin/master").setName(
  224. "master").call();
  225. targetConfig
  226. .setString("branch", "master", "merge", "refs/heads/master");
  227. targetConfig.setBoolean("branch", "master", "rebase", true);
  228. targetConfig.save();
  229. assertFileContentsEqual(targetFile, "Hello world");
  230. }
  231. private void writeToFile(File actFile, String string) throws IOException {
  232. FileOutputStream fos = null;
  233. try {
  234. fos = new FileOutputStream(actFile);
  235. fos.write(string.getBytes("UTF-8"));
  236. fos.close();
  237. } finally {
  238. if (fos != null)
  239. fos.close();
  240. }
  241. }
  242. private void assertFileContentsEqual(File actFile, String string)
  243. throws IOException {
  244. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  245. FileInputStream fis = null;
  246. byte[] buffer = new byte[100];
  247. try {
  248. fis = new FileInputStream(actFile);
  249. int read = fis.read(buffer);
  250. while (read > 0) {
  251. bos.write(buffer, 0, read);
  252. read = fis.read(buffer);
  253. }
  254. String content = new String(bos.toByteArray(), "UTF-8");
  255. assertEquals(string, content);
  256. } finally {
  257. if (fis != null)
  258. fis.close();
  259. }
  260. }
  261. }