TL;DR: git rebase --exec --reschedule-failed-exec "run-your-tests" <sha>
will run tests after every commit after <sha>
: https://git-scm.com/docs/git-rebase#Documentation/git-rebase.txt---execltcmdgt
=======
It would be nice if we could somehow have git rebase --continue
simply run all our normal commit hooks.
In leu of this, "post-rewrite" and "pre-push" may be useful to verify quality of code that was just rebased, and/or check it before pushing.
But - how do we verify each intermediate commit has passing tests/working build/no lint errors?
Git creators/maintainers have an answer:
git rebase --exec "run-your-tests" <base-commit-ish>
This is better than a "wrapper around git rebase
" or "separate checking tool" that @sleske referred too, it's a native git feature!
When a commit has an issue, you'll see:
... output from failed tests ...
warning: execution failed: run-your-tests
You can fix the problem, and then run
git rebase --continue
Here's some "pseudo" code you could run post-rewrite or pre-push:
rm -rf node_modules
git rebase
--exec "no-conflict-markers"
--exec "npm install"
--exec "ensure-clean-git-status"
--exec "npm run lint"
--exec "npm run test"
--exec "npm run type-check"
master
(To run lint
, test
, and type-check
in parallel for npm/node, see run-p)
When you yourself are doing an interactive rebase, you'll want to do something like:
git rebase -i --exec "run-your-tests" --reschedule-failed-exec <sha>
You could alias this to something shorter:
git quality-rebase <sha>
Using --resheculed-failed-exec
does exactly what it says it does. If you fail a quality check, fix it, and run git rebase --continue
, it will run your quality checks again (which OP obviously wants).
Thanks to git worktree
, you can make your quality checks as HEAVY as you want.
As you are waiting for your quality checks to finish, you can open new terminal, run cd myapp; git worktree add ../myapp2 master; git checkout <whatever>
and keep on coding in a new working directory, unaffected by the status of the rebase-exec quality checks.
I think whenever you have good working code it's good to back it up to your git remote, which also allows others to discover it, and you can also have other webhook thingys "broadcast" that a new branch was published. Also, if your quality checks take some time, you won't want to sit and watch it. Therefore, I believe it's best to use any heavier checks as a pre-push
hook. Simply attempt to git push
and then when and if the rebase-exec quality checks finish, it'll be pushed up. You could use say
command to tell you if they failed: git push && say "successfully pushed changed" || say "quality checks failed"
Here's my specific command I'd recommend in the interim, you'll need to edit the --exec
command for your specific project:
CURRENT_BRANCH=`git branch --show-current`
git checkout master
git pull # this step should probably be automated via post-checkout hook. Or maybe warning should be generated when you create branch off an old local master branch.
git checkout $CURRENT_BRANCH
git fetch origin master # ensure latest origin/master, even if checkout commands fail
git rebase -i --reapply-cherry-picks --empty=ask --reschedule-failed-exec --rerere-autoupdate --exec "yarn install && yarn test -u && yarn run-p --print-label lint type-check" origin/master