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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * Copyright (C) 2010, 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 java.util.concurrent.Callable;
  23. import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
  24. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  25. import org.eclipse.jgit.api.errors.NoHeadException;
  26. import org.eclipse.jgit.junit.JGitTestUtil;
  27. import org.eclipse.jgit.junit.RepositoryTestCase;
  28. import org.eclipse.jgit.lib.Constants;
  29. import org.eclipse.jgit.lib.ObjectId;
  30. import org.eclipse.jgit.lib.RefUpdate;
  31. import org.eclipse.jgit.lib.Repository;
  32. import org.eclipse.jgit.lib.RepositoryState;
  33. import org.eclipse.jgit.lib.StoredConfig;
  34. import org.eclipse.jgit.merge.ContentMergeStrategy;
  35. import org.eclipse.jgit.merge.MergeStrategy;
  36. import org.eclipse.jgit.revwalk.RevCommit;
  37. import org.eclipse.jgit.revwalk.RevSort;
  38. import org.eclipse.jgit.revwalk.RevWalk;
  39. import org.eclipse.jgit.transport.RefSpec;
  40. import org.eclipse.jgit.transport.RemoteConfig;
  41. import org.eclipse.jgit.transport.URIish;
  42. import org.junit.Before;
  43. import org.junit.Test;
  44. public class PullCommandTest extends RepositoryTestCase {
  45. /** Second Test repository */
  46. protected Repository dbTarget;
  47. private Git source;
  48. private Git target;
  49. private File sourceFile;
  50. private File targetFile;
  51. @Test
  52. public void testPullFastForward() throws Exception {
  53. PullResult res = target.pull().call();
  54. // nothing to update since we don't have different data yet
  55. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  56. assertTrue(res.getMergeResult().getMergeStatus().equals(
  57. MergeStatus.ALREADY_UP_TO_DATE));
  58. assertFileContentsEqual(targetFile, "Hello world");
  59. // change the source file
  60. writeToFile(sourceFile, "Another change");
  61. source.add().addFilepattern("SomeFile.txt").call();
  62. source.commit().setMessage("Some change in remote").call();
  63. res = target.pull().call();
  64. assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  65. assertEquals(res.getMergeResult().getMergeStatus(),
  66. MergeStatus.FAST_FORWARD);
  67. assertFileContentsEqual(targetFile, "Another change");
  68. assertEquals(RepositoryState.SAFE, target.getRepository()
  69. .getRepositoryState());
  70. res = target.pull().call();
  71. assertEquals(res.getMergeResult().getMergeStatus(),
  72. MergeStatus.ALREADY_UP_TO_DATE);
  73. }
  74. @Test
  75. public void testPullMerge() throws Exception {
  76. PullResult res = target.pull().call();
  77. // nothing to update since we don't have different data yet
  78. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  79. assertTrue(res.getMergeResult().getMergeStatus()
  80. .equals(MergeStatus.ALREADY_UP_TO_DATE));
  81. writeToFile(sourceFile, "Source change");
  82. source.add().addFilepattern("SomeFile.txt");
  83. RevCommit sourceCommit = source.commit()
  84. .setMessage("Source change in remote").call();
  85. File targetFile2 = new File(dbTarget.getWorkTree(), "OtherFile.txt");
  86. writeToFile(targetFile2, "Unconflicting change");
  87. target.add().addFilepattern("OtherFile.txt").call();
  88. RevCommit targetCommit = target.commit()
  89. .setMessage("Unconflicting change in local").call();
  90. res = target.pull().call();
  91. MergeResult mergeResult = res.getMergeResult();
  92. ObjectId[] mergedCommits = mergeResult.getMergedCommits();
  93. assertEquals(targetCommit.getId(), mergedCommits[0]);
  94. assertEquals(sourceCommit.getId(), mergedCommits[1]);
  95. try (RevWalk rw = new RevWalk(dbTarget)) {
  96. RevCommit mergeCommit = rw.parseCommit(mergeResult.getNewHead());
  97. String message = "Merge branch 'master' of "
  98. + db.getWorkTree().getAbsolutePath();
  99. assertEquals(message, mergeCommit.getShortMessage());
  100. }
  101. }
  102. @Test
  103. public void testPullConflict() throws Exception {
  104. PullResult res = target.pull().call();
  105. // nothing to update since we don't have different data yet
  106. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  107. assertTrue(res.getMergeResult().getMergeStatus().equals(
  108. MergeStatus.ALREADY_UP_TO_DATE));
  109. assertFileContentsEqual(targetFile, "Hello world");
  110. // change the source file
  111. writeToFile(sourceFile, "Source change");
  112. source.add().addFilepattern("SomeFile.txt").call();
  113. source.commit().setMessage("Source change in remote").call();
  114. // change the target file
  115. writeToFile(targetFile, "Target change");
  116. target.add().addFilepattern("SomeFile.txt").call();
  117. target.commit().setMessage("Target change in local").call();
  118. res = target.pull().call();
  119. String sourceChangeString = "Source change\n>>>>>>> branch 'master' of "
  120. + target.getRepository().getConfig().getString("remote",
  121. "origin", "url");
  122. assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  123. assertEquals(res.getMergeResult().getMergeStatus(),
  124. MergeStatus.CONFLICTING);
  125. String result = "<<<<<<< HEAD\nTarget change\n=======\n"
  126. + sourceChangeString + "\n";
  127. assertFileContentsEqual(targetFile, result);
  128. assertEquals(RepositoryState.MERGING, target.getRepository()
  129. .getRepositoryState());
  130. }
  131. @Test
  132. public void testPullConflictTheirs() throws Exception {
  133. PullResult res = target.pull().call();
  134. // nothing to update since we don't have different data yet
  135. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  136. assertTrue(res.getMergeResult().getMergeStatus()
  137. .equals(MergeStatus.ALREADY_UP_TO_DATE));
  138. assertFileContentsEqual(targetFile, "Hello world");
  139. // change the source file
  140. writeToFile(sourceFile, "Source change");
  141. source.add().addFilepattern("SomeFile.txt").call();
  142. source.commit().setMessage("Source change in remote").call();
  143. // change the target file
  144. writeToFile(targetFile, "Target change");
  145. target.add().addFilepattern("SomeFile.txt").call();
  146. target.commit().setMessage("Target change in local").call();
  147. res = target.pull().setStrategy(MergeStrategy.THEIRS).call();
  148. assertTrue(res.isSuccessful());
  149. assertFileContentsEqual(targetFile, "Source change");
  150. assertEquals(RepositoryState.SAFE,
  151. target.getRepository().getRepositoryState());
  152. assertTrue(target.status().call().isClean());
  153. }
  154. @Test
  155. public void testPullConflictXtheirs() throws Exception {
  156. PullResult res = target.pull().call();
  157. // nothing to update since we don't have different data yet
  158. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  159. assertTrue(res.getMergeResult().getMergeStatus()
  160. .equals(MergeStatus.ALREADY_UP_TO_DATE));
  161. assertFileContentsEqual(targetFile, "Hello world");
  162. // change the source file
  163. writeToFile(sourceFile, "a\nHello\nb\n");
  164. source.add().addFilepattern("SomeFile.txt").call();
  165. source.commit().setMessage("Multi-line change in remote").call();
  166. // Pull again
  167. res = target.pull().call();
  168. assertTrue(res.isSuccessful());
  169. assertFileContentsEqual(targetFile, "a\nHello\nb\n");
  170. // change the source file
  171. writeToFile(sourceFile, "a\nSource change\nb\n");
  172. source.add().addFilepattern("SomeFile.txt").call();
  173. source.commit().setMessage("Source change in remote").call();
  174. // change the target file
  175. writeToFile(targetFile, "a\nTarget change\nb\nc\n");
  176. target.add().addFilepattern("SomeFile.txt").call();
  177. target.commit().setMessage("Target change in local").call();
  178. res = target.pull().setContentMergeStrategy(ContentMergeStrategy.THEIRS)
  179. .call();
  180. assertTrue(res.isSuccessful());
  181. assertFileContentsEqual(targetFile, "a\nSource change\nb\nc\n");
  182. assertEquals(RepositoryState.SAFE,
  183. target.getRepository().getRepositoryState());
  184. assertTrue(target.status().call().isClean());
  185. }
  186. @Test
  187. public void testPullWithUntrackedStash() throws Exception {
  188. target.pull().call();
  189. // change the source file
  190. writeToFile(sourceFile, "Source change");
  191. source.add().addFilepattern("SomeFile.txt").call();
  192. source.commit().setMessage("Source change in remote").call();
  193. // write untracked file
  194. writeToFile(new File(dbTarget.getWorkTree(), "untracked.txt"),
  195. "untracked");
  196. RevCommit stash = target.stashCreate().setIndexMessage("message here")
  197. .setIncludeUntracked(true).call();
  198. assertNotNull(stash);
  199. assertTrue(target.status().call().isClean());
  200. // pull from source
  201. assertTrue(target.pull().call().isSuccessful());
  202. assertEquals("[SomeFile.txt, mode:100644, content:Source change]",
  203. indexState(dbTarget, CONTENT));
  204. assertFalse(JGitTestUtil.check(dbTarget, "untracked.txt"));
  205. assertEquals("Source change",
  206. JGitTestUtil.read(dbTarget, "SomeFile.txt"));
  207. // apply the stash
  208. target.stashApply().setStashRef(stash.getName()).call();
  209. assertEquals("[SomeFile.txt, mode:100644, content:Source change]",
  210. indexState(dbTarget, CONTENT));
  211. assertEquals("untracked", JGitTestUtil.read(dbTarget, "untracked.txt"));
  212. assertEquals("Source change",
  213. JGitTestUtil.read(dbTarget, "SomeFile.txt"));
  214. }
  215. @Test
  216. public void testPullLocalConflict() throws Exception {
  217. target.branchCreate().setName("basedOnMaster").setStartPoint(
  218. "refs/heads/master").setUpstreamMode(SetupUpstreamMode.TRACK)
  219. .call();
  220. target.getRepository().updateRef(Constants.HEAD).link(
  221. "refs/heads/basedOnMaster");
  222. PullResult res = target.pull().call();
  223. // nothing to update since we don't have different data yet
  224. assertNull(res.getFetchResult());
  225. assertTrue(res.getMergeResult().getMergeStatus().equals(
  226. MergeStatus.ALREADY_UP_TO_DATE));
  227. assertFileContentsEqual(targetFile, "Hello world");
  228. // change the file in master
  229. target.getRepository().updateRef(Constants.HEAD).link(
  230. "refs/heads/master");
  231. writeToFile(targetFile, "Master change");
  232. target.add().addFilepattern("SomeFile.txt").call();
  233. target.commit().setMessage("Source change in master").call();
  234. // change the file in slave
  235. target.getRepository().updateRef(Constants.HEAD).link(
  236. "refs/heads/basedOnMaster");
  237. writeToFile(targetFile, "Slave change");
  238. target.add().addFilepattern("SomeFile.txt").call();
  239. target.commit().setMessage("Source change in based on master").call();
  240. res = target.pull().call();
  241. String sourceChangeString = "Master change\n>>>>>>> branch 'master' of local repository";
  242. assertNull(res.getFetchResult());
  243. assertEquals(res.getMergeResult().getMergeStatus(),
  244. MergeStatus.CONFLICTING);
  245. String result = "<<<<<<< HEAD\nSlave change\n=======\n"
  246. + sourceChangeString + "\n";
  247. assertFileContentsEqual(targetFile, result);
  248. assertEquals(RepositoryState.MERGING, target.getRepository()
  249. .getRepositoryState());
  250. }
  251. @Test(expected = NoHeadException.class)
  252. public void testPullEmptyRepository() throws Exception {
  253. Repository empty = createWorkRepository();
  254. RefUpdate delete = empty.updateRef(Constants.HEAD, true);
  255. delete.setForceUpdate(true);
  256. delete.delete();
  257. Git.wrap(empty).pull().call();
  258. }
  259. @Test
  260. public void testPullMergeProgrammaticConfiguration() throws Exception {
  261. // create another commit on another branch in source
  262. source.checkout().setCreateBranch(true).setName("other").call();
  263. sourceFile = new File(db.getWorkTree(), "file2.txt");
  264. writeToFile(sourceFile, "content");
  265. source.add().addFilepattern("file2.txt").call();
  266. RevCommit sourceCommit = source.commit()
  267. .setMessage("source commit on branch other").call();
  268. File targetFile2 = new File(dbTarget.getWorkTree(), "OtherFile.txt");
  269. writeToFile(targetFile2, "Unconflicting change");
  270. target.add().addFilepattern("OtherFile.txt").call();
  271. RevCommit targetCommit = target.commit()
  272. .setMessage("Unconflicting change in local").call();
  273. PullResult res = target.pull().setRemote("origin")
  274. .setRemoteBranchName("other")
  275. .setRebase(false).call();
  276. MergeResult mergeResult = res.getMergeResult();
  277. ObjectId[] mergedCommits = mergeResult.getMergedCommits();
  278. assertEquals(targetCommit.getId(), mergedCommits[0]);
  279. assertEquals(sourceCommit.getId(), mergedCommits[1]);
  280. try (RevWalk rw = new RevWalk(dbTarget)) {
  281. RevCommit mergeCommit = rw.parseCommit(mergeResult.getNewHead());
  282. String message = "Merge branch 'other' of "
  283. + db.getWorkTree().getAbsolutePath();
  284. assertEquals(message, mergeCommit.getShortMessage());
  285. }
  286. }
  287. @Test
  288. public void testPullMergeProgrammaticConfigurationImpliedTargetBranch()
  289. throws Exception {
  290. // create another commit on another branch in source
  291. source.checkout().setCreateBranch(true).setName("other").call();
  292. sourceFile = new File(db.getWorkTree(), "file2.txt");
  293. writeToFile(sourceFile, "content");
  294. source.add().addFilepattern("file2.txt").call();
  295. RevCommit sourceCommit = source.commit()
  296. .setMessage("source commit on branch other").call();
  297. target.checkout().setCreateBranch(true).setName("other").call();
  298. File targetFile2 = new File(dbTarget.getWorkTree(), "OtherFile.txt");
  299. writeToFile(targetFile2, "Unconflicting change");
  300. target.add().addFilepattern("OtherFile.txt").call();
  301. RevCommit targetCommit = target.commit()
  302. .setMessage("Unconflicting change in local").call();
  303. // the source branch "other" matching the target branch should be
  304. // implied
  305. PullResult res = target.pull().setRemote("origin").setRebase(false)
  306. .call();
  307. MergeResult mergeResult = res.getMergeResult();
  308. ObjectId[] mergedCommits = mergeResult.getMergedCommits();
  309. assertEquals(targetCommit.getId(), mergedCommits[0]);
  310. assertEquals(sourceCommit.getId(), mergedCommits[1]);
  311. try (RevWalk rw = new RevWalk(dbTarget)) {
  312. RevCommit mergeCommit = rw.parseCommit(mergeResult.getNewHead());
  313. String message = "Merge branch 'other' of "
  314. + db.getWorkTree().getAbsolutePath() + " into other";
  315. assertEquals(message, mergeCommit.getShortMessage());
  316. }
  317. }
  318. private enum TestPullMode {
  319. MERGE, REBASE, REBASE_PREASERVE
  320. }
  321. @Test
  322. /** global rebase config should be respected */
  323. public void testPullWithRebasePreserve1Config() throws Exception {
  324. Callable<PullResult> setup = () -> {
  325. StoredConfig config = dbTarget.getConfig();
  326. config.setString("pull", null, "rebase", "preserve");
  327. config.save();
  328. return target.pull().call();
  329. };
  330. doTestPullWithRebase(setup, TestPullMode.REBASE_PREASERVE);
  331. }
  332. @Test
  333. /** the branch-local config should win over the global config */
  334. public void testPullWithRebasePreserveConfig2() throws Exception {
  335. Callable<PullResult> setup = () -> {
  336. StoredConfig config = dbTarget.getConfig();
  337. config.setString("pull", null, "rebase", "false");
  338. config.setString("branch", "master", "rebase", "preserve");
  339. config.save();
  340. return target.pull().call();
  341. };
  342. doTestPullWithRebase(setup, TestPullMode.REBASE_PREASERVE);
  343. }
  344. @Test
  345. /** the branch-local config should be respected */
  346. public void testPullWithRebasePreserveConfig3() throws Exception {
  347. Callable<PullResult> setup = () -> {
  348. StoredConfig config = dbTarget.getConfig();
  349. config.setString("branch", "master", "rebase", "preserve");
  350. config.save();
  351. return target.pull().call();
  352. };
  353. doTestPullWithRebase(setup, TestPullMode.REBASE_PREASERVE);
  354. }
  355. @Test
  356. /** global rebase config should be respected */
  357. public void testPullWithRebaseConfig1() throws Exception {
  358. Callable<PullResult> setup = () -> {
  359. StoredConfig config = dbTarget.getConfig();
  360. config.setString("pull", null, "rebase", "true");
  361. config.save();
  362. return target.pull().call();
  363. };
  364. doTestPullWithRebase(setup, TestPullMode.REBASE);
  365. }
  366. @Test
  367. /** the branch-local config should win over the global config */
  368. public void testPullWithRebaseConfig2() throws Exception {
  369. Callable<PullResult> setup = () -> {
  370. StoredConfig config = dbTarget.getConfig();
  371. config.setString("pull", null, "rebase", "preserve");
  372. config.setString("branch", "master", "rebase", "true");
  373. config.save();
  374. return target.pull().call();
  375. };
  376. doTestPullWithRebase(setup, TestPullMode.REBASE);
  377. }
  378. @Test
  379. /** the branch-local config should be respected */
  380. public void testPullWithRebaseConfig3() throws Exception {
  381. Callable<PullResult> setup = () -> {
  382. StoredConfig config = dbTarget.getConfig();
  383. config.setString("branch", "master", "rebase", "true");
  384. config.save();
  385. return target.pull().call();
  386. };
  387. doTestPullWithRebase(setup, TestPullMode.REBASE);
  388. }
  389. @Test
  390. /** without config it should merge */
  391. public void testPullWithoutConfig() throws Exception {
  392. Callable<PullResult> setup = target.pull()::call;
  393. doTestPullWithRebase(setup, TestPullMode.MERGE);
  394. }
  395. @Test
  396. /** the branch local config should win over the global config */
  397. public void testPullWithMergeConfig() throws Exception {
  398. Callable<PullResult> setup = () -> {
  399. StoredConfig config = dbTarget.getConfig();
  400. config.setString("pull", null, "rebase", "true");
  401. config.setString("branch", "master", "rebase", "false");
  402. config.save();
  403. return target.pull().call();
  404. };
  405. doTestPullWithRebase(setup, TestPullMode.MERGE);
  406. }
  407. @Test
  408. /** the branch local config should win over the global config */
  409. public void testPullWithMergeConfig2() throws Exception {
  410. Callable<PullResult> setup = () -> {
  411. StoredConfig config = dbTarget.getConfig();
  412. config.setString("pull", null, "rebase", "false");
  413. config.save();
  414. return target.pull().call();
  415. };
  416. doTestPullWithRebase(setup, TestPullMode.MERGE);
  417. }
  418. private void doTestPullWithRebase(Callable<PullResult> pullSetup,
  419. TestPullMode expectedPullMode) throws Exception {
  420. // simple upstream change
  421. writeToFile(sourceFile, "content");
  422. source.add().addFilepattern(sourceFile.getName()).call();
  423. RevCommit sourceCommit = source.commit().setMessage("source commit")
  424. .call();
  425. // create a merge commit in target
  426. File loxalFile = new File(dbTarget.getWorkTree(), "local.txt");
  427. writeToFile(loxalFile, "initial\n");
  428. target.add().addFilepattern("local.txt").call();
  429. RevCommit t1 = target.commit().setMessage("target commit 1").call();
  430. target.checkout().setCreateBranch(true).setName("side").call();
  431. String newContent = "initial\n" + "and more\n";
  432. writeToFile(loxalFile, newContent);
  433. target.add().addFilepattern("local.txt").call();
  434. RevCommit t2 = target.commit().setMessage("target commit 2").call();
  435. target.checkout().setName("master").call();
  436. MergeResult mergeResult = target.merge()
  437. .setFastForward(MergeCommand.FastForwardMode.NO_FF).include(t2)
  438. .call();
  439. assertEquals(MergeStatus.MERGED, mergeResult.getMergeStatus());
  440. assertFileContentsEqual(loxalFile, newContent);
  441. ObjectId merge = mergeResult.getNewHead();
  442. // pull
  443. PullResult res = pullSetup.call();
  444. assertNotNull(res.getFetchResult());
  445. if (expectedPullMode == TestPullMode.MERGE) {
  446. assertEquals(MergeStatus.MERGED, res.getMergeResult()
  447. .getMergeStatus());
  448. assertNull(res.getRebaseResult());
  449. } else {
  450. assertNull(res.getMergeResult());
  451. assertEquals(RebaseResult.OK_RESULT, res.getRebaseResult());
  452. }
  453. assertFileContentsEqual(sourceFile, "content");
  454. try (RevWalk rw = new RevWalk(dbTarget)) {
  455. rw.sort(RevSort.TOPO);
  456. rw.markStart(rw.parseCommit(dbTarget.resolve("refs/heads/master")));
  457. RevCommit next;
  458. if (expectedPullMode == TestPullMode.MERGE) {
  459. next = rw.next();
  460. assertEquals(2, next.getParentCount());
  461. assertEquals(merge, next.getParent(0));
  462. assertEquals(sourceCommit, next.getParent(1));
  463. // since both parents are known do no further checks here
  464. } else {
  465. if (expectedPullMode == TestPullMode.REBASE_PREASERVE) {
  466. next = rw.next();
  467. assertEquals(2, next.getParentCount());
  468. }
  469. next = rw.next();
  470. assertEquals(t2.getShortMessage(), next.getShortMessage());
  471. next = rw.next();
  472. assertEquals(t1.getShortMessage(), next.getShortMessage());
  473. next = rw.next();
  474. assertEquals(sourceCommit, next);
  475. next = rw.next();
  476. assertEquals("Initial commit for source",
  477. next.getShortMessage());
  478. next = rw.next();
  479. assertNull(next);
  480. }
  481. }
  482. }
  483. @Override
  484. @Before
  485. public void setUp() throws Exception {
  486. super.setUp();
  487. dbTarget = createWorkRepository();
  488. source = new Git(db);
  489. target = new Git(dbTarget);
  490. // put some file in the source repo
  491. sourceFile = new File(db.getWorkTree(), "SomeFile.txt");
  492. writeToFile(sourceFile, "Hello world");
  493. // and commit it
  494. source.add().addFilepattern("SomeFile.txt").call();
  495. source.commit().setMessage("Initial commit for source").call();
  496. // configure the target repo to connect to the source via "origin"
  497. StoredConfig targetConfig = dbTarget.getConfig();
  498. targetConfig.setString("branch", "master", "remote", "origin");
  499. targetConfig
  500. .setString("branch", "master", "merge", "refs/heads/master");
  501. RemoteConfig config = new RemoteConfig(targetConfig, "origin");
  502. config
  503. .addURI(new URIish(source.getRepository().getWorkTree()
  504. .getAbsolutePath()));
  505. config.addFetchRefSpec(new RefSpec(
  506. "+refs/heads/*:refs/remotes/origin/*"));
  507. config.update(targetConfig);
  508. targetConfig.save();
  509. targetFile = new File(dbTarget.getWorkTree(), "SomeFile.txt");
  510. // make sure we have the same content
  511. target.pull().call();
  512. assertFileContentsEqual(targetFile, "Hello world");
  513. }
  514. private static void writeToFile(File actFile, String string)
  515. throws IOException {
  516. try (FileOutputStream fos = new FileOutputStream(actFile)) {
  517. fos.write(string.getBytes(UTF_8));
  518. }
  519. }
  520. private static void assertFileContentsEqual(File actFile, String string)
  521. throws IOException {
  522. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  523. byte[] buffer = new byte[100];
  524. try (FileInputStream fis = new FileInputStream(actFile)) {
  525. int read = fis.read(buffer);
  526. while (read > 0) {
  527. bos.write(buffer, 0, read);
  528. read = fis.read(buffer);
  529. }
  530. String content = new String(bos.toByteArray(), UTF_8);
  531. assertEquals(string, content);
  532. }
  533. }
  534. }