-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlit.go
103 lines (79 loc) · 1.8 KB
/
lit.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
package main
import (
"errors"
"os"
"path/filepath"
)
type Lit struct {
config *LitConfig
logger Logger
}
type LitConfig struct {
DefaultBranchName string
HeadPath string
IgnoreFileName string
RootDir string
ObjectsDir string
}
type Logger interface {
Debug(v ...interface{})
Info(v ...interface{})
Warn(v ...interface{})
Error(v ...interface{})
Fatal(v ...interface{})
}
var (
RepositoryNotInitialized = errors.New("fatal: not a lit repository (or any of the parent directories)")
NotValidObjectName = func(name string) error {
return errors.New("fatal: Not a valid object name: " + "'" + name + "'.")
}
)
func NewLit(logger Logger) *Lit {
return &Lit{
config: &LitConfig{
DefaultBranchName: "master",
HeadPath: "HEAD",
IgnoreFileName: ".litignore",
RootDir: ".lit",
ObjectsDir: "objects",
},
logger: logger,
}
}
// Root finds the closest parent of the lit directory (.lit)
// from the CWD
func (lit *Lit) Root() (string, error) {
litDir, err := lit.LitDir()
if err != nil {
return "", err
}
return filepath.Dir(litDir), nil
}
// LitDir finds the closest lit directory (.lit) from the CWD
func (lit *Lit) LitDir() (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
for {
if lit.isInitializedIn(cwd) {
return filepath.Join(cwd, lit.config.RootDir), nil
}
prev := filepath.Dir(cwd)
if cwd == prev {
return "", RepositoryNotInitialized
}
cwd = prev
}
}
func (lit *Lit) isInitializedIn(path string) bool {
_, err := os.Stat(filepath.Join(path, lit.config.RootDir))
return !os.IsNotExist(err)
}
func (lit *Lit) objectsDir() string {
litDir, err := lit.LitDir()
if err != nil {
lit.logger.Fatal(err)
}
return filepath.Join(litDir, lit.config.ObjectsDir)
}