Created
February 5, 2024 11:59
-
-
Save rr-it/a0c2c888806c1be2ec2d2d61e0ab661b to your computer and use it in GitHub Desktop.
Git hook to call `git submodule update` automatically via yorkie and Node.js
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
# Files excluded from git packages | |
.githooks/ export-ignore |
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
{ | |
"gitHooks": { | |
"post-checkout": ".githooks/update-submodule checkout", | |
"post-rewrite": ".githooks/update-submodule $GIT_PARAMS" | |
}, | |
"devDependencies": { | |
"yorkie": "^2.0.0" | |
} | |
} |
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 | |
const process = require('node:process'); | |
const util = require('node:util'); | |
const exec = util.promisify(require('node:child_process').exec); | |
const calledBy = process.argv[2] ? '' + process.argv[2] : ''; | |
const getGitChangedFiles = async function () { | |
console.log('getting changed files'); | |
const {stdout, stderr} = await exec('git diff-tree --no-commit-id --name-only HEAD@\\{1\\} HEAD'); | |
if (stderr) { | |
console.error(stderr); | |
} | |
return '' + stdout; | |
} | |
const execGitSubmoduleUpdate = async function () { | |
console.log('initializing & updating submodule(s)'); | |
const {stdout, stderr} = await exec('git submodule update --init --recursive'); | |
console.log(stdout); | |
if (stderr) { | |
console.error(stderr); | |
} | |
} | |
const run = async function () { | |
// Call "git submodule update" if the file .gitmodules is changed. | |
console.log('[update-submodule hook: ' + calledBy + ']'); | |
const changedFiles = await getGitChangedFiles(); | |
if (/(^|\s)\.gitmodules(\s|$)/.test(changedFiles)) { | |
await execGitSubmoduleUpdate(); | |
} | |
} | |
run() | |
.finally(function () { | |
/* | |
* In case this script is run by hook post-checkout: | |
* This scripts exit status becomes the exit status for `git switch` or `git checkout`. | |
* | |
* @see: https://git-scm.com/docs/githooks#_post_checkout | |
*/ | |
process.exit(0); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is based on https://gist.github.com/digitaljhelms/f74eaf56835262d6bf3f by @digitaljhelms
Thanks for this!