Java Code Examples for org.eclipse.jgit.diff.HistogramDiff

The following examples show how to use org.eclipse.jgit.diff.HistogramDiff. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: diff-check   Source File: Main.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
private static List<File> calculateGitDiffFiles(PMDConfiguration configuration, Set<Language> languages) {
    LanguageFilenameFilter fileSelector = new LanguageFilenameFilter(languages);
    File repoDir = new File(configuration.getGitDir());
    if (!repoDir.isDirectory()) {
        System.out.println("git directory " + configuration.getGitDir() + " is not a directory!");
        System.exit(1);
    }
    String oldRev = configuration.getBaseRev();
    boolean includeStagedCodes = configuration.isIncludeStagedCodes();
    if (StringUtils.isBlank(oldRev)) {
        oldRev = includeStagedCodes ? "HEAD" : "HEAD~";
    }
    String newRev = "HEAD";
    DiffCalculator calculator = DiffCalculator.builder().diffAlgorithm(new HistogramDiff()).build();
    try {
        List<DiffEntryWrapper> diffEntryList = calculator.calculateDiff(repoDir, oldRev, newRev, includeStagedCodes)
                .stream()
                .filter(diffEntry -> !diffEntry.isDeleted())
                .filter(diffEntry -> fileSelector.accept(diffEntry.getNewFile().getParentFile(), diffEntry.getNewFile().getName()))
                .collect(Collectors.toList());

        if (StringUtils.isNotBlank(configuration.getExcludeRegexp())) {
            Pattern excludePattern = Pattern.compile(configuration.getExcludeRegexp());
            diffEntryList.removeIf(diffEntry -> excludePattern.matcher(diffEntry.getAbsoluteNewPath()).matches());
        }

         DIFF_ENTRY_LIST.addAll(diffEntryList);

         return diffEntryList.stream()
                .map(DiffEntryWrapper::getNewFile)
                .collect(Collectors.toList());

     } catch (Exception e) {
        e.printStackTrace();
        System.out.println("error happened when calculate git diff");
        return Collections.emptyList();
    }
}
 
Example 2
Source Project: diff-check   Source File: Main.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
private static List<File> getGitDiffFilesToProcess(List<Pattern> patternsToExclude, CommandLine commandLine) {
    String gitDirPath = commandLine.getOptionValue(OPTION_GIT_DIR_NAME);
    File repoDir = new File(gitDirPath);
    if (!repoDir.isDirectory()) {
        System.out.println("git directory " + gitDirPath + " is not a directory!");
        System.exit(1);
    }
    String oldRev = commandLine.getOptionValue(OPTION_GIT_BASE_REV_NAME);
    boolean includeStagedCodes = commandLine.hasOption(OPTION_GIT_INCLUDE_STAGED_CODES_NAME);
    if (StringUtils.isEmptyOrNull(oldRev)) {
        oldRev = includeStagedCodes ? "HEAD" : "HEAD~";
    }
    String newRev = "HEAD";
    DiffCalculator calculator = DiffCalculator.builder().diffAlgorithm(new HistogramDiff()).build();
    try {
        List<DiffEntryWrapper> diffEntryList = calculator.calculateDiff(repoDir, oldRev, newRev, includeStagedCodes)
                .stream()
                .filter(diffEntry -> !diffEntry.isDeleted())
                .filter(diffEntry -> patternsToExclude.stream()
                        .noneMatch(p -> p.matcher(diffEntry.getNewPath()).matches()))
                .collect(Collectors.toList());

        DIFF_ENTRY_LIST.addAll(diffEntryList);

        return diffEntryList.stream()
                .map(DiffEntryWrapper::getNewFile)
                .collect(Collectors.toList());

    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("error happened when calculate git diff");
        return Collections.emptyList();
    }
}
 
Example 3
@Test
public void testDoCalculateCommitDiffWhenFileAdded() throws Exception {
    try (Git git = new Git(db);
            ObjectReader reader = git.getRepository().newObjectReader()) {
        File repoDir = git.getRepository().getDirectory().getParentFile();
        File fileToAdd = new File(repoDir, "added.txt");
        File fileRemainUnchanged = new File(repoDir, "unchanged.txt");
        writeStringToFile(fileRemainUnchanged, "no change");
        git.add()
            .addFilepattern(fileRemainUnchanged.getName())
            .call();
        RevCommit oldCommit = doCommit(git);

        writeStringToFile(fileToAdd, "add file");
        git.add().addFilepattern(fileToAdd.getName()).call();
        RevCommit newCommit = doCommit(git);

        DiffCalculator calculator = DiffCalculator.builder()
                .diffAlgorithm(new HistogramDiff())
                .build();
        List<DiffEntryWrapper> wrappers = Whitebox.<List<DiffEntryWrapper>>invokeMethod(
                calculator, "doCalculateCommitDiff",
                oldCommit, newCommit, reader, git, repoDir, Collections.emptySet());

        Assert.assertEquals(1, wrappers.size());

        DiffEntryWrapper wrapper = wrappers.get(0);
        Assert.assertEquals(fileToAdd, wrapper.getNewFile());

        List<Edit> edits = wrapper.getEditList();

        Edit insertEdit = edits.get(0);
        Assert.assertEquals(Edit.Type.INSERT, insertEdit.getType());
        Assert.assertEquals(0, insertEdit.getBeginB());
        Assert.assertEquals(1, insertEdit.getEndB());

    }

}
 
