diff --git a/lfs/lfs.go b/lfs/lfs.go index d293d4b5..c76a0b44 100644 --- a/lfs/lfs.go +++ b/lfs/lfs.go @@ -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 {