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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 " + db.getWorkTree();
  124. assertEquals(message, mergeCommit.getShortMessage());
  125. }
  126. @Test
  127. public void testPullConflict() throws Exception {
  128. PullResult res = target.pull().call();
  129. // nothing to update since we don't have different data yet
  130. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  131. assertTrue(res.getMergeResult().getMergeStatus().equals(
  132. MergeStatus.ALREADY_UP_TO_DATE));
  133. assertFileContentsEqual(targetFile, "Hello world");
  134. // change the source file
  135. writeToFile(sourceFile, "Source change");
  136. source.add().addFilepattern("SomeFile.txt").call();
  137. source.commit().setMessage("Source change in remote").call();
  138. // change the target file
  139. writeToFile(targetFile, "Target change");
  140. target.add().addFilepattern("SomeFile.txt").call();
  141. target.commit().setMessage("Target change in local").call();
  142. res = target.pull().call();
  143. String sourceChangeString = "Source change\n>>>>>>> branch 'master' of "
  144. + target.getRepository().getConfig().getString("remote",
  145. "origin", "url");
  146. assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  147. assertEquals(res.getMergeResult().getMergeStatus(),
  148. MergeStatus.CONFLICTING);
  149. String result = "<<<<<<< HEAD\nTarget change\n=======\n"
  150. + sourceChangeString + "\n";
  151. assertFileContentsEqual(targetFile, result);
  152. assertEquals(RepositoryState.MERGING, target.getRepository()
  153. .getRepositoryState());
  154. }
  155. @Test
  156. public void testPullLocalConflict() throws Exception {
  157. target.branchCreate().setName("basedOnMaster").setStartPoint(
  158. "refs/heads/master").setUpstreamMode(SetupUpstreamMode.TRACK)
  159. .call();
  160. target.getRepository().updateRef(Constants.HEAD).link(
  161. "refs/heads/basedOnMaster");
  162. PullResult res = target.pull().call();
  163. // nothing to update since we don't have different data yet
  164. assertNull(res.getFetchResult());
  165. assertTrue(res.getMergeResult().getMergeStatus().equals(
  166. MergeStatus.ALREADY_UP_TO_DATE));
  167. assertFileContentsEqual(targetFile, "Hello world");
  168. // change the file in master
  169. target.getRepository().updateRef(Constants.HEAD).link(
  170. "refs/heads/master");
  171. writeToFile(targetFile, "Master change");
  172. target.add().addFilepattern("SomeFile.txt").call();
  173. target.commit().setMessage("Source change in master").call();
  174. // change the file in slave
  175. target.getRepository().updateRef(Constants.HEAD).link(
  176. "refs/heads/basedOnMaster");
  177. writeToFile(targetFile, "Slave change");
  178. target.add().addFilepattern("SomeFile.txt").call();
  179. target.commit().setMessage("Source change in based on master").call();
  180. res = target.pull().call();
  181. String sourceChangeString = "Master change\n>>>>>>> branch 'master' of local repository";
  182. assertNull(res.getFetchResult());
  183. assertEquals(res.getMergeResult().getMergeStatus(),
  184. MergeStatus.CONFLICTING);
  185. String result = "<<<<<<< HEAD\nSlave change\n=======\n"
  186. + sourceChangeString + "\n";
  187. assertFileContentsEqual(targetFile, result);
  188. assertEquals(RepositoryState.MERGING, target.getRepository()
  189. .getRepositoryState());
  190. }
  191. @Test(expected = NoHeadException.class)
  192. public void testPullEmptyRepository() throws Exception {
  193. Repository empty = createWorkRepository();
  194. RefUpdate delete = empty.updateRef(Constants.HEAD, true);
  195. delete.setForceUpdate(true);
  196. delete.delete();
  197. Git.wrap(empty).pull().call();
  198. }
  199. @Override
  200. @Before
  201. public void setUp() throws Exception {
  202. super.setUp();
  203. dbTarget = createWorkRepository();
  204. source = new Git(db);
  205. target = new Git(dbTarget);
  206. // put some file in the source repo
  207. sourceFile = new File(db.getWorkTree(), "SomeFile.txt");
  208. writeToFile(sourceFile, "Hello world");
  209. // and commit it
  210. source.add().addFilepattern("SomeFile.txt").call();
  211. source.commit().setMessage("Initial commit for source").call();
  212. // configure the target repo to connect to the source via "origin"
  213. StoredConfig targetConfig = dbTarget.getConfig();
  214. targetConfig.setString("branch", "master", "remote", "origin");
  215. targetConfig
  216. .setString("branch", "master", "merge", "refs/heads/master");
  217. RemoteConfig config = new RemoteConfig(targetConfig, "origin");
  218. config
  219. .addURI(new URIish(source.getRepository().getWorkTree()
  220. .getPath()));
  221. config.addFetchRefSpec(new RefSpec(
  222. "+refs/heads/*:refs/remotes/origin/*"));
  223. config.update(targetConfig);
  224. targetConfig.save();
  225. targetFile = new File(dbTarget.getWorkTree(), "SomeFile.txt");
  226. // make sure we have the same content
  227. target.pull().call();
  228. assertFileContentsEqual(targetFile, "Hello world");
  229. }
  230. private static void writeToFile(File actFile, String string)
  231. 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 static 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. }