Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Git commands executed in a working directory that isn't the repository root #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

marcusrbrown
Copy link

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.

… 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.
@therealklanni
Copy link
Owner

How difficult would it be to add a test around this?

@marcusrbrown
Copy link
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')?

@therealklanni
Copy link
Owner

Yeah, you could do it that way. Hit me up on Slack if you need any help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants