Search in sources :

Example 1 with BranchOperation

use of org.eclipse.egit.core.op.BranchOperation in project egit by eclipse.

the class GitRepositoriesViewBranchHandlingTest method testCheckoutRemote.

@Test
public void testCheckoutRemote() throws Exception {
    Repository repo = lookupRepository(clonedRepositoryFile);
    BranchOperation bop = new BranchOperation(repo, "refs/heads/master");
    bop.execute(null);
    assertEquals("master", repo.getBranch());
    SWTBotTree tree = getOrOpenView().bot().tree();
    SWTBotTreeItem item = myRepoViewUtil.getLocalBranchesItem(tree, clonedRepositoryFile).expand();
    touchAndSubmit(null);
    refreshAndWait();
    item = TestUtil.expandAndWait(myRepoViewUtil.getRemoteBranchesItem(tree, clonedRepositoryFile));
    List<String> children = item.getNodes();
    assertEquals("Wrong number of remote children", 2, children.size());
    item.getNode("origin/stable").select();
    ContextMenuHelper.clickContextMenuSync(tree, myUtil.getPluginLocalizedValue("CheckoutCommand"));
    TestUtil.joinJobs(JobFamilies.CHECKOUT);
    refreshAndWait();
    GitLightweightDecorator.refresh();
    assertTrue("Branch should not be symbolic", ObjectId.isId(lookupRepository(clonedRepositoryFile).getBranch()));
    // now let's try to create a local branch from the remote one
    item = myRepoViewUtil.getRemoteBranchesItem(tree, clonedRepositoryFile);
    TestUtil.expandAndWait(item).getNode("origin/stable").select();
    ContextMenuHelper.clickContextMenu(tree, myUtil.getPluginLocalizedValue("CreateBranchCommand"));
    SWTBotShell createPage = bot.shell(UIText.CreateBranchWizard_NewBranchTitle);
    createPage.activate();
    assertEquals("Wrong suggested branch name", "stable", createPage.bot().textWithId("BranchName").getText());
    createPage.close();
    // checkout master again
    item = myRepoViewUtil.getLocalBranchesItem(tree, clonedRepositoryFile);
    TestUtil.expandAndWait(item).getNode("master").select();
    ContextMenuHelper.clickContextMenu(tree, myUtil.getPluginLocalizedValue("CheckoutCommand"));
    TestUtil.joinJobs(JobFamilies.CHECKOUT);
    refreshAndWait();
}
Also used : Repository(org.eclipse.jgit.lib.Repository) BranchOperation(org.eclipse.egit.core.op.BranchOperation) SWTBotTree(org.eclipse.swtbot.swt.finder.widgets.SWTBotTree) SWTBotTreeItem(org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem) SWTBotShell(org.eclipse.swtbot.swt.finder.widgets.SWTBotShell) Test(org.junit.Test)

Example 2 with BranchOperation

use of org.eclipse.egit.core.op.BranchOperation in project egit by eclipse.

the class BranchOperationTest method testBranchOperation.

@Test
public void testBranchOperation() throws Exception {
    // create first commit containing a dummy file
    testRepository.createInitialCommit("testBranchOperation\n\nfirst commit\n");
    // create branch test and switch to branch test
    testRepository.createBranch(MASTER, TEST);
    new BranchOperation(repository, TEST).execute(null);
    assertTrue(repository.getFullBranch().equals(TEST));
    // add .project to version control and commit
    String path = project.getProject().getLocation().append(".project").toOSString();
    File file = new File(path);
    testRepository.track(file);
    testRepository.commit("Add .project file");
    // switch back to master branch
    // .project must disappear, related Eclipse project must be deleted
    new BranchOperation(repository, MASTER).execute(null);
    assertFalse(file.exists());
    assertFalse(project.getProject().exists());
    // switch back to master test
    // .project must reappear
    new BranchOperation(repository, TEST).execute(null);
    assertTrue(file.exists());
}
Also used : BranchOperation(org.eclipse.egit.core.op.BranchOperation) File(java.io.File) Test(org.junit.Test)

Example 3 with BranchOperation

use of org.eclipse.egit.core.op.BranchOperation in project egit by eclipse.

the class PushToUpstreamTest method checkoutNewLocalBranch.

private void checkoutNewLocalBranch(String branchName) throws Exception {
    CreateLocalBranchOperation createBranch = new CreateLocalBranchOperation(repository, branchName, repository.findRef("master"), null);
    createBranch.execute(null);
    BranchOperation checkout = new BranchOperation(repository, branchName);
    checkout.execute(null);
}
Also used : CreateLocalBranchOperation(org.eclipse.egit.core.op.CreateLocalBranchOperation) BranchOperation(org.eclipse.egit.core.op.BranchOperation) CreateLocalBranchOperation(org.eclipse.egit.core.op.CreateLocalBranchOperation)

