Class GitDiffer

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

public class GitDiffer extends Object
This class provides utility functions for obtaining diffs from git repositories.

Then a CommitDiff is created for each commit. File changes in each commit are filtered using the DiffFilter of the repository. 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
  • Constructor Details

    • GitDiffer

      private GitDiffer()
  • Method Details

    • createCommitDiffFromFirstParent

      public static CommitDiffResult createCommitDiffFromFirstParent(Repository repository, String commitHash) throws IOException
      Throws:
      IOException
    • createCommitDiffFromFirstParent

      public static CommitDiffResult createCommitDiffFromFirstParent(Repository repository, org.eclipse.jgit.revwalk.RevCommit currentCommit)
      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.

      This honors the diff filter and the parser options of the repository.

      Parameters:
      repository - The git repo which the commit stems from.
      currentCommit - The commit from which to create a CommitDiff
      Returns:
      The CommitDiff of the given commit
    • createCommitDiff

      public static CommitDiffResult createCommitDiff(Repository repository, org.eclipse.jgit.revwalk.RevCommit parentCommit, org.eclipse.jgit.revwalk.RevCommit childCommit)
      Creates a CommitDiff that describes all changes made by the given childCommit to the given parentCommit.

      This honors the diff filter and the parser options of the repository.

      Parameters:
      repository - The git repo which the commit stems from.
      Returns:
      The CommitDiff describing all changes between the two commits.
    • createWorkingTreeDiff

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

      private static CommitDiffResult getPatchDiffs(Repository repository, 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.

      This honors the diff filter and the parser options of the repository.

      Parameters:
      repository - The git repo which the commit stems from
      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(Repository repository, org.eclipse.jgit.revwalk.RevCommit commit, String filename) throws IOException
      Gets the full content of a file before a commit.
      Parameters:
      repository - The repository which contains commit
      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