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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
package git
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"srcd.works/go-git.v4/config"
"srcd.works/go-git.v4/plumbing"
"srcd.works/go-git.v4/plumbing/filemode"
"srcd.works/go-git.v4/plumbing/format/index"
"srcd.works/go-git.v4/plumbing/object"
"srcd.works/go-billy.v1"
)
var ErrWorktreeNotClean = errors.New("worktree is not clean")
var ErrSubmoduleNotFound = errors.New("submodule not found")
type Worktree struct {
r *Repository
fs billy.Filesystem
}
func (w *Worktree) Checkout(commit plumbing.Hash) error {
s, err := w.Status()
if err != nil {
return err
}
if !s.IsClean() {
return ErrWorktreeNotClean
}
c, err := w.r.Commit(commit)
if err != nil {
return err
}
t, err := c.Tree()
if err != nil {
return err
}
idx := &index.Index{Version: 2}
walker := object.NewTreeWalker(t, true)
for {
name, entry, err := walker.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if err := w.checkoutEntry(name, &entry, idx); err != nil {
return err
}
}
return w.r.Storer.SetIndex(idx)
}
func (w *Worktree) checkoutEntry(name string, e *object.TreeEntry, idx *index.Index) error {
if e.Mode == filemode.Submodule {
return w.addIndexFromTreeEntry(name, e, idx)
}
if e.Mode == filemode.Dir {
return nil
}
return w.checkoutFile(name, e, idx)
}
func (w *Worktree) checkoutFile(name string, e *object.TreeEntry, idx *index.Index) error {
blob, err := object.GetBlob(w.r.Storer, e.Hash)
if err != nil {
return err
}
from, err := blob.Reader()
if err != nil {
return err
}
defer from.Close()
mode, err := e.Mode.ToOSFileMode()
if err != nil {
return err
}
to, err := w.fs.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode.Perm())
if err != nil {
return err
}
defer to.Close()
if _, err := io.Copy(to, from); err != nil {
return err
}
return w.addIndexFromFile(name, e, idx)
}
var fillSystemInfo func(e *index.Entry, sys interface{})
func (w *Worktree) addIndexFromTreeEntry(name string, f *object.TreeEntry, idx *index.Index) error {
idx.Entries = append(idx.Entries, index.Entry{
Hash: f.Hash,
Name: name,
Mode: filemode.Submodule,
})
return nil
}
func (w *Worktree) addIndexFromFile(name string, f *object.TreeEntry, idx *index.Index) error {
fi, err := w.fs.Stat(name)
if err != nil {
return err
}
mode, err := filemode.NewFromOSFileMode(fi.Mode())
if err != nil {
return err
}
e := index.Entry{
Hash: f.Hash,
Name: name,
Mode: mode,
ModifiedAt: fi.ModTime(),
Size: uint32(fi.Size()),
}
// if the FileInfo.Sys() comes from os the ctime, dev, inode, uid and gid
// can be retrieved, otherwise this doesn't apply
if fillSystemInfo != nil {
fillSystemInfo(&e, fi.Sys())
}
idx.Entries = append(idx.Entries, e)
return nil
}
func (w *Worktree) Status() (Status, error) {
idx, err := w.r.Storer.Index()
if err != nil {
return nil, err
}
files, err := readDirAll(w.fs)
if err != nil {
return nil, err
}
s := make(Status, 0)
for _, e := range idx.Entries {
fi, ok := files[e.Name]
delete(files, e.Name)
if !ok {
s.File(e.Name).Worktree = Deleted
continue
}
status, err := w.compareFileWithEntry(fi, &e)
if err != nil {
return nil, err
}
s.File(e.Name).Worktree = status
}
for f := range files {
s.File(f).Worktree = Untracked
}
return s, nil
}
func (w *Worktree) compareFileWithEntry(fi billy.FileInfo, e *index.Entry) (StatusCode, error) {
if fi.Size() != int64(e.Size) {
return Modified, nil
}
mode, err := filemode.NewFromOSFileMode(fi.Mode())
if err != nil {
return Modified, err
}
if mode != e.Mode {
return Modified, nil
}
h, err := calcSHA1(w.fs, e.Name)
if h != e.Hash || err != nil {
return Modified, err
}
return Unmodified, nil
}
const gitmodulesFile = ".gitmodules"
// Submodule returns the submodule with the given name
func (w *Worktree) Submodule(name string) (*Submodule, error) {
l, err := w.Submodules()
if err != nil {
return nil, err
}
for _, m := range l {
if m.Config().Name == name {
return m, nil
}
}
return nil, ErrSubmoduleNotFound
}
// Submodules returns all the available submodules
func (w *Worktree) Submodules() (Submodules, error) {
l := make(Submodules, 0)
m, err := w.readGitmodulesFile()
if err != nil || m == nil {
return l, err
}
c, err := w.r.Config()
for _, s := range m.Submodules {
l = append(l, w.newSubmodule(s, c.Submodules[s.Name]))
}
return l, nil
}
func (w *Worktree) newSubmodule(fromModules, fromConfig *config.Submodule) *Submodule {
m := &Submodule{w: w}
m.initialized = fromConfig != nil
if !m.initialized {
m.c = fromModules
return m
}
m.c = fromConfig
m.c.Path = fromModules.Path
return m
}
func (w *Worktree) readGitmodulesFile() (*config.Modules, error) {
f, err := w.fs.Open(gitmodulesFile)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
input, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
m := config.NewModules()
return m, m.Unmarshal(input)
}
func (w *Worktree) readIndexEntry(path string) (index.Entry, error) {
var e index.Entry
idx, err := w.r.Storer.Index()
if err != nil {
return e, err
}
for _, e := range idx.Entries {
if e.Name == path {
return e, nil
}
}
return e, fmt.Errorf("unable to find %q entry in the index", path)
}
// Status current status of a Worktree
type Status map[string]*FileStatus
func (s Status) File(filename string) *FileStatus {
if _, ok := (s)[filename]; !ok {
s[filename] = &FileStatus{}
}
return s[filename]
}
func (s Status) IsClean() bool {
for _, status := range s {
if status.Worktree != Unmodified || status.Staging != Unmodified {
return false
}
}
return true
}
func (s Status) String() string {
var names []string
for name := range s {
names = append(names, name)
}
var output string
for _, name := range names {
status := s[name]
if status.Staging == 0 && status.Worktree == 0 {
continue
}
if status.Staging == Renamed {
name = fmt.Sprintf("%s -> %s", name, status.Extra)
}
output += fmt.Sprintf("%s%s %s\n", status.Staging, status.Worktree, name)
}
return output
}
// FileStatus status of a file in the Worktree
type FileStatus struct {
Staging StatusCode
Worktree StatusCode
Extra string
}
// StatusCode status code of a file in the Worktree
type StatusCode int8
const (
Unmodified StatusCode = iota
Untracked
Modified
Added
Deleted
Renamed
Copied
UpdatedButUnmerged
)
func (c StatusCode) String() string {
switch c {
case Unmodified:
return " "
case Modified:
return "M"
case Added:
return "A"
case Deleted:
return "D"
case Renamed:
return "R"
case Copied:
return "C"
case UpdatedButUnmerged:
return "U"
case Untracked:
return "?"
default:
return "-"
}
}
func calcSHA1(fs billy.Filesystem, filename string) (plumbing.Hash, error) {
file, err := fs.Open(filename)
if err != nil {
return plumbing.ZeroHash, err
}
stat, err := fs.Stat(filename)
if err != nil {
return plumbing.ZeroHash, err
}
h := plumbing.NewHasher(plumbing.BlobObject, stat.Size())
if _, err := io.Copy(h, file); err != nil {
return plumbing.ZeroHash, err
}
return h.Sum(), nil
}
func readDirAll(filesystem billy.Filesystem) (map[string]billy.FileInfo, error) {
all := make(map[string]billy.FileInfo, 0)
return all, doReadDirAll(filesystem, "", all)
}
func doReadDirAll(fs billy.Filesystem, path string, files map[string]billy.FileInfo) error {
if path == defaultDotGitPath {
return nil
}
l, err := fs.ReadDir(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
for _, info := range l {
file := fs.Join(path, info.Name())
if file == defaultDotGitPath {
continue
}
if !info.IsDir() {
files[file] = info
continue
}
if err := doReadDirAll(fs, file, files); err != nil {
return err
}
}
return nil
}
|