fix: use correct folder to fetch git info for

This commit is contained in:
Jan De Dobbeleer 2020-12-01 20:43:30 +01:00 committed by Jan De Dobbeleer
parent 732035dfa1
commit 2a33b51f00
4 changed files with 19 additions and 4 deletions

View file

@ -29,6 +29,7 @@ type environmentInfo interface {
getcwd() string
homeDir() string
hasFiles(pattern string) bool
hasFilesInDir(dir, pattern string) bool
hasFolder(folder string) bool
getFileContent(file string) string
getPathSeperator() string
@ -95,6 +96,15 @@ func (env *environment) hasFiles(pattern string) bool {
return len(matches) > 0
}
func (env *environment) hasFilesInDir(dir, pattern string) bool {
pattern = dir + env.getPathSeperator() + pattern
matches, err := filepath.Glob(pattern)
if err != nil {
return false
}
return len(matches) > 0
}
func (env *environment) hasFolder(folder string) bool {
_, err := os.Stat(folder)
return !os.IsNotExist(err)

View file

@ -279,8 +279,8 @@ func (g *git) getGitHEADContext(ref string) string {
}
func (g *git) hasGitFile(file string) bool {
files := fmt.Sprintf("%s/.git/%s", g.repo.root, file)
return g.env.hasFiles(files)
files := fmt.Sprintf(".git/%s", file)
return g.env.hasFilesInDir(g.repo.root, files)
}
func (g *git) hasGitFolder(folder string) bool {

View file

@ -72,8 +72,8 @@ func setupHEADContextEnv(context *detachedContext) *git {
env.On("getFileContent", "/.git/rebase-apply/head-name").Return(context.origin)
env.On("getFileContent", "/.git/CHERRY_PICK_HEAD").Return(context.cherryPickSHA)
env.On("getFileContent", "/.git/MERGE_HEAD").Return(context.mergeHEAD)
env.On("hasFiles", "/.git/CHERRY_PICK_HEAD").Return(context.cherryPick)
env.On("hasFiles", "/.git/MERGE_HEAD").Return(context.merge)
env.On("hasFilesInDir", "", ".git/CHERRY_PICK_HEAD").Return(context.cherryPick)
env.On("hasFilesInDir", "", ".git/MERGE_HEAD").Return(context.merge)
env.mockGitCommand(context.currentCommit, "rev-parse", "--short", "HEAD")
env.mockGitCommand(context.tagName, "describe", "--tags", "--exact-match")
env.mockGitCommand(context.origin, "name-rev", "--name-only", "--exclude=tags/*", context.origin)

View file

@ -33,6 +33,11 @@ func (env *MockedEnvironment) hasFiles(pattern string) bool {
return args.Bool(0)
}
func (env *MockedEnvironment) hasFilesInDir(dir, pattern string) bool {
args := env.Called(dir, pattern)
return args.Bool(0)
}
func (env *MockedEnvironment) hasFolder(folder string) bool {
args := env.Called(folder)
return args.Bool(0)