Class GitDiffer

java.lang.Object
org.variantsync.diffdetective.diff.git.GitDiffer

public class GitDiffer extends Object
This class creates a GitDiff-object from a git repository (Git-object).

The commits from the repository are first filtered using the given DiffFilter. Then a CommitDiff is created for each commit. File changes in each commit are filtered using the given DiffFilter. Then a PatchDiff is created from each file change. Finally, each patch is parsed to a VariationDiff.

Author:
Soeren Viegener, Paul Maximilian Bittner
  • Field Details

    • BOM_PATTERN

      private static final Pattern BOM_PATTERN
    • DIFF_HUNK_PATTERN

      private static final Pattern DIFF_HUNK_PATTERN
    • GIT_HEADER_PATTERN

      private static final Pattern GIT_HEADER_PATTERN
    • DIFF_HEADER_PATTERN

      private static final Pattern DIFF_HEADER_PATTERN
    • NO_NEWLINE_PATTERN

      private static final Pattern NO_NEWLINE_PATTERN
    • git

      private final org.eclipse.jgit.api.Git git
    • diffFilter

      private final DiffFilter diffFilter
    • parseOptions

      private final PatchDiffParseOptions parseOptions
  • Constructor Details

    • GitDiffer

      public GitDiffer(Repository repository)
      Create a differ operating on the given repository.
      Parameters:
      repository - The repository for whose history to obtain diffs.
  • Method Details

    • yieldRevCommits

      public org.variantsync.functjonal.iteration.Yield<org.eclipse.jgit.revwalk.RevCommit> yieldRevCommits()
      Returns all commits in the repository's history.
    • yieldRevCommitsAfter

      public org.variantsync.functjonal.iteration.Yield<org.eclipse.jgit.revwalk.RevCommit> yieldRevCommitsAfter(Function<org.eclipse.jgit.revwalk.RevCommit,org.eclipse.jgit.revwalk.RevCommit> f)
      The same as yieldRevCommits() but applies the given function f to each commit before returning it.
      Parameters:
      f - A function to map over all commits before they can be accessed. Each returned commit was processed by f exactly once.
      Returns:
      All commits in the repository's history after applying the given function to each commit.
    • yieldAllValidIn

      private org.variantsync.functjonal.iteration.Yield<org.eclipse.jgit.revwalk.RevCommit> yieldAllValidIn(Iterator<org.eclipse.jgit.revwalk.RevCommit> commitsIterator)
      Filters all undesired commits from the given set of commits using the DiffFilter of this differs repository.
      Parameters:
      commitsIterator - Commits to filter.
      Returns:
      All commits from the given set that should not be filtered.
      See Also:
    • getCommit

      public org.eclipse.jgit.revwalk.RevCommit getCommit(String commitHash) throws IOException
      Throws:
      IOException
    • createCommitDiff

      public CommitDiffResult createCommitDiff(String commitHash) throws IOException
      Throws:
      IOException
    • createCommitDiff

      public CommitDiffResult createCommitDiff(org.eclipse.jgit.revwalk.RevCommit revCommit)
    • createCommitDiffFromFirstParent

      public static CommitDiffResult createCommitDiffFromFirstParent(org.eclipse.jgit.api.Git git, DiffFilter diffFilter, org.eclipse.jgit.revwalk.RevCommit currentCommit, PatchDiffParseOptions parseOptions)
      Creates a CommitDiff from a given commit. For this, the git diff is retrieved using JGit. For each file in the diff, a PatchDiff is created.
      Parameters:
      git - The git repo which the commit stems from.
      currentCommit - The commit from which to create a CommitDiff
      parseOptions -
      Returns:
      The CommitDiff of the given commit
    • createCommitDiff

      public static CommitDiffResult createCommitDiff(org.eclipse.jgit.api.Git git, DiffFilter diffFilter, org.eclipse.jgit.revwalk.RevCommit parentCommit, org.eclipse.jgit.revwalk.RevCommit childCommit, PatchDiffParseOptions parseOptions)
      Creates a CommitDiff that describes all changes made by the given childCommit to the given parentCommit.
      Parameters:
      git - The git repo which the commits stem from.
      Returns:
      The CommitDiff describing all changes between the two commits.
    • createWorkingTreeDiff

      public static CommitDiffResult createWorkingTreeDiff(org.eclipse.jgit.api.Git git, DiffFilter diffFilter, org.eclipse.jgit.revwalk.RevCommit commit, PatchDiffParseOptions parseOptions)
      The same as createCommitDiff(Git, DiffFilter, RevCommit, RevCommit, PatchDiffParseOptions) but diffs the given commit against the current working tree.
      Parameters:
      git - The git repo which the commit stems from
      commit - The commit which the working tree is compared with
      parseOptions - PatchDiffParseOptions
      Returns:
      The CommitDiff of the given commit
    • getPatchDiffs

      private static CommitDiffResult getPatchDiffs(org.eclipse.jgit.api.Git git, DiffFilter diffFilter, PatchDiffParseOptions parseOptions, org.eclipse.jgit.treewalk.AbstractTreeIterator prevTreeParser, org.eclipse.jgit.treewalk.AbstractTreeIterator currentTreeParser, org.eclipse.jgit.revwalk.RevCommit parentCommit, org.eclipse.jgit.revwalk.RevCommit childCommit)
      Obtains the CommitDiff between two commit's trees.
      Parameters:
      git - The git repo which the commit stems from
      diffFilter - DiffFilter
      parseOptions - PatchDiffParseOptions
      prevTreeParser - The tree parser for parentCommit
      currentTreeParser - The tree parser for childCommit or the working tree
      parentCommit - The RevCommit for the parent commit
      childCommit - The RevCommit for the child commit (equal to parentCommit if working tree is requested)
      Returns:
      CommitDiffResult
    • getFullDiff

      public static String getFullDiff(BufferedReader beforeFile, BufferedReader gitDiff)
      Creates a full git diff from a file before the change and the git diff containing only the changed lines.
      Parameters:
      beforeFile - The full file before the change
      gitDiff - The git diff containing only the changed lines
      Returns:
      A full git diff containing the complete file and all changes
    • getBeforeFullFile

      public static BufferedReader getBeforeFullFile(org.eclipse.jgit.api.Git git, org.eclipse.jgit.revwalk.RevCommit commit, String filename) throws IOException
      Gets the full content of a file before a commit.
      Parameters:
      commit - The commit in which the file was changed
      filename - The name of the file
      Returns:
      The full content of the file before the commit
      Throws:
      IOException
    • getJGitRepo

      public org.eclipse.jgit.api.Git getJGitRepo()
      Returns the internal representation of this differs repository.