Example 4 with BranchOperation

use of org.eclipse.egit.core.op.BranchOperation in project egit by eclipse.

the class GitFlowOperation method mergeTo.

/**
 * @param monitor
 * @param branchName
 * @param targetBranchName
 * @param squash
 * @param fastForwardSingleCommit Has no effect if {@code squash} is true.
 * @return result of merging back to targetBranchName
 * @throws CoreException
 * @since 4.1
 */
@NonNull
protected MergeResult mergeTo(IProgressMonitor monitor, String branchName, String targetBranchName, boolean squash, boolean fastForwardSingleCommit) throws CoreException {
    try {
        if (!repository.hasBranch(targetBranchName)) {
            throw new RuntimeException(String.format(CoreText.GitFlowOperation_branchNotFound, targetBranchName));
        }
        SubMonitor progress = SubMonitor.convert(monitor, 2);
        boolean dontCloseProjects = false;
        BranchOperation branchOperation = new BranchOperation(repository.getRepository(), targetBranchName, dontCloseProjects);
        branchOperation.execute(progress.newChild(1));
        Status status = branchOperation.getResult().getStatus();
        if (!CheckoutResult.Status.OK.equals(status)) {
            throw new CoreException(error(format(CoreText.GitFlowOperation_unableToCheckout, branchName, status.toString())));
        }
        MergeOperation mergeOperation = new MergeOperation(repository.getRepository(), branchName);
        mergeOperation.setSquash(squash);
        if (squash) {
            mergeOperation.setCommit(true);
        }
        if (!squash && (!fastForwardSingleCommit || hasMultipleCommits(branchName))) {
            mergeOperation.setFastForwardMode(NO_FF);
        }
        mergeOperation.execute(progress.newChild(1));
        MergeResult result = mergeOperation.getResult();
        if (result == null) {
            throw new CoreException(error(format(CoreText.GitFlowOperation_unableToMerge, branchName, targetBranchName)));
        }
        return result;
    } catch (GitAPIException | IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Status(org.eclipse.jgit.api.CheckoutResult.Status) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) MergeOperation(org.eclipse.egit.core.op.MergeOperation) CoreException(org.eclipse.core.runtime.CoreException) CreateLocalBranchOperation(org.eclipse.egit.core.op.CreateLocalBranchOperation) DeleteBranchOperation(org.eclipse.egit.core.op.DeleteBranchOperation) BranchOperation(org.eclipse.egit.core.op.BranchOperation) SubMonitor(org.eclipse.core.runtime.SubMonitor) MergeResult(org.eclipse.jgit.api.MergeResult) IOException(java.io.IOException) NonNull(org.eclipse.jgit.annotations.NonNull)

Example 5 with BranchOperation

use of org.eclipse.egit.core.op.BranchOperation in project egit by eclipse.

the class AbstractDualRepositoryTestCase method assertCommitArrivedAtRemote.

protected void assertCommitArrivedAtRemote(RevCommit branchCommit, Repository remote) throws CoreException {
    GitFlowRepository gfRepo = new GitFlowRepository(remote);
    BranchOperation checkoutOperation = new BranchOperation(remote, gfRepo.getConfig().getFullFeatureBranchName(MY_FEATURE));
    checkoutOperation.execute(null);
    RevCommit developHead = findHead(remote);
    assertEquals(branchCommit, developHead);
}
Also used : BranchOperation(org.eclipse.egit.core.op.BranchOperation) GitFlowRepository(org.eclipse.egit.gitflow.GitFlowRepository) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

BranchOperation (org.eclipse.egit.core.op.BranchOperation)19 Test (org.junit.Test)8 CreateLocalBranchOperation (org.eclipse.egit.core.op.CreateLocalBranchOperation)7 GitFlowRepository (org.eclipse.egit.gitflow.GitFlowRepository)7 Repository (org.eclipse.jgit.lib.Repository)7 SubMonitor (org.eclipse.core.runtime.SubMonitor)5 File (java.io.File)3 IOException (java.io.IOException)3 CoreException (org.eclipse.core.runtime.CoreException)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 IFile (org.eclipse.core.resources.IFile)2 CommitOperation (org.eclipse.egit.core.op.CommitOperation)2 DeleteBranchOperation (org.eclipse.egit.core.op.DeleteBranchOperation)2 WrongGitFlowStateException (org.eclipse.egit.gitflow.WrongGitFlowStateException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 IProject (org.eclipse.core.resources.IProject)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1