Skip to content

Commit c9842c5

Browse files
committed
Move prepush scripts to precommit
Fetch master branch before diffing Each process diff unstaged against staged Make sure API report uses same diff Revert firestore file Restore merge-base behavior Clearer comment
1 parent 8143c83 commit c9842c5

File tree

5 files changed

+61
-32
lines changed

5 files changed

+61
-32
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: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,24 @@ async function doLicenseCommit(changedFiles) {
115115
symbol: '✅'
116116
});
117117

118-
const hasDiff = await git.diff();
118+
// Diff unstaged (prettier 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 = {

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

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const { doPrettierCommit } = require('./prettier');
1919
const { doLicenseCommit } = 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,11 +45,33 @@ $ 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
@@ -57,6 +80,17 @@ $ git stash pop
5780
// Validate License headers exist
5881
await doLicenseCommit(changedFiles);
5982

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+
}
93+
6094
console.log(chalk`
6195
Pre-Push Validation Succeeded
6296

tools/gitHooks/prettier.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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,25 +90,21 @@ 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 = {

0 commit comments

Comments
 (0)