Added support for environment variables GIT_DIR and GIT_WORK_TREE

We are still missing support for config option `core.worktree` and
command line options `--work-tree` and `--git-dir`.
This commit is contained in:
Michael Käufl 2015-06-02 22:30:00 +02:00
parent 968faba7e1
commit 646e50207e

@ -113,6 +113,13 @@ func init() {
}
func resolveGitDir() (string, string, error) {
gitDir := Config.Getenv("GIT_DIR")
workTree := Config.Getenv("GIT_WORK_TREE")
if gitDir != "" {
return processGitDirVar(gitDir, workTree)
}
wd, err := os.Getwd()
if err != nil {
return "", "", err
@ -121,6 +128,24 @@ func resolveGitDir() (string, string, error) {
return recursiveResolveGitDir(wd)
}
func processGitDirVar(gitDir, workTree string) (string, string, error) {
if workTree != "" {
return workTree, gitDir, nil
}
// See `core.worktree` in `man git-config`:
// “If --git-dir or GIT_DIR is specified but none of --work-tree, GIT_WORK_TREE and
// core.worktree is specified, the current working directory is regarded as the top
// level of your working tree.”
wd, err := os.Getwd()
if err != nil {
return "", "", err
}
return wd, gitDir, nil
}
func recursiveResolveGitDir(dir string) (string, string, error) {
var cleanDir = filepath.Clean(dir)
if cleanDir[len(cleanDir)-1] == os.PathSeparator {