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

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

Introduction

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

Prototype

public PersonIdent getSourceAuthor(int idx) 

Source Link

Document

Get the author 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  www. ja  v a  2 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;//from   w  w w.j av a2s  .c  o m
        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.sonar.plugins.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());
    org.eclipse.jgit.blame.BlameResult blameResult = git.blame()
            // Equivalent to -w command line option
            .setTextComparator(RawTextComparator.WS_IGNORE_ALL).setFilePath(filename).call();
    List<BlameLine> lines = new ArrayList<BlameLine>();
    for (int i = 0; i < blameResult.getResultContents().size(); i++) {
        if (blameResult.getSourceAuthor(i) == null || blameResult.getSourceCommit(i) == null) {
            LOG.info("Author: " + blameResult.getSourceAuthor(i));
            LOG.info("Source commit: " + blameResult.getSourceCommit(i));
            throw new IllegalStateException("Unable to blame file " + inputFile.relativePath()
                    + ". No blame info at line " + (i + 1) + ". Is file commited?");
        }/*from   ww w  .  ja v  a 2s  . c o  m*/
        lines.add(new org.sonar.api.batch.scm.BlameLine().date(blameResult.getSourceAuthor(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);
}

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.  ja  v a  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);
}

From source file:playRepository.GitRepository.java

License:Apache License

/**
 * Finds authors who modified or removed lines from the given edit list and
 * the result of git blame./*from  w  w w  . j  a  v  a  2  s . c om*/
 *
 * @param edits an edit list
 * @param blameResult the result of blame
 * @return a set of authors
 */
private static Set<User> getAuthorsFromBlameResult(EditList edits, BlameResult blameResult) {
    Set<User> authors = new HashSet<>();
    for (Edit edit : edits) {
        if (isTypeMatching(edit.getType(), Type.REPLACE, Type.DELETE)) {
            for (int i = edit.getBeginA(); i < edit.getEndA(); i++) {
                authors.add(findAuthorByPersonIdent(blameResult.getSourceAuthor(i)));
            }
        }
    }
    return authors;
}