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

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

Introduction

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

Prototype

public CheckoutCommand checkout() 

Source Link

Document

Return a command object to execute a checkout command

Usage

From source file:br.com.metricminer2.scm.GitRepository.java

License:Apache License

public void checkout(String hash) {
    Git git = null;
    try {//www  .j a  va  2s.c  om
        git = Git.open(new File(path));
        deleteMMBranch(git);
        git.checkout().setCreateBranch(true).setName("mm").setStartPoint(hash).setForce(true).call();

    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (git != null)
            git.close();
    }
}

From source file:br.com.metricminer2.scm.GitRepository.java

License:Apache License

public void reset() {
    Git git = null;
    try {//w  w w .ja va  2  s.com
        git = Git.open(new File(path));

        git.checkout().setName("master").setForce(true).call();
        git.branchDelete().setBranchNames("mm").setForce(true).call();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (git != null)
            git.close();
    }

}

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

License:Open Source License

/**
 * Returns the conflicting files of the given scenario.
 * // ww  w  .j  av  a 2s  .c  om
 * @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);
    CheckoutCommand ckoutCmd = git.checkout();
    ckoutCmd.setName(scenario.getLeft().getName());
    ckoutCmd.setStartPoint(scenario.getLeft());
    ckoutCmd.call();//from  ww w.ja  v  a 2 s  .  c  om

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

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

License:Open Source License

/**
 * Creates a collaboration scenario with five developers (Devs A, B, C, D,
 * and E) and two files (Foo.java and Bar.java).
 * /*  w  ww  . j a v a2s .  co  m*/
 * @return
 * @throws Exception
 */
public MergeScenario setCollaborationScenarioInBareRepository() throws Exception {
    Git git = Git.wrap(db);

    RevCommit mergeBaseCommit, lastMasterCommit, lastSideCommit;

    TestRepository<Repository> db_t = new TestRepository<Repository>(db);

    BranchBuilder master = db_t.branch("master");

    // first versions of Foo and Bar
    master.commit().add("Foo.java", "1").add("Bar.java", "1").message("initial commit").author(devs.get("devY"))
            .create();

    mergeBaseCommit = master.commit().add("Foo.java", "1\n2\n3\n4\n5\n6\n7\n8\n")
            .add("Bar.java", "1\n2\n3\n4\n").message("m0").author(devs.get("devX")).create();

    // Dev E changes Foo
    master.commit().add("Foo.java", "1\n2\n3\n4\n5-master\n6\n7\n8\n").message("m1").author(devs.get("devE"))
            .create();

    // Dev C changes Foo
    master.commit().add("Foo.java", "1\n2\n3\n4-master\n5\n6\n7\n8\n")
            .add("Bar.java", "1\n2\n3-master\n4-master\n").message("m2").author(devs.get("devC")).create();

    // Dev B changes
    lastMasterCommit = master.commit().add("Bar.java", "1\n2\n3\n4-master\n").message("m3")
            .author(devs.get("devB")).create();

    // updating the tree with the changes
    db_t.getRevWalk().parseCommit(mergeBaseCommit);

    // creating a new branc: side
    BranchBuilder side = db_t.branch("side");

    // Dev D changes Foo
    side.commit().parent(mergeBaseCommit).add("Foo.java", "1\n2\n3\n4-side\n5\n6\n7\n8\n").message("s1")
            .author(devs.get("devD")).create();

    // Dev E changes Bar
    side.commit().add("Bar.java", "1\n2\n3\n4\n5\n").message("s2").author(devs.get("devE")).create();

    // Dev A changes Bar
    lastSideCommit = side.commit().add("Bar.java", "1\n2\n3-side\n4-side\n5\n").message("s3")
            .author(devs.get("devA")).create();

    git.checkout().setName("master").setStartPoint(lastMasterCommit).call();
    return new MergeScenario(null, mergeBaseCommit, lastMasterCommit, lastSideCommit, null, null);
}

From source file:br.edu.ifpb.scm.api.git.Github.java

public Ref checkoutBranch(Git repository, String branch) throws GitAPIException {
    return repository.checkout().setCreateBranch(true).setName(branch)
            .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).setStartPoint("origin/" + branch)
            .call();//  ww w . j a  va2 s  . co  m

}

From source file:br.edu.ifpb.scm.api.git.Github.java

public Ref checkout(Git git, String check) throws GitAPIException {
    return git.checkout().setCreateBranch(true).setName(check)
            .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).setStartPoint("origin/" + check)
            .call();//w ww .  ja  v a 2s  . com
}

From source file:br.edu.ifpb.scm.api.git.Github.java

public Ref checkoutByCommit(Git git, String check) throws GitAPIException {
    //        git.checkout().setName(check).
    //                setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).
    //                setStartPoint("origin/" + check).
    //                call();

    return git.checkout().setName(check).setName("master").call();
    //        git.checkout().setCreateBranch(true).setName("new-branch").setStartPoint(check).call();
    //                git.checkout().
    //                setCreateBranch(true).
    //                setName("master").
    //                setStartPoint(check).
    //                call();
}

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

License:Apache License

private Git checkoutOrCreateBranch(final Git git) {
    try {/*  w w  w.  java2  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:ch.sbb.releasetrain.git.GitRepoImpl.java

License:Apache License

/**
 * Use with care!//from  w w  w .  ja v  a 2 s.c om
 */
public boolean deleteBranch() {
    if (!branch.startsWith("feature/")) {
        throw new GitException("Can only delete feature branch.");
    }
    try {

        final Git git = gitOpen();
        git.checkout().setCreateBranch(true).setName("feature/temp_" + UUID.randomUUID()).call();
        List<String> deletedBranches = git.branchDelete().setBranchNames(branch).setForce(true).call();
        if (deletedBranches.size() == 1) {
            // delete branch 'branchToDelete' on remote 'origin'
            RefSpec refSpec = new RefSpec().setSource(null).setDestination("refs/heads/" + branch);
            Iterable<PushResult> results = git.push().setCredentialsProvider(credentialsProvider())
                    .setRefSpecs(refSpec).setRemote("origin").call();
            for (PushResult result : results) {
                RemoteRefUpdate myUpdate = result.getRemoteUpdate("refs/heads/" + branch);
                if (myUpdate.isDelete() && myUpdate.getStatus() == RemoteRefUpdate.Status.OK) {
                    return true;
                }
            }
        }
        return false;
    } catch (IOException | GitAPIException e) {
        throw new GitException("Delete branch failed", e);
    }
}