-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceutil.go
104 lines (95 loc) · 2.19 KB
/
serviceutil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package serviceutil
import (
"fmt"
"os"
"path/filepath"
"strings"
)
// GitRelWorkdir returns git relative workdir of current directory.
//
// It should return the same output as `git rev-parse --show-prefix`.
// It does not execute `git` command to avoid needless git binary dependency.
//
// An example problem due to `git` command dependency: https://github.com/khulnasoft/codebase/issues/1158
func GitRelWorkdir() (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
root, err := findGitRoot(cwd)
if err != nil {
return "", err
}
if !strings.HasPrefix(cwd, root) {
return "", fmt.Errorf("cannot get GitRelWorkdir: cwd=%q, root=%q", cwd, root)
}
const separator = string(filepath.Separator)
path := strings.Trim(strings.TrimPrefix(cwd, root), separator)
if path != "" {
path += separator
}
return path, nil
}
func GetGitRoot() (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
return findGitRoot(cwd)
}
func findGitRoot(path string) (string, error) {
gitPath, err := findDotGitPath(path)
if err != nil {
return "", err
}
return filepath.Dir(gitPath), nil
}
func findDotGitPath(path string) (string, error) {
// normalize the path
path, err := filepath.Abs(path)
if err != nil {
return "", err
}
for {
fi, err := os.Stat(filepath.Join(path, ".git"))
if err == nil {
if !fi.IsDir() {
return "", fmt.Errorf(".git exist but is not a directory")
}
return filepath.Join(path, ".git"), nil
}
if !os.IsNotExist(err) {
// unknown error
return "", err
}
// detect bare repo
ok, err := isGitDir(path)
if err != nil {
return "", err
}
if ok {
return path, nil
}
parent := filepath.Dir(path)
if parent == path {
return "", fmt.Errorf(".git not found")
}
path = parent
}
}
// ref: https://github.com/git/git/blob/3bab5d56259722843359702bc27111475437ad2a/setup.c#L328-L338
func isGitDir(path string) (bool, error) {
markers := []string{"HEAD", "objects", "refs"}
for _, marker := range markers {
_, err := os.Stat(filepath.Join(path, marker))
if err == nil {
continue
}
if !os.IsNotExist(err) {
// unknown error
return false, err
}
return false, nil
}
return true, nil
}