diff options
| author | Andrew Donnellan <ajd@linux.ibm.com> | 2019-12-12 17:04:58 +1100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2019-12-12 09:56:25 +0100 |
| commit | 08003f6440deafc4e193b159c4acece64f7864b1 (patch) | |
| tree | 6c788af0742acb9a0f3d5fc6daf4fa1d8c8cadab /pkg/vcs/git.go | |
| parent | df191c7fd0bb6b36fbf55791a49f3c1daa3135e9 (diff) | |
pkg/vcs: Unset various git environment variables when invoking git
If you try to run git-using tests while the GIT_DIR environment variable
(and GIT_WORK_TREE, etc) happens to be set, the tests are going to do fun
and exciting things on a repository that isn't the test repository it tries
to set up.
As it turns out, if you try to run "make test" using git rebase -x, you'll
end up with GIT_DIR set to the syzkaller tree. Hilarity ensues.
Unset GIT_DIR, GIT_WORK_TREE and a few other environment variables when
invoking git - that way it'll default to looking at the working directory
that we have given it, which is what we expect.
Signed-off-by: Andrew Donnellan <ajd@linux.ibm.com>
Diffstat (limited to 'pkg/vcs/git.go')
| -rw-r--r-- | pkg/vcs/git.go | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/pkg/vcs/git.go b/pkg/vcs/git.go index a42f12edc..27064de7b 100644 --- a/pkg/vcs/git.go +++ b/pkg/vcs/git.go @@ -33,6 +33,26 @@ func newGit(dir string, ignoreCC map[string]bool) *git { } } +func filterEnv() []string { + // We have to filter various git environment variables - if + // these variables are set (e.g. if a test is being run as + // part of a rebase) we're going to be acting on some other + // repository (e.g the syzkaller tree itself) rather than the + // intended repo. + env := os.Environ() + for i := 0; i < len(env); i++ { + if strings.HasPrefix(env[i], "GIT_DIR") || + strings.HasPrefix(env[i], "GIT_WORK_TREE") || + strings.HasPrefix(env[i], "GIT_INDEX_FILE") || + strings.HasPrefix(env[i], "GIT_OBJECT_DIRECTORY") { + env = append(env[:i], env[i+1:]...) + i-- + } + } + + return env +} + func (git *git) Poll(repo, branch string) (*Commit, error) { git.reset() origin, err := git.git("remote", "get-url", "origin") @@ -315,6 +335,7 @@ func (git *git) fetchCommits(since, base, user, domain string, greps []string, f args = append(args, base) cmd := exec.Command("git", args...) cmd.Dir = git.dir + cmd.Env = filterEnv() if err := osutil.Sandbox(cmd, true, false); err != nil { return nil, err } @@ -359,7 +380,7 @@ func (git *git) fetchCommits(since, base, user, domain string, greps []string, f } func (git *git) git(args ...string) ([]byte, error) { - return runSandboxed(git.dir, "git", args...) + return runSandboxedEnv(git.dir, "git", filterEnv(), args...) } func splitEmail(email string) (user, domain string, err error) { |
