forked from muja/goconfig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath.go
327 lines (284 loc) · 6.59 KB
/
path.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package goconfig
import (
"bufio"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
)
const (
gitSystemConfigEnv = "TEST_GIT_SYSTEM_CONFIG"
)
// homeDir returns home directory
func homeDir() (string, error) {
var (
home string
)
if runtime.GOOS == "windows" {
home = os.Getenv("USERPROFILE")
if home == "" {
home = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
}
}
if home == "" {
home = os.Getenv("HOME")
}
if home == "" {
return "", fmt.Errorf("cannot find HOME")
}
return home, nil
}
func xdgConfigHome(file string) (string, error) {
var (
home string
err error
)
home = os.Getenv("XDG_CONFIG_HOME")
if home != "" {
return filepath.Join(home, "git", file), nil
}
home, err = homeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".config", "git", file), nil
}
// expendHome expends path prefix "~/" to home dir
func expendHome(name string) (string, error) {
if filepath.IsAbs(name) {
return name, nil
}
home, err := homeDir()
if err != nil {
return "", err
}
if len(name) == 0 || name == "~" {
return home, nil
} else if len(name) > 1 && name[0] == '~' && (name[1] == '/' || name[1] == '\\') {
return filepath.Join(home, name[2:]), nil
}
return filepath.Join(home, name), nil
}
// absPath returns absolute path and will expend homedir if path has "~/' prefix
func absPath(name string) (string, error) {
if name == "" {
return os.Getwd()
}
if filepath.IsAbs(name) {
return name, nil
}
if len(name) > 0 && name[0] == '~' && (len(name) == 1 || name[1] == '/' || name[1] == '\\') {
return expendHome(name)
}
return filepath.Abs(name)
}
// absJoin returns absolute path, and use <dir> as parent dir for relative path
func absJoin(dir, name string) (string, error) {
if name == "" {
return filepath.Abs(dir)
}
if filepath.IsAbs(name) {
return name, nil
}
if len(name) > 0 && name[0] == '~' && (len(name) == 1 || name[1] == '/' || name[1] == '\\') {
return expendHome(name)
}
return absPath(filepath.Join(dir, name))
}
func getWorkTree(gitDir string) (string, error) {
var err error
if !filepath.IsAbs(gitDir) {
gitDir, err = filepath.Abs(gitDir)
if err != nil {
return "", err
}
}
// A git-worktree gitdir?
fn := filepath.Join(gitDir, "gitdir")
f, err := os.Open(fn)
if err == nil {
s := bufio.NewScanner(f)
if s.Scan() {
if !filepath.IsAbs(s.Text()) {
gitDir = filepath.Join(gitDir, s.Text())
} else {
gitDir = s.Text()
}
}
f.Close()
}
if filepath.Base(gitDir) != ".git" {
return "", fmt.Errorf("repository name is not '.git', a bare repository?")
}
return filepath.Dir(gitDir), nil
}
func getGitCommonDir(gitDir string) (string, error) {
commonDir := gitDir
if IsFile(filepath.Join(gitDir, "commondir")) {
f, err := os.Open(filepath.Join(gitDir, "commondir"))
if err == nil {
s := bufio.NewScanner(f)
if s.Scan() {
commonDir = s.Text()
if !filepath.IsAbs(commonDir) {
commonDir = filepath.Join(gitDir, commonDir)
}
}
f.Close()
}
}
if IsFile(filepath.Join(commonDir, "config")) &&
IsDir(filepath.Join(commonDir, "refs")) &&
IsDir(filepath.Join(commonDir, "objects")) {
return commonDir, nil
}
return "", fmt.Errorf("'%s' is not a valid gitdir", commonDir)
}
// isGitDir test whether dir is a valid git dir
func isGitDir(dir string) bool {
if !IsFile(filepath.Join(dir, "HEAD")) {
return false
}
_, err := getGitCommonDir(dir)
return err == nil
}
// findGitDir searches git dir
func findGitDir(dir string) (string, error) {
var err error
dir, err = absPath(dir)
if err != nil {
return "", err
}
for {
// Check if is in a bare repo
if isGitDir(dir) {
return dir, nil
}
// Check .git
gitdir := filepath.Join(dir, ".git")
fi, err := os.Stat(gitdir)
if err != nil {
// Test parent dir
oldDir := dir
dir = filepath.Dir(dir)
if oldDir == dir {
break
}
continue
} else if fi.IsDir() {
if isGitDir(gitdir) {
return gitdir, nil
}
return "", fmt.Errorf("corrupt git dir: %s", gitdir)
} else {
f, err := os.Open(gitdir)
if err != nil {
return "", fmt.Errorf("cannot open gitdir file '%s'", gitdir)
}
defer f.Close()
reader := bufio.NewReader(f)
line, err := reader.ReadString('\n')
if strings.HasPrefix(line, "gitdir:") {
realgit := strings.TrimSpace(strings.TrimPrefix(line, "gitdir:"))
if !filepath.IsAbs(realgit) {
realgit, err = absJoin(filepath.Dir(gitdir), realgit)
if err != nil {
return "", err
}
}
if isGitDir(realgit) {
return realgit, nil
}
return "", fmt.Errorf("gitdir '%s' points to corrupt git repo: %s", gitdir, realgit)
}
return "", fmt.Errorf("bad gitdir file '%s'", gitdir)
}
}
return "", ErrNotInGitDir
}
// FindGitConfig returns local git config file
func FindGitConfig(dir string) (string, error) {
var err error
if dir, err = findGitDir(dir); err != nil {
return "", err
}
if dir, err = getGitCommonDir(dir); err != nil {
return "", err
}
return filepath.Join(dir, "config"), nil
}
// SystemConfigFile returns system git config file
func SystemConfigFile() string {
file := os.Getenv(gitSystemConfigEnv)
if file == "" {
file = "/etc/gitconfig"
}
return file
}
// GlobalConfigFile returns global git config file
func GlobalConfigFile() (string, error) {
var (
file string
err error
)
file, err = xdgConfigHome("config")
if err != nil {
return "", err
}
// xdg config not exist, use ~/.gitconfig
if _, err := os.Stat(file); err != nil {
file, err = expendHome(".gitconfig")
if err != nil {
return "", err
}
}
return file, nil
}
// unsetHome unsets HOME related environments
func unsetHome() {
if runtime.GOOS == "windows" {
os.Unsetenv("USERPROFILE")
os.Unsetenv("HOMEDRIVE")
os.Unsetenv("HOMEPATH")
}
os.Unsetenv("HOME")
}
// setHome sets proper HOME environments
func setHome(home string) {
if runtime.GOOS == "windows" {
os.Setenv("USERPROFILE", home)
if strings.Contains(home, ":\\") {
slices := strings.SplitN(home, ":\\", 2)
if len(slices) == 2 {
os.Setenv("HOMEDRIVE", slices[0]+":")
os.Setenv("HOMEPATH", "\\"+slices[1])
}
}
} else {
os.Setenv("HOME", home)
}
}
// Exist check if path is exist.
func Exist(name string) bool {
if _, err := os.Stat(name); err == nil {
return true
}
return false
}
// IsFile returns true if path is exist and is a file.
func IsFile(name string) bool {
fi, err := os.Stat(name)
if err != nil || fi.IsDir() {
return false
}
return true
}
// IsDir returns true if path is exist and is a directory.
func IsDir(name string) bool {
fi, err := os.Stat(name)
if err != nil || !fi.IsDir() {
return false
}
return true
}