Last active
October 2, 2020 16:57
-
-
Save ThisIsMissEm/cbe1ff8e9cb984046d28710ddfab2a66 to your computer and use it in GitHub Desktop.
The only post-checkout git hook I'll agree with
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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