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

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