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
16 changes: 13 additions & 3 deletions src/Task/Git/CommitMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class CommitMessage implements TaskInterface
public const MERGE_COMMIT_REGEX =
'(Merge branch|tag \'.+\'(?:\s.+)?|Merge remote-tracking branch \'.+\'|Merge pull request #\d+\s.+)';

/**
* Subject prefixes added by git's autosquash helpers
* (`git commit --fixup`, `--squash`, `--fixup=amend:` and `--fixup=reword:`).
*/
private const AUTOSQUASH_PREFIXES = 'fixup|squash|amend';

/**
* @var TaskConfigInterface
*/
Expand Down Expand Up @@ -252,7 +258,7 @@ private function runMatcher(array $config, string $commitMessage, string $rule,

private function getSpecialPrefixLength(string $string): int
{
if (1 !== preg_match('/^(fixup|squash)! /', $string, $match)) {
if (1 !== preg_match('/^(' . self::AUTOSQUASH_PREFIXES . ')! /', $string, $match)) {
return 0;
}

Expand Down Expand Up @@ -312,7 +318,11 @@ private function subjectIsCapitalized(GitCommitMsgContext $context): bool

$firstLetter = $match[1] ?? '';

return !(1 !== preg_match('/^(fixup|squash)!/u', $subject) && 1 !== preg_match('/[[:upper:]]/u', $firstLetter));
if (1 === preg_match('/^(' . self::AUTOSQUASH_PREFIXES . ')!/u', $subject)) {
return true;
}

return 1 === preg_match('/[[:upper:]]/u', $firstLetter);
}

private function subjectIsSingleLined(GitCommitMsgContext $context): bool
Expand Down Expand Up @@ -375,7 +385,7 @@ private function checkTypeScopeConventions(GitCommitMsgContext $context): void

$scopes = $config['type_scope_conventions']['scopes'] ?? [];

$specialPrefix = '(?:(?:fixup|squash)! )?';
$specialPrefix = '(?:(?:' . self::AUTOSQUASH_PREFIXES . ')! )?';
$typesPattern = '([a-zA-Z0-9]+)';
$scopesPattern = '(:\s|(\(.+\)?:\s))';

Expand Down
59 changes: 59 additions & 0 deletions test/Unit/Task/Git/CommitMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,14 @@ function () {
function () {
}
];
yield 'dont-enforce_capitalized_subject_amend' => [
[
'enforce_capitalized_subject' => true,
],
self::mockCommitMsgContext(self::amend('no capital subject')),
function () {
}
];
yield 'enforce_capitalized_subject_special_utf8_char' => [
[
'enforce_capitalized_subject' => true,
Expand Down Expand Up @@ -560,6 +568,14 @@ function () {
function () {
}
];
yield 'enforce_text_with_special_prefix_amend' => [
[
'max_subject_width' => 10,
],
self::mockCommitMsgContext(self::amend('123456789')),
function () {
}
];
yield 'enforce_text_with_ignore_below_comment' => [
[
'enforce_single_lined_subject' => false,
Expand Down Expand Up @@ -688,6 +704,38 @@ function () {
function () {
},
];
yield 'amend_type_scope_conventions_match_type_without_scope' => [
[
'enforce_capitalized_subject' => false,
'type_scope_conventions' => [
'types' => [
'fix'
],
'scopes' => [
'app'
]
],
],
self::mockCommitMsgContext(self::amend('fix: match type scope convention')),
function () {
},
];
yield 'amend_type_scope_conventions_match_type_with_scope' => [
[
'enforce_capitalized_subject' => false,
'type_scope_conventions' => [
'types' => [
'fix'
],
'scopes' => [
'app'
]
],
],
self::mockCommitMsgContext(self::amend('fix(app): match type scope convention')),
function () {
},
];
yield 'skip_type_scope_conventions_on_merge' => [
[
'enforce_capitalized_subject' => false,
Expand Down Expand Up @@ -947,4 +995,15 @@ private static function squash(string ... $messages): string
...$messages
);
}

private static function amend(string ... $messages): string
{
$subject = array_shift($messages);

return self::buildMessage(
'amend! '.$subject,
'# This was created by running git commit --fixup=amend:...',
...$messages
);
}
}
Loading