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
|
package git
import (
"bytes"
"io"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
"gopkg.in/src-d/go-git.v4/plumbing/format/index"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/utils/ioutil"
"gopkg.in/src-d/go-git.v4/utils/merkletrie"
"gopkg.in/src-d/go-git.v4/utils/merkletrie/filesystem"
mindex "gopkg.in/src-d/go-git.v4/utils/merkletrie/index"
"gopkg.in/src-d/go-git.v4/utils/merkletrie/noder"
)
// Status returns the working tree status.
func (w *Worktree) Status() (Status, error) {
ref, err := w.r.Head()
if err == plumbing.ErrReferenceNotFound {
return make(Status, 0), nil
}
if err != nil {
return nil, err
}
return w.status(ref.Hash())
}
func (w *Worktree) status(commit plumbing.Hash) (Status, error) {
s := make(Status, 0)
left, err := w.diffCommitWithStaging(commit, false)
if err != nil {
return nil, err
}
for _, ch := range left {
a, err := ch.Action()
if err != nil {
return nil, err
}
fs := s.File(nameFromAction(&ch))
fs.Worktree = Unmodified
switch a {
case merkletrie.Delete:
s.File(ch.From.String()).Staging = Deleted
case merkletrie.Insert:
s.File(ch.To.String()).Staging = Added
case merkletrie.Modify:
s.File(ch.To.String()).Staging = Modified
}
}
right, err := w.diffStagingWithWorktree()
if err != nil {
return nil, err
}
for _, ch := range right {
a, err := ch.Action()
if err != nil {
return nil, err
}
fs := s.File(nameFromAction(&ch))
if fs.Staging == Untracked {
fs.Staging = Unmodified
}
switch a {
case merkletrie.Delete:
fs.Worktree = Deleted
case merkletrie.Insert:
fs.Worktree = Untracked
fs.Staging = Untracked
case merkletrie.Modify:
fs.Worktree = Modified
}
}
return s, nil
}
func nameFromAction(ch *merkletrie.Change) string {
name := ch.To.String()
if name == "" {
return ch.From.String()
}
return name
}
func (w *Worktree) diffStagingWithWorktree() (merkletrie.Changes, error) {
idx, err := w.r.Storer.Index()
if err != nil {
return nil, err
}
from := mindex.NewRootNode(idx)
submodules, err := w.getSubmodulesStatus()
if err != nil {
return nil, err
}
to := filesystem.NewRootNode(w.fs, submodules)
return merkletrie.DiffTree(from, to, diffTreeIsEquals)
}
func (w *Worktree) getSubmodulesStatus() (map[string]plumbing.Hash, error) {
o := map[string]plumbing.Hash{}
sub, err := w.Submodules()
if err != nil {
return nil, err
}
status, err := sub.Status()
if err != nil {
return nil, err
}
for _, s := range status {
if s.Current.IsZero() {
o[s.Path] = s.Expected
continue
}
o[s.Path] = s.Current
}
return o, nil
}
func (w *Worktree) diffCommitWithStaging(commit plumbing.Hash, reverse bool) (merkletrie.Changes, error) {
idx, err := w.r.Storer.Index()
if err != nil {
return nil, err
}
c, err := w.r.CommitObject(commit)
if err != nil {
return nil, err
}
t, err := c.Tree()
if err != nil {
return nil, err
}
to := mindex.NewRootNode(idx)
from := object.NewTreeRootNode(t)
if reverse {
return merkletrie.DiffTree(to, from, diffTreeIsEquals)
}
return merkletrie.DiffTree(from, to, diffTreeIsEquals)
}
var emptyNoderHash = make([]byte, 24)
// diffTreeIsEquals is a implementation of noder.Equals, used to compare
// noder.Noder, it compare the content and the length of the hashes.
//
// Since some of the noder.Noder implementations doesn't compute a hash for
// some directories, if any of the hashes is a 24-byte slice of zero values
// the comparison is not done and the hashes are take as different.
func diffTreeIsEquals(a, b noder.Hasher) bool {
hashA := a.Hash()
hashB := b.Hash()
if bytes.Equal(hashA, emptyNoderHash) || bytes.Equal(hashB, emptyNoderHash) {
return false
}
return bytes.Equal(hashA, hashB)
}
// Add adds the file contents of a file in the worktree to the index. if the
// file is already stagged in the index no error is returned.
func (w *Worktree) Add(path string) (plumbing.Hash, error) {
s, err := w.Status()
if err != nil {
return plumbing.ZeroHash, err
}
h, err := w.calculateBlobHash(path)
if err != nil {
return h, err
}
if s.File(path).Worktree == Unmodified {
return h, nil
}
if err := w.addOrUpdateFileToIndex(path, h); err != nil {
return h, err
}
return h, err
}
func (w *Worktree) calculateBlobHash(filename string) (hash plumbing.Hash, err error) {
fi, err := w.fs.Stat(filename)
if err != nil {
return plumbing.ZeroHash, err
}
f, err := w.fs.Open(filename)
if err != nil {
return plumbing.ZeroHash, err
}
defer ioutil.CheckClose(f, &err)
h := plumbing.NewHasher(plumbing.BlobObject, fi.Size())
if _, err := io.Copy(h, f); err != nil {
return plumbing.ZeroHash, err
}
hash = h.Sum()
return
}
func (w *Worktree) addOrUpdateFileToIndex(filename string, h plumbing.Hash) error {
idx, err := w.r.Storer.Index()
if err != nil {
return err
}
_, err = idx.Entry(filename)
if err == index.ErrEntryNotFound {
err = w.doAddFileToIndex(idx, filename)
}
if err != nil {
return err
}
err = w.doUpdateFileToIndex(idx, filename, h)
if err != nil {
return err
}
return w.r.Storer.SetIndex(idx)
}
func (w *Worktree) doAddFileToIndex(idx *index.Index, filename string) error {
idx.Entries = append(idx.Entries, &index.Entry{
Name: filename,
})
return nil
}
func (w *Worktree) doUpdateFileToIndex(idx *index.Index, filename string, h plumbing.Hash) error {
info, err := w.fs.Stat(filename)
if err != nil {
return err
}
e, err := idx.Entry(filename)
if err != nil {
return err
}
e.Hash = h
e.ModifiedAt = info.ModTime()
e.Mode, err = filemode.NewFromOSFileMode(info.Mode())
if err != nil {
return err
}
fillSystemInfo(e, info.Sys())
return nil
}
|