git-guppy
git-guppy copied to clipboard
Fix Git commands executed in a working directory that isn't the repository root
This is an alternative to #73 that only relies on the environment variables passed down through Git hooks.
A Git hook is executed from the location of the root .git directory, and some hooks (all?) will set the GIT_DIR and GIT_INDEX_FILE variables relative to that location, instead of the working directory. In the case of a working directory or project (containing a Gulpfile with hook tasks) beneath the repository root, GIT_DIR is set to '.git' and GIT_INDEX_FILE is set to '.git/index'. These variables act as an implicit --git-dir argument that is passed to any Git commands executed from a hook. This breaks the getIndexed() and streamFromIndex() functions because '.git' doesn't exist in the working directory and so the Git commands run from these functions are unable to locate the repository root.
The approach that I've seen other software use is to detect whether the value of GIT_DIR exists as a directory relative to the working directory, and if it doesn't then remove it from the environment. Without this implicit --git-dir argument the Git commands run from hooks will autodetect the .git directory (climbing up the tree) as if they had been run from the command line.
I couldn't figure out how to add tests for this functionality since I didn't modify any functions.
How difficult would it be to add a test around this?
@therealklanni I'm not sure. I'm familiar with Mocha and Chai, but not sinon or proxyquire. Are those tests simulating the git commands? Would I simulate setting those variables and expecting the typical message output when the commands fail (i.e. 'failure not a repository')?
Yeah, you could do it that way. Hit me up on Slack if you need any help.