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

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

Introduction

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

Prototype

@Override
public void close() 

Source Link

Document

Free resources associated with this instance.

Usage

From source file:org.eclipse.emf.compare.ide.ui.tests.models.ModelTestCase.java

License:Open Source License

protected Status status(Repository repo) throws Exception {
    Git git = new Git(repo);
    try {/*from   w  ww  . j  a  v a  2s  . c o m*/
        return git.status().call();
    } finally {
        git.close();
    }
}

From source file:org.eclipse.wst.jsdt.bower.core.api.InstallCommand.java

License:Open Source License

/**
 * Downloads the package with the given packageId matching the given range.
 *
 * @param packageId/*from  w ww  .  j  ava  2 s.  co m*/
 *            The package id
 * @param rangeExpression
 *            The range
 * @return The dependencies of the downloaded package
 */
private Map<String, String> download(String packageId, String rangeExpression) {
    Map<String, String> dependencies = new HashMap<String, String>();

    Optional<String> packageUrl = this.getGitUrlFromPackageId(packageId);
    String packageName = this.getNameFromPackageId(packageId);

    if (packageUrl.isPresent() && this.monitor.isPresent() && !this.monitor.get().isCancelled()) {
        if (this.monitor.isPresent()) {
            this.monitor.get().beginTask(I18n.getString(I18nKeys.DOWNLOADING_LABEL, packageName), 10);
        }

        try {
            File tempFile = new File("/tmp"); //$NON-NLS-1$
            final Repository db = FileRepositoryBuilder.create(tempFile);
            Collection<Ref> refs = new Git(db).lsRemote().setRemote(packageUrl.get()).setTags(true).call();

            Optional<Ref> bestMatch = this.findBestMatch(refs, rangeExpression);
            if (bestMatch.isPresent() && outputDirectory.isPresent()) {
                File downloadedDependencyFolder = new File(outputDirectory.get(), packageName);
                if (!downloadedDependencyFolder.exists()) {
                    Git git = Git.cloneRepository().setProgressMonitor(monitor.get()).setURI(packageUrl.get())
                            .setDirectory(downloadedDependencyFolder).setBranch(bestMatch.get().getName())
                            .setBare(false).setNoCheckout(false).call();
                    git.close();

                    File gitFolder = new File(downloadedDependencyFolder, IBowerConstants.GIT_EXTENSION);
                    this.delete(gitFolder);

                    File bowerJsonFile = new File(downloadedDependencyFolder, IBowerConstants.BOWER_JSON);
                    Optional<BowerJson> dependencyBowerJson = this.getBowerJson(bowerJsonFile);
                    if (dependencyBowerJson.isPresent()) {
                        dependencies.putAll(dependencyBowerJson.get().getDependencies());
                    }
                }
            }

            db.close();
        } catch (GitAPIException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (this.monitor.isPresent()) {
            this.monitor.get().endTask();
        }
    }
    return dependencies;
}

From source file:org.gradle.vcs.git.internal.GitVersionControlSystem.java

License:Apache License

private static void cloneRepo(File workingDir, GitVersionControlSpec gitSpec, VersionRef ref) {
    CloneCommand clone = Git.cloneRepository().setURI(gitSpec.getUrl().toString()).setDirectory(workingDir)
            .setCloneSubmodules(true);//  w ww  .j  a  va  2  s.  c o  m
    Git git = null;
    try {
        git = clone.call();
        git.reset().setMode(ResetCommand.ResetType.HARD).setRef(ref.getCanonicalId()).call();
    } catch (GitAPIException e) {
        throw wrapGitCommandException("clone", gitSpec.getUrl(), workingDir, e);
    } catch (JGitInternalException e) {
        throw wrapGitCommandException("clone", gitSpec.getUrl(), workingDir, e);
    } finally {
        if (git != null) {
            git.close();
        }
    }
}

From source file:org.gradle.vcs.git.internal.GitVersionControlSystem.java

License:Apache License

private static void resetRepo(File workingDir, GitVersionControlSpec gitSpec, VersionRef ref) {
    Git git = null;
    try {/*from   w  ww .  j a  va2  s  .c om*/
        git = Git.open(workingDir);
        git.reset().setMode(ResetCommand.ResetType.HARD).setRef(ref.getCanonicalId()).call();
        updateSubModules(git);
    } catch (IOException e) {
        throw wrapGitCommandException("reset", gitSpec.getUrl(), workingDir, e);
    } catch (GitAPIException e) {
        throw wrapGitCommandException("reset", gitSpec.getUrl(), workingDir, e);
    } catch (JGitInternalException e) {
        throw wrapGitCommandException("reset", gitSpec.getUrl(), workingDir, e);
    } finally {
        if (git != null) {
            git.close();
        }
    }
}

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

License:Open Source License

@Override
public void close(final Git repo) {
    if (repo != null) {
        repo.close();
    }
}

From source file:org.jboss.tools.openshift.reddeer.utils.TestUtils.java

License:Open Source License

public static void closeGitRepository(File repoDir) {
    try {/* ww w  .j a v  a 2  s .  c  o m*/
        Git git = Git.open(repoDir);
        git.getRepository().close();
        git.close();
    } catch (IOException ex) {
        // DO NOTHING
    }
}

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

/**
 * Git Plugin//from  ww w.  j av  a2s. c o  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("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 w w  w  .  j  a  va 2 s .  c o  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);
}

From source file:org.kantega.reststop.maven.CreatePluginMojo.java

License:Apache License

private void addNewFilesToGit(File pluginsDir, File pluginPomFile, File pluginClassFile) throws IOException {
    File gitDir = new File(new File(pluginsDir.getParent()), ".git");
    if (gitDir.exists()) {
        File workDir = new File(gitDir.getParent());
        Git git = null;

        try {//w w  w.jav  a  2  s  .com
            git = Git.open(workDir);
            git.add().addFilepattern(getRelativeTo(pluginPomFile, workDir)).call();
            git.add().addFilepattern(getRelativeTo(pluginClassFile, workDir)).call();
        } catch (GitAPIException e) {
            // ignore
        } finally {
            if (git != null) {
                git.close();
            }

        }
    }
}

From source file:org.kie.eclipse.server.KieRepositoryHandler.java

License:Open Source License

@Override
public Object load() {
    if (repository == null) {
        try {//from   w w  w . java  2 s . c om
            final File repoRoot = new File(PreferencesUtils.getRepoRoot(this));
            final Set<File> gitDirs = new HashSet<File>();
            GitUtils.findGitDirsRecursive(repoRoot, gitDirs, false);
            for (File dir : gitDirs) {
                if (getName().equals(dir.getParentFile().getName())) {
                    Git git = Git.open(dir);
                    Repository repository = git.getRepository();
                    StoredConfig storedConfig = repository.getConfig();
                    Set<String> remotes = storedConfig.getSubsections("remote");
                    for (String remoteName : remotes) {
                        try {
                            String url = storedConfig.getString("remote", remoteName, "url");
                            URI uri = new URI(url);
                            int port = uri.getPort();
                            String host = uri.getHost();
                            String scheme = uri.getScheme();
                            String path[] = uri.getPath().split("/");
                            String repoName = path[path.length - 1];
                            if (name.equals(repoName) && host.equals(getServer().getHost())
                                    && port == getDelegate().getGitPort() && "ssh".equals(scheme)) {
                                this.repository = repository;
                                break;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            if (git != null) {
                                git.close();
                                git = null;
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return repository;
}