Example 4
@Test
public void testDoCalculateIndexedDiffWhenFileAdded() throws Exception {
    try (Git git = new Git(db);
            ObjectReader reader = git.getRepository().newObjectReader()) {
        File repoDir = git.getRepository().getDirectory().getParentFile();
        File fileToAdd = new File(repoDir, "added.txt");
        File fileRemainUnchanged = new File(repoDir, "unchanged.txt");
        writeStringToFile(fileRemainUnchanged, "no change");
        git.add()
            .addFilepattern(fileRemainUnchanged.getName())
            .call();
        RevCommit oldCommit = doCommit(git);

        writeStringToFile(fileToAdd, "add file");
        git.add().addFilepattern(fileToAdd.getName()).call();

        DiffCalculator calculator = DiffCalculator.builder()
                .diffAlgorithm(new HistogramDiff())
                .build();
        List<DiffEntryWrapper> wrappers = Whitebox.<List<DiffEntryWrapper>>invokeMethod(
                calculator, "doCalculateIndexedDiff",
                oldCommit, reader, git, repoDir);

        Assert.assertEquals(1, wrappers.size());

        DiffEntryWrapper wrapper = wrappers.get(0);
        Assert.assertEquals(fileToAdd, wrapper.getNewFile());

        List<Edit> edits = wrapper.getEditList();

        Edit insertEdit = edits.get(0);
        Assert.assertEquals(Edit.Type.INSERT, insertEdit.getType());
        Assert.assertEquals(0, insertEdit.getBeginB());
        Assert.assertEquals(1, insertEdit.getEndB());

    }

}
 
Example 5
@Test
public void testCalculateDiff() throws Exception {
    try (Git git = new Git(db);
            ObjectReader reader = git.getRepository().newObjectReader()) {
        File repoDir = git.getRepository().getDirectory().getParentFile();
        File fileToChange = new File(repoDir, "changed.txt");
        File fileRemainUnchanged = new File(repoDir, "unchanged.txt");
        writeStringToFile(fileToChange, new StringBuilder()
                .append("first line")
                .append("\n")
                .append("second line")
                .append("\n")
                .append("third line")
                .append("\n")
                .toString());
        writeStringToFile(fileRemainUnchanged, "no change");
        git.add()
            .addFilepattern(fileToChange.getName())
            .addFilepattern(fileRemainUnchanged.getName())
            .call();
        RevCommit oldCommit = doCommit(git);

        writeStringToFile(fileToChange, new StringBuilder()
                .append("first line")
                .append("\n")
                .append("second line changed")
                .append("\n")
                .append("third line")
                .append("\n")
                .append("fourth line")
                .toString());
        git.add().addFilepattern(fileToChange.getName()).call();
        RevCommit newCommit = doCommit(git);

        DiffCalculator calculator = DiffCalculator.builder()
                .diffAlgorithm(new HistogramDiff())
                .comparator(RawTextComparator.DEFAULT)
                .bigFileThreshold(DiffHelper.DEFAULT_BIG_FILE_THRESHOLD)
                .build();
        List<DiffEntryWrapper> wrappers = calculator.calculateDiff(
                repoDir, oldCommit.name(), newCommit.name(), true);

        Assert.assertEquals(1, wrappers.size());

        DiffEntryWrapper wrapper = wrappers.get(0);
        Assert.assertEquals(fileToChange, wrapper.getNewFile());

        List<Edit> edits = wrapper.getEditList();

        Edit replaceEdit = edits.get(0);
        Assert.assertEquals(Edit.Type.REPLACE, replaceEdit.getType());
        Assert.assertEquals(1, replaceEdit.getBeginB());
        Assert.assertEquals(2, replaceEdit.getEndB());

        Edit insertEdit = edits.get(1);
        Assert.assertEquals(Edit.Type.INSERT, insertEdit.getType());
        Assert.assertEquals(3, insertEdit.getBeginB());
        Assert.assertEquals(4, insertEdit.getEndB());
    }

}
 
