Skip to content

Commit 1753148

Browse files
authored
Move prepush scripts to precommit (#3026)
1 parent f30de15 commit 1753148

File tree

5 files changed

+69
-40
lines changed

5 files changed

+69
-40
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
},
139139
"husky": {
140140
"hooks": {
141-
"pre-push": "node tools/gitHooks/prepush.js"
141+
"pre-commit": "node tools/gitHooks/precommit.js"
142142
}
143143
}
144144
}

scripts/utils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ async function getChangedFiles() {
3232
}
3333
exports.getChangedFiles = getChangedFiles;
3434

35-
async function getChangedPackages() {
35+
async function getChangedPackages(changedFiles) {
3636
const changedPackages = new Set();
37-
for (const filename of await getChangedFiles()) {
37+
const files = changedFiles || (await getChangedFiles());
38+
for (const filename of files) {
3839
// Check for changed files inside package dirs.
3940
const match = filename.match('^(packages(-exp)?/[a-zA-Z0-9-]+)/.*');
4041
if (match && match[1]) {

tools/gitHooks/license.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function rewriteCopyrightLine(contents) {
7676
return newLines.join('\n');
7777
}
7878

79-
async function doLicenseCommit(changedFiles) {
79+
async function doLicense(changedFiles) {
8080
const licenseSpinner = ora(' Validating License Headers').start();
8181

8282
const paths = changedFiles.filter(line => line.match(/(js|ts)$/));
@@ -115,28 +115,26 @@ async function doLicenseCommit(changedFiles) {
115115
symbol: '✅'
116116
});
117117

118-
const hasDiff = await git.diff();
118+
// Diff unstaged (license writes) against staged.
119+
const stageDiff = await git.diff(['--name-only']);
119120

120-
if (!hasDiff) {
121+
if (!stageDiff) {
122+
console.log(chalk`\n{red License pass caused no changes.}\n`);
123+
return;
124+
} else {
121125
console.log(
122-
chalk`\n{red License pass caused no changes.} Skipping commit.\n`
126+
`License script modified ${stageDiff.split('\n').length - 1} files.`
123127
);
124-
return;
125128
}
126129

127-
const gitSpinner = ora(' Creating automated license commit').start();
130+
const gitSpinner = ora(' Git staging license text modifications.').start();
128131
await git.add('.');
129132

130-
const commit = await git.commit('[AUTOMATED]: License Headers');
131-
132133
gitSpinner.stopAndPersist({
133-
symbol: ''
134+
symbol: '▶️'
134135
});
135-
console.log(
136-
chalk`{green Commited ${commit.commit} to branch ${commit.branch}}`
137-
);
138136
}
139137

140138
module.exports = {
141-
doLicenseCommit
139+
doLicense
142140
};

tools/gitHooks/prepush.js renamed to tools/gitHooks/precommit.js

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
* limitations under the License.
1616
*/
1717

18-
const { doPrettierCommit } = require('./prettier');
19-
const { doLicenseCommit } = require('./license');
18+
const { doPrettier } = require('./prettier');
19+
const { doLicense } = require('./license');
2020
const { resolve } = require('path');
2121
const simpleGit = require('simple-git/promise');
22+
const ora = require('ora');
2223
const chalk = require('chalk');
2324

2425
// Computed Deps
@@ -44,18 +45,51 @@ $ git stash pop
4445
return process.exit(1);
4546
}
4647

47-
const diff = await git.diff([
48-
'--name-only',
49-
'--diff-filter=d',
50-
'origin/master...HEAD'
51-
]);
48+
// Try to get most current origin/master.
49+
const fetchSpinner = ora(
50+
' Fetching latest version of master branch.'
51+
).start();
52+
try {
53+
await git.fetch('origin', 'master');
54+
fetchSpinner.stopAndPersist({
55+
symbol: '✅'
56+
});
57+
} catch (e) {
58+
fetchSpinner.stopAndPersist({
59+
symbol: '⚠️'
60+
});
61+
console.warn(
62+
chalk`\n{yellow} Unable to fetch latest version of master, diff may be stale.`
63+
);
64+
}
65+
66+
// Diff staged changes against origin/master...HEAD (common ancestor of HEAD and origin/master).
67+
const mergeBase = await git.raw(['merge-base', 'origin/master', 'HEAD']);
68+
let diffOptions = ['--name-only', '--diff-filter=d', '--cached'];
69+
if (mergeBase) {
70+
diffOptions.push(mergeBase.trim());
71+
} else {
72+
diffOptions.push('origin/master');
73+
}
74+
const diff = await git.diff(diffOptions);
5275
const changedFiles = diff.split('\n');
5376

5477
// Style the code
55-
await doPrettierCommit(changedFiles);
78+
await doPrettier(changedFiles);
5679

5780
// Validate License headers exist
58-
await doLicenseCommit(changedFiles);
81+
await doLicense(changedFiles);
82+
83+
// Diff staged changes against last commit. Don't do an empty commit.
84+
const postDiff = await git.diff(['--cached']);
85+
if (!postDiff) {
86+
console.error(chalk`
87+
{red Staged files are identical to previous commit after running formatting
88+
steps. Skipping commit.}
89+
90+
`);
91+
return process.exit(1);
92+
}
5993

6094
console.log(chalk`
6195
Pre-Push Validation Succeeded

tools/gitHooks/prettier.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function checkVersion() {
5656
});
5757
}
5858

59-
async function doPrettierCommit(changedFiles) {
59+
async function doPrettier(changedFiles) {
6060
try {
6161
await checkVersion();
6262
} catch (e) {
@@ -73,7 +73,7 @@ async function doPrettierCommit(changedFiles) {
7373
}
7474

7575
const stylingSpinner = ora(
76-
` Formatting ${targetFiles.length} files with prettier`
76+
` Checking ${targetFiles.length} files with prettier`
7777
).start();
7878
await spawn(
7979
'prettier',
@@ -90,27 +90,23 @@ async function doPrettierCommit(changedFiles) {
9090
symbol: '✅'
9191
});
9292

93-
const hasDiff = await git.diff();
93+
// Diff unstaged (prettier writes) against staged.
94+
const stageDiff = await git.diff(['--name-only']);
9495

95-
if (!hasDiff) {
96-
console.log(
97-
chalk`\n{red Prettier formatting caused no changes.} Skipping commit.\n`
98-
);
96+
if (!stageDiff) {
97+
console.log(chalk`\n{red Prettier formatting caused no changes.}\n`);
9998
return;
99+
} else {
100+
console.log(`Prettier modified ${stageDiff.split('\n').length - 1} files.`);
100101
}
101102

102-
const gitSpinner = ora(' Creating automated style commit').start();
103+
const gitSpinner = ora(' Git staging prettier formatting changes.').start();
103104
await git.add(targetFiles);
104-
105-
const commit = await git.commit('[AUTOMATED]: Prettier Code Styling');
106105
gitSpinner.stopAndPersist({
107-
symbol: ''
106+
symbol: '▶️'
108107
});
109-
console.log(
110-
chalk`{green Commited ${commit.commit} to branch ${commit.branch}}`
111-
);
112108
}
113109

114110
module.exports = {
115-
doPrettierCommit
111+
doPrettier
116112
};

0 commit comments

Comments
 (0)