Skip to content

Commit ccf187b

Browse files
authored
Merge pull request #1224 from reynkonig/feature/commit-message-amend-prefix
Support git autosquash `amend!` prefix in commit message task
2 parents 7c9292f + ea60826 commit ccf187b

2 files changed

Lines changed: 72 additions & 3 deletions

File tree

src/Task/Git/CommitMessage.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ class CommitMessage implements TaskInterface
2323
public const MERGE_COMMIT_REGEX =
2424
'(Merge branch|tag \'.+\'(?:\s.+)?|Merge remote-tracking branch \'.+\'|Merge pull request #\d+\s.+)';
2525

26+
/**
27+
* Subject prefixes added by git's autosquash helpers
28+
* (`git commit --fixup`, `--squash`, `--fixup=amend:` and `--fixup=reword:`).
29+
*/
30+
private const AUTOSQUASH_PREFIXES = 'fixup|squash|amend';
31+
2632
/**
2733
* @var TaskConfigInterface
2834
*/
@@ -252,7 +258,7 @@ private function runMatcher(array $config, string $commitMessage, string $rule,
252258

253259
private function getSpecialPrefixLength(string $string): int
254260
{
255-
if (1 !== preg_match('/^(fixup|squash)! /', $string, $match)) {
261+
if (1 !== preg_match('/^(' . self::AUTOSQUASH_PREFIXES . ')! /', $string, $match)) {
256262
return 0;
257263
}
258264

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

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

315-
return !(1 !== preg_match('/^(fixup|squash)!/u', $subject) && 1 !== preg_match('/[[:upper:]]/u', $firstLetter));
321+
if (1 === preg_match('/^(' . self::AUTOSQUASH_PREFIXES . ')!/u', $subject)) {
322+
return true;
323+
}
324+
325+
return 1 === preg_match('/[[:upper:]]/u', $firstLetter);
316326
}
317327

318328
private function subjectIsSingleLined(GitCommitMsgContext $context): bool
@@ -375,7 +385,7 @@ private function checkTypeScopeConventions(GitCommitMsgContext $context): void
375385

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

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

test/Unit/Task/Git/CommitMessageTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,14 @@ function () {
488488
function () {
489489
}
490490
];
491+
yield 'dont-enforce_capitalized_subject_amend' => [
492+
[
493+
'enforce_capitalized_subject' => true,
494+
],
495+
self::mockCommitMsgContext(self::amend('no capital subject')),
496+
function () {
497+
}
498+
];
491499
yield 'enforce_capitalized_subject_special_utf8_char' => [
492500
[
493501
'enforce_capitalized_subject' => true,
@@ -560,6 +568,14 @@ function () {
560568
function () {
561569
}
562570
];
571+
yield 'enforce_text_with_special_prefix_amend' => [
572+
[
573+
'max_subject_width' => 10,
574+
],
575+
self::mockCommitMsgContext(self::amend('123456789')),
576+
function () {
577+
}
578+
];
563579
yield 'enforce_text_with_ignore_below_comment' => [
564580
[
565581
'enforce_single_lined_subject' => false,
@@ -688,6 +704,38 @@ function () {
688704
function () {
689705
},
690706
];
707+
yield 'amend_type_scope_conventions_match_type_without_scope' => [
708+
[
709+
'enforce_capitalized_subject' => false,
710+
'type_scope_conventions' => [
711+
'types' => [
712+
'fix'
713+
],
714+
'scopes' => [
715+
'app'
716+
]
717+
],
718+
],
719+
self::mockCommitMsgContext(self::amend('fix: match type scope convention')),
720+
function () {
721+
},
722+
];
723+
yield 'amend_type_scope_conventions_match_type_with_scope' => [
724+
[
725+
'enforce_capitalized_subject' => false,
726+
'type_scope_conventions' => [
727+
'types' => [
728+
'fix'
729+
],
730+
'scopes' => [
731+
'app'
732+
]
733+
],
734+
],
735+
self::mockCommitMsgContext(self::amend('fix(app): match type scope convention')),
736+
function () {
737+
},
738+
];
691739
yield 'skip_type_scope_conventions_on_merge' => [
692740
[
693741
'enforce_capitalized_subject' => false,
@@ -947,4 +995,15 @@ private static function squash(string ... $messages): string
947995
...$messages
948996
);
949997
}
998+
999+
private static function amend(string ... $messages): string
1000+
{
1001+
$subject = array_shift($messages);
1002+
1003+
return self::buildMessage(
1004+
'amend! '.$subject,
1005+
'# This was created by running git commit --fixup=amend:...',
1006+
...$messages
1007+
);
1008+
}
9501009
}

0 commit comments

Comments
 (0)