Skip to content

Commit

Permalink
fix: use correct folder to fetch git info for
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Dec 1, 2020
1 parent 732035d commit 2a33b51
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
10 changes: 10 additions & 0 deletions environment.go
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions segment_git.go
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions segment_git_test.go
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions segment_path_test.go
Expand Up @@ -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)
Expand Down

0 comments on commit 2a33b51

Please sign in to comment.