Example usage for org.eclipse.jgit.blame BlameResult getSourceCommitter

List of usage examples for org.eclipse.jgit.blame BlameResult getSourceCommitter

Introduction

In this page you can find the example usage for org.eclipse.jgit.blame BlameResult getSourceCommitter.

Prototype

public PersonIdent getSourceCommitter(int idx) 

Source Link

Document

Get the committer that provided the specified line of the result.

Usage

From source file:org.apache.maven.scm.provider.git.jgit.command.blame.JGitBlameCommand.java

License:Apache License

@Override
public BlameScmResult executeBlameCommand(ScmProviderRepository repo, ScmFileSet workingDirectory,
        String filename) throws ScmException {

    Git git = null;/*from   ww  w . java2  s  . c o  m*/
    File basedir = workingDirectory.getBasedir();
    try {
        git = Git.open(basedir);
        BlameResult blameResult = git.blame().setFilePath(filename).call();

        List<BlameLine> lines = new ArrayList<BlameLine>();

        int i = 0;
        while ((i = blameResult.computeNext()) != -1) {
            lines.add(new BlameLine(blameResult.getSourceAuthor(i).getWhen(),
                    blameResult.getSourceCommit(i).getName(), blameResult.getSourceAuthor(i).getName(),
                    blameResult.getSourceCommitter(i).getName()));
        }

        return new BlameScmResult("JGit blame", lines);
    } catch (Exception e) {
        throw new ScmException("JGit blame failure!", e);
    } finally {
        JGitUtils.closeRepo(git);
    }
}

From source file:org.repodriller.scm.GitRepository.java

License:Apache License

public List<BlamedLine> blame(String file, String commitToBeBlamed, boolean priorCommit) {
    try (Git git = openRepository()) {
        ObjectId gitCommitToBeBlamed;/*  ww w  . ja va  2  s. c om*/
        if (priorCommit) {
            Iterable<RevCommit> commits = git.log().add(git.getRepository().resolve(commitToBeBlamed)).call();
            gitCommitToBeBlamed = commits.iterator().next().getParent(0).getId();
        } else {
            gitCommitToBeBlamed = git.getRepository().resolve(commitToBeBlamed);
        }

        BlameResult blameResult = git.blame().setFilePath(file).setStartCommit(gitCommitToBeBlamed)
                .setFollowFileRenames(true).call();
        if (blameResult != null) {
            int rows = blameResult.getResultContents().size();
            List<BlamedLine> result = new ArrayList<>();
            for (int i = 0; i < rows; i++) {
                result.add(new BlamedLine(i, blameResult.getResultContents().getString(i),
                        blameResult.getSourceAuthor(i).getName(), blameResult.getSourceCommitter(i).getName(),
                        blameResult.getSourceCommit(i).getId().getName()));
            }

            return result;
        } else {
            throw new RuntimeException("BlameResult not found.");
        }

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.sonarsource.scm.git.JGitBlameCommand.java

License:Open Source License

private void blame(BlameOutput output, Git git, File gitBaseDir, InputFile inputFile) throws GitAPIException {
    String filename = pathResolver.relativePath(gitBaseDir, inputFile.file());
    LOG.debug("Blame file {}", filename);
    org.eclipse.jgit.blame.BlameResult blameResult;
    try {/*from ww  w.j a va 2  s  .  c  om*/
        blameResult = git.blame()
                // Equivalent to -w command line option
                .setTextComparator(RawTextComparator.WS_IGNORE_ALL).setFilePath(filename).call();
    } catch (Exception e) {
        throw new IllegalStateException("Unable to blame file " + inputFile.relativePath(), e);
    }
    List<BlameLine> lines = new ArrayList<>();
    if (blameResult == null) {
        LOG.debug("Unable to blame file {}. It is probably a symlink.", inputFile.relativePath());
        return;
    }
    for (int i = 0; i < blameResult.getResultContents().size(); i++) {
        if (blameResult.getSourceAuthor(i) == null || blameResult.getSourceCommit(i) == null) {
            LOG.debug(
                    "Unable to blame file {}. No blame info at line {}. Is file committed? [Author: {} Source commit: {}]",
                    inputFile.relativePath(), i + 1, blameResult.getSourceAuthor(i),
                    blameResult.getSourceCommit(i));
            return;
        }
        lines.add(new org.sonar.api.batch.scm.BlameLine().date(blameResult.getSourceCommitter(i).getWhen())
                .revision(blameResult.getSourceCommit(i).getName())
                .author(blameResult.getSourceAuthor(i).getEmailAddress()));
    }
    if (lines.size() == inputFile.lines() - 1) {
        // SONARPLUGINS-3097 Git do not report blame on last empty line
        lines.add(lines.get(lines.size() - 1));
    }
    output.blameResult(inputFile, lines);
}