Fix Git commands executed in a working directory that isn't the repository root#80
Open
marcusrbrown wants to merge 2 commits into
Open
Fix Git commands executed in a working directory that isn't the repository root#80marcusrbrown wants to merge 2 commits into
marcusrbrown wants to merge 2 commits into
Conversation
… repository root Git hooks pass several environment variables (prefixed with "GIT_") to be used by hook processes and Git commands executed from hooks. Two of these variables, GIT_DIR and GIT_INDEX_FILE, are set to the location and path of the root .git directory and .git/index file. Git commands executed from hooks use these environment variables as an implicit "--git-dir" argument, and if they don't refer to valid directories then those commands will fail. Because Git executes hooks from the location of the .git directory, these two variables will be incorrectly set for guppy hooks if the directory containing the gulpfile is not the same as the repository root. If we detect that the value of GIT_DIR does not exist as a directory from our current working directory, then we remove both GIT_DIR and GIT_INDEX_FILE from the environment so that subsequent Git commands will autodetect the repository root and function properly.
Owner
|
How difficult would it be to add a test around this? |
Author
|
@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')? |
Owner
|
Yeah, you could do it that way. Hit me up on Slack if you need any help. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.