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:org.jboss.forge.addon.git.GitUtilsImpl.java

License:Open Source License

@Override
public Ref switchBranch(final Git repo, final String branchName) {
    try {/*w  w  w .j a  v a 2s  . c o m*/
        return repo.checkout().setName(branchName).call();
    } catch (GitAPIException e) {
        throw new RuntimeException("Couldn't switch to branch " + branchName + ": " + e.getMessage(), e);
    }
}

From source file:org.jboss.forge.addon.git.GitUtilsImpl.java

License:Open Source License

@Override
public List<String> getLogForBranch(final Git repo, String branchName) throws GitAPIException, IOException {
    String oldBranch = repo.getRepository().getBranch();
    repo.checkout().setName(branchName).call();

    List<String> results = getLogForCurrentBranch(repo);

    repo.checkout().setName(oldBranch).call();

    return results;
}

From source file:org.jboss.forge.addon.manager.impl.ui.AddonGitBuildAndInstallCommand.java

License:Open Source License

private void cloneTo(GitUtils gitUtils, DirectoryResource projectRoot) throws GitAPIException, IOException {
    Git git = null;
    try {//from ww w.j a va  2  s.c  om
        git = gitUtils.clone(projectRoot, url.getValue().getFullyQualifiedName());
        if (ref.hasValue()) {
            String refName = ref.getValue();
            String currentBranch = git.getRepository().getBranch();
            // No need to checkout if the branch name is the same
            if (!currentBranch.equals(refName)) {
                git.checkout().setCreateBranch(true).setName(refName).setUpstreamMode(SetupUpstreamMode.TRACK)
                        .setStartPoint("origin/" + refName).call();
            }
        }
    } finally {
        gitUtils.close(git);
    }
}

From source file:org.jboss.forge.git.GitUtils.java

License:Open Source License

public static Ref checkout(final Git git, final String remote, final boolean createBranch,
        final SetupUpstreamMode mode, final boolean force) throws GitAPIException {
    CheckoutCommand checkout = git.checkout();
    checkout.setCreateBranch(createBranch);
    checkout.setName(remote);//from ww  w . j a  v a  2  s.  c om
    checkout.setForce(force);
    checkout.setUpstreamMode(mode);
    return checkout.call();
}

From source file:org.jboss.forge.git.GitUtils.java

License:Open Source License

public static Ref checkout(final Git git, final Ref localRef, final boolean createBranch,
        final SetupUpstreamMode mode, final boolean force) throws GitAPIException {
    CheckoutCommand checkout = git.checkout();
    checkout.setName(Repository.shortenRefName(localRef.getName()));
    checkout.setForce(force);//  w ww .j  av  a2s  .  c om
    checkout.setUpstreamMode(mode);
    return checkout.call();
}

From source file:org.jboss.forge.git.GitUtils.java

License:Open Source License

public static Ref switchBranch(final Git repo, final String branchName) {
    Ref switchedBranch = null;/* w  w  w.ja v a 2 s  . com*/
    try {
        switchedBranch = repo.checkout().setName(branchName).call();
        if (switchedBranch == null)
            throw new RuntimeException("Couldn't switch to branch " + branchName);
    } catch (GitAPIException e) {
        e.printStackTrace();
    }

    return switchedBranch;
}

From source file:org.jboss.forge.git.GitUtils.java

License:Open Source License

public static Iterable<RevCommit> getLogForBranch(final Git repo, String branchName)
        throws GitAPIException, IOException {
    String oldBranch = repo.getRepository().getBranch();
    repo.checkout().setName(branchName).call();

    Iterable<RevCommit> commits = repo.log().call();

    repo.checkout().setName(oldBranch).call();

    return commits;
}

From source file:org.jboss.tools.aerogear.hybrid.core.plugin.CordovaPluginManager.java

License:Open Source License

