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: int | None = None) Object[source]

Uses git to fetch the full SHA hash of the current 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: int | None = 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 to get the desired commit’s SHA hash instead.

Returns:

A str representing the commit’s SHA hash.

cpp_linter.git.get_diff(parents: int = 1) Diff[source]

Retrieve the diff info about a specified commit.

Parameters:
parents: int = 1

The number of parent commits related to the current commit.

Returns:

A str of 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 updates.

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.