(
commitMsg: string | Commit,
options: ValidateCommitMessageOptions = {},
)
| 54 | |
| 55 | /** Validate a commit message against using the local repo's config. */ |
| 56 | export async function validateCommitMessage( |
| 57 | commitMsg: string | Commit, |
| 58 | options: ValidateCommitMessageOptions = {}, |
| 59 | ): Promise<ValidateCommitMessageResult> { |
| 60 | const _config = await getConfig(); |
| 61 | assertValidCommitMessageConfig(_config); |
| 62 | const config = _config.commitMessage; |
| 63 | const commit = typeof commitMsg === 'string' ? parseCommitMessage(commitMsg) : commitMsg; |
| 64 | const errors: string[] = []; |
| 65 | |
| 66 | /** Perform the validation checks against the parsed commit. */ |
| 67 | function validateCommitAndCollectErrors() { |
| 68 | //////////////////////////////////// |
| 69 | // Checking revert, squash, fixup // |
| 70 | //////////////////////////////////// |
| 71 | |
| 72 | // All revert commits are considered valid. |
| 73 | if (commit.isRevert) { |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | // All squashes are considered valid, as the commit will be squashed into another in |
| 78 | // the git history anyway, unless the options provided to not allow squash commits. |
| 79 | if (commit.isSquash) { |
| 80 | if (options.disallowSquash) { |
| 81 | errors.push('The commit must be manually squashed into the target commit'); |
| 82 | return false; |
| 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | // Fixups commits are considered valid, unless nonFixupCommitHeaders are provided to check |
| 88 | // against. If `nonFixupCommitHeaders` is not empty, we check whether there is a corresponding |
| 89 | // non-fixup commit (i.e. a commit whose header is identical to this commit's header after |
| 90 | // stripping the `fixup! ` prefix), otherwise we assume this verification will happen in another |
| 91 | // check. |
| 92 | if (commit.isFixup) { |
| 93 | if (config.disallowFixup) { |
| 94 | errors.push( |
| 95 | 'The commit must be manually fixed-up into the target commit as fixup commits are disallowed', |
| 96 | ); |
| 97 | |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | if (options.nonFixupCommitHeaders && !options.nonFixupCommitHeaders.includes(commit.header)) { |
| 102 | errors.push( |
| 103 | 'Unable to find match for fixup commit among prior commits: ' + |
| 104 | (options.nonFixupCommitHeaders.map((x) => `\n ${x}`).join('') || '-'), |
| 105 | ); |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | //////////////////////////// |
| 113 | // Checking commit header // |
no test coverage detected