Skip to content

Commit

Permalink
WIP: File level diff stats
Browse files Browse the repository at this point in the history
Relates to #314
  • Loading branch information
jorisroovers committed Sep 6, 2022
1 parent 9c7eac6 commit 3bc79fc
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions gitlint-core/gitlint/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def __init__(
author_email=None,
parents=None,
changed_files=None,
changed_files_stats=None,
branches=None,
):
self.context = context
Expand All @@ -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
Expand Down Expand Up @@ -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"
"-----------------------"
)

Expand All @@ -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
)

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3bc79fc

Please sign in to comment.