Search in sources :

Example 1 with AnnotatedLine

use of com.gitblit.models.AnnotatedLine in project gitblit by gitblit.

the class DiffUtilsTest method testBlame.

@Test
public void testBlame() throws Exception {
    Repository repository = GitBlitSuite.getHelloworldRepository();
    List<AnnotatedLine> lines = DiffUtils.blame(repository, "java.java", "1d0c2933a4ae69c362f76797d42d6bd182d05176");
    repository.close();
    assertTrue(lines.size() > 0);
    assertEquals("c6d31dccf5cc75e8e46299fc62d38f60ec6d41e0", lines.get(0).commitId);
}
Also used : AnnotatedLine(com.gitblit.models.AnnotatedLine) Repository(org.eclipse.jgit.lib.Repository) Test(org.junit.Test)

Example 2 with AnnotatedLine

use of com.gitblit.models.AnnotatedLine in project gitblit by gitblit.

the class BlamePage method initializeColors.

private Map<?, String> initializeColors(BlameType blameType, List<AnnotatedLine> lines) {
    ColorFactory colorFactory = new ColorFactory();
    Map<?, String> colorMap;
    if (BlameType.AGE == blameType) {
        Set<Date> keys = new TreeSet<Date>(new Comparator<Date>() {

            @Override
            public int compare(Date o1, Date o2) {
                // younger code has a brighter, older code lightens to white
                return o1.compareTo(o2);
            }
        });
        for (AnnotatedLine line : lines) {
            keys.add(line.when);
        }
        // TODO consider making this a setting
        colorMap = colorFactory.getGraduatedColorMap(keys, Color.decode("#FFA63A"));
    } else {
        Set<String> keys = new HashSet<String>();
        for (AnnotatedLine line : lines) {
            if (blameType == BlameType.AUTHOR) {
                keys.add(line.author);
            } else {
                keys.add(line.commitId);
            }
        }
        colorMap = colorFactory.getRandomColorMap(keys);
    }
    return colorMap;
}
Also used : AnnotatedLine(com.gitblit.models.AnnotatedLine) TreeSet(java.util.TreeSet) ColorFactory(com.gitblit.utils.ColorFactory) Date(java.util.Date) HashSet(java.util.HashSet)

Example 3 with AnnotatedLine

use of com.gitblit.models.AnnotatedLine in project gitblit by gitblit.

the class DiffUtils method blame.

/**
	 * Returns the list of lines in the specified source file annotated with the
	 * source commit metadata.
	 *
	 * @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;
}
Also used : AnnotatedLine(com.gitblit.models.AnnotatedLine) BlameResult(org.eclipse.jgit.blame.BlameResult) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) BlameCommand(org.eclipse.jgit.api.BlameCommand) RawText(org.eclipse.jgit.diff.RawText) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

AnnotatedLine (com.gitblit.models.AnnotatedLine)3 ColorFactory (com.gitblit.utils.ColorFactory)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 BlameCommand (org.eclipse.jgit.api.BlameCommand)1 BlameResult (org.eclipse.jgit.blame.BlameResult)1 RawText (org.eclipse.jgit.diff.RawText)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 Repository (org.eclipse.jgit.lib.Repository)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 Test (org.junit.Test)1