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.

PullCommandTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (C) 2010, 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.errors.NoHeadException;
  56. import org.eclipse.jgit.junit.RepositoryTestCase;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.eclipse.jgit.lib.RefUpdate;
  60. import org.eclipse.jgit.lib.Repository;
  61. import org.eclipse.jgit.lib.RepositoryState;
  62. import org.eclipse.jgit.lib.StoredConfig;
  63. import org.eclipse.jgit.revwalk.RevCommit;
  64. import org.eclipse.jgit.revwalk.RevWalk;
  65. import org.eclipse.jgit.transport.RefSpec;
  66. import org.eclipse.jgit.transport.RemoteConfig;
  67. import org.eclipse.jgit.transport.URIish;
  68. import org.junit.Before;
  69. import org.junit.Test;
  70. public class PullCommandTest extends RepositoryTestCase {
  71. /** Second Test repository */
  72. protected Repository dbTarget;
  73. private Git source;
  74. private Git target;
  75. private File sourceFile;
  76. private File targetFile;
  77. @Test
  78. public void testPullFastForward() throws Exception {
  79. PullResult res = target.pull().call();
  80. // nothing to update since we don't have different data yet
  81. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  82. assertTrue(res.getMergeResult().getMergeStatus().equals(
  83. MergeStatus.ALREADY_UP_TO_DATE));
  84. assertFileContentsEqual(targetFile, "Hello world");
  85. // change the source file
  86. writeToFile(sourceFile, "Another change");
  87. source.add().addFilepattern("SomeFile.txt").call();
  88. source.commit().setMessage("Some change in remote").call();
  89. res = target.pull().call();
  90. assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  91. assertEquals(res.getMergeResult().getMergeStatus(),
  92. MergeStatus.FAST_FORWARD);
  93. assertFileContentsEqual(targetFile, "Another change");
  94. assertEquals(RepositoryState.SAFE, target.getRepository()
  95. .getRepositoryState());
  96. res = target.pull().call();
  97. assertEquals(res.getMergeResult().getMergeStatus(),
  98. MergeStatus.ALREADY_UP_TO_DATE);
  99. }
  100. @Test
  101. public void testPullMerge() throws Exception {
  102. PullResult res = target.pull().call();
  103. // nothing to update since we don't have different data yet
  104. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  105. assertTrue(res.getMergeResult().getMergeStatus()
  106. .equals(MergeStatus.ALREADY_UP_TO_DATE));
  107. writeToFile(sourceFile, "Source change");
  108. source.add().addFilepattern("SomeFile.txt");
  109. RevCommit sourceCommit = source.commit()
  110. .setMessage("Source change in remote").call();
  111. File targetFile2 = new File(dbTarget.getWorkTree(), "OtherFile.txt");
  112. writeToFile(targetFile2, "Unconflicting change");
  113. target.add().addFilepattern("OtherFile.txt").call();
  114. RevCommit targetCommit = target.commit()
  115. .setMessage("Unconflicting change in local").call();
  116. res = target.pull().call();
  117. MergeResult mergeResult = res.getMergeResult();
  118. ObjectId[] mergedCommits = mergeResult.getMergedCommits();
  119. assertEquals(targetCommit.getId(), mergedCommits[0]);
  120. assertEquals(sourceCommit.getId(), mergedCommits[1]);
  121. RevCommit mergeCommit = new RevWalk(dbTarget).parseCommit(mergeResult
  122. .getNewHead());
  123. String message = "Merge branch 'master' of "
  124. + db.getWorkTree().getAbsolutePath();
  125. assertEquals(message, mergeCommit.getShortMessage());
  126. }
  127. @Test
  128. public void testPullConflict() throws Exception {
  129. PullResult res = target.pull().call();
  130. // nothing to update since we don't have different data yet
  131. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  132. assertTrue(res.getMergeResult().getMergeStatus().equals(
  133. MergeStatus.ALREADY_UP_TO_DATE));
  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 sourceChangeString = "Source change\n>>>>>>> branch 'master' of "
  145. + target.getRepository().getConfig().getString("remote",
  146. "origin", "url");
  147. assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  148. assertEquals(res.getMergeResult().getMergeStatus(),
  149. MergeStatus.CONFLICTING);
  150. String result = "<<<<<<< HEAD\nTarget change\n=======\n"
  151. + sourceChangeString + "\n";
  152. assertFileContentsEqual(targetFile, result);
  153. assertEquals(RepositoryState.MERGING, target.getRepository()
  154. .getRepositoryState());
  155. }
  156. @Test
  157. public void testPullLocalConflict() throws Exception {
  158. target.branchCreate().setName("basedOnMaster").setStartPoint(
  159. "refs/heads/master").setUpstreamMode(SetupUpstreamMode.TRACK)
  160. .call();
  161. target.getRepository().updateRef(Constants.HEAD).link(
  162. "refs/heads/basedOnMaster");
  163. PullResult res = target.pull().call();
  164. // nothing to update since we don't have different data yet
  165. assertNull(res.getFetchResult());
  166. assertTrue(res.getMergeResult().getMergeStatus().equals(
  167. MergeStatus.ALREADY_UP_TO_DATE));
  168. assertFileContentsEqual(targetFile, "Hello world");
  169. // change the file in master
  170. target.getRepository().updateRef(Constants.HEAD).link(
  171. "refs/heads/master");
  172. writeToFile(targetFile, "Master change");
  173. target.add().addFilepattern("SomeFile.txt").call();
  174. target.commit().setMessage("Source change in master").call();
  175. // change the file in slave
  176. target.getRepository().updateRef(Constants.HEAD).link(
  177. "refs/heads/basedOnMaster");
  178. writeToFile(targetFile, "Slave change");
  179. target.add().addFilepattern("SomeFile.txt").call();
  180. target.commit().setMessage("Source change in based on master").call();
  181. res = target.pull().call();
  182. String sourceChangeString = "Master change\n>>>>>>> branch 'master' of local repository";
  183. assertNull(res.getFetchResult());
  184. assertEquals(res.getMergeResult().getMergeStatus(),
  185. MergeStatus.CONFLICTING);
  186. String result = "<<<<<<< HEAD\nSlave change\n=======\n"
  187. + sourceChangeString + "\n";
  188. assertFileContentsEqual(targetFile, result);
  189. assertEquals(RepositoryState.MERGING, target.getRepository()
  190. .getRepositoryState());
  191. }
  192. @Test(expected = NoHeadException.class)
  193. public void testPullEmptyRepository() throws Exception {
  194. Repository empty = createWorkRepository();
  195. RefUpdate delete = empty.updateRef(Constants.HEAD, true);
  196. delete.setForceUpdate(true);
  197. delete.delete();
  198. Git.wrap(empty).pull().call();
  199. }
  200. @Override
  201. @Before
  202. public void setUp() throws Exception {
  203. super.setUp();
  204. dbTarget = createWorkRepository();
  205. source = new Git(db);
  206. target = new Git(dbTarget);
  207. // put some file in the source repo
  208. sourceFile = new File(db.getWorkTree(), "SomeFile.txt");
  209. writeToFile(sourceFile, "Hello world");
  210. // and commit it
  211. source.add().addFilepattern("SomeFile.txt").call();
  212. source.commit().setMessage("Initial commit for source").call();
  213. // configure the target repo to connect to the source via "origin"
  214. StoredConfig targetConfig = dbTarget.getConfig();
  215. targetConfig.setString("branch", "master", "remote", "origin");
  216. targetConfig
  217. .setString("branch", "master", "merge", "refs/heads/master");
  218. RemoteConfig config = new RemoteConfig(targetConfig, "origin");
  219. config
  220. .addURI(new URIish(source.getRepository().getWorkTree()
  221. .getAbsolutePath()));
  222. config.addFetchRefSpec(new RefSpec(
  223. "+refs/heads/*:refs/remotes/origin/*"));
  224. config.update(targetConfig);
  225. targetConfig.save();
  226. targetFile = new File(dbTarget.getWorkTree(), "SomeFile.txt");
  227. // make sure we have the same content
  228. target.pull().call();
  229. assertFileContentsEqual(targetFile, "Hello world");
  230. }
  231. private static void writeToFile(File actFile, String string)
  232. throws IOException {
  233. FileOutputStream fos = null;
  234. try {
  235. fos = new FileOutputStream(actFile);
  236. fos.write(string.getBytes("UTF-8"));
  237. fos.close();
  238. } finally {
  239. if (fos != null)
  240. fos.close();
  241. }
  242. }
  243. private static void assertFileContentsEqual(File actFile, String string)
  244. throws IOException {
  245. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  246. FileInputStream fis = null;
  247. byte[] buffer = new byte[100];
  248. try {
  249. fis = new FileInputStream(actFile);
  250. int read = fis.read(buffer);
  251. while (read > 0) {
  252. bos.write(buffer, 0, read);
  253. read = fis.read(buffer);
  254. }
  255. String content = new String(bos.toByteArray(), "UTF-8");
  256. assertEquals(string, content);
  257. } finally {
  258. if (fis != null)
  259. fis.close();
  260. }
  261. }
  262. }