Skip to content

Commit c5904d6

Browse files
authored
refactor: save a git call when the author is on ignore_authors (#694)
* refactor: save a git call when the author is on ignore_authors The author checks asked git whether a commit exists before they read ignore_authors, so an author on the list cost one `git rev-parse` more than in 2.20.4. Read the list first again, and look the commit up only when the list said no; a repository with no commits then asks the list a second time about the identity the first commit will carry. One test of the fallback to the last commit's author ran that lookup against the real repository. It now patches it like the other git calls. * test: pass has_head instead of patching the commit lookup Patching has_commits in the fallback test built a mock that costs more than the git call it replaced, by CodSpeed's count. The test now tells _get_author_value that a commit exists, so nothing is looked up. --------- Co-authored-by: Xianpeng Shen <[email protected]>
1 parent 079bb74 commit c5904d6

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

‎commit_check/engine.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,9 @@ class AuthorValidator(BaseValidator):
694694
"""Validates author information."""
695695

696696
def validate(self, context: ValidationContext) -> ValidationResult:
697+
if self._author_in_ignore_list(context):
698+
return ValidationResult.SKIP
699+
697700
# With no message and no --rev the commit checks skip a repository
698701
# that has no commits. The author checks do not: the value under
699702
# test is the identity the next commit will carry, and git knows
@@ -702,8 +705,8 @@ def validate(self, context: ValidationContext) -> ValidationResult:
702705
has_head: bool | None = None
703706
if not self._message_was_supplied(context):
704707
has_head = has_commits()
705-
if self._author_in_ignore_list(context, pending=has_head is False):
706-
return ValidationResult.SKIP
708+
if not has_head and self._author_in_ignore_list(context, pending=True):
709+
return ValidationResult.SKIP
707710

708711
author_value = self._get_author_value(context, has_head)
709712
if author_value is None:

‎tests/engine_test.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,8 @@ def test_get_author_value_with_email_format(self):
733733
"commit_check.engine.get_commit_info", return_value="[email protected]"
734734
),
735735
):
736-
author_value = validator._get_author_value(context)
736+
# has_head: a commit exists, so git is not asked whether one does.
737+
author_value = validator._get_author_value(context, has_head=True)
737738
assert author_value == "[email protected]"
738739

739740
def test_no_identity_anywhere_leaves_nothing_to_judge(self):

0 commit comments

Comments
 (0)