|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {info} from 'console'; |
| 10 | +import {writeFileSync} from 'fs'; |
| 11 | + |
| 12 | +import {loadCommitMessageDraft} from './commit-message-draft'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Restore the commit message draft to the git to be used as the default commit message. |
| 16 | + * |
| 17 | + * The source provided may be one of the sources described in |
| 18 | + * https://git-scm.com/docs/githooks#_prepare_commit_msg |
| 19 | + */ |
| 20 | +export function restoreCommitMessage( |
| 21 | + filePath: string, source?: 'message'|'template'|'squash'|'commit') { |
| 22 | + if (!!source) { |
| 23 | + info('Skipping commit message restoration attempt'); |
| 24 | + if (source === 'message') { |
| 25 | + info('A commit message was already provided via the command with a -m or -F flag'); |
| 26 | + } |
| 27 | + if (source === 'template') { |
| 28 | + info('A commit message was already provided via the -t flag or config.template setting'); |
| 29 | + } |
| 30 | + if (source === 'squash') { |
| 31 | + info('A commit message was already provided as a merge action or via .git/MERGE_MSG'); |
| 32 | + } |
| 33 | + if (source === 'commit') { |
| 34 | + info('A commit message was already provided through a revision specified via --fixup, -c,'); |
| 35 | + info('-C or --amend flag'); |
| 36 | + } |
| 37 | + process.exit(0); |
| 38 | + } |
| 39 | + /** A draft of a commit message. */ |
| 40 | + const commitMessage = loadCommitMessageDraft(filePath); |
| 41 | + |
| 42 | + // If the commit message draft has content, restore it into the provided filepath. |
| 43 | + if (commitMessage) { |
| 44 | + writeFileSync(filePath, commitMessage); |
| 45 | + } |
| 46 | + // Exit the process |
| 47 | + process.exit(0); |
| 48 | +} |
0 commit comments