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

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