Example usage for org.eclipse.jgit.lib Repository close

List of usage examples for org.eclipse.jgit.lib Repository close

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository close.

Prototype

@Override
public void close() 

Source Link

Document

Decrement the use count, and maybe close resources.

Usage

From source file:org.gradle.vcs.fixtures.GitFileRepository.java

License:Apache License

public RevCommit addSubmodule(GitFileRepository submoduleRepo) throws GitAPIException {
    Repository submodule = null;
    try {/*from  w  ww  . j  a  va 2 s .  c om*/
        submodule = git.submoduleAdd().setURI(submoduleRepo.getWorkTree().toString())
                .setPath(submoduleRepo.getName()).call();
        return commit("add submodule " + submoduleRepo.getName(), submoduleRepo.getName());
    } finally {
        if (submodule != null) {
            submodule.close();
        }
    }
}

From source file:org.gradle.vcs.fixtures.GitFileRepository.java

License:Apache License

/**
 * Updates any submodules in this repository to the latest in the submodule origin repository
 *//*from w w  w. j  a v a 2 s.com*/
public RevCommit updateSubmodulesToLatest() throws GitAPIException {
    List<String> submodulePaths = Lists.newArrayList();
    try {
        SubmoduleWalk walker = SubmoduleWalk.forIndex(git.getRepository());
        try {
            while (walker.next()) {
                Repository submodule = walker.getRepository();
                try {
                    submodulePaths.add(walker.getPath());
                    Git.wrap(submodule).pull().call();
                } finally {
                    submodule.close();
                }
            }
        } finally {
            walker.close();
        }
        return commit("update submodules", submodulePaths.toArray(new String[submodulePaths.size()]));
    } catch (IOException e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
}

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

License:Apache License

private static void updateSubModules(Git git) throws IOException, GitAPIException {
    SubmoduleWalk walker = SubmoduleWalk.forIndex(git.getRepository());
    try {// ww w.j a  v a 2  s  . c om
        while (walker.next()) {
            Repository submodule = walker.getRepository();
            try {
                Git submoduleGit = Git.wrap(submodule);
                submoduleGit.fetch().call();
                git.submoduleUpdate().addPath(walker.getPath()).call();
                submoduleGit.reset().setMode(ResetCommand.ResetType.HARD).call();
                updateSubModules(submoduleGit);
            } finally {
                submodule.close();
            }
        }
    } finally {
        walker.close();
    }
}

From source file:org.jboss.tools.openshift.internal.ui.wizard.importapp.ImportApplicationWizardModel.java

License:Open Source License

private Repository updateRepository(String newRepoPath, String oldRepoPath) {
    Repository repository = this.repository;
    if (!StringUtils.equals(newRepoPath, oldRepoPath)) {
        if (repository != null) {
            repository.close();
        }/*from w w  w .ja v  a 2  s.com*/
        repository = EGitUtils.getRepository(getCloneDestination(newRepoPath));
    }
    return this.repository = repository;
}

From source file:org.kercoin.magrit.core.user.UserIdentityServiceImplTest.java

License:Open Source License

private static void closeRepo(Repository... repos) {
    for (Repository repo : repos) {
        if (repo != null) {
            repo.close();
            tests.FilesUtils.recursiveDelete(repo.getDirectory().getParentFile());
            repo = null;/*from ww w  .j av  a2  s .  c  o m*/
        }
    }
}

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

License:Apache License

public static Git cloneRepository(final File repoFolder, final String fromURI, final boolean bare,
        final CredentialsProvider credentialsProvider) {

    if (!repoFolder.getName().endsWith(DOT_GIT_EXT)) {
        throw new RuntimeException("Invalid name");
    }/*from w w  w . ja  va2 s .  co m*/

    try {
        final File gitDir = RepositoryCache.FileKey.resolve(repoFolder, DETECTED);
        final Repository repository;
        final Git git;
        if (gitDir != null && gitDir.exists()) {
            repository = new FileRepository(gitDir);
            git = new Git(repository);
        } else {
            git = Git.cloneRepository().setBare(bare).setCloneAllBranches(true).setURI(fromURI)
                    .setDirectory(repoFolder).setCredentialsProvider(credentialsProvider).call();
            repository = git.getRepository();
        }

        fetchRepository(git, credentialsProvider);

        repository.close();

        return git;
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.kuali.student.git.importer.JGitPushMain.java

License:Educational Community License

/**
 * @param args/*w ww.  ja v a 2 s .  co  m*/
 */
public static void main(String[] args) {

    if (args.length != 4 && args.length != 5) {
        System.err.println("USAGE: <git repository> <bare> <remote name> <branch prefixes> [<ref prefix>]");
        System.err.println(
                "\t<branch prefixes> : like enrollment_ks-enroll (colon seperated for multiple prefixes)");
        System.err.println(
                "\t<ref prefix> : like refs/heads/ or refs/remotes/origin/ needs to end with a trailing slash /");
        System.exit(-1);
    }

    boolean bare = false;

    if (args[1].trim().equals("true")) {
        bare = true;
    }

    String remoteName = args[2];

    String branchPrefixes[] = args[3].split(":");

    String refPrefix = Constants.R_HEADS;

    if (args.length == 5)
        refPrefix = args[4].trim();

    try {

        Repository repo = GitRepositoryUtils.buildFileRepository(new File(args[0]).getAbsoluteFile(), false,
                bare);

        Collection<Ref> refsToPush = repo.getRefDatabase().getRefs(refPrefix).values();

        List<RemoteRefUpdate> refsToUpdate = new ArrayList<>();

        for (String branchPrefix : branchPrefixes) {

            String adjustedBranchPrefix = refPrefix + branchPrefix;

            for (Ref candidateRef : refsToPush) {

                if (candidateRef.getName().startsWith(adjustedBranchPrefix)) {

                    String candidateBranchName = candidateRef.getName().substring(refPrefix.length());

                    String targetRefName = Constants.R_HEADS + candidateBranchName;

                    log.info("pushing " + adjustedBranchPrefix + " to " + targetRefName);

                    refsToUpdate.add(new RemoteRefUpdate(repo, candidateRef, targetRefName, true, null, null));
                }
            }
        }

        RevWalk rw = new RevWalk(repo);

        push(repo, remoteName, refsToUpdate, null, null);

        rw.release();

        repo.close();

    } catch (Exception e) {

        log.error("unexpected Exception ", e);
    }
}

From source file:org.kuali.student.git.model.TestKSRevision27975.java

License:Educational Community License

@Test
public void testRevision27975() throws IOException {
    File gitRepository = new File("target/ks-r27975");

    FileUtils.deleteDirectory(gitRepository);

    Repository repository = GitRepositoryUtils.buildFileRepository(gitRepository, true);

    GitImporterMain//from  w  w  w  .j  ava2 s. c  o m
            .main(new String[] { "src/test/resources/ks-r27975.dump.bz2", gitRepository.getAbsolutePath(),
                    "target/ks-r27975-ks-veto.log", "target/ks-r27975-ks-copyFrom-skipped.log",
                    "target/ks-r27975-blob.log", "0", "https://svn.kuali.org/repos/student", "uuid" });

    RevWalk rw = new RevWalk(repository);

    Ref branch = repository.getRef("sandbox_ks-1.3-core-slice-demo_modules_ks-pom_trunk");

    Assert.assertNotNull(branch);

    RevCommit headCommit = rw.parseCommit(branch.getObjectId());

    TreeWalk tw = new TreeWalk(repository);

    tw.addTree(headCommit.getTree().getId());

    tw.setRecursive(true);

    int blobCount = 0;

    ObjectId blobId = null;

    while (tw.next()) {

        if (tw.getFileMode(0) == FileMode.REGULAR_FILE) {
            blobCount++;

            blobId = tw.getObjectId(0);
        }
    }

    Assert.assertEquals(1, blobCount);

    ObjectLoader blobLoader = repository.getObjectDatabase().newReader().open(blobId);

    List<String> lines = IOUtils.readLines(blobLoader.openStream());

    Assert.assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", lines.get(0));

    tw.release();

    rw.release();
    repository.close();

}

From source file:org.moxie.utils.JGitUtils.java

License:Apache License

public static String getCommitId(File folder) {
    // try specified folder or subfolder
    File gitDir = FileKey.resolve(folder, FS.DETECTED);

    if (gitDir == null || !gitDir.exists()) {
        // try parent folder
        gitDir = FileKey.resolve(folder.getParentFile(), FS.DETECTED);
    }/*from  w w w . j a v  a2  s  .com*/
    if (gitDir == null || !gitDir.exists()) {
        throw new MoxieException("Can not find .git folder for " + folder);
    }

    String hashid = "";
    try {
        Repository repository = new FileRepository(gitDir);
        ObjectId objectId = repository.resolve(org.eclipse.jgit.lib.Constants.HEAD);
        hashid = objectId.getName().toString();
        repository.close();
    } catch (IOException io) {
        io.printStackTrace();
        throw new MoxieException("IOException accessing " + gitDir.getAbsolutePath(), io);
    }
    return hashid;
}

From source file:org.sjanisch.skillview.git.GitContributionService.java

License:Open Source License

@Override
public Stream<Contribution> retrieveContributions(Instant startExclusive, Instant endInclusive) {
    Objects.requireNonNull(startExclusive, "startExclusive");
    Objects.requireNonNull(endInclusive, "endInclusive");

    Repository repository = repositorySupplier.get();
    Runnable closeRepository = () -> repository.close();
    try (Git git = new Git(repository)) {
        try {/*from  w w w  .ja  v  a2  s .  c  o  m*/
            Helper helper = new Helper(repository, project, git, startExclusive, endInclusive);
            Stream<Contribution> contributions = helper.readContributions().onClose(closeRepository);
            return contributions;
        } catch (Exception e) {
            String msg = "Could not retrieve contributions between %s and %s";
            msg = String.format(msg, startExclusive, endInclusive);
            throw new ContributionRetrievalException(msg, e);
        }
    }
}