@Test public void testPullConflict() throws Exception { PullResult res = target.pull().call(); // nothing to update since we don't have different data yet assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty()); assertTrue(res.getMergeResult().getMergeStatus().equals(MergeStatus.ALREADY_UP_TO_DATE)); assertFileContentsEqual(targetFile, "Hello world"); // change the source file writeToFile(sourceFile, "Source change"); source.add().addFilepattern("SomeFile.txt").call(); source.commit().setMessage("Source change in remote").call(); // change the target file writeToFile(targetFile, "Target change"); target.add().addFilepattern("SomeFile.txt").call(); target.commit().setMessage("Target change in local").call(); res = target.pull().call(); String sourceChangeString = "Source change\n>>>>>>> branch 'refs/heads/master' of " + target.getRepository().getConfig().getString("remote", "origin", "url"); assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty()); assertEquals(res.getMergeResult().getMergeStatus(), MergeStatus.CONFLICTING); String result = "<<<<<<< HEAD\nTarget change\n=======\n" + sourceChangeString + "\n"; assertFileContentsEqual(targetFile, result); assertEquals(RepositoryState.MERGING, target.getRepository().getRepositoryState()); }
/* * Pull latest from 'source' and 'binary' repository. */ public boolean gitpull() throws GitException { boolean result = false; // do 'pull' on 'source' Git srcgit = Git.wrap(sourceRepository); Git bingit = Git.wrap(binaryRepository); try { PullResult srcpull = srcgit.pull().call(); PullResult binpull = bingit.pull().call(); if (srcpull.isSuccessful() && binpull.isSuccessful()) { if (srcpull.getFetchResult() != null && (srcpull.getFetchResult().getTrackingRefUpdates().size() > 0)) { result = true; } if (srcpull.getMergeResult() != null && (srcpull.getMergeResult().getMergeStatus() == MergeStatus.FAST_FORWARD || srcpull.getMergeResult().getMergeStatus() == MergeStatus.MERGED)) { result = true; } // TODO: rebase status needs to be checked but it is ignored for now } } catch (WrongRepositoryStateException e) { throw new GitException(e); } catch (InvalidConfigurationException e) { throw new GitException(e); } catch (DetachedHeadException e) { throw new GitException(e); } catch (InvalidRemoteException e) { throw new GitException(e); } catch (CanceledException e) { throw new GitException(e); } catch (RefNotFoundException e) { throw new GitException(e); } catch (NoHeadException e) { throw new GitException(e); } catch (TransportException e) { throw new GitException(e); } catch (GitAPIException e) { throw new GitException(e); } return result; }
@Override @Before public void setUp() throws Exception { super.setUp(); dbTarget = createWorkRepository(); source = new Git(db); target = new Git(dbTarget); // put some file in the source repo sourceFile = new File(db.getWorkTree(), "SomeFile.txt"); writeToFile(sourceFile, "Hello world"); // and commit it source.add().addFilepattern("SomeFile.txt").call(); source.commit().setMessage("Initial commit for source").call(); // configure the target repo to connect to the source via "origin" StoredConfig targetConfig = dbTarget.getConfig(); targetConfig.setString("branch", "master", "remote", "origin"); targetConfig.setString("branch", "master", "merge", "refs/heads/master"); RemoteConfig config = new RemoteConfig(targetConfig, "origin"); config.addURI(new URIish(source.getRepository().getWorkTree().getPath())); config.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*")); config.update(targetConfig); targetConfig.save(); targetFile = new File(dbTarget.getWorkTree(), "SomeFile.txt"); // make sure we have the same content target.pull().call(); assertFileContentsEqual(targetFile, "Hello world"); }
@Test public void testPullLocalConflict() throws Exception { target .branchCreate() .setName("basedOnMaster") .setStartPoint("refs/heads/master") .setUpstreamMode(SetupUpstreamMode.TRACK) .call(); target.getRepository().updateRef(Constants.HEAD).link("refs/heads/basedOnMaster"); PullResult res = target.pull().call(); // nothing to update since we don't have different data yet assertNull(res.getFetchResult()); assertTrue(res.getMergeResult().getMergeStatus().equals(MergeStatus.ALREADY_UP_TO_DATE)); assertFileContentsEqual(targetFile, "Hello world"); // change the file in master target.getRepository().updateRef(Constants.HEAD).link("refs/heads/master"); writeToFile(targetFile, "Master change"); target.add().addFilepattern("SomeFile.txt").call(); target.commit().setMessage("Source change in master").call(); // change the file in slave target.getRepository().updateRef(Constants.HEAD).link("refs/heads/basedOnMaster"); writeToFile(targetFile, "Slave change"); target.add().addFilepattern("SomeFile.txt").call(); target.commit().setMessage("Source change in based on master").call(); res = target.pull().call(); String sourceChangeString = "Master change\n>>>>>>> branch 'refs/heads/master' of local repository"; assertNull(res.getFetchResult()); assertEquals(res.getMergeResult().getMergeStatus(), MergeStatus.CONFLICTING); String result = "<<<<<<< HEAD\nSlave change\n=======\n" + sourceChangeString + "\n"; assertFileContentsEqual(targetFile, result); assertEquals(RepositoryState.MERGING, target.getRepository().getRepositoryState()); }
@Test public void testPullFastForward() throws Exception { PullResult res = target.pull().call(); // nothing to update since we don't have different data yet assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty()); assertTrue(res.getMergeResult().getMergeStatus().equals(MergeStatus.ALREADY_UP_TO_DATE)); assertFileContentsEqual(targetFile, "Hello world"); // change the source file writeToFile(sourceFile, "Another change"); source.add().addFilepattern("SomeFile.txt").call(); source.commit().setMessage("Some change in remote").call(); res = target.pull().call(); assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty()); assertEquals(res.getMergeResult().getMergeStatus(), MergeStatus.FAST_FORWARD); assertFileContentsEqual(targetFile, "Another change"); assertEquals(RepositoryState.SAFE, target.getRepository().getRepositoryState()); res = target.pull().call(); assertEquals(res.getMergeResult().getMergeStatus(), MergeStatus.ALREADY_UP_TO_DATE); }