Example 6
@Test
public void testDoCalculateCommitDiffWhenFileChanged() throws Exception {
    try (Git git = new Git(db);
            ObjectReader reader = git.getRepository().newObjectReader()) {
        File repoDir = git.getRepository().getDirectory().getParentFile();
        File fileToChange = new File(repoDir, "changed.txt");
        File fileRemainUnchanged = new File(repoDir, "unchanged.txt");
        writeStringToFile(fileToChange, new StringBuilder()
                .append("first line")
                .append("\n")
                .append("second line")
                .append("\n")
                .append("third line")
                .append("\n")
                .toString());
        writeStringToFile(fileRemainUnchanged, "no change");
        git.add()
            .addFilepattern(fileToChange.getName())
            .addFilepattern(fileRemainUnchanged.getName())
            .call();
        RevCommit oldCommit = doCommit(git);

        writeStringToFile(fileToChange, new StringBuilder()
                .append("first line")
                .append("\n")
                .append("second line changed")
                .append("\n")
                .append("third line")
                .append("\n")
                .append("fourth line")
                .toString());
        git.add().addFilepattern(fileToChange.getName()).call();
        RevCommit newCommit = doCommit(git);

        DiffCalculator calculator = DiffCalculator.builder()
                .diffAlgorithm(new HistogramDiff())
                .build();
        List<DiffEntryWrapper> wrappers = Whitebox.<List<DiffEntryWrapper>>invokeMethod(
                calculator, "doCalculateCommitDiff",
                oldCommit, newCommit, reader, git, repoDir, Collections.emptySet());

        Assert.assertEquals(1, wrappers.size());

        DiffEntryWrapper wrapper = wrappers.get(0);
        Assert.assertEquals(fileToChange, wrapper.getNewFile());

        List<Edit> edits = wrapper.getEditList();

        Edit replaceEdit = edits.get(0);
        Assert.assertEquals(Edit.Type.REPLACE, replaceEdit.getType());
        Assert.assertEquals(1, replaceEdit.getBeginB());
        Assert.assertEquals(2, replaceEdit.getEndB());

        Edit insertEdit = edits.get(1);
        Assert.assertEquals(Edit.Type.INSERT, insertEdit.getType());
        Assert.assertEquals(3, insertEdit.getBeginB());
        Assert.assertEquals(4, insertEdit.getEndB());
    }

}
 
Example 7
@Test
public void testDoCalculateIndexedDiffWhenFileChanged() throws Exception {
    try (Git git = new Git(db);
            ObjectReader reader = git.getRepository().newObjectReader()) {
        File repoDir = git.getRepository().getDirectory().getParentFile();
        File fileToChange = new File(repoDir, "changed.txt");
        File fileRemainUnchanged = new File(repoDir, "unchanged.txt");
        writeStringToFile(fileToChange, new StringBuilder()
                .append("first line")
                .append("\n")
                .append("second line")
                .append("\n")
                .append("third line")
                .append("\n")
                .toString());
        writeStringToFile(fileRemainUnchanged, "no change");
        git.add()
            .addFilepattern(fileToChange.getName())
            .addFilepattern(fileRemainUnchanged.getName())
            .call();
        RevCommit oldCommit = doCommit(git);

        writeStringToFile(fileToChange, new StringBuilder()
                .append("first line")
                .append("\n")
                .append("second line changed")
                .append("\n")
                .append("third line")
                .append("\n")
                .append("fourth line")
                .toString());
        git.add().addFilepattern(fileToChange.getName()).call();

        DiffCalculator calculator = DiffCalculator.builder()
                .diffAlgorithm(new HistogramDiff())
                .build();
        List<DiffEntryWrapper> wrappers = Whitebox.<List<DiffEntryWrapper>>invokeMethod(
                calculator, "doCalculateIndexedDiff",
                oldCommit, reader, git, repoDir);

        Assert.assertEquals(1, wrappers.size());

        DiffEntryWrapper wrapper = wrappers.get(0);
        Assert.assertEquals(fileToChange, wrapper.getNewFile());

        List<Edit> edits = wrapper.getEditList();

        Edit replaceEdit = edits.get(0);
        Assert.assertEquals(Edit.Type.REPLACE, replaceEdit.getType());
        Assert.assertEquals(1, replaceEdit.getBeginB());
        Assert.assertEquals(2, replaceEdit.getEndB());

        Edit insertEdit = edits.get(1);
        Assert.assertEquals(Edit.Type.INSERT, insertEdit.getType());
        Assert.assertEquals(3, insertEdit.getBeginB());
        Assert.assertEquals(4, insertEdit.getEndB());
    }

}
 
Example 8
Source Project: onedev   Source File: MergeAlgorithm.java    License: MIT License 2 votes vote down vote up
/**
 * Creates a new MergeAlgorithm which uses
 * {@link org.eclipse.jgit.diff.HistogramDiff} as diff algorithm
 */
public MergeAlgorithm() {
	this(new HistogramDiff());
}