From 9da566fd73de02d6ef79dedb7e887176a24ab93b Mon Sep 17 00:00:00 2001 From: Joris Roovers Date: Tue, 6 Sep 2022 09:37:41 +0000 Subject: [PATCH] WIP: File level diff stats Relates to #314 --- gitlint-core/gitlint/git.py | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/gitlint-core/gitlint/git.py b/gitlint-core/gitlint/git.py index f27b7ee9..134b0696 100644 --- a/gitlint-core/gitlint/git.py +++ b/gitlint-core/gitlint/git.py @@ -152,6 +152,7 @@ def __init__( author_email=None, parents=None, changed_files=None, + changed_files_stats=None, branches=None, ): self.context = context @@ -162,6 +163,7 @@ def __init__( self.author_email = author_email self.parents = parents or [] # parent commit hashes self.changed_files = changed_files or [] + self.changed_files_stats = changed_files_stats or {} self.branches = branches or [] @property @@ -198,6 +200,7 @@ def __str__(self): f"is-revert-commit: {self.is_revert_commit}\n" f"Branches: {self.branches}\n" f"Changed Files: {self.changed_files}\n" + f"Changed Files Stats: {self.changed_files_stats}\n" "-----------------------" ) @@ -217,6 +220,7 @@ def __eq__(self, other): and self.is_squash_commit == other.is_squash_commit and self.is_revert_commit == other.is_revert_commit and self.changed_files == other.changed_files + and self.changed_files_stats == other.changed_files_stats and self.branches == other.branches ) @@ -307,18 +311,31 @@ def is_merge_commit(self): @property def changed_files(self): - def cache_changed_files(): - self._cache["changed_files"] = _git( + return self._try_cache("changed_files", self.changed_files_stats) + + @property + def changed_files_stats(self): + def cache_changed_files_stats(): + changed_files_stats_raw = _git( "diff-tree", "--no-commit-id", - "--name-only", + "--numstat", "-r", "--root", self.sha, _cwd=self.context.repository_path, - ).split() + ).split("\n") + + changed_files_stats = {} + for line in changed_files_stats_raw: + line_stats = line.split() + # TODO(jroovers): create a ChangedFileStats object? + changed_files_stats[line_stats[2]] = {"added": int(line_stats[0]), "deleted": int(line_stats[1])} - return self._try_cache("changed_files", cache_changed_files) + self._cache["changed_files_stats"] = changed_files_stats + self._cache["changed_files"] = changed_files_stats.keys() + + return self._try_cache("changed_files_stats", cache_changed_files_stats) class StagedLocalGitCommit(GitCommit, PropertyCache): @@ -371,6 +388,12 @@ def branches(self): def changed_files(self): return _git("diff", "--staged", "--name-only", "-r", _cwd=self.context.repository_path).split() + @property + def changed_files_stats(self): + raise NotImplementedError() + # TODO(jroovers): implement this! + # return _git("diff", "--staged", "--name-only", "-r", _cwd=self.context.repository_path).split() + class GitContext(PropertyCache): """Class representing the git context in which gitlint is operating: a data object storing information about