Skip to content

Commit 24b5d5e

Browse files
committed
smartJoin(): new function, extracted from NewRepository()
1 parent c63ebdf commit 24b5d5e

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

git/git.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ type Repository struct {
6464
path string
6565
}
6666

67+
// smartJoin returns the path that can be described as `relPath`
68+
// relative to `path`, given that `path` is either absolute or is
69+
// relative to the current directory.
70+
func smartJoin(path, relPath string) string {
71+
if filepath.IsAbs(relPath) {
72+
return relPath
73+
} else {
74+
return filepath.Join(path, relPath)
75+
}
76+
}
77+
6778
func NewRepository(path string) (*Repository, error) {
6879
cmd := exec.Command("git", "-C", path, "rev-parse", "--git-dir")
6980
out, err := cmd.Output()
@@ -87,10 +98,7 @@ func NewRepository(path string) (*Repository, error) {
8798
return nil, err
8899
}
89100
}
90-
gitDir := string(bytes.TrimSpace(out))
91-
if !filepath.IsAbs(gitDir) {
92-
gitDir = filepath.Join(path, gitDir)
93-
}
101+
gitDir := smartJoin(path, string(bytes.TrimSpace(out)))
94102
repo := &Repository{
95103
path: gitDir,
96104
}

0 commit comments

Comments
 (0)