Example usage for org.eclipse.jgit.api CheckoutCommand call

List of usage examples for org.eclipse.jgit.api CheckoutCommand call

Introduction

In this page you can find the example usage for org.eclipse.jgit.api CheckoutCommand call.

Prototype

@Override
public Ref call() throws GitAPIException, RefAlreadyExistsException, RefNotFoundException,
        InvalidRefNameException, CheckoutConflictException 

Source Link

Usage

From source file:br.com.riselabs.cotonet.builder.NetworkBuilder.java

License:Open Source License

/**
 * Returns the conflicting files of the given scenario.
 * /*www .j  av a2 s  . com*/
 * @param scenario
 * @return
 * @throws CheckoutConflictException
 * @throws GitAPIException
 */
private List<File> getConflictingFiles(MergeScenario scenario)
        throws CheckoutConflictException, GitAPIException {
    Git git = Git.wrap(getProject().getRepository());
    // this is for the cases of restarting after exception in a conflict
    // scenario analysis
    try {
        git.reset().setRef(scenario.getLeft().getName()).setMode(ResetType.HARD).call();
    } catch (JGitInternalException e) {
        Logger.log(log, "[" + project.getName() + "] JGit Reset Command ended with exception."
                + " Trying external reset command.");
        ExternalGitCommand egit = new ExternalGitCommand();
        try {
            egit.setType(CommandType.RESET).setDirectory(project.getRepository().getDirectory().getParentFile())
                    .call();
        } catch (BlameException e1) {
            Logger.logStackTrace(log, e1);
            return null;
        }
    }

    CheckoutCommand ckoutCmd = git.checkout();
    ckoutCmd.setName(scenario.getLeft().getName());
    ckoutCmd.setStartPoint(scenario.getLeft());
    ckoutCmd.call();

    MergeCommand mergeCmd = git.merge();
    mergeCmd.setCommit(false);
    mergeCmd.setStrategy(MergeStrategy.RECURSIVE);
    mergeCmd.include(scenario.getRight());

    Set<String> conflictingPaths;
    try {
        // dealing with MissingObjectException
        MergeResult mResult = mergeCmd.call();
        // dealing with Ghosts conflicts
        conflictingPaths = mResult.getConflicts().keySet();
    } catch (NullPointerException | JGitInternalException e) {
        StringBuilder sb = new StringBuilder();
        sb.append("[" + project.getName() + ":" + project.getUrl() + "] " + "Skipping merge scenario due to '"
                + e.getMessage() + "'\n");
        sb.append("--> Exception: " + e.getClass());
        sb.append("--> Skipped scenario:\n");
        sb.append("::Base:" + scenario.getBase().getName() + "\n");
        sb.append("::Left:" + scenario.getLeft().getName() + "\n");
        sb.append("::Right:" + scenario.getRight().getName() + "\n");
        Logger.log(log, sb.toString());
        return null;
    }
    List<File> result = new ArrayList<File>();
    for (String path : conflictingPaths) {
        result.add(new File(getProject().getRepository().getDirectory().getParent(), path));
    }
    return result;
}

From source file:br.com.riselabs.cotonet.test.helpers.ConflictBasedRepositoryTestCase.java

License:Open Source License

public MergeResult runMerge(MergeScenario scenario) throws RefAlreadyExistsException, RefNotFoundException,
        InvalidRefNameException, CheckoutConflictException, GitAPIException {
    Git git = Git.wrap(db);//from   ww  w. ja v a2  s. c  om
    CheckoutCommand ckoutCmd = git.checkout();
    ckoutCmd.setName(scenario.getLeft().getName());
    ckoutCmd.setStartPoint(scenario.getLeft());
    ckoutCmd.call();

    MergeCommand mergeCmd = git.merge();
    mergeCmd.setCommit(false);
    mergeCmd.include(scenario.getRight());
    return mergeCmd.call();
}

From source file:ch.sbb.releasetrain.git.GitRepoImpl.java

