-
Notifications
You must be signed in to change notification settings - Fork 0
/
workspace.go
80 lines (66 loc) · 1.47 KB
/
workspace.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
package core
import (
"io"
"log"
"os"
"path/filepath"
"sync"
"github.com/izhujiang/gogit/core/internal/utils"
)
// workspace include working area, staging area and git repository
const (
repositoryName = ".git"
repositoryRoot = ".git"
)
type Workspace struct {
repository *Repository
stageingArea *StagingArea
workingArea *WorkingArea
references *References
}
var lock = &sync.Mutex{}
var singleInstance *Workspace
func GetWorkspace() (*Workspace, error) {
if singleInstance == nil {
lock.Lock()
defer lock.Unlock()
if singleInstance == nil {
singleInstance = &Workspace{
repository: &Repository{Name: repositoryName, Path: repositoryRoot},
stageingArea: &StagingArea{
path: filepath.Join(repositoryRoot, "index"),
},
workingArea: &WorkingArea{},
references: &References{
root: repositoryRoot,
headpath: filepath.Join(repositoryRoot, "HEAD"),
},
}
}
}
return singleInstance, nil
}
// TODO: log and return err
func (ws *Workspace) InitWorkspace(w io.Writer, root string) {
if root == "" {
root = "./"
} else {
os.MkdirAll(root, 0755)
}
ws.repository.InitRepository(w, root)
}
func GetRepository() *Repository {
w, _ := GetWorkspace()
if !utils.DirectoryExists(w.repository.Path) {
log.Fatal(errRepositoryNotExists)
}
return w.repository
}
func GetStagingArea() *StagingArea {
w, _ := GetWorkspace()
return w.stageingArea
}
func GetReferencs() *References {
w, _ := GetWorkspace()
return w.references
}