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
|
package object
import (
"sort"
"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/storer"
"gopkg.in/src-d/go-git.v4/storage/filesystem"
"gopkg.in/src-d/go-git.v4/utils/merkletrie"
fixtures "github.com/src-d/go-git-fixtures"
. "gopkg.in/check.v1"
)
type ChangeSuite struct {
fixtures.Suite
Storer storer.EncodedObjectStorer
Fixture *fixtures.Fixture
}
func (s *ChangeSuite) SetUpSuite(c *C) {
s.Suite.SetUpSuite(c)
s.Fixture = fixtures.ByURL("https://github.com/src-d/go-git.git").
ByTag(".git").One()
sto, err := filesystem.NewStorage(s.Fixture.DotGit())
c.Assert(err, IsNil)
s.Storer = sto
}
func (s *ChangeSuite) tree(c *C, h plumbing.Hash) *Tree {
t, err := GetTree(s.Storer, h)
c.Assert(err, IsNil)
return t
}
var _ = Suite(&ChangeSuite{})
func (s *ChangeSuite) TestInsert(c *C) {
// Commit a5078b19f08f63e7948abd0a5e2fb7d319d3a565 of the go-git
// fixture inserted "examples/clone/main.go".
//
// On that commit, the "examples/clone" tree is
// 6efca3ff41cab651332f9ebc0c96bb26be809615
//
// and the "examples/colone/main.go" is
// f95dc8f7923add1a8b9f72ecb1e8db1402de601a
path := "examples/clone/main.go"
name := "main.go"
mode := filemode.Regular
blob := plumbing.NewHash("f95dc8f7923add1a8b9f72ecb1e8db1402de601a")
tree := plumbing.NewHash("6efca3ff41cab651332f9ebc0c96bb26be809615")
change := &Change{
From: empty,
To: ChangeEntry{
Name: path,
Tree: s.tree(c, tree),
TreeEntry: TreeEntry{
Name: name,
Mode: mode,
Hash: blob,
},
},
}
action, err := change.Action()
c.Assert(err, IsNil)
c.Assert(action, Equals, merkletrie.Insert)
from, to, err := change.Files()
c.Assert(err, IsNil)
c.Assert(from, IsNil)
c.Assert(to.Name, Equals, name)
c.Assert(to.Blob.Hash, Equals, blob)
str := change.String()
c.Assert(str, Equals, "<Action: Insert, Path: examples/clone/main.go>")
}
func (s *ChangeSuite) TestDelete(c *C) {
// Commit f6011d65d57c2a866e231fc21a39cb618f86f9ea of the go-git
// fixture deleted "utils/difftree/difftree.go".
//
// The parent of that commit is
// 9b4a386db3d98a4362516a00ef3d04d4698c9bcd.
//
// On that parent commit, the "utils/difftree" tree is
// f3d11566401ce4b0808aab9dd6fad3d5abf1481a.
//
// and the "utils/difftree/difftree.go" is
// e2cb9a5719daf634d45a063112b4044ee81da13ea.
path := "utils/difftree/difftree.go"
name := "difftree.go"
mode := filemode.Regular
blob := plumbing.NewHash("e2cb9a5719daf634d45a063112b4044ee81da13e")
tree := plumbing.NewHash("f3d11566401ce4b0808aab9dd6fad3d5abf1481a")
change := &Change{
From: ChangeEntry{
Name: path,
Tree: s.tree(c, tree),
TreeEntry: TreeEntry{
Name: name,
Mode: mode,
Hash: blob,
},
},
To: empty,
}
action, err := change.Action()
c.Assert(err, IsNil)
c.Assert(action, Equals, merkletrie.Delete)
from, to, err := change.Files()
c.Assert(err, IsNil)
c.Assert(to, IsNil)
c.Assert(from.Name, Equals, name)
c.Assert(from.Blob.Hash, Equals, blob)
str := change.String()
c.Assert(str, Equals, "<Action: Delete, Path: utils/difftree/difftree.go>")
}
func (s *ChangeSuite) TestModify(c *C) {
// Commit 7beaad711378a4daafccc2c04bc46d36df2a0fd1 of the go-git
// fixture modified "examples/latest/latest.go".
// the "examples/latest" tree is
// b1f01b730b855c82431918cb338ad47ed558999b.
// and "examples/latest/latest.go" is blob
// 05f583ace3a9a078d8150905a53a4d82567f125f.
//
// The parent of that commit is
// 337148ef6d751477796922ac127b416b8478fcc4.
// the "examples/latest" tree is
// 8b0af31d2544acb5c4f3816a602f11418cbd126e.
// and "examples/latest/latest.go" is blob
// de927fad935d172929aacf20e71f3bf0b91dd6f9.
path := "utils/difftree/difftree.go"
name := "difftree.go"
mode := filemode.Regular
fromBlob := plumbing.NewHash("05f583ace3a9a078d8150905a53a4d82567f125f")
fromTree := plumbing.NewHash("b1f01b730b855c82431918cb338ad47ed558999b")
toBlob := plumbing.NewHash("de927fad935d172929aacf20e71f3bf0b91dd6f9")
toTree := plumbing.NewHash("8b0af31d2544acb5c4f3816a602f11418cbd126e")
change := &Change{
From: ChangeEntry{
Name: path,
Tree: s.tree(c, fromTree),
TreeEntry: TreeEntry{
Name: name,
Mode: mode,
Hash: fromBlob,
},
},
To: ChangeEntry{
Name: path,
Tree: s.tree(c, toTree),
TreeEntry: TreeEntry{
Name: name,
Mode: mode,
Hash: toBlob,
},
},
}
action, err := change.Action()
c.Assert(err, IsNil)
c.Assert(action, Equals, merkletrie.Modify)
from, to, err := change.Files()
c.Assert(err, IsNil)
c.Assert(from.Name, Equals, name)
c.Assert(from.Blob.Hash, Equals, fromBlob)
c.Assert(to.Name, Equals, name)
c.Assert(to.Blob.Hash, Equals, toBlob)
str := change.String()
c.Assert(str, Equals, "<Action: Modify, Path: utils/difftree/difftree.go>")
}
func (s *ChangeSuite) TestEmptyChangeFails(c *C) {
change := &Change{}
_, err := change.Action()
c.Assert(err, ErrorMatches, "malformed.*")
_, _, err = change.Files()
c.Assert(err, ErrorMatches, "malformed.*")
str := change.String()
c.Assert(str, Equals, "malformed change")
}
// test reproducing bug #317
func (s *ChangeSuite) TestNoFileFilemodes(c *C) {
s.Suite.SetUpSuite(c)
f := fixtures.ByURL("https://github.com/git-fixtures/submodule.git").One()
sto, err := filesystem.NewStorage(f.DotGit())
c.Assert(err, IsNil)
iter, err := sto.IterEncodedObjects(plumbing.AnyObject)
c.Assert(err, IsNil)
var commits []*Commit
iter.ForEach(func(o plumbing.EncodedObject) error {
if o.Type() == plumbing.CommitObject {
commit, err := GetCommit(sto, o.Hash())
c.Assert(err, IsNil)
commits = append(commits, commit)
}
return nil
})
c.Assert(len(commits), Not(Equals), 0)
var prev *Commit
for _, commit := range commits {
if prev == nil {
prev = commit
continue
}
tree, err := commit.Tree()
c.Assert(err, IsNil)
prevTree, err := prev.Tree()
c.Assert(err, IsNil)
changes, err := DiffTree(tree, prevTree)
c.Assert(err, IsNil)
for _, change := range changes {
_, _, err := change.Files()
if err != nil {
panic(err)
c.Assert(err, IsNil)
}
}
prev = commit
}
}
func (s *ChangeSuite) TestErrorsFindingChildsAreDetected(c *C) {
// Commit 7beaad711378a4daafccc2c04bc46d36df2a0fd1 of the go-git
// fixture modified "examples/latest/latest.go".
// the "examples/latest" tree is
// b1f01b730b855c82431918cb338ad47ed558999b.
// and "examples/latest/latest.go" is blob
// 05f583ace3a9a078d8150905a53a4d82567f125f.
//
// The parent of that commit is
// 337148ef6d751477796922ac127b416b8478fcc4.
// the "examples/latest" tree is
// 8b0af31d2544acb5c4f3816a602f11418cbd126e.
// and "examples/latest/latest.go" is blob
// de927fad935d172929aacf20e71f3bf0b91dd6f9.
path := "utils/difftree/difftree.go"
name := "difftree.go"
mode := filemode.Regular
fromBlob := plumbing.NewHash("aaaa") // does not exists
fromTree := plumbing.NewHash("b1f01b730b855c82431918cb338ad47ed558999b")
toBlob := plumbing.NewHash("bbbb") // does not exists
toTree := plumbing.NewHash("8b0af31d2544acb5c4f3816a602f11418cbd126e")
change := &Change{
From: ChangeEntry{
Name: path,
Tree: s.tree(c, fromTree),
TreeEntry: TreeEntry{
Name: name,
Mode: mode,
Hash: fromBlob,
},
},
To: ChangeEntry{},
}
_, _, err := change.Files()
c.Assert(err, ErrorMatches, "object not found")
change = &Change{
From: empty,
To: ChangeEntry{
Name: path,
Tree: s.tree(c, toTree),
TreeEntry: TreeEntry{
Name: name,
Mode: mode,
Hash: toBlob,
},
},
}
_, _, err = change.Files()
c.Assert(err, ErrorMatches, "object not found")
}
func (s *ChangeSuite) TestChangesString(c *C) {
expected := "[]"
changes := Changes{}
obtained := changes.String()
c.Assert(obtained, Equals, expected)
expected = "[<Action: Modify, Path: bla>]"
changes = make([]*Change, 1)
changes[0] = &Change{}
changes[0].From.Name = "bla"
changes[0].To.Name = "bla"
obtained = changes.String()
c.Assert(obtained, Equals, expected)
expected = "[<Action: Modify, Path: bla>, <Action: Delete, Path: foo/bar>]"
changes = make([]*Change, 2)
changes[0] = &Change{}
changes[0].From.Name = "bla"
changes[0].To.Name = "bla"
changes[1] = &Change{}
changes[1].From.Name = "foo/bar"
obtained = changes.String()
c.Assert(obtained, Equals, expected)
}
func (s *ChangeSuite) TestChangesSort(c *C) {
changes := make(Changes, 3)
changes[0] = &Change{}
changes[0].From.Name = "z"
changes[0].To.Name = "z"
changes[1] = &Change{}
changes[1].From.Name = "b/b"
changes[2] = &Change{}
changes[2].To.Name = "b/a"
expected := "[<Action: Insert, Path: b/a>, " +
"<Action: Delete, Path: b/b>, " +
"<Action: Modify, Path: z>]"
sort.Sort(changes)
c.Assert(changes.String(), Equals, expected)
}
|