Skip to content

Move prepush scripts to precommit #3026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
},
"husky": {
"hooks": {
"pre-push": "node tools/gitHooks/prepush.js"
"pre-commit": "node tools/gitHooks/precommit.js"
}
}
}
5 changes: 3 additions & 2 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ async function getChangedFiles() {
}
exports.getChangedFiles = getChangedFiles;

async function getChangedPackages() {
async function getChangedPackages(changedFiles) {
const changedPackages = new Set();
for (const filename of await getChangedFiles()) {
const files = changedFiles || (await getChangedFiles());
for (const filename of files) {
// Check for changed files inside package dirs.
const match = filename.match('^(packages(-exp)?/[a-zA-Z0-9-]+)/.*');
if (match && match[1]) {
Expand Down
24 changes: 11 additions & 13 deletions tools/gitHooks/license.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function rewriteCopyrightLine(contents) {
return newLines.join('\n');
}

async function doLicenseCommit(changedFiles) {
async function doLicense(changedFiles) {
const licenseSpinner = ora(' Validating License Headers').start();

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

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

if (!hasDiff) {
if (!stageDiff) {
console.log(chalk`\n{red License pass caused no changes.}\n`);
return;
} else {
console.log(
chalk`\n{red License pass caused no changes.} Skipping commit.\n`
`License script modified ${stageDiff.split('\n').length - 1} files.`
);
return;
}

const gitSpinner = ora(' Creating automated license commit').start();
const gitSpinner = ora(' Git staging license text modifications.').start();
await git.add('.');

const commit = await git.commit('[AUTOMATED]: License Headers');

gitSpinner.stopAndPersist({
symbol: ''
symbol: '▶️'
});
console.log(
chalk`{green Commited ${commit.commit} to branch ${commit.branch}}`
);
}

module.exports = {
doLicenseCommit
doLicense
};
52 changes: 43 additions & 9 deletions tools/gitHooks/prepush.js → tools/gitHooks/precommit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
* limitations under the License.
*/

const { doPrettierCommit } = require('./prettier');
const { doLicenseCommit } = require('./license');
const { doPrettier } = require('./prettier');
const { doLicense } = require('./license');
const { resolve } = require('path');
const simpleGit = require('simple-git/promise');
const ora = require('ora');
const chalk = require('chalk');

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

const diff = await git.diff([
'--name-only',
'--diff-filter=d',
'origin/master...HEAD'
]);
// Try to get most current origin/master.
const fetchSpinner = ora(
' Fetching latest version of master branch.'
).start();
try {
await git.fetch('origin', 'master');
fetchSpinner.stopAndPersist({
symbol: '✅'
});
} catch (e) {
fetchSpinner.stopAndPersist({
symbol: '⚠️'
});
console.warn(
chalk`\n{yellow} Unable to fetch latest version of master, diff may be stale.`
);
}

// Diff staged changes against origin/master...HEAD (common ancestor of HEAD and origin/master).
const mergeBase = await git.raw(['merge-base', 'origin/master', 'HEAD']);
let diffOptions = ['--name-only', '--diff-filter=d', '--cached'];
if (mergeBase) {
diffOptions.push(mergeBase.trim());
} else {
diffOptions.push('origin/master');
}
const diff = await git.diff(diffOptions);
const changedFiles = diff.split('\n');

// Style the code
await doPrettierCommit(changedFiles);
await doPrettier(changedFiles);

// Validate License headers exist
await doLicenseCommit(changedFiles);
await doLicense(changedFiles);

// Diff staged changes against last commit. Don't do an empty commit.
const postDiff = await git.diff(['--cached']);
if (!postDiff) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this condition will never be true since we are committing something from the staging area to begin with.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a case I ran into while testing where if you only make formatting changes, and they are wrong, and Prettier reformats them back the way they were, there is no longer a diff between your changes and the last commit. I don't think this would ever happen on push but it could happen on a small commit if you did a lot of undos and don't realize the only remaining changes are formatting.

console.error(chalk`
{red Staged files are identical to previous commit after running formatting
steps. Skipping commit.}

`);
return process.exit(1);
}

console.log(chalk`
Pre-Push Validation Succeeded
Expand Down
26 changes: 11 additions & 15 deletions tools/gitHooks/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function checkVersion() {
});
}

async function doPrettierCommit(changedFiles) {
async function doPrettier(changedFiles) {
try {
await checkVersion();
} catch (e) {
Expand All @@ -73,7 +73,7 @@ async function doPrettierCommit(changedFiles) {
}

const stylingSpinner = ora(
` Formatting ${targetFiles.length} files with prettier`
` Checking ${targetFiles.length} files with prettier`
).start();
await spawn(
'prettier',
Expand All @@ -90,27 +90,23 @@ async function doPrettierCommit(changedFiles) {
symbol: '✅'
});

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

if (!hasDiff) {
console.log(
chalk`\n{red Prettier formatting caused no changes.} Skipping commit.\n`
);
if (!stageDiff) {
console.log(chalk`\n{red Prettier formatting caused no changes.}\n`);
return;
} else {
console.log(`Prettier modified ${stageDiff.split('\n').length - 1} files.`);
}

const gitSpinner = ora(' Creating automated style commit').start();
const gitSpinner = ora(' Git staging prettier formatting changes.').start();
await git.add(targetFiles);

const commit = await git.commit('[AUTOMATED]: Prettier Code Styling');
gitSpinner.stopAndPersist({
symbol: ''
symbol: '▶️'
});
console.log(
chalk`{green Commited ${commit.commit} to branch ${commit.branch}}`
);
}

module.exports = {
doPrettierCommit
doPrettier
};