Whenever I commit, I worry that I may have missed out a dependency and I’m looking for the simplest way to test my git tree in isolation to ensure whatever is in the git index (“staged”) will in fact compile/run on their own.
My code’s dependencies exist in the filesystem from where I do the ‘git add’ and so simple compiling and running tests doesn’t guarantee that whatever I checkin would compile/run if the tree (or staging area) was checkout out onto a clean filesystem.
I could have a continuous build that would check after submission but I prefer not to have any bad commits in the history that I later have to patch. I therefore want a way of creating an isolated environment that includes a checkout of the tree as well as the index/staging area.
One thing I’ve considered is using git stash twice, i.e.:
- Invoke ‘git stash’ to save the files in the index
- Somehow get a list of the files not tracked, ‘git add’ all these files, save a new stash
- Delete all the previously untracked files
- Restore the original stash
- I should now have a clean environment that has only the code already checked-in and the code in the staging area which I can compile & test.
- Once finished, I restore the stash of the untracked files and then untrack them to leave me in the same position that I was in originally.
(These untracked files may be useful but not necessarily things I want to check in to the repository – e.g. eclipse projects).
I have a feeling I’m overengineering a simple problem, though.
Install this script (or something like it — mine is stolen too) as a pre-commit hook. It copies the index to a temporary working dir and runs a build there. It will catch files that you’ve missed.
I know there are at least one or two other SO questions that address this exact issue — testing/validating the index instead of the working dir in a pre-commit hook — but I can’t seem to find them right now.
(For completeness, I’ve got this script in my repo as .git-hooks/pre-commit/test-the-index; there are a couple of other scripts there. See below for what I’m using as .git/hooks/pre-commit.)
This is my actual .git/hooks/pre-commit: