@Test public void testConflictsFromOtherPush() throws Exception { // now Repository 2 update the file and push File file1 = new File(workdir2, "file.txt"); repositoryUtil.appendFileContent(file1, "-->from Repository 2"); Git git = new Git(repository2); git.add().addFilepattern("file.txt").call(); git.commit().setMessage("Second Commit").call(); URIish remote = new URIish("file:///" + repository1.getDirectory().toString()); // now push the Repository 2 into the Repository 1 RefSpec rs = new RefSpec(); rs = rs.setSourceDestination("refs/heads/master", "refs/heads/master"); PushOperation po = new PushOperation(repository2, remote.toString(), Arrays.asList(rs), false, 0); po.execute(); System.out.println(po.toString()); // Now Repository 3 update the file and push File file2 = new File(workdir3, "file.txt"); repositoryUtil.appendFileContent(file2, "-->from Repository 3"); git = new Git(repository3); git.add().addFilepattern("file.txt").call(); git.commit().setMessage("Third Commit").call(); // now push the Repository 3 into the Repository 1 rs = new RefSpec(); rs = rs.setSourceDestination("refs/heads/master", "refs/heads/master"); // rs=rs.setForceUpdate(true); po = new PushOperation(repository3, remote.toString(), Arrays.asList(rs), false, 0); po.execute(); System.out.println(po.toString()); }
/** * Push from repository1 "master" into "test" of repository2. * * @throws Exception */ @Test public void testPush() throws Exception { // push from repository1 to repository2 System.out.println(repository2.getBranch()); PushOperation pop = createPushOperation(); pop.execute(); assertEquals( org.eclipse.jgit.transport.RemoteRefUpdate.Status.UP_TO_DATE, getStatus(pop.getOperationResult())); System.out.println(pop.toString()); ArrayList<String> files = new ArrayList<String>(); File file = new File(workdir, "file2.txt"); FileUtils.createNewFile(file); repositoryUtil.appendFileContent(file, "new file"); files.add(repositoryUtil.getRepoRelativePath(file.getAbsolutePath())); AddToIndexOperation trop = new AddToIndexOperation(files, repository1); trop.execute(); CommitOperation cop = new CommitOperation(repository1, files, files, AUTHOR, COMMITTER, "added files"); cop.execute(); pop = createPushOperation(); pop.execute(); assertEquals( org.eclipse.jgit.transport.RemoteRefUpdate.Status.OK, getStatus(pop.getOperationResult())); System.out.println(pop.toString()); try { // assert that we cannot run this again pop.execute(); fail("Expected Exception not thrown"); } catch (IllegalStateException e) { // expected } pop = createPushOperation(); pop.execute(); assertEquals( org.eclipse.jgit.transport.RemoteRefUpdate.Status.UP_TO_DATE, getStatus(pop.getOperationResult())); System.out.println(pop.toString()); File testFile = new File(workdir2, repositoryUtil.getRepoRelativePath(file.getAbsolutePath())); assertFalse(testFile.exists()); testFile = new File(workdir, repositoryUtil.getRepoRelativePath(file.getAbsolutePath())); assertTrue(testFile.exists()); // check out test and verify the file is there new Git(repository2).checkout().setName("refs/heads/test").call(); testFile = new File(workdir2, repositoryUtil.getRepoRelativePath(file.getAbsolutePath())); assertTrue(testFile.exists()); }
@Test public void testUpdateTrackingBranchIfSpecifiedInRemoteRefUpdate() throws Exception { // Commit on repository 2 // RevCommit commit = repository2.addAndCommit(project, new File(workdir2, "test.txt"), "Commit // in repository 2"); System.out.println(repository2.getBranch()); new Git(repository2).checkout().setName("refs/heads/test").call(); System.out.println(repository2.getBranch()); ArrayList<String> files = new ArrayList<String>(); File file = new File(workdir2, "test.txt"); FileUtils.createNewFile(file); repositoryUtil.appendFileContent(file, "create file of test.txt in repository 2"); files.add(repositoryUtil.getRepoRelativePath(file.getAbsolutePath())); AddToIndexOperation trop = new AddToIndexOperation(files, repository2); trop.execute(); CommitOperation cop = new CommitOperation(repository2, files, files, AUTHOR, COMMITTER, "Commit in repository 2"); cop.execute(); // We want to push from repository 2 to 1 (because repository 2 already // has tracking set up) // URIish remote = repository1.getUri(); URIish remote = new URIish("file:///" + repository1.getDirectory().toString()); String trackingRef = "refs/remotes/origin/master"; RemoteRefUpdate update = new RemoteRefUpdate(repository2, "HEAD", "refs/heads/master", false, trackingRef, null); PushOperationSpecification spec = new PushOperationSpecification(); spec.addURIRefUpdates(remote, Arrays.asList(update)); PushOperation push = new PushOperation(repository2, spec, false, 0); push.execute(); PushOperationResult result = push.getOperationResult(); PushResult pushResult = result.getPushResult(remote); TrackingRefUpdate trf = pushResult.getTrackingRefUpdate(trackingRef); System.out.println(trf.getLocalName()); System.out.println(trf.getRemoteName()); assertNotNull( "Expected result to have tracking ref update", pushResult.getTrackingRefUpdate(trackingRef)); ObjectId trackingId = repository2.resolve(trackingRef); assertEquals("Expected tracking branch to be updated", cop.getCommit().getId(), trackingId); new Git(repository1).checkout().setName("refs/heads/master").call(); File testFile = new File(workdir2, repositoryUtil.getRepoRelativePath(file.getAbsolutePath())); assertTrue(testFile.exists()); }
@Test public void testBranchOperation() throws Exception { String br1 = repository.getFullBranch(); System.out.println(br1); File file = new File(workdir, "file1.txt"); FileUtils.createNewFile(file); List<String> list = Arrays.asList(repositoryUtil.getRepoRelativePath(file.getAbsolutePath())); AddToIndexOperation addfile = new AddToIndexOperation(list, repository); addfile.execute(); CommitOperation commitfile = new CommitOperation(repository, AUTHOR, COMMITTER, "first commit"); commitfile.execute(); // create branch of "test1" CreateLocalBranchOperation branchCre = new CreateLocalBranchOperation( repository, "test1", repository.getAllRefs().get(MASTER), null); branchCre.execute(); // checkout branch of test1 BranchOperation bo = new BranchOperation(repository, "test1"); bo.execute(); String br2 = repository.getFullBranch(); System.out.println(br2); assertTrue(repository.getFullBranch().equals(TEST)); System.out.println(bo.toString()); }
@Override @After public void tearDown() throws Exception { repositoryUtil.dispose(); if (workdir.exists()) FileUtils.delete(workdir, FileUtils.RECURSIVE | FileUtils.RETRY); super.tearDown(); }
@Override @Before public void setUp() throws Exception { super.setUp(); workdir = new File("D://Repository1"); if (workdir.exists()) { FileUtils.delete(workdir, FileUtils.RECURSIVE | FileUtils.RETRY); } FileUtils.mkdir(workdir, true); repositoryUtil = new RepositoryUtil(new File(workdir, Constants.DOT_GIT)); repository = repositoryUtil.getRepository(); }
@Override @Before public void setUp() throws Exception { super.setUp(); // create Main Git Repository workdir = new File("D://Repository1"); if (workdir.exists()) { FileUtils.delete(workdir, FileUtils.RECURSIVE | FileUtils.RETRY); } FileUtils.mkdir(workdir, true); // init RepositoryUtil class repositoryUtil = new RepositoryUtil(new File(workdir, Constants.DOT_GIT)); repository1 = repositoryUtil.getRepository(); // create file, add and commit File file = new File(workdir, "file.txt"); FileUtils.createNewFile(file); repositoryUtil.appendFileContent(file, "Contect file"); Git git = new Git(repository1); git.add().addFilepattern("file.txt").call(); git.commit().setMessage("First Commit").call(); // clone Git Repository 2 workdir2 = new File("D:/Repository2"); if (workdir2.exists()) { FileUtils.delete(workdir2, FileUtils.RECURSIVE | FileUtils.RETRY); } FileUtils.mkdir(workdir2, true); URIish uri = new URIish("file:///" + repository1.getDirectory().toString()); CloneOperation clop = new CloneOperation( uri.toString(), true, null, workdir2, "refs/heads/master", "origin", 0, null, null); clop.execute(); repository2 = new FileRepository(new File(workdir2, Constants.DOT_GIT)); // clone Git Repository 3 workdir3 = new File("D:/Repository3"); if (workdir3.exists()) { FileUtils.delete(workdir3, FileUtils.RECURSIVE | FileUtils.RETRY); } FileUtils.mkdir(workdir3, true); uri = new URIish("file:///" + repository1.getDirectory().toString()); clop = new CloneOperation( uri.toString(), true, null, workdir3, "refs/heads/master", "origin", 0, null, null); clop.execute(); repository3 = new FileRepository(new File(workdir3, Constants.DOT_GIT)); RefUpdate createBranch = repository2.updateRef("refs/heads/test"); createBranch.setNewObjectId(repository2.resolve("refs/heads/master")); createBranch.update(); }