License:Apache License

private Git checkoutOrCreateBranch(final Git git) {
    try {//from  w  ww .ja  va2 s. co m
        if (!branch.equals(git.getRepository().getBranch())) {
            CheckoutCommand checkoutCommand = git.checkout().setCreateBranch(true).setName(branch)
                    .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK);

            if (remoteBranchExists(git)) {
                checkoutCommand.setStartPoint("origin/" + branch);
            }
            checkoutCommand.call();
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return git;
}

From source file:com.barchart.jenkins.cascade.PluginScmGit.java

License:BSD License

/**
 * See {@link Git#checkout()}//www .  j a  va 2 s.  co m
 */
public static CheckoutResult doCheckout(final File workspace, final String localBranch, final String remoteName,
        final String remoteBranch) {
    try {
        final Git git = Git.open(workspace);
        final CheckoutCommand command = git.checkout().setName(localBranch).setForce(true);
        if (findRef(workspace, localBranch) == null) {
            command.setCreateBranch(true).setUpstreamMode(SetupUpstreamMode.TRACK)
                    .setStartPoint(remote(remoteName, remoteBranch)).call();
        } else {
            command.call();
        }
        return command.getResult();
    } catch (final Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:com.ejwa.gitdepmavenplugin.DownloaderMojo.java

License:Open Source License

private void checkout(Git git, Pom pom, GitDependency dependency) throws MojoExecutionException {
    final GitDependencyHandler dependencyHandler = new GitDependencyHandler(dependency);
    final String version = dependencyHandler.getDependencyVersion(pom);

    try {//from ww  w .j a v  a 2 s .c o m
        final Repository repository = git.getRepository();
        final ObjectId rev = repository.resolve(version);
        final RevCommit rc = new RevWalk(repository).parseCommit(rev);
        final CheckoutCommand checkout = git.checkout();

        checkout.setName("maven-gitdep-branch-" + rc.getCommitTime());
        checkout.setStartPoint(rc);
        checkout.setCreateBranch(true);
        checkout.call();

        final Status status = checkout.getResult().getStatus();

        if (status != Status.OK) {
            throw new MojoExecutionException(
                    String.format("Invalid checkout state (%s) of dependency.", status));
        }
    } catch (IOException | InvalidRefNameException | RefAlreadyExistsException | RefNotFoundException ex) {
        throw new MojoExecutionException(String.format("Failed to check out dependency for %s.%s",
                dependency.getGroupId(), dependency.getArtifactId()), ex);
    }
}

From source file:com.ejwa.mavengitdepplugin.DownloaderMojo.java

License:Open Source License

private void checkout(Git git, POM pom, GitDependency dependency) {
    final GitDependencyHandler dependencyHandler = new GitDependencyHandler(dependency);
    final String version = dependencyHandler.getDependencyVersion(pom);

    try {/*  w  ww  .jav a  2 s. c  o  m*/
        final ObjectId rev = git.getRepository().resolve(version);
        final RevCommit rc = new RevWalk(git.getRepository()).parseCommit(rev);
        final CheckoutCommand checkout = git.checkout();

        checkout.setName("maven-gitdep-branch-" + rc.getCommitTime());
        checkout.setStartPoint(rc);
        checkout.setCreateBranch(true);
        checkout.call();

        final Status status = checkout.getResult().getStatus();

        if (!status.equals(Status.OK)) {
            getLog().error("Status of checkout: " + status);
            throw new IllegalStateException("Invalid checkout state of dependency.");
        }
    } catch (Exception ex) {
        getLog().error(ex);
        throw new IllegalStateException("Failed to check out dependency.");
    }
}

From source file:com.google.gerrit.acceptance.git.GitUtil.java

License:Apache License

public static void checkout(Git git, String name) throws GitAPIException {
    CheckoutCommand checkout = git.checkout();
    checkout.setName(name);//from   www. j a v a2 s.c  o m
    checkout.call();
}

From source file:com.meltmedia.cadmium.core.git.GitService.java

License:Apache License

public void switchBranch(String branchName) throws RefNotFoundException, Exception {
    Repository repository = git.getRepository();
    if (branchName != null && !repository.getBranch().equals(branchName)) {
        log.info("Switching branch from {} to {}", repository.getBranch(), branchName);
        CheckoutCommand checkout = git.checkout();
        if (isTag(branchName)) {
            checkout.setName(branchName);
        } else {/*from   ww w. j  av a2 s  .  co m*/
            checkout.setName("refs/heads/" + branchName);
            if (repository.getRef("refs/heads/" + branchName) == null) {
                CreateBranchCommand create = git.branchCreate();
                create.setName(branchName);
                create.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);
                create.setStartPoint("origin/" + branchName);
                create.call();
            }
        }
        checkout.call();
    }
}

From source file:com.mortardata.git.GitUtil.java

License:Apache License

/**
 * Checkout a branch, tracking remote branch if it exists.
 *  /*from w  w  w  .  j  a va 2s .  c o  m*/
 * @param git Git repository
 * @param branch Name of branch to checkout
 * @throws GitAPIException if error running git command
 */
public void checkout(Git git, String branch) throws GitAPIException {
    List<Ref> branches = getBranches(git);

    CheckoutCommand checkout = git.checkout().setName(branch);

    String localBranchRefName = "refs/heads/" + branch;
    String remoteBranchName = "origin/" + branch;
    String remoteBranchRefName = "refs/remotes/" + remoteBranchName;
    if (containsRef(branches, localBranchRefName)) {
        logger.debug("git checkout " + branch);
        // local branch exists: no create branch
        checkout.setCreateBranch(false);
    } else if (containsRef(branches, remoteBranchRefName)) {
        logger.debug("git checkout --set-upstream -b " + branch + " " + remoteBranchName);
        // remote branch exists: track existing branch
        checkout.setCreateBranch(true).setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
                .setStartPoint(remoteBranchName);
    } else {
        // no remote branch exists: create a new branch, no tracking
        logger.debug("git checkout -b " + branch);
        checkout.setCreateBranch(true);
    }

    checkout.call();
}

From source file:com.mpdeimos.ct_tests.looper.GitRevisionLooper.java

License:Apache License

/** {@inheritDoc} */
@Override/*from   w  w w  . j a  v  a 2s . co  m*/
public RevIterator iterator() {

    return new RevIterator() {
        int index = commits.size();

        @Override
        public void remove() {
            throw new IllegalStateException();
        }

        @Override
        public RevisionInfo next() {
            final Commit commit = commits.get(--index);

            return new RevisionInfo(commits.size() - index - 1, commit.getId(), commit, root) {
                /** {@inheritDoc} 
                 * @throws ConQATException */
                @Override
                public File getPath() throws ConQATException {
                    CheckoutCommand checkout = new CheckoutCommand(repository) {
                        /**/};
                    checkout.setName(commit.getId());
                    //checkout.setForce(true);
                    try {
                        checkout.call();
                    } catch (Exception e) {
                        getLogger().error(e);
                        ResetCommand reset = new ResetCommand(repository) {
                            /**/};
                        reset.setMode(ResetType.HARD);
                        reset.setRef(commit.getId());
                        try {
                            reset.call();
                            CheckoutCommand checkout2 = new CheckoutCommand(repository) {
                                /**/};
                            checkout2.setName(commit.getId());
                            //                        checkout2.setForce(true);
                            checkout2.call();
                        } catch (Exception e1) {
                            getLogger().error(e1);
                            throw new ConQATException(e1);
                        }
                    }
                    return super.getPath();
                }
            };
        }

        @Override
        public boolean hasNext() {
            return index > 0;
        }

        @Override
        public Commit peekCommit() {
            if (!hasNext())
                throw new IllegalStateException();

            return commits.get(index - 1);
        }
    };
}