/**
 * Installs a Cordova plugin from a git repository. 
 * This method delegates to {@link #installPlugin(File)} after cloning the
 * repository to a temporary location to complete the installation of the 
 * plugin. /*from  www.  j a  v  a2 s.co m*/
 * <br/>
 * If commit is not null the cloned repository will be checked out to 
 * commit. 
 * <br/>
 * If subdir is not null it is assumed that the subdir path exists and installation 
 * will be done from that location. 
 * 
 * @param uri
 * @param commit 
 * @param subdir
 * @param overwrite
 * @param monitor 
 * @throws CoreException
 */
public void installPlugin(URI uri, String commit, String subdir, FileOverwriteCallback overwrite,
        IProgressMonitor monitor) throws CoreException {
    File tempRepoDirectory = new File(FileUtils.getTempDirectory(),
            "cordova_plugin_tmp_" + Long.toString(System.currentTimeMillis()));
    tempRepoDirectory.deleteOnExit();
    try {
        if (monitor.isCanceled())
            return;
        monitor.subTask("Clone plugin repository");
        Git git = Git.cloneRepository().setDirectory(tempRepoDirectory).setURI(uri.toString()).call();
        if (commit != null && !monitor.isCanceled()) {
            git.checkout().setName(commit).call();
        }
        monitor.worked(1);
        SubProgressMonitor sm = new SubProgressMonitor(monitor, 1);
        sm.setTaskName("Installing to " + this.project.getProject().getName());
        File pluginDirectory = tempRepoDirectory;
        if (subdir != null) {
            pluginDirectory = new File(tempRepoDirectory, subdir);
            if (!pluginDirectory.isDirectory()) {
                throw new CoreException(new Status(IStatus.ERROR, HybridCore.PLUGIN_ID,
                        subdir + " does not exist in this repo"));
            }
        }
        this.installPlugin(pluginDirectory, overwrite, sm);
    } catch (GitAPIException e) {
        throw new CoreException(
                new Status(IStatus.ERROR, HybridCore.PLUGIN_ID, "Error cloning the plugin repository", e));
    } finally {
        monitor.done();
    }
}

From source file:org.jenkinsci.plugins.pretestedintegration.integration.scm.git.MatrixProjectCompatabilityTestIT.java

/**
 * Git Plugin/* ww  w.j a va  2  s. com*/
 *
 * Test that show that a ready/feature_1 branch get integrated into master
 * using a Matrix job type.
 *
 * Pretested integration:
 *  - 'Integration branch' : master (default)
 *  - 'Repository name' : origin (default) 
 *  - 'Strategy' : Squash Commit
 *
 * GitSCM:
 *  - 'Name' : (empty)
 *
 * Workflow
 *  - Create a repository containing a 'ready' branch.
 *  - The build is triggered. 
 *
 * Results
 *  - We expect that the plugin triggers, and that the commits on ready branch
 *    is merged into our integration branch master and build result becomes SUCCESS.
 *
 * @throws Exception
 */
@Test
public void oneBuildBasicSmokeTest() throws Exception {
    repository = TestUtilsFactory.createValidRepository("test-repo");

    File workDir = new File("test-repo");

    Git.cloneRepository().setURI("file:///" + repository.getDirectory().getAbsolutePath()).setDirectory(workDir)
            .setBare(false).setCloneAllBranches(true).setNoCheckout(false).call().close();

    Git git = Git.open(workDir);

    System.out.println("Opening git repository in: " + workDir.getAbsolutePath());

    String readmeFromIntegration = FileUtils.readFileToString(new File("test-repo/readme"));

    git.checkout().setName(FEATURE_BRANCH_NAME).setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
            .setCreateBranch(true).call();
    final int COMMIT_COUNT_ON_FEATURE_BEFORE_EXECUTION = TestUtilsFactory.countCommits(git);
    git.checkout().setName("master").setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).call();

    GitProjectBuilder builder = new GitProjectBuilder()
            .setGitRepos(Collections.singletonList(new UserRemoteConfig(
                    "file://" + repository.getDirectory().getAbsolutePath(), null, null, null)))
            .setUseSlaves(true).setRule(jenkinsRule).setStrategy(GitProjectBuilder.STRATEGY_TYPE.ACCUMULATED);
    builder.setJobType(MatrixProject.class);

    MatrixProject project = (MatrixProject) builder.generateJenkinsJob();
    TestUtilsFactory.triggerProject(project);

    jenkinsRule.waitUntilNoActivityUpTo(60000);

    assertEquals("2 runs for this particular matrix build", 2, project.getLastBuild().getRuns().size());

    String readmeFileContents = FileUtils.readFileToString(new File("test-repo/readme"));
    assertEquals(readmeFromIntegration, readmeFileContents);

    git.pull().call();

    final int COMMIT_COUNT_ON_MASTER_AFTER_EXECUTION = TestUtilsFactory.countCommits(git);

    git.close();

    //We assert that 2 commits from branch gets merged + 1 combined merge commit since we do --no-ff
    assertEquals(COMMIT_COUNT_ON_FEATURE_BEFORE_EXECUTION + 3, COMMIT_COUNT_ON_MASTER_AFTER_EXECUTION);
}

From source file:org.jenkinsci.plugins.pretestedintegration.integration.scm.git.MatrixProjectCompatibilityTestIT.java

/**
 * Git Plugin/*from   ww  w. j  a  v a  2  s.co m*/
 *
 * Test that show that a ready/feature_1 branch get integrated into master
 * using a Matrix job type.
 *
 * Pretested integration:
 *  - 'Integration branch' : master (default)
 *  - 'Repository name' : origin (default)
 *  - 'Strategy' : Squash Commit
 *
 * GitSCM:
 *  - 'Name' : (empty)
 *
 * Workflow
 *  - Create a repository containing a 'ready' branch.
 *  - The build is triggered.
 *
 * Results
 *  - We expect that the plugin triggers, and that the commits on ready branch
 *    is merged into our integration branch master and build result becomes SUCCESS.
 *
 * @throws Exception
 */

@Test
public void oneBuildBasicSmokeTest() throws Exception {
    repository = TestUtilsFactory.createValidRepository("test-repo");
    File workDir = new File(TestUtilsFactory.WORKDIR, "test-repo");
    Git.cloneRepository().setURI("file:///" + repository.getDirectory().getAbsolutePath()).setDirectory(workDir)
            .setBare(false).setCloneAllBranches(true).setNoCheckout(false).call().close();

    Git git = Git.open(workDir);

    System.out.println("Opening git repository in: " + workDir.getAbsolutePath());

    String readmeFromIntegration = FileUtils.readFileToString(new File(workDir, "readme"));

    git.checkout().setName(FEATURE_BRANCH_NAME).setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
            .setCreateBranch(true).call();
    final int COMMIT_COUNT_ON_FEATURE_BEFORE_EXECUTION = TestUtilsFactory.countCommits(git);
    git.checkout().setName("master").setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).call();

    MatrixProjectBuilder builder = new MatrixProjectBuilder()
            .setGitRepos(Collections.singletonList(new UserRemoteConfig(
                    "file://" + repository.getDirectory().getAbsolutePath(), null, null, null)))
            .setUseSlaves(true).setRule(jenkinsRule).setStrategy(TestUtilsFactory.STRATEGY_TYPE.ACCUMULATED);
    builder.setJobType(MatrixProject.class);

    MatrixProject project = (MatrixProject) builder.generateJenkinsJob();
    TestUtilsFactory.triggerProject(project);

    jenkinsRule.waitUntilNoActivityUpTo(60000);

    String readmeFileContents = FileUtils.readFileToString(new File(workDir, "readme"));
    assertEquals(readmeFromIntegration, readmeFileContents);
    git.pull().call();

    assertEquals("3 runs for this particular matrix build", 3, project.getLastBuild().getRuns().size());

    final int COMMIT_COUNT_ON_MASTER_AFTER_EXECUTION = TestUtilsFactory.countCommits(git);
    git.close();
    //We assert that 2 commits from branch gets merged + 1 combined merge commit since we do --no-ff
    assertEquals(COMMIT_COUNT_ON_FEATURE_BEFORE_EXECUTION + 3, COMMIT_COUNT_ON_MASTER_AFTER_EXECUTION);
}