Skip to content

Instantly share code, notes, and snippets.

@ThisIsMissEm
Last active October 2, 2020 16:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThisIsMissEm/cbe1ff8e9cb984046d28710ddfab2a66 to your computer and use it in GitHub Desktop.
Save ThisIsMissEm/cbe1ff8e9cb984046d28710ddfab2a66 to your computer and use it in GitHub Desktop.
The only post-checkout git hook I'll agree with
#! /usr/bin/env node
/**
* This file does not use typescript as it's run before you've necessarily run `yarn install`
* As such, we can only use built-in modules here.
*/
function main() {
let [
// Arguments from the githook: https://git-scm.com/docs/githooks#_post_checkout
prevHead,
head,
isFileCheckout,
] = process.env.HUSKY_GIT_PARAMS.split(' ', 3);
// we're doing `git checkout foo.js` so don't run:
if (isFileCheckout === '0') {
return;
}
const diffArgs = [prevHead, head, '--', 'package.json'];
const args = ['diff', '--exit-code', '--quiet', ...diffArgs];
const result = require('child_process').spawnSync('git', args, {
encoding: 'utf8',
});
if (result.status === 1) {
console.log(`
⚠️ package.json was changed! You may wish to run:
$ yarn install
You can see the changes with:
$ git diff ${diffArgs.join(' ')}
`);
}
}
main();
#!/bin/bash
prevHead=("$1")
nextHead=("$2")
isBranchCheckout=("$3")
# If we're doing a checkout of a branch and not a file, then:
if [ $isBranchCheckout == "1" ]; then
# Run git diff between the two heads on package.json and yarn.lock, capturing the output
diffArgs="$prevHead $nextHead -- package.json yarn.lock"
git diff --exit-code --quiet $diffArgs
# If the exit code is 1, then the files had a difference:
if [ "$?" == "1" ]; then
echo "⚠️ package.json was changed! You may wish to run:"
echo " \$ yarn install"
echo "You can see the changes with:"
echo " \$ git diff $diffArgs"
fi;
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment