Skip to content

Commit

Permalink
Prints error message when rejecting commit (#1108)
Browse files Browse the repository at this point in the history
* Writes commit message after verification failed

* Fixes spaces issues

* Adds tests to the feature 1108

This feature is "prints commit message after its validation failed"

* Removes duplicate test

* Added commit below all tests

* Removes commit verification for the scope regex

* Does not use the array type `buildFailureMessage` function

* Removes original commit message when it is empty

* Removes useless changes on AbstractTaskTestCase
  • Loading branch information
EwenQuim authored Sep 19, 2023
1 parent fb70da3 commit 9970720
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 65 deletions.
36 changes: 24 additions & 12 deletions src/Task/Git/CommitMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class CommitMessage implements TaskInterface
{
const MERGE_COMMIT_REGEX =
public const MERGE_COMMIT_REGEX =
'(Merge branch|tag \'.+\'(?:\s.+)?|Merge remote-tracking branch \'.+\'|Merge pull request #\d+\s.+)';

/**
Expand Down Expand Up @@ -93,6 +93,18 @@ public function canRunInContext(ContextInterface $context): bool
return $context instanceof GitCommitMsgContext;
}

private static function withCommitMessage(string $errorMessage, string $commitMessage): string
{
return sprintf(
"%s%sOriginal commit message:%s%s",
$errorMessage,
PHP_EOL,
PHP_EOL,
$commitMessage
);
}


public function run(ContextInterface $context): TaskResultInterface
{
assert($context instanceof GitCommitMsgContext);
Expand All @@ -110,39 +122,39 @@ public function run(ContextInterface $context): TaskResultInterface
return TaskResult::createFailed(
$this,
$context,
'Commit message should not be empty.'
'Commit message should not be empty.',
);
}

if ((bool) $config['enforce_capitalized_subject'] && !$this->subjectIsCapitalized($context)) {
return TaskResult::createFailed(
$this,
$context,
'Subject should start with a capital letter.'
self::withCommitMessage('Subject should start with a capital letter.', $commitMessage)
);
}

if ((bool) $config['enforce_single_lined_subject'] && !$this->subjectIsSingleLined($context)) {
return TaskResult::createFailed(
$this,
$context,
'Subject should be one line and followed by a blank line.'
self::withCommitMessage('Subject should be one line and followed by a blank line.', $commitMessage)
);
}

if ((bool) $config['enforce_no_subject_punctuations'] && $this->subjectHasPunctuations($context)) {
return TaskResult::createFailed(
$this,
$context,
'Please omit all punctuations from commit message subject.'
self::withCommitMessage('Please omit all punctuations from commit message subject.', $commitMessage)
);
}

if ((bool) $config['enforce_no_subject_trailing_period'] && $this->subjectHasTrailingPeriod($context)) {
return TaskResult::createFailed(
$this,
$context,
'Please omit trailing period from commit message subject.'
self::withCommitMessage('Please omit trailing period from commit message subject.', $commitMessage)
);
}

Expand All @@ -166,11 +178,7 @@ public function run(ContextInterface $context): TaskResultInterface
return TaskResult::createFailed(
$this,
$context,
implode(PHP_EOL, $exceptions).PHP_EOL.sprintf(
'Original commit message: %s%s',
PHP_EOL,
$commitMessage
)
self::withCommitMessage(implode(PHP_EOL, $exceptions), $commitMessage)
);
}

Expand Down Expand Up @@ -211,7 +219,11 @@ private function enforceTextWidth(GitCommitMsgContext $context): TaskResult
}

if (\count($errors)) {
return TaskResult::createFailed($this, $context, implode(PHP_EOL, $errors));
return TaskResult::createFailed(
$this,
$context,
self::withCommitMessage(implode(PHP_EOL, $errors), $commitMessage)
);
}

return TaskResult::createPassed($this, $context);
Expand Down
Loading

0 comments on commit 9970720

Please sign in to comment.