Merge pull request #692 from sinbad/fix-bare-repo

Fix resolve of git dirs finding a parent non-bare repo if wd is a bare repo
This commit is contained in:
risk danger olson 2015-10-19 14:12:37 -06:00
commit 4cd71f6c1b
3 changed files with 64 additions and 42 deletions

@ -7,6 +7,7 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
@ -340,3 +341,31 @@ func GetCommitSummary(commit string) (*CommitSummary, error) {
}
}
func RootDir() (string, error) {
cmd := execCommand("git", "rev-parse", "--show-toplevel")
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("Failed to call git rev-parse --show-toplevel: %v %v", err, string(out))
}
path := strings.TrimSpace(string(out))
if len(path) > 0 {
return filepath.Abs(path)
}
return "", nil
}
func GitDir() (string, error) {
cmd := execCommand("git", "rev-parse", "--git-dir")
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("Failed to call git rev-parse --git-dir: %v %v", err, string(out))
}
path := strings.TrimSpace(string(out))
if len(path) > 0 {
return filepath.Abs(path)
}
return "", nil
}

@ -8,6 +8,7 @@ import (
"runtime"
"strings"
"github.com/github/git-lfs/git"
"github.com/github/git-lfs/vendor/_nuts/github.com/rubyist/tracerx"
)
@ -145,12 +146,7 @@ func resolveGitDir() (string, string, error) {
return processGitDirVar(gitDir, workTree)
}
wd, err := os.Getwd()
if err != nil {
return "", "", err
}
workTreeR, gitDirR, err := recursiveResolveGitDir(wd)
workTreeR, gitDirR, err := resolveGitDirFromCurrentDir()
if err != nil {
return "", "", err
}
@ -195,32 +191,19 @@ func processWorkTreeVar(gitDir, workTree string) (string, string, error) {
return absWorkTree, gitDir, nil
}
func recursiveResolveGitDir(dir string) (string, string, error) {
var cleanDir = filepath.Clean(dir)
if cleanDir[len(cleanDir)-1] == os.PathSeparator {
return "", "", fmt.Errorf("Git repository not found")
}
func resolveGitDirFromCurrentDir() (string, string, error) {
if filepath.Base(dir) == gitExt {
// We're in the `.git` directory. Make no assumptions about the working directory.
return "", dir, nil
}
gitDir := filepath.Join(dir, gitExt)
info, err := os.Stat(gitDir)
// Get root of the git working dir
gitDir, err := git.GitDir()
if err != nil {
// Found neither a directory nor a file named `.git`.
// Move one directory up.
return recursiveResolveGitDir(filepath.Dir(dir))
return "", "", err
}
if !info.IsDir() {
// Found a file named `.git` (we're in a submodule).
return resolveDotGitFile(gitDir)
}
// Allow this to fail, will do so if GIT_DIR isn't set but GIT_WORK_TREE is rel
// Dealt with by parent
rootDir, _ := git.RootDir()
// Found the `.git` directory.
return dir, gitDir, nil
return rootDir, gitDir, nil
}
func resolveDotGitFile(file string) (string, string, error) {

@ -323,21 +323,6 @@ git config filter.lfs.clean = \"\"
actual5=$(GIT_DIR=$gitDir GIT_WORK_TREE=a/b git lfs env)
[ "$expected5" = "$actual5" ]
expected6=$(printf "%s\n%s\n
LocalWorkingDir=$TRASHDIR/$reponame/a/b
LocalGitDir=$TRASHDIR/$reponame/.git
LocalGitStorageDir=$TRASHDIR/$reponame/.git
LocalMediaDir=$TRASHDIR/$reponame/.git/lfs/objects
TempDir=$TRASHDIR/$reponame/.git/lfs/tmp
ConcurrentTransfers=3
BatchTransfer=true
$(GIT_WORK_TREE=a/b env | grep "^GIT")
git config filter.lfs.smudge = \"\"
git config filter.lfs.clean = \"\"
" "$(git lfs version)" "$(git version)")
actual6=$(GIT_WORK_TREE=a/b git lfs env)
[ "$expected6" = "$actual6" ]
cd $TRASHDIR/$reponame/a/b
expected7=$(printf "%s\n%s\n
LocalWorkingDir=$TRASHDIR/$reponame/a/b
@ -369,3 +354,28 @@ $(GIT_WORK_TREE=$workTree env | grep "^GIT")
[ "$expected8" = "$actual8" ]
)
end_test
begin_test "env with bare repo"
(
set -e
reponame="env-with-bare-repo"
git init --bare $reponame
cd $reponame
expected=$(printf "%s\n%s\n
LocalWorkingDir=
LocalGitDir=$TRASHDIR/$reponame
LocalGitStorageDir=$TRASHDIR/$reponame
LocalMediaDir=$TRASHDIR/$reponame/lfs/objects
TempDir=$TRASHDIR/$reponame/lfs/tmp
ConcurrentTransfers=3
BatchTransfer=true
$(env | grep "^GIT")
%s
" "$(git lfs version)" "$(git version)" "$envInitConfig")
actual=$(git lfs env)
[ "$expected" = "$actual" ]
)
end_test