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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. public PullResult call() throws Exception {
  301. StoredConfig config = dbTarget.getConfig();
  302. config.setString("pull", null, "rebase", "preserve");
  303. config.save();
  304. return target.pull().call();
  305. }
  306. };
  307. doTestPullWithRebase(setup, TestPullMode.REBASE_PREASERVE);
  308. }
  309. @Test
  310. /** the branch-local config should win over the global config */
  311. public void testPullWithRebasePreserveConfig2() throws Exception {
  312. Callable<PullResult> setup = new Callable<PullResult>() {
  313. public PullResult call() throws Exception {
  314. StoredConfig config = dbTarget.getConfig();
  315. config.setString("pull", null, "rebase", "false");
  316. config.setString("branch", "master", "rebase", "preserve");
  317. config.save();
  318. return target.pull().call();
  319. }
  320. };
  321. doTestPullWithRebase(setup, TestPullMode.REBASE_PREASERVE);
  322. }
  323. @Test
  324. /** the branch-local config should be respected */
  325. public void testPullWithRebasePreserveConfig3() throws Exception {
  326. Callable<PullResult> setup = new Callable<PullResult>() {
  327. public PullResult call() throws Exception {
  328. StoredConfig config = dbTarget.getConfig();
  329. config.setString("branch", "master", "rebase", "preserve");
  330. config.save();
  331. return target.pull().call();
  332. }
  333. };
  334. doTestPullWithRebase(setup, TestPullMode.REBASE_PREASERVE);
  335. }
  336. @Test
  337. /** global rebase config should be respected */
  338. public void testPullWithRebaseConfig1() throws Exception {
  339. Callable<PullResult> setup = new Callable<PullResult>() {
  340. public PullResult call() throws Exception {
  341. StoredConfig config = dbTarget.getConfig();
  342. config.setString("pull", null, "rebase", "true");
  343. config.save();
  344. return target.pull().call();
  345. }
  346. };
  347. doTestPullWithRebase(setup, TestPullMode.REBASE);
  348. }
  349. @Test
  350. /** the branch-local config should win over the global config */
  351. public void testPullWithRebaseConfig2() throws Exception {
  352. Callable<PullResult> setup = new Callable<PullResult>() {
  353. public PullResult call() throws Exception {
  354. StoredConfig config = dbTarget.getConfig();
  355. config.setString("pull", null, "rebase", "preserve");
  356. config.setString("branch", "master", "rebase", "true");
  357. config.save();
  358. return target.pull().call();
  359. }
  360. };
  361. doTestPullWithRebase(setup, TestPullMode.REBASE);
  362. }
  363. @Test
  364. /** the branch-local config should be respected */
  365. public void testPullWithRebaseConfig3() throws Exception {
  366. Callable<PullResult> setup = new Callable<PullResult>() {
  367. public PullResult call() throws Exception {
  368. StoredConfig config = dbTarget.getConfig();
  369. config.setString("branch", "master", "rebase", "true");
  370. config.save();
  371. return target.pull().call();
  372. }
  373. };
  374. doTestPullWithRebase(setup, TestPullMode.REBASE);
  375. }
  376. @Test
  377. /** without config it should merge */
  378. public void testPullWithoutConfig() throws Exception {
  379. Callable<PullResult> setup = new Callable<PullResult>() {
  380. public PullResult call() throws Exception {
  381. return target.pull().call();
  382. }
  383. };
  384. doTestPullWithRebase(setup, TestPullMode.MERGE);
  385. }
  386. @Test
  387. /** the branch local config should win over the global config */
  388. public void testPullWithMergeConfig() throws Exception {
  389. Callable<PullResult> setup = new Callable<PullResult>() {
  390. public PullResult call() throws Exception {
  391. StoredConfig config = dbTarget.getConfig();
  392. config.setString("pull", null, "rebase", "true");
  393. config.setString("branch", "master", "rebase", "false");
  394. config.save();
  395. return target.pull().call();
  396. }
  397. };
  398. doTestPullWithRebase(setup, TestPullMode.MERGE);
  399. }
  400. @Test
  401. /** the branch local config should win over the global config */
  402. public void testPullWithMergeConfig2() throws Exception {
  403. Callable<PullResult> setup = new Callable<PullResult>() {
  404. public PullResult call() throws Exception {
  405. StoredConfig config = dbTarget.getConfig();
  406. config.setString("pull", null, "rebase", "false");
  407. config.save();
  408. return target.pull().call();
  409. }
  410. };
  411. doTestPullWithRebase(setup, TestPullMode.MERGE);
  412. }
  413. private void doTestPullWithRebase(Callable<PullResult> pullSetup,
  414. TestPullMode expectedPullMode) throws Exception {
  415. // simple upstream change
  416. writeToFile(sourceFile, "content");
  417. source.add().addFilepattern(sourceFile.getName()).call();
  418. RevCommit sourceCommit = source.commit().setMessage("source commit")
  419. .call();
  420. // create a merge commit in target
  421. File loxalFile = new File(dbTarget.getWorkTree(), "local.txt");
  422. writeToFile(loxalFile, "initial\n");
  423. target.add().addFilepattern("local.txt").call();
  424. RevCommit t1 = target.commit().setMessage("target commit 1").call();
  425. target.checkout().setCreateBranch(true).setName("side").call();
  426. String newContent = "initial\n" + "and more\n";
  427. writeToFile(loxalFile, newContent);
  428. target.add().addFilepattern("local.txt").call();
  429. RevCommit t2 = target.commit().setMessage("target commit 2").call();
  430. target.checkout().setName("master").call();
  431. MergeResult mergeResult = target.merge()
  432. .setFastForward(MergeCommand.FastForwardMode.NO_FF).include(t2)
  433. .call();
  434. assertEquals(MergeStatus.MERGED, mergeResult.getMergeStatus());
  435. assertFileContentsEqual(loxalFile, newContent);
  436. ObjectId merge = mergeResult.getNewHead();
  437. // pull
  438. PullResult res = pullSetup.call();
  439. assertNotNull(res.getFetchResult());
  440. if (expectedPullMode == TestPullMode.MERGE) {
  441. assertEquals(MergeStatus.MERGED, res.getMergeResult()
  442. .getMergeStatus());
  443. assertNull(res.getRebaseResult());
  444. } else {
  445. assertNull(res.getMergeResult());
  446. assertEquals(RebaseResult.OK_RESULT, res.getRebaseResult());
  447. }
  448. assertFileContentsEqual(sourceFile, "content");
  449. try (RevWalk rw = new RevWalk(dbTarget)) {
  450. rw.sort(RevSort.TOPO);
  451. rw.markStart(rw.parseCommit(dbTarget.resolve("refs/heads/master")));
  452. RevCommit next;
  453. if (expectedPullMode == TestPullMode.MERGE) {
  454. next = rw.next();
  455. assertEquals(2, next.getParentCount());
  456. assertEquals(merge, next.getParent(0));
  457. assertEquals(sourceCommit, next.getParent(1));
  458. // since both parents are known do no further checks here
  459. } else {
  460. if (expectedPullMode == TestPullMode.REBASE_PREASERVE) {
  461. next = rw.next();
  462. assertEquals(2, next.getParentCount());
  463. }
  464. next = rw.next();
  465. assertEquals(t2.getShortMessage(), next.getShortMessage());
  466. next = rw.next();
  467. assertEquals(t1.getShortMessage(), next.getShortMessage());
  468. next = rw.next();
  469. assertEquals(sourceCommit, next);
  470. next = rw.next();
  471. assertEquals("Initial commit for source",
  472. next.getShortMessage());
  473. next = rw.next();
  474. assertNull(next);
  475. }
  476. }
  477. }
  478. @Override
  479. @Before
  480. public void setUp() throws Exception {
  481. super.setUp();
  482. dbTarget = createWorkRepository();
  483. source = new Git(db);
  484. target = new Git(dbTarget);
  485. // put some file in the source repo
  486. sourceFile = new File(db.getWorkTree(), "SomeFile.txt");
  487. writeToFile(sourceFile, "Hello world");
  488. // and commit it
  489. source.add().addFilepattern("SomeFile.txt").call();
  490. source.commit().setMessage("Initial commit for source").call();
  491. // configure the target repo to connect to the source via "origin"
  492. StoredConfig targetConfig = dbTarget.getConfig();
  493. targetConfig.setString("branch", "master", "remote", "origin");
  494. targetConfig
  495. .setString("branch", "master", "merge", "refs/heads/master");
  496. RemoteConfig config = new RemoteConfig(targetConfig, "origin");
  497. config
  498. .addURI(new URIish(source.getRepository().getWorkTree()
  499. .getAbsolutePath()));
  500. config.addFetchRefSpec(new RefSpec(
  501. "+refs/heads/*:refs/remotes/origin/*"));
  502. config.update(targetConfig);
  503. targetConfig.save();
  504. targetFile = new File(dbTarget.getWorkTree(), "SomeFile.txt");
  505. // make sure we have the same content
  506. target.pull().call();
  507. assertFileContentsEqual(targetFile, "Hello world");
  508. }
  509. private static void writeToFile(File actFile, String string)
  510. throws IOException {
  511. FileOutputStream fos = null;
  512. try {
  513. fos = new FileOutputStream(actFile);
  514. fos.write(string.getBytes("UTF-8"));
  515. fos.close();
  516. } finally {
  517. if (fos != null)
  518. fos.close();
  519. }
  520. }
  521. private static void assertFileContentsEqual(File actFile, String string)
  522. throws IOException {
  523. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  524. FileInputStream fis = null;
  525. byte[] buffer = new byte[100];
  526. try {
  527. fis = new FileInputStream(actFile);
  528. int read = fis.read(buffer);
  529. while (read > 0) {
  530. bos.write(buffer, 0, read);
  531. read = fis.read(buffer);
  532. }
  533. String content = new String(bos.toByteArray(), "UTF-8");
  534. assertEquals(string, content);
  535. } finally {
  536. if (fis != null)
  537. fis.close();
  538. }
  539. }
  540. }