git

This module uses git CLI to get commit info. It also holds some functions related to parsing diff output into a list of changed files.

cpp_linter.git.get_sha(repo: Repository, parent: None | int | str = None) Object[source]

Uses git to fetch the full SHA hash of a commit.

Note

This function is only used in local development environments, not in a Continuous Integration workflow.

Parameters:
repo: Repository

The object representing the git repository.

parent: None | int | str = None

This parameter’s default value will fetch the SHA of the last commit. Set this parameter to the number of parent commits from the current tree’s HEAD or a valid git revision to get the desired commit’s SHA hash instead.

Returns:

A pygit2.Object representing the resolved commit.

cpp_linter.git.get_diff(parents: None | int | str = None, ignore_index: bool = False) Diff[source]

Retrieve the diff info about a specified commit.

Parameters:
parents: None | int | str = None

The commit or ref to use as the base of the diff. If set to None, and there are staged changes to be used, then it will be HEAD and the diff will consist of just the staged changes. If there are no staged changes or the index is ignored, it will be HEAD~1.

ignore_index: bool = False

Setting this flag to true will ignore any staged files in the index when producing a diff.

Returns:

A pygit2.Diff object representing the fetched diff.

cpp_linter.git.parse_diff(diff_obj: Diff | str, file_filter: FileFilter, lines_changed_only: int) list[FileObj][source]

Parse a given diff into file objects.

Parameters:
diff_obj: Diff | str

The complete git diff object for an event.

file_filter: FileFilter

A FileFilter object.

lines_changed_only: int

A value that dictates what file changes to focus on.

Returns:

A list of FileObj describing information about the files changed.

Note

Deleted files are omitted because we only want to analyze additions.

cpp_linter.git.parse_patch(patch: list[DiffHunk]) tuple[list[list[int]], list[int]][source]

Parse a diff’s patch accordingly.

Parameters:
patch: list[DiffHunk]

The patch of hunks for 1 file.

Returns:

A tuple of lists where

  • Index 0 is the ranges of lines in the diff. Each item in this list is a 2 element list describing the starting and ending line numbers.

  • Index 1 is a list of the line numbers that contain additions.