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:au.id.soundadvice.systemdesign.versioning.jgit.GitVersionControl.java

License:Open Source License

public static void init(Path path) throws GitAPIException {
    Git repo = Git.init().setDirectory(path.toFile()).call();
    try {//from   w w  w  . j  a v a  2  s.  co m
        repo.add().addFilepattern(".").call();
    } finally {
        repo.close();
    }
}

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

License:Apache License

public SCMRepository info() {
    RevWalk rw = null;/*from w  ww.j  a v  a2s . c  om*/
    Git git = null;
    try {
        git = Git.open(new File(path));
        AnyObjectId headId = git.getRepository().resolve(Constants.HEAD);

        rw = new RevWalk(git.getRepository());
        RevCommit root = rw.parseCommit(headId);
        rw.sort(RevSort.REVERSE);
        rw.markStart(root);
        RevCommit lastCommit = rw.next();

        String origin = git.getRepository().getConfig().getString("remote", "origin", "url");

        return new SCMRepository(this, origin, path, headId.getName(), lastCommit.getName());
    } catch (Exception e) {
        throw new RuntimeException("error when info " + path, e);
    } finally {
        if (rw != null)
            rw.release();
        if (git != null)
            git.close();
    }

}

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

License:Apache License

public ChangeSet getHead() {
    Git git = null;
    try {//from ww w. j  a v  a  2  s  . co  m
        git = Git.open(new File(path));
        ObjectId head = git.getRepository().resolve(Constants.HEAD);

        RevWalk revWalk = new RevWalk(git.getRepository());
        RevCommit r = revWalk.parseCommit(head);
        return new ChangeSet(r.getName(), convertToDate(r));

    } catch (Exception e) {
        throw new RuntimeException("error in getHead() for " + path, e);
    } finally {
        if (git != null)
            git.close();
    }

}

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

License:Apache License

@Override
public List<ChangeSet> getChangeSets() {
    Git git = null;
    try {//w ww . jav a2  s  . co m
        git = Git.open(new File(path));

        List<ChangeSet> allCs = new ArrayList<ChangeSet>();

        for (RevCommit r : git.log().all().call()) {
            String hash = r.getName();
            GregorianCalendar date = convertToDate(r);

            allCs.add(new ChangeSet(hash, date));
        }

        return allCs;
    } catch (Exception e) {
        throw new RuntimeException("error in getChangeSets for " + path, e);
    } finally {
        if (git != null)
            git.close();
    }
}

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

License:Apache License

@Override
public Commit getCommit(String id) {
    Git git = null;
    try {/*from   w  w w.  jav a 2s  . c  o  m*/
        git = Git.open(new File(path));
        Repository repo = git.getRepository();

        Iterable<RevCommit> commits = git.log().add(repo.resolve(id)).call();
        Commit theCommit = null;

        for (RevCommit jgitCommit : commits) {

            Developer author = new Developer(jgitCommit.getAuthorIdent().getName(),
                    jgitCommit.getAuthorIdent().getEmailAddress());
            Developer committer = new Developer(jgitCommit.getCommitterIdent().getName(),
                    jgitCommit.getCommitterIdent().getEmailAddress());

            String msg = jgitCommit.getFullMessage().trim();
            String hash = jgitCommit.getName().toString();
            long epoch = jgitCommit.getCommitTime();
            String parent = (jgitCommit.getParentCount() > 0) ? jgitCommit.getParent(0).getName().toString()
                    : "";

            GregorianCalendar date = new GregorianCalendar();
            date.setTime(new Date(epoch * 1000L));

            theCommit = new Commit(hash, author, committer, date, msg, parent);

            List<DiffEntry> diffsForTheCommit = diffsForTheCommit(repo, jgitCommit);
            if (diffsForTheCommit.size() > MAX_NUMBER_OF_FILES_IN_A_COMMIT) {
                log.error("commit " + id + " has more than files than the limit");
                throw new RuntimeException("commit " + id + " too big, sorry");
            }

            for (DiffEntry diff : diffsForTheCommit) {

                ModificationType change = Enum.valueOf(ModificationType.class, diff.getChangeType().toString());

                String oldPath = diff.getOldPath();
                String newPath = diff.getNewPath();

                String diffText = "";
                String sc = "";
                if (diff.getChangeType() != ChangeType.DELETE) {
                    diffText = getDiffText(repo, diff);
                    sc = getSourceCode(repo, diff);
                }

                if (diffText.length() > MAX_SIZE_OF_A_DIFF) {
                    log.error("diff for " + newPath + " too big");
                    diffText = "-- TOO BIG --";
                }

                theCommit.addModification(oldPath, newPath, change, diffText, sc);

            }

            break;
        }

        return theCommit;
    } catch (Exception e) {
        throw new RuntimeException("error detailing " + id + " in " + path, e);
    } finally {
        if (git != null)
            git.close();
    }
}

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

License:Apache License

public void checkout(String hash) {
    Git git = null;
    try {//w w w .  j  a  v  a 2s . c o  m
        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  .j ava 2  s  . c  om
        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.metricminer2.scm.GitRepository.java

License:Apache License

@Override
public String blame(String file, String currentCommit, Integer line) {
    Git git = null;
    try {/*from w ww.  j a va 2s.c o  m*/
        git = Git.open(new File(path));

        Iterable<RevCommit> commits = git.log().add(git.getRepository().resolve(currentCommit)).call();
        ObjectId prior = commits.iterator().next().getParent(0).getId();

        BlameResult blameResult = git.blame().setFilePath(file).setStartCommit(prior).setFollowFileRenames(true)
                .call();

        return blameResult.getSourceCommit(line).getId().getName();

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

}

From source file:com.github.checkstyle.regression.extract.CheckstyleInjector.java

License:Open Source License

/**
 * Clears the injected files and the generated info file in checkstyle repository.
 * @throws InjectException failure of clearing
 *//*from w  w  w  .j  a  va  2  s .co m*/
public void clearInjections() throws InjectException {
    final Git git = new Git(repository);

    try {
        git.clean().setCleanDirectories(true).call();
    } catch (GitAPIException ex) {
        throw new InjectException("unable to clear injections", ex);
    } finally {
        git.close();
    }
}

From source file:com.github.checkstyle.regression.extract.CheckstyleInjector.java

License:Open Source License

/**
 * Checkouts to the PR branch in the given repository.
 * @throws GitAPIException JGit library exception
 *//*w  ww.  j  av  a 2 s .  c  om*/
private void checkoutToPrBranch() throws GitAPIException {
    final Git git = new Git(repository);

    try {
        git.checkout().setName(branch).call();
    } finally {
        git.close();
    }
}