Skip to content

Commit f4ced74

Browse files
josephperrottatscott
authored andcommitted
feat(dev-infra): save invalid commit message attempts to be restored on next commit attempt (angular#38304)
When a commit message fails validation, rather than throwing out the commit message entirely the commit message is saved into a draft file and restored on the next commit attempt. PR Close angular#38304
1 parent 8366eff commit f4ced74

File tree

6 files changed

+110
-1
lines changed

6 files changed

+110
-1
lines changed

dev-infra/commit-message/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ ts_library(
55
name = "commit-message",
66
srcs = [
77
"cli.ts",
8+
"commit-message-draft.ts",
89
"config.ts",
910
"parse.ts",
11+
"restore-commit-message.ts",
1012
"validate.ts",
1113
"validate-file.ts",
1214
"validate-range.ts",

dev-infra/commit-message/cli.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,36 @@ import * as yargs from 'yargs';
99

1010
import {info} from '../utils/console';
1111

12+
import {restoreCommitMessage} from './restore-commit-message';
1213
import {validateFile} from './validate-file';
1314
import {validateCommitRange} from './validate-range';
1415

1516
/** Build the parser for the commit-message commands. */
1617
export function buildCommitMessageParser(localYargs: yargs.Argv) {
1718
return localYargs.help()
1819
.strict()
20+
.command(
21+
'restore-commit-message-draft', false, {
22+
'file-env-variable': {
23+
type: 'string',
24+
conflicts: ['file'],
25+
required: true,
26+
description:
27+
'The key for the environment variable which holds the arguments for the ' +
28+
'prepare-commit-msg hook as described here: ' +
29+
'https://git-scm.com/docs/githooks#_prepare_commit_msg',
30+
coerce: arg => {
31+
const [file, source] = (process.env[arg] || '').split(' ');
32+
if (!file) {
33+
throw new Error(`Provided environment variable "${arg}" was not found.`);
34+
}
35+
return [file, source];
36+
},
37+
}
38+
},
39+
args => {
40+
restoreCommitMessage(args.fileEnvVariable[0], args.fileEnvVariable[1]);
41+
})
1942
.command(
2043
'pre-commit-validate', 'Validate the most recent commit message', {
2144
'file': {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
import {existsSync, readFileSync, unlinkSync, writeFileSync} from 'fs';
9+
10+
/** Load the commit message draft from the file system if it exists. */
11+
export function loadCommitMessageDraft(basePath: string) {
12+
const commitMessageDraftPath = `${basePath}.ngDevSave`;
13+
if (existsSync(commitMessageDraftPath)) {
14+
return readFileSync(commitMessageDraftPath).toString();
15+
}
16+
return '';
17+
}
18+
19+
/** Remove the commit message draft from the file system. */
20+
export function deleteCommitMessageDraft(basePath: string) {
21+
const commitMessageDraftPath = `${basePath}.ngDevSave`;
22+
if (existsSync(commitMessageDraftPath)) {
23+
unlinkSync(commitMessageDraftPath);
24+
}
25+
}
26+
27+
/** Save the commit message draft to the file system for later retrieval. */
28+
export function saveCommitMessageDraft(basePath: string, commitMessage: string) {
29+
writeFileSync(`${basePath}.ngDevSave`, commitMessage);
30+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

dev-infra/commit-message/validate-file.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@ import {resolve} from 'path';
1111
import {getRepoBaseDir} from '../utils/config';
1212
import {info} from '../utils/console';
1313

14+
import {deleteCommitMessageDraft, saveCommitMessageDraft} from './commit-message-draft';
1415
import {validateCommitMessage} from './validate';
1516

1617
/** Validate commit message at the provided file path. */
1718
export function validateFile(filePath: string) {
1819
const commitMessage = readFileSync(resolve(getRepoBaseDir(), filePath), 'utf8');
1920
if (validateCommitMessage(commitMessage)) {
2021
info('√ Valid commit message');
22+
deleteCommitMessageDraft(filePath);
2123
return;
2224
}
25+
// On all invalid commit messages, the commit message should be saved as a draft to be
26+
// restored on the next commit attempt.
27+
saveCommitMessageDraft(filePath, commitMessage);
2328
// If the validation did not return true, exit as a failure.
2429
process.exit(1);
2530
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@
210210
"husky": {
211211
"hooks": {
212212
"pre-commit": "yarn -s ng-dev format staged",
213-
"commit-msg": "yarn -s ng-dev commit-message pre-commit-validate --file-env-variable HUSKY_GIT_PARAMS"
213+
"commit-msg": "yarn -s ng-dev commit-message pre-commit-validate --file-env-variable HUSKY_GIT_PARAMS",
214+
"prepare-commit-msg": "yarn -s ng-dev commit-message restore-commit-message-draft --file-env-variable HUSKY_GIT_PARAMS"
214215
}
215216
}
216217
}

0 commit comments

Comments
 (0)