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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.RebaseResult.Status;
  55. import org.eclipse.jgit.lib.Constants;
  56. import org.eclipse.jgit.lib.RepositoryState;
  57. import org.eclipse.jgit.lib.RepositoryTestCase;
  58. import org.eclipse.jgit.lib.StoredConfig;
  59. import org.eclipse.jgit.storage.file.FileRepository;
  60. import org.eclipse.jgit.transport.RefSpec;
  61. import org.eclipse.jgit.transport.RemoteConfig;
  62. import org.eclipse.jgit.transport.URIish;
  63. import org.junit.Before;
  64. import org.junit.Test;
  65. public class PullCommandWithRebaseTest extends RepositoryTestCase {
  66. /** Second Test repository */
  67. protected FileRepository dbTarget;
  68. private Git source;
  69. private Git target;
  70. private File sourceFile;
  71. private File targetFile;
  72. @Test
  73. public void testPullFastForward() throws Exception {
  74. PullResult res = target.pull().call();
  75. // nothing to update since we don't have different data yet
  76. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  77. assertEquals(Status.UP_TO_DATE, res.getRebaseResult().getStatus());
  78. assertFileContentsEqual(targetFile, "Hello world");
  79. // change the source file
  80. writeToFile(sourceFile, "Another change");
  81. source.add().addFilepattern("SomeFile.txt").call();
  82. source.commit().setMessage("Some change in remote").call();
  83. res = target.pull().call();
  84. assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  85. assertEquals(Status.FAST_FORWARD, res.getRebaseResult().getStatus());
  86. assertFileContentsEqual(targetFile, "Another change");
  87. assertEquals(RepositoryState.SAFE, target.getRepository()
  88. .getRepositoryState());
  89. res = target.pull().call();
  90. assertEquals(Status.UP_TO_DATE, res.getRebaseResult().getStatus());
  91. }
  92. @Test
  93. public void testPullConflict() throws Exception {
  94. PullResult res = target.pull().call();
  95. // nothing to update since we don't have different data yet
  96. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  97. assertTrue(res.getRebaseResult().getStatus().equals(Status.UP_TO_DATE));
  98. assertFileContentsEqual(targetFile, "Hello world");
  99. // change the source file
  100. writeToFile(sourceFile, "Source change");
  101. source.add().addFilepattern("SomeFile.txt").call();
  102. source.commit().setMessage("Source change in remote").call();
  103. // change the target file
  104. writeToFile(targetFile, "Target change");
  105. target.add().addFilepattern("SomeFile.txt").call();
  106. target.commit().setMessage("Target change in local").call();
  107. res = target.pull().call();
  108. assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  109. assertTrue(res.getRebaseResult().getStatus().equals(Status.STOPPED));
  110. String result = "<<<<<<< OURS\nSource change\n=======\nTarget change\n>>>>>>> THEIRS\n";
  111. assertFileContentsEqual(targetFile, result);
  112. assertEquals(RepositoryState.REBASING_INTERACTIVE, target
  113. .getRepository().getRepositoryState());
  114. }
  115. @Test
  116. public void testPullLocalConflict() throws Exception {
  117. target.branchCreate().setName("basedOnMaster").setStartPoint(
  118. "refs/heads/master").setUpstreamMode(SetupUpstreamMode.NOTRACK)
  119. .call();
  120. StoredConfig config = target.getRepository().getConfig();
  121. config.setString("branch", "basedOnMaster", "remote", ".");
  122. config.setString("branch", "basedOnMaster", "rebase",
  123. "refs/heads/master");
  124. config.save();
  125. target.getRepository().updateRef(Constants.HEAD).link(
  126. "refs/heads/basedOnMaster");
  127. PullResult res = target.pull().call();
  128. // nothing to update since we don't have different data yet
  129. assertNull(res.getFetchResult());
  130. assertEquals(Status.UP_TO_DATE, res.getRebaseResult().getStatus());
  131. assertFileContentsEqual(targetFile, "Hello world");
  132. // change the file in master
  133. target.getRepository().updateRef(Constants.HEAD).link(
  134. "refs/heads/master");
  135. writeToFile(targetFile, "Master change");
  136. target.add().addFilepattern("SomeFile.txt").call();
  137. target.commit().setMessage("Source change in master").call();
  138. // change the file in slave
  139. target.getRepository().updateRef(Constants.HEAD).link(
  140. "refs/heads/basedOnMaster");
  141. writeToFile(targetFile, "Slave change");
  142. target.add().addFilepattern("SomeFile.txt").call();
  143. target.commit().setMessage("Source change in based on master").call();
  144. res = target.pull().call();
  145. assertNull(res.getFetchResult());
  146. assertEquals(Status.STOPPED, res.getRebaseResult().getStatus());
  147. String result = "<<<<<<< OURS\nMaster change\n=======\nSlave change\n>>>>>>> THEIRS\n";
  148. assertFileContentsEqual(targetFile, result);
  149. assertEquals(RepositoryState.REBASING_INTERACTIVE, target
  150. .getRepository().getRepositoryState());
  151. }
  152. @Override
  153. @Before
  154. public void setUp() throws Exception {
  155. super.setUp();
  156. dbTarget = createWorkRepository();
  157. source = new Git(db);
  158. target = new Git(dbTarget);
  159. // put some file in the source repo
  160. sourceFile = new File(db.getWorkTree(), "SomeFile.txt");
  161. writeToFile(sourceFile, "Hello world");
  162. // and commit it
  163. source.add().addFilepattern("SomeFile.txt").call();
  164. source.commit().setMessage("Initial commit for source").call();
  165. // configure the target repo to connect to the source via "origin"
  166. StoredConfig targetConfig = dbTarget.getConfig();
  167. targetConfig.setString("branch", "master", "remote", "origin");
  168. targetConfig
  169. .setString("branch", "master", "merge", "refs/heads/master");
  170. RemoteConfig config = new RemoteConfig(targetConfig, "origin");
  171. config
  172. .addURI(new URIish(source.getRepository().getWorkTree()
  173. .getPath()));
  174. config.addFetchRefSpec(new RefSpec(
  175. "+refs/heads/*:refs/remotes/origin/*"));
  176. config.update(targetConfig);
  177. targetConfig.save();
  178. targetFile = new File(dbTarget.getWorkTree(), "SomeFile.txt");
  179. // make sure we have the same content
  180. target.pull().call();
  181. target.checkout().setStartPoint("refs/remotes/origin/master").setName(
  182. "master").call();
  183. targetConfig.setString("branch", "master", "rebase",
  184. "refs/remotes/origin/master");
  185. targetConfig.unset("branch", "master", "merge");
  186. targetConfig.save();
  187. assertFileContentsEqual(targetFile, "Hello world");
  188. }
  189. private void writeToFile(File actFile, String string) throws IOException {
  190. FileOutputStream fos = null;
  191. try {
  192. fos = new FileOutputStream(actFile);
  193. fos.write(string.getBytes("UTF-8"));
  194. fos.close();
  195. } finally {
  196. if (fos != null)
  197. fos.close();
  198. }
  199. }
  200. private void assertFileContentsEqual(File actFile, String string)
  201. throws IOException {
  202. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  203. FileInputStream fis = null;
  204. byte[] buffer = new byte[100];
  205. try {
  206. fis = new FileInputStream(actFile);
  207. int read = fis.read(buffer);
  208. while (read > 0) {
  209. bos.write(buffer, 0, read);
  210. read = fis.read(buffer);
  211. }
  212. String content = new String(bos.toByteArray(), "UTF-8");
  213. assertEquals(string, content);
  214. } finally {
  215. if (fis != null)
  216. fis.close();
  217. }
  218. }
  219. }