-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
151 lines (124 loc) · 3.09 KB
/
init.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
package gitdb
import (
"errors"
"fmt"
"os"
"path/filepath"
"sync"
)
var mu sync.Mutex
var conns map[string]GitDb
//Open opens a connection to GitDB
func Open(config *Config) (GitDb, error) {
//clone config to avoid changes from outside of package
x := *config
cfg := &x
if err := cfg.Validate(); err != nil {
return nil, err
}
conn := newConnection()
conn.configure(cfg)
err := conn.boot()
logMsg := "Db booted fine"
if err != nil {
logMsg = fmt.Sprintf("Db booted with errors - %s", err)
}
log(logMsg)
if err != nil {
return nil, err
}
//if boot() returned an error do not start event loop
if !conn.loopStarted {
conn.startEventLoop()
conn.startSyncClock()
conn.startUI()
conn.loopStarted = true
}
if conns == nil {
conns = make(map[string]GitDb)
}
conns[cfg.ConnectionName] = conn
return conn, nil
}
//Conn returns the last connection started by Open(*Config)
// if you opened more than one connection use GetConn(name) instead
func Conn() GitDb {
if len(conns) > 1 {
panic("Multiple gitdb connections found. Use GetConn function instead")
}
if len(conns) == 0 {
panic("No open gitdb connections found")
}
var connName string
for k := range conns {
connName = k
break
}
if _, ok := conns[connName]; !ok {
panic("No gitdb connection found - " + connName)
}
return conns[connName]
}
//GetConn returns a specific gitdb connection by name
func GetConn(name string) GitDb {
if _, ok := conns[name]; !ok {
panic("No gitdb connection found")
}
return conns[name]
}
func (g *gitdb) boot() error {
g.lastIds = make(map[string]int64)
log("Booting up db using " + g.config.GitDriver.name() + " driver")
//create .ssh dir
err := g.generateSSHKeyPair()
if err != nil {
return err
}
os.Setenv("GIT_SSH_COMMAND", fmt.Sprintf("ssh -i '%s' -o 'StrictHostKeyChecking no'", g.privateKeyFilePath()))
// if .db directory does not exist and create it and attempt
// to do a git pull from remote
dataDir := g.dbDir()
dotGitDir := filepath.Join(dataDir, ".git")
if _, err := os.Stat(dataDir); err != nil {
log("database not initialized")
//create db directory
err = os.MkdirAll(dataDir, 0755)
if err != nil {
return err
}
if len(g.config.OnlineRemote) > 0 {
err = g.gitClone()
if err != nil {
return err
}
err = g.gitAddRemote()
if err != nil {
return err
}
} else {
err = g.gitInit()
if err != nil {
return err
}
}
} else if _, err := os.Stat(dotGitDir); err != nil {
log(err.Error())
return errors.New(g.config.DbPath + " is not a git repository")
} else if len(g.config.OnlineRemote) > 0 { //TODO Review this properly
//if remote is configured i.e stat .git/refs/remotes/online
//if remote dir does not exist add remotes
remotesPath := filepath.Join(dataDir, ".git", "refs", "remotes", "online")
if _, err := os.Stat(remotesPath); err != nil {
err = g.gitAddRemote()
if err != nil {
return err
}
}
}
//rebuild index if we have to
if _, err := os.Stat(g.indexDir()); err != nil {
//no index directory found so we need to re-index the whole db
go g.buildIndex()
}
return nil
}