Skip to content

Commit 18e8829

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 fd12d77 commit 18e8829

File tree

6 files changed

+68
-40
lines changed

6 files changed

+68
-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/api-report.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ const simpleGit = require('simple-git/promise');
3030

3131
const git = simpleGit(projectRoot);
3232

33-
async function doApiReportsCommit() {
34-
const changedPackages = await getChangedPackages();
33+
async function doApiReportsCommit(changedFiles) {
34+
const changedPackages = await getChangedPackages(changedFiles);
3535
const packageInfo = await getPackageInfo();
3636
const packageLocations = [];
3737
for (const packageName of changedPackages) {
@@ -52,16 +52,15 @@ async function doApiReportsCommit() {
5252
}
5353
}
5454

55+
// Diff unstaged (api-report writes) against staged.
5556
const hasDiff = await git.diff();
5657

5758
if (!hasDiff) return;
5859

59-
const gitSpinner = ora(' Creating automated API reports commit').start();
60+
const addSpinner = ora(` Git staging automated API reports.`).start();
6061
await git.add('.');
61-
62-
await git.commit('[AUTOMATED]: API Reports');
63-
gitSpinner.stopAndPersist({
64-
symbol: '✅'
62+
addSpinner.stopAndPersist({
63+
symbol: '▶️'
6564
});
6665
}
6766

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: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const { doLicenseCommit } = require('./license');
2020
const { doApiReportsCommit } = require('./api-report');
2121
const { resolve } = require('path');
2222
const simpleGit = require('simple-git/promise');
23+
const ora = require('ora');
2324
const chalk = require('chalk');
2425

2526
// Computed Deps
@@ -45,11 +46,33 @@ $ git stash pop
4546
return process.exit(1);
4647
}
4748

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

5578
// Style the code
@@ -59,7 +82,18 @@ $ git stash pop
5982
await doLicenseCommit(changedFiles);
6083

6184
// Generate API reports
62-
await doApiReportsCommit();
85+
await doApiReportsCommit(changedFiles);
86+
87+
// Diff staged changes against last commit. Don't do an empty commit.
88+
const postDiff = await git.diff(['--cached']);
89+
if (!postDiff) {
90+
console.error(chalk`
91+
{red Staged files are identical to previous commit after running formatting
92+
steps. Skipping commit.}
93+
94+
`);
95+
return process.exit(1);
96+
}
6397

6498
console.log(chalk`
6599
Pre-Push Validation Succeeded

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)