Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions commit_check/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ def check_commit_signoff(checks: list, commit_msg_file: str = "") -> int:
return PASS

commit_msg = read_commit_msg(commit_msg_file)

# Extract the subject line (first line of commit message)
subject = commit_msg.split('\n')[0].strip()

# Skip if merge commit
if subject.startswith('Merge'):
return PASS

commit_hash = get_commit_info("H")
result = re.search(check['regex'], commit_msg)
if result is None:
Expand Down
2 changes: 1 addition & 1 deletion commit_check/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def main() -> int:
if args.branch:
check_results.append(branch.check_branch(checks))
if args.commit_signoff:
check_results.append(commit.check_commit_signoff(checks))
check_results.append(commit.check_commit_signoff(checks, args.commit_msg_file))
if args.merge_base:
check_results.append(branch.check_merge_base(checks))
if args.imperative:
Expand Down
71 changes: 71 additions & 0 deletions tests/commit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ def test_check_commit_signoff(mocker):
m_print_suggestion = mocker.patch(
f"{LOCATION}.print_suggestion"
)
# Ensure commit message is NOT a merge commit
mocker.patch(
"commit_check.commit.read_commit_msg",
return_value="feat: add new feature"
)
retval = check_commit_signoff(checks)
assert retval == FAIL
assert m_re_search.call_count == 1
Expand Down Expand Up @@ -179,6 +184,72 @@ def test_check_commit_signoff_with_empty_checks(mocker):
assert m_re_match.call_count == 0


@pytest.mark.benchmark
def test_check_commit_signoff_skip_merge_commit(mocker):
"""Test commit signoff check skips merge commits."""
checks = [{
"check": "commit_signoff",
"regex": "Signed-off-by:",
"error": "Signed-off-by not found",
"suggest": "Use --signoff"
}]

mocker.patch(
"commit_check.commit.read_commit_msg",
return_value="Merge branch 'feature/test' into main"
)

retval = check_commit_signoff(checks, MSG_FILE)
assert retval == PASS


@pytest.mark.benchmark
def test_check_commit_signoff_skip_merge_pr_commit(mocker):
"""Test commit signoff check skips GitHub merge PR commits."""
checks = [{
"check": "commit_signoff",
"regex": "Signed-off-by:",
"error": "Signed-off-by not found",
"suggest": "Use --signoff"
}]

mocker.patch(
"commit_check.commit.read_commit_msg",
return_value="Merge pull request #123 from user/feature\n\nAdd new feature"
)

retval = check_commit_signoff(checks, MSG_FILE)
assert retval == PASS


@pytest.mark.benchmark
def test_check_commit_signoff_still_fails_non_merge_without_signoff(mocker):
"""Test commit signoff check still fails for non-merge commits without signoff."""
checks = [{
"check": "commit_signoff",
"regex": "Signed-off-by:",
"error": "Signed-off-by not found",
"suggest": "Use --signoff"
}]

mocker.patch(
"commit_check.commit.read_commit_msg",
return_value="feat: add new feature\n\nThis adds a new feature"
)

m_print_error_message = mocker.patch(
f"{LOCATION}.print_error_message"
)
m_print_suggestion = mocker.patch(
f"{LOCATION}.print_suggestion"
)

retval = check_commit_signoff(checks, MSG_FILE)
assert retval == FAIL
assert m_print_error_message.call_count == 1
assert m_print_suggestion.call_count == 1


@pytest.mark.benchmark
def test_check_imperative_pass(mocker):
"""Test imperative mood check passes for valid imperative mood."""
Expand Down
Loading