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

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

Introduction

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

Prototype

public RawText getResultContents() 

Source Link

Document

Get result contents

Usage

From source file:com.gitblit.utils.DiffUtils.java

License:Apache License

/**
 * Returns the list of lines in the specified source file annotated with the
 * source commit metadata./*w  w w.j a  v  a2s.co m*/
 *
 * @param repository
 * @param blobPath
 * @param objectId
 * @return list of annotated lines
 */
public static List<AnnotatedLine> blame(Repository repository, String blobPath, String objectId) {
    List<AnnotatedLine> lines = new ArrayList<AnnotatedLine>();
    try {
        ObjectId object;
        if (StringUtils.isEmpty(objectId)) {
            object = JGitUtils.getDefaultBranch(repository);
        } else {
            object = repository.resolve(objectId);
        }
        BlameCommand blameCommand = new BlameCommand(repository);
        blameCommand.setFilePath(blobPath);
        blameCommand.setStartCommit(object);
        BlameResult blameResult = blameCommand.call();
        RawText rawText = blameResult.getResultContents();
        int length = rawText.size();
        for (int i = 0; i < length; i++) {
            RevCommit commit = blameResult.getSourceCommit(i);
            AnnotatedLine line = new AnnotatedLine(commit, i + 1, rawText.getString(i));
            lines.add(line);
        }
    } catch (Throwable t) {
        LOGGER.error(MessageFormat.format("failed to generate blame for {0} {1}!", blobPath, objectId), t);
    }
    return lines;
}

From source file:com.google.gitiles.BlameCache.java

License:Open Source License

private List<BlameCache.Region> loadBlame(Key key) throws IOException {
    try {/*w w w .j a v  a 2s .  co m*/
        BlameGenerator gen = new BlameGenerator(key.repo, key.path);
        BlameResult blame;
        try {
            gen.push(null, key.commitId);
            blame = gen.computeBlameResult();
        } finally {
            gen.release();
        }
        if (blame == null) {
            return ImmutableList.of();
        }
        int lineCount = blame.getResultContents().size();
        blame.discardResultContents();

        List<BlameCache.Region> regions = Lists.newArrayList();
        for (int i = 0; i < lineCount; i++) {
            if (regions.isEmpty() || !regions.get(regions.size() - 1).growFrom(blame, i)) {
                regions.add(new BlameCache.Region(blame, i));
            }
        }
        return Collections.unmodifiableList(regions);
    } finally {
        key.repo = null;
    }
}

From source file:com.tasktop.c2c.server.scm.service.GitBrowseUtil.java

License:Open Source License

public static Blame getBlame(Repository r, String revision, String path) throws IOException, GitAPIException {

    if (path.startsWith("/")) {
        path = path.substring(1);/*from  ww  w .  jav a  2 s  .c o  m*/
    }

    Git git = new Git(r);

    ObjectId revId = r.resolve(revision);

    BlameCommand bc = git.blame();
    bc.setStartCommit(revId);
    bc.setFilePath(path);
    BlameResult br = bc.call();

    Blame blame = new Blame();
    blame.path = path;
    blame.revision = revision;
    blame.commits = new ArrayList<Commit>();
    blame.lines = new ArrayList<Blame.Line>();

    Map<String, Integer> sha2idx = new HashMap<String, Integer>();

    RawText resultContents = br.getResultContents();

    for (int i = 0; i < br.getResultContents().size(); i++) {

        RevCommit sourceCommit = br.getSourceCommit(i); // XXX should it really be the source commit
        String sha = sourceCommit.getName();
        Integer cIdx = sha2idx.get(sha);
        if (cIdx == null) {
            cIdx = blame.commits.size();
            Commit commit = GitDomain.createCommit(sourceCommit);
            blame.commits.add(commit);
            sha2idx.put(sha, cIdx);
        }

        Blame.Line bl = new Blame.Line();
        bl.commit = cIdx;
        bl.text = resultContents.getString(i);
        blame.lines.add(bl);
    }

    return blame;
}

From source file:org.eclipse.egit.ui.internal.blame.BlameOperation.java

License:Open Source License

public void execute(IProgressMonitor monitor) throws CoreException {
    final RevisionInformation info = new RevisionInformation();
    info.setHoverControlCreator(new BlameInformationControlCreator(false));
    info.setInformationPresenterControlCreator(new BlameInformationControlCreator(true));

    final BlameCommand command = new BlameCommand(repository).setFollowFileRenames(true).setFilePath(path);
    if (startCommit != null)
        command.setStartCommit(startCommit);
    if (Activator.getDefault().getPreferenceStore().getBoolean(UIPreferences.BLAME_IGNORE_WHITESPACE))
        command.setTextComparator(RawTextComparator.WS_IGNORE_ALL);

    BlameResult result;
    try {/*from  ww w. j  a v a 2s  .  com*/
        result = command.call();
    } catch (Exception e1) {
        Activator.error(e1.getMessage(), e1);
        return;
    }
    if (result == null)
        return;

    Map<RevCommit, BlameRevision> revisions = new HashMap<RevCommit, BlameRevision>();
    int lineCount = result.getResultContents().size();
    BlameRevision previous = null;
    for (int i = 0; i < lineCount; i++) {
        RevCommit commit = result.getSourceCommit(i);
        if (commit == null) {
            // Unregister the current revision
            if (previous != null) {
                previous.register();
                previous = null;
            }
            continue;
        }
        BlameRevision revision = revisions.get(commit);
        if (revision == null) {
            revision = new BlameRevision();
            revision.setRepository(repository);
            revision.setCommit(commit);
            revisions.put(commit, revision);
            info.addRevision(revision);
        }
        if (previous != null)
            if (previous == revision)
                previous.addLine();
            else {
                previous.register();
                previous = revision.reset(i);
            }
        else
            previous = revision.reset(i);
    }
    if (previous != null)
        previous.register();

    shell.getDisplay().asyncExec(new Runnable() {
        public void run() {
            openEditor(info);
        }
    });
}

From source file:org.eclipse.orion.server.git.servlets.GitBlameHandlerV1.java

License:Open Source License

public void doBlame(Blame blame, Repository db) throws GitAPIException, IOException {

    String filePath = blame.getFilePath();

    if (db != null && filePath != null) {

        BlameCommand blameCommand = new BlameCommand(db);
        blameCommand.setFilePath(filePath);
        blameCommand.setFollowFileRenames(true);
        blameCommand.setTextComparator(RawTextComparator.WS_IGNORE_ALL);

        if (blame.getStartCommit() != null) {
            blameCommand.setStartCommit(blame.getStartCommit());
        }/*from   w w w  .j a v  a  2  s.  c o  m*/
        BlameResult result;

        try {
            result = blameCommand.call();
        } catch (Exception e1) {
            return;
        }
        if (result != null) {
            blame.clearLines();
            RevCommit commit;
            RevCommit prevCommit = null;
            String path;
            String prevPath = null;
            for (int i = 0; i < result.getResultContents().size(); i++) {
                try {
                    commit = result.getSourceCommit(i);
                    prevCommit = commit;
                } catch (NullPointerException e) {
                    commit = prevCommit;
                }

                if (!blame.commitExists(commit)) {
                    if (commit != null) {
                        blame.addCommit(commit);
                    }
                }

                try {
                    path = commit.getId().getName();
                    prevPath = path;
                } catch (NullPointerException e) {
                    path = prevPath;
                }
                blame.addLine(path);
            }
        }
    }
}

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   www  .  j av  a  2  s  .  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 w ww.j a  v a2s.c  om
        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 {// ww  w.ja  v a  2  s  .c  o  m
        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);
}