-
Notifications
You must be signed in to change notification settings - Fork 2
/
fs.go
71 lines (58 loc) · 1.23 KB
/
fs.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
package go4git
import (
"errors"
"os"
"path/filepath"
)
func fileExists(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
} else {
return false
}
}
func folderExists(path string) bool {
if s, err := os.Stat(path); err == nil {
return s.IsDir()
} else {
return false
}
}
func isRepo(root string) bool {
dotGit := filepath.Join(root, ".git")
return folderExists(dotGit)
}
func isBareRepo(root string) bool {
objFolder := filepath.Join(root, "objects")
headFile := filepath.Join(root, "HEAD")
return folderExists(objFolder) && fileExists(headFile)
}
func getGitDir(root string) (string, error) {
absRoot, err := filepath.Abs(root)
if err != nil {
return "", err
}
switch {
case isRepo(absRoot):
return filepath.Join(absRoot, ".git"), nil
case isBareRepo(absRoot):
return absRoot, nil
default:
return "", errors.New("not a git repository")
}
}
func createFolders(root string) error {
var folders = []string{
"branches", "hooks", "info",
"objects", "objects/info", "objects/pack",
"refs", "refs/heads", "refs/tags",
}
for _, f := range folders {
folderPath := filepath.Join(root, f)
err := os.MkdirAll(folderPath, os.ModePerm)
if err != nil {
return err
}
}
return nil
}