Skip to content

Commit

Permalink
Prints error message when rejecting commit
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Sep 14, 2023
1 parent affbb6c commit e2d744e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
26 changes: 14 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,12 @@ public function canRunInContext(ContextInterface $context): bool
return $context instanceof GitCommitMsgContext;
}

private static function withCommitMessage(string $errorMessage, string $commitMessage): string
{
return $errorMessage;//.PHP_EOL.'Original commit message:'.PHP_EOL.$commitMessage;
}


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

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 +172,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 +213,7 @@ 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
2 changes: 1 addition & 1 deletion test/Unit/Task/Git/CommitMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ function () {
yield 'allow_starts_with_custom_comment_char' => [
[
'allow_empty_message' => false,
'enforce_capitalized_subject' => true,
'enforce_capitalized_subject' => true,
'enforce_no_subject_trailing_period' => false,
'enforce_single_lined_subject' => false,
],
Expand Down

0 comments on commit e2d744e

Please sign in to comment.