Example usage for org.eclipse.jgit.api Git branchDelete

List of usage examples for org.eclipse.jgit.api Git branchDelete

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git branchDelete.

Prototype

public DeleteBranchCommand branchDelete() 

Source Link

Document

Return a command object used to delete branches

Usage

From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java

License:Apache License

public static void deleteBranch(final Git git, final Ref branch) {
    try {// w ww.j av  a  2 s.  c  o m
        git.branchDelete().setBranchNames(branch.getName()).setForce(true).call();
    } catch (final GitAPIException e) {
        throw new IOException(e);
    }
}

From source file:org.mule.module.git.GitConnector.java

License:Open Source License

/**
 * Delete local branch/* w w  w.j  a  va 2  s.c  o  m*/
 *
 * {@sample.xml ../../../doc/mule-module-git.xml.sample git:delete-branch}
 *
 * @param branchName  Name of the branch to delete
 * @param force If false a check will be performed whether the branch to be deleted is already merged into the current branch and deletion will be refused in this case
 * @param overrideDirectory Name of the directory to use for git repository
 */
@Processor
public void deleteBranch(String branchName, boolean force, @Optional String overrideDirectory) {
    try {
        Git git = new Git(getGitRepo(overrideDirectory));
        DeleteBranchCommand deleteBranch = git.branchDelete();
        deleteBranch.setBranchNames(branchName);
        deleteBranch.setForce(force);

        deleteBranch.call();
    } catch (Exception e) {
        throw new RuntimeException("Unable to create branch " + branchName, e);
    }
}

From source file:org.repodriller.scm.GitRepository.java

License:Apache License

private synchronized void deleteMMBranch(Git git) throws GitAPIException {
    List<Ref> refs = git.branchList().call();
    for (Ref r : refs) {
        if (r.getName().endsWith(BRANCH_MM)) {
            git.branchDelete().setBranchNames(BRANCH_MM).setForce(true).call();
            break;
        }//from  w w w .  j a  va2s.  c om
    }
}

From source file:org.uberfire.java.nio.fs.jgit.util.commands.BranchUtil.java

License:Apache License

public static void deleteUnfilteredBranches(final Repository repository, final List<String> branchesToKeep)
        throws GitAPIException {
    if (branchesToKeep == null || branchesToKeep.isEmpty()) {
        return;//from   w  w  w. j  a  v a  2s  . c  om
    }

    final org.eclipse.jgit.api.Git git = org.eclipse.jgit.api.Git.wrap(repository);
    final String[] toDelete = git.branchList().call().stream().map(Ref::getName)
            .map(fullname -> fullname.substring(fullname.lastIndexOf('/') + 1))
            .filter(name -> !branchesToKeep.contains(name)).toArray(String[]::new);
    git.branchDelete().setBranchNames(toDelete).setForce(true).call();
}

From source file:org.zanata.sync.jobs.plugin.git.service.impl.GitSyncService.java

License:Open Source License

protected static void checkOutBranch(Git git, String branch) throws GitAPIException, IOException {
    String currentBranch = git.getRepository().getBranch();
    if (currentBranch.equals(branch)) {
        log.info("already on branch: {}. will do a git pull", branch);
        PullResult pullResult = git.pull().call();
        log.debug("pull result: {}", pullResult);
        Preconditions.checkState(pullResult.isSuccessful());
        return;/* w w w. j  a v a2  s  .c o m*/
    }

    List<Ref> refs = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call();
    /* refs will have name like these:
    refs/heads/master,
    refs/heads/trans,
    refs/heads/zanata,
    refs/remotes/origin/HEAD,
    refs/remotes/origin/master,
    refs/remotes/origin/trans,
    refs/remotes/origin/zanata
            
    where the local branches are: master, trans, zanata
    remote branches are: master, trans, zanata
    */
    Optional<Ref> localBranchRef = Optional.empty();
    Optional<Ref> remoteBranchRef = Optional.empty();
    for (Ref ref : refs) {
        String refName = ref.getName();
        if (refName.equals("refs/heads/" + branch)) {
            localBranchRef = Optional.of(ref);
        }
        if (refName.equals("refs/remotes/origin/" + branch)) {
            remoteBranchRef = Optional.of(ref);
        }
    }

    // if local branch exists and we are now on a different branch,
    // we delete it first then re-checkout
    if (localBranchRef.isPresent()) {
        log.debug("deleting local branch {}", branch);
        git.branchDelete().setBranchNames(branch).call();
    }

    if (remoteBranchRef.isPresent()) {
        // if remote branch exists, we create a new local branch based on it.
        git.checkout().setCreateBranch(true).setForce(true).setName(branch).setStartPoint("origin/" + branch)
                .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).call();
    } else {
        // If branch does not exists in remote, create new local branch based on master branch.
        git.checkout().setCreateBranch(true).setForce(true).setName(branch).setStartPoint("origin/master")
                .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).call();
    }
    if (log.isDebugEnabled()) {
        log.debug("current branch is: {}", git.getRepository().getBranch());
    }
}

From source file:org.zanata.sync.plugin.git.service.impl.GitSyncService.java

License:Open Source License

private void checkOutBranch(File destPath, String branch) {
    try {/*from  w  w w. ja va 2 s  .c  om*/
        Git git = Git.open(destPath);
        List<Ref> refs = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call();
        /* refs will have name like these:
        refs/heads/master
        refs/remotes/origin/master
        refs/remotes/origin/zanata
        */
        Optional<Ref> localBranchRef = Optional.empty();
        Optional<Ref> remoteBranchRef = Optional.empty();
        for (Ref ref : refs) {
            String refName = ref.getName();
            if (refName.equals("refs/heads/" + branch)) {
                localBranchRef = Optional.of(ref);
            }
            if (refName.equals("refs/remotes/origin/" + branch)) {
                remoteBranchRef = Optional.of(ref);
            }
        }

        if (branch.equals("master")) {
            log.debug("merging origin/master");
            git.checkout().setName("master").call();
            git.merge().setFastForward(MergeCommand.FastForwardMode.FF_ONLY).include(remoteBranchRef.get())
                    .call();
            return;
        }

        /**
         * If branch found in local and is not master, delete it, create new local branch from remote.
         * If branch does not exists in remote, create new local branch based on master branch.
         */
        Ref ref;
        if (localBranchRef.isPresent()) {
            git.checkout().setName("master").setForce(true).call();
            git.branchDelete().setBranchNames(branch).call();
        }

        if (remoteBranchRef.isPresent()) {
            ref = git.branchCreate().setForce(true).setName(branch).setStartPoint("origin/" + branch)
                    .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM).call();
        } else {
            ref = git.branchCreate().setForce(true).setName(branch).setStartPoint("origin/master")
                    .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM).call();
        }
        log.debug("checked out {}", ref);
        git.checkout().setName(branch).call();
        if (log.isDebugEnabled()) {
            log.debug("current branch is: {}", git.getRepository().getBranch());
        }
    } catch (IOException | GitAPIException e) {
        throw new RepoSyncException(e);
    }
}