forked from muja/goconfig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepository.go
69 lines (60 loc) · 1.44 KB
/
repository.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
package goconfig
import "path/filepath"
// Repository defines struct for a Git repository.
type Repository struct {
gitDir string
gitCommonDir string
workDir string
gitConfig GitConfig
}
// GitDir returns GitDir
func (v Repository) GitDir() string {
return v.gitDir
}
// GitCommonDir returns commondir where contains "config" file
func (v Repository) GitCommonDir() string {
return v.gitCommonDir
}
// WorkDir returns workdir
func (v Repository) WorkDir() string {
return v.workDir
}
// IsBare indicates a repository is a bare repository.
func (v Repository) IsBare() bool {
return v.workDir == ""
}
// Config returns git config object
func (v Repository) Config() GitConfig {
return v.gitConfig
}
// FindRepository locates repository object search from the given dir.
func FindRepository(dir string) (*Repository, error) {
var (
gitDir string
commonDir string
workDir string
gitConfig GitConfig
err error
)
gitDir, err = findGitDir(dir)
if err != nil {
return nil, err
}
commonDir, err = getGitCommonDir(gitDir)
if err != nil {
return nil, err
}
gitConfig, err = loadConfigFile(filepath.Join(commonDir, "config"))
if err != nil {
return nil, err
}
if !gitConfig.HasKey("core.bare") || !gitConfig.GetBool("core.bare", false) {
workDir, _ = getWorkTree(gitDir)
}
return &Repository{
gitDir: gitDir,
gitCommonDir: commonDir,
workDir: workDir,
gitConfig: gitConfig,
}, nil
}