2020-07-30 17:01:03 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google LLC All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {writeFileSync} from 'fs';
|
|
|
|
|
2020-09-14 16:21:31 -04:00
|
|
|
import {debug, log} from '../../utils/console';
|
2020-09-03 19:01:56 -04:00
|
|
|
|
2021-03-19 15:11:21 -04:00
|
|
|
import {loadCommitMessageDraft} from './commit-message-draft';
|
|
|
|
import {CommitMsgSource} from './commit-message-source';
|
2020-07-30 17:01:03 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore the commit message draft to the git to be used as the default commit message.
|
|
|
|
*
|
|
|
|
* The source provided may be one of the sources described in
|
|
|
|
* https://git-scm.com/docs/githooks#_prepare_commit_msg
|
|
|
|
*/
|
2020-09-14 16:21:31 -04:00
|
|
|
export function restoreCommitMessage(filePath: string, source?: CommitMsgSource) {
|
2020-07-30 17:01:03 -04:00
|
|
|
if (!!source) {
|
|
|
|
if (source === 'message') {
|
2020-09-03 19:01:56 -04:00
|
|
|
debug('A commit message was already provided via the command with a -m or -F flag');
|
2020-07-30 17:01:03 -04:00
|
|
|
}
|
|
|
|
if (source === 'template') {
|
2020-09-03 19:01:56 -04:00
|
|
|
debug('A commit message was already provided via the -t flag or config.template setting');
|
2020-07-30 17:01:03 -04:00
|
|
|
}
|
|
|
|
if (source === 'squash') {
|
2020-09-03 19:01:56 -04:00
|
|
|
debug('A commit message was already provided as a merge action or via .git/MERGE_MSG');
|
2020-07-30 17:01:03 -04:00
|
|
|
}
|
|
|
|
if (source === 'commit') {
|
2020-09-03 19:01:56 -04:00
|
|
|
debug('A commit message was already provided through a revision specified via --fixup, -c,');
|
|
|
|
debug('-C or --amend flag');
|
2020-07-30 17:01:03 -04:00
|
|
|
}
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
/** A draft of a commit message. */
|
|
|
|
const commitMessage = loadCommitMessageDraft(filePath);
|
|
|
|
|
|
|
|
// If the commit message draft has content, restore it into the provided filepath.
|
|
|
|
if (commitMessage) {
|
|
|
|
writeFileSync(filePath, commitMessage);
|
|
|
|
}
|
|
|
|
// Exit the process
|
|
|
|
process.exit(0);
|
|
|
|
}
|