Debian Codegraph @

plumbing/object/change_test.go

raw
1 package object
2
3 import (
4 "context"
5 "sort"
6
7 fixtures "github.com/go-git/go-git-fixtures/v4"
8 "github.com/go-git/go-git/v5/plumbing"
9 "github.com/go-git/go-git/v5/plumbing/cache"
10 "github.com/go-git/go-git/v5/plumbing/filemode"
11 "github.com/go-git/go-git/v5/plumbing/format/diff"
12 "github.com/go-git/go-git/v5/plumbing/storer"
13 "github.com/go-git/go-git/v5/storage/filesystem"
14 "github.com/go-git/go-git/v5/utils/merkletrie"
15
16 . "gopkg.in/check.v1"
17 )
18
19 type ChangeSuite struct {
20 fixtures.Suite
21 Storer storer.EncodedObjectStorer
22 Fixture *fixtures.Fixture
23 }
24
25 func (s *ChangeSuite) SetUpSuite(c *C) {
26 s.Fixture = fixtures.ByURL("https://github.com/src-d/go-git.git").
27 ByTag(".git").One()
28 sto := filesystem.NewStorage(s.Fixture.DotGit(), cache.NewObjectLRUDefault())
29 s.Storer = sto
30 }
31
32 func (s *ChangeSuite) tree(c *C, h plumbing.Hash) *Tree {
33 t, err := GetTree(s.Storer, h)
34 c.Assert(err, IsNil)
35 return t
36 }
37
38 var _ = Suite(&ChangeSuite{})
39
40 func (s *ChangeSuite) TestInsert(c *C) {
41 // Commit a5078b19f08f63e7948abd0a5e2fb7d319d3a565 of the go-git
42 // fixture inserted "examples/clone/main.go".
43 //
44 // On that commit, the "examples/clone" tree is
45 // 6efca3ff41cab651332f9ebc0c96bb26be809615
46 //
47 // and the "examples/colone/main.go" is
48 // f95dc8f7923add1a8b9f72ecb1e8db1402de601a
49
50 path := "examples/clone/main.go"
51 name := "main.go"
52 mode := filemode.Regular
53 blob := plumbing.NewHash("f95dc8f7923add1a8b9f72ecb1e8db1402de601a")
54 tree := plumbing.NewHash("6efca3ff41cab651332f9ebc0c96bb26be809615")
55
56 change := &Change{
57 From: empty,
58 To: ChangeEntry{
59 Name: path,
60 Tree: s.tree(c, tree),
61 TreeEntry: TreeEntry{
62 Name: name,
63 Mode: mode,
64 Hash: blob,
65 },
66 },
67 }
68
69 action, err := change.Action()
70 c.Assert(err, IsNil)
71 c.Assert(action, Equals, merkletrie.Insert)
72
73 from, to, err := change.Files()
74 c.Assert(err, IsNil)
75 c.Assert(from, IsNil)
76 c.Assert(to.Name, Equals, name)
77 c.Assert(to.Blob.Hash, Equals, blob)
78
79 p, err := change.Patch()
80 c.Assert(err, IsNil)
81 c.Assert(len(p.FilePatches()), Equals, 1)
82 c.Assert(len(p.FilePatches()[0].Chunks()), Equals, 1)
83 c.Assert(p.FilePatches()[0].Chunks()[0].Type(), Equals, diff.Add)
84
85 p, err = change.PatchContext(context.Background())
86 c.Assert(err, IsNil)
87 c.Assert(len(p.FilePatches()), Equals, 1)
88 c.Assert(len(p.FilePatches()[0].Chunks()), Equals, 1)
89 c.Assert(p.FilePatches()[0].Chunks()[0].Type(), Equals, diff.Add)
90
91 str := change.String()
92 c.Assert(str, Equals, "<Action: Insert, Path: examples/clone/main.go>")
93 }
94
95 func (s *ChangeSuite) TestDelete(c *C) {
96 // Commit f6011d65d57c2a866e231fc21a39cb618f86f9ea of the go-git
97 // fixture deleted "utils/difftree/difftree.go".
98 //
99 // The parent of that commit is
100 // 9b4a386db3d98a4362516a00ef3d04d4698c9bcd.
101 //
102 // On that parent commit, the "utils/difftree" tree is
103 // f3d11566401ce4b0808aab9dd6fad3d5abf1481a.
104 //
105 // and the "utils/difftree/difftree.go" is
106 // e2cb9a5719daf634d45a063112b4044ee81da13ea.
107
108 path := "utils/difftree/difftree.go"
109 name := "difftree.go"
110 mode := filemode.Regular
111 blob := plumbing.NewHash("e2cb9a5719daf634d45a063112b4044ee81da13e")
112 tree := plumbing.NewHash("f3d11566401ce4b0808aab9dd6fad3d5abf1481a")
113
114 change := &Change{
115 From: ChangeEntry{
116 Name: path,
117 Tree: s.tree(c, tree),
118 TreeEntry: TreeEntry{
119 Name: name,
120 Mode: mode,
121 Hash: blob,
122 },
123 },
124 To: empty,
125 }
126
127 action, err := change.Action()
128 c.Assert(err, IsNil)
129 c.Assert(action, Equals, merkletrie.Delete)
130
131 from, to, err := change.Files()
132 c.Assert(err, IsNil)
133 c.Assert(to, IsNil)
134 c.Assert(from.Name, Equals, name)
135 c.Assert(from.Blob.Hash, Equals, blob)
136
137 p, err := change.Patch()
138 c.Assert(err, IsNil)
139 c.Assert(len(p.FilePatches()), Equals, 1)
140 c.Assert(len(p.FilePatches()[0].Chunks()), Equals, 1)
141 c.Assert(p.FilePatches()[0].Chunks()[0].Type(), Equals, diff.Delete)
142
143 p, err = change.PatchContext(context.Background())
144 c.Assert(err, IsNil)
145 c.Assert(len(p.FilePatches()), Equals, 1)
146 c.Assert(len(p.FilePatches()[0].Chunks()), Equals, 1)
147 c.Assert(p.FilePatches()[0].Chunks()[0].Type(), Equals, diff.Delete)
148
149 str := change.String()
150 c.Assert(str, Equals, "<Action: Delete, Path: utils/difftree/difftree.go>")
151 }
152
153 func (s *ChangeSuite) TestModify(c *C) {
154 // Commit 7beaad711378a4daafccc2c04bc46d36df2a0fd1 of the go-git
155 // fixture modified "examples/latest/latest.go".
156 // the "examples/latest" tree is
157 // b1f01b730b855c82431918cb338ad47ed558999b.
158 // and "examples/latest/latest.go" is blob
159 // 05f583ace3a9a078d8150905a53a4d82567f125f.
160 //
161 // The parent of that commit is
162 // 337148ef6d751477796922ac127b416b8478fcc4.
163 // the "examples/latest" tree is
164 // 8b0af31d2544acb5c4f3816a602f11418cbd126e.
165 // and "examples/latest/latest.go" is blob
166 // de927fad935d172929aacf20e71f3bf0b91dd6f9.
167
168 path := "utils/difftree/difftree.go"
169 name := "difftree.go"
170 mode := filemode.Regular
171 fromBlob := plumbing.NewHash("05f583ace3a9a078d8150905a53a4d82567f125f")
172 fromTree := plumbing.NewHash("b1f01b730b855c82431918cb338ad47ed558999b")
173 toBlob := plumbing.NewHash("de927fad935d172929aacf20e71f3bf0b91dd6f9")
174 toTree := plumbing.NewHash("8b0af31d2544acb5c4f3816a602f11418cbd126e")
175
176 change := &Change{
177 From: ChangeEntry{
178 Name: path,
179 Tree: s.tree(c, fromTree),
180 TreeEntry: TreeEntry{
181 Name: name,
182 Mode: mode,
183 Hash: fromBlob,
184 },
185 },
186 To: ChangeEntry{
187 Name: path,
188 Tree: s.tree(c, toTree),
189 TreeEntry: TreeEntry{
190 Name: name,
191 Mode: mode,
192 Hash: toBlob,
193 },
194 },
195 }
196
197 action, err := change.Action()
198 c.Assert(err, IsNil)
199 c.Assert(action, Equals, merkletrie.Modify)
200
201 from, to, err := change.Files()
202 c.Assert(err, IsNil)
203
204 c.Assert(from.Name, Equals, name)
205 c.Assert(from.Blob.Hash, Equals, fromBlob)
206 c.Assert(to.Name, Equals, name)
207 c.Assert(to.Blob.Hash, Equals, toBlob)
208
209 p, err := change.Patch()
210 c.Assert(err, IsNil)
211 c.Assert(len(p.FilePatches()), Equals, 1)
212 c.Assert(len(p.FilePatches()[0].Chunks()), Equals, 7)
213 c.Assert(p.FilePatches()[0].Chunks()[0].Type(), Equals, diff.Equal)
214 c.Assert(p.FilePatches()[0].Chunks()[1].Type(), Equals, diff.Delete)
215 c.Assert(p.FilePatches()[0].Chunks()[2].Type(), Equals, diff.Add)
216 c.Assert(p.FilePatches()[0].Chunks()[3].Type(), Equals, diff.Equal)
217 c.Assert(p.FilePatches()[0].Chunks()[4].Type(), Equals, diff.Delete)
218 c.Assert(p.FilePatches()[0].Chunks()[5].Type(), Equals, diff.Add)
219 c.Assert(p.FilePatches()[0].Chunks()[6].Type(), Equals, diff.Equal)
220
221 p, err = change.PatchContext(context.Background())
222 c.Assert(err, IsNil)
223 c.Assert(len(p.FilePatches()), Equals, 1)
224 c.Assert(len(p.FilePatches()[0].Chunks()), Equals, 7)
225 c.Assert(p.FilePatches()[0].Chunks()[0].Type(), Equals, diff.Equal)
226 c.Assert(p.FilePatches()[0].Chunks()[1].Type(), Equals, diff.Delete)
227 c.Assert(p.FilePatches()[0].Chunks()[2].Type(), Equals, diff.Add)
228 c.Assert(p.FilePatches()[0].Chunks()[3].Type(), Equals, diff.Equal)
229 c.Assert(p.FilePatches()[0].Chunks()[4].Type(), Equals, diff.Delete)
230 c.Assert(p.FilePatches()[0].Chunks()[5].Type(), Equals, diff.Add)
231 c.Assert(p.FilePatches()[0].Chunks()[6].Type(), Equals, diff.Equal)
232
233 str := change.String()
234 c.Assert(str, Equals, "<Action: Modify, Path: utils/difftree/difftree.go>")
235 }
236
237 func (s *ChangeSuite) TestEmptyChangeFails(c *C) {
238 change := &Change{}
239
240 _, err := change.Action()
241 c.Assert(err, ErrorMatches, "malformed.*")
242
243 _, _, err = change.Files()
244 c.Assert(err, ErrorMatches, "malformed.*")
245
246 str := change.String()
247 c.Assert(str, Equals, "malformed change")
248 }
249
250 // test reproducing bug #317
251 func (s *ChangeSuite) TestNoFileFilemodes(c *C) {
252 f := fixtures.ByURL("https://github.com/git-fixtures/submodule.git").One()
253
254 sto := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault())
255
256 iter, err := sto.IterEncodedObjects(plumbing.AnyObject)
257 c.Assert(err, IsNil)
258 var commits []*Commit
259 iter.ForEach(func(o plumbing.EncodedObject) error {
260 if o.Type() == plumbing.CommitObject {
261 commit, err := GetCommit(sto, o.Hash())
262 c.Assert(err, IsNil)
263 commits = append(commits, commit)
264
265 }
266
267 return nil
268 })
269
270 c.Assert(len(commits), Not(Equals), 0)
271
272 var prev *Commit
273 for _, commit := range commits {
274 if prev == nil {
275 prev = commit
276 continue
277 }
278 tree, err := commit.Tree()
279 c.Assert(err, IsNil)
280 prevTree, err := prev.Tree()
281 c.Assert(err, IsNil)
282 changes, err := DiffTree(tree, prevTree)
283 c.Assert(err, IsNil)
284 for _, change := range changes {
285 _, _, err := change.Files()
286 c.Assert(err, IsNil)
287 }
288
289 prev = commit
290 }
291 }
292
293 func (s *ChangeSuite) TestErrorsFindingChildsAreDetected(c *C) {
294 // Commit 7beaad711378a4daafccc2c04bc46d36df2a0fd1 of the go-git
295 // fixture modified "examples/latest/latest.go".
296 // the "examples/latest" tree is
297 // b1f01b730b855c82431918cb338ad47ed558999b.
298 // and "examples/latest/latest.go" is blob
299 // 05f583ace3a9a078d8150905a53a4d82567f125f.
300 //
301 // The parent of that commit is
302 // 337148ef6d751477796922ac127b416b8478fcc4.
303 // the "examples/latest" tree is
304 // 8b0af31d2544acb5c4f3816a602f11418cbd126e.
305 // and "examples/latest/latest.go" is blob
306 // de927fad935d172929aacf20e71f3bf0b91dd6f9.
307
308 path := "utils/difftree/difftree.go"
309 name := "difftree.go"
310 mode := filemode.Regular
311 fromBlob := plumbing.NewHash("aaaa") // does not exists
312 fromTree := plumbing.NewHash("b1f01b730b855c82431918cb338ad47ed558999b")
313 toBlob := plumbing.NewHash("bbbb") // does not exists
314 toTree := plumbing.NewHash("8b0af31d2544acb5c4f3816a602f11418cbd126e")
315
316 change := &Change{
317 From: ChangeEntry{
318 Name: path,
319 Tree: s.tree(c, fromTree),
320 TreeEntry: TreeEntry{
321 Name: name,
322 Mode: mode,
323 Hash: fromBlob,
324 },
325 },
326 To: ChangeEntry{},
327 }
328
329 _, _, err := change.Files()
330 c.Assert(err, ErrorMatches, "object not found")
331
332 change = &Change{
333 From: empty,
334 To: ChangeEntry{
335 Name: path,
336 Tree: s.tree(c, toTree),
337 TreeEntry: TreeEntry{
338 Name: name,
339 Mode: mode,
340 Hash: toBlob,
341 },
342 },
343 }
344
345 _, _, err = change.Files()
346 c.Assert(err, ErrorMatches, "object not found")
347 }
348
349 func (s *ChangeSuite) TestChangesString(c *C) {
350 expected := "[]"
351 changes := Changes{}
352 obtained := changes.String()
353 c.Assert(obtained, Equals, expected)
354
355 expected = "[<Action: Modify, Path: bla>]"
356 changes = make([]*Change, 1)
357 changes[0] = &Change{}
358 changes[0].From.Name = "bla"
359 changes[0].To.Name = "bla"
360
361 obtained = changes.String()
362 c.Assert(obtained, Equals, expected)
363
364 expected = "[<Action: Modify, Path: bla>, <Action: Delete, Path: foo/bar>]"
365 changes = make([]*Change, 2)
366 changes[0] = &Change{}
367 changes[0].From.Name = "bla"
368 changes[0].To.Name = "bla"
369 changes[1] = &Change{}
370 changes[1].From.Name = "foo/bar"
371 obtained = changes.String()
372 c.Assert(obtained, Equals, expected)
373 }
374
375 func (s *ChangeSuite) TestChangesSort(c *C) {
376 changes := make(Changes, 3)
377 changes[0] = &Change{}
378 changes[0].From.Name = "z"
379 changes[0].To.Name = "z"
380 changes[1] = &Change{}
381 changes[1].From.Name = "b/b"
382 changes[2] = &Change{}
383 changes[2].To.Name = "b/a"
384
385 expected := "[<Action: Insert, Path: b/a>, " +
386 "<Action: Delete, Path: b/b>, " +
387 "<Action: Modify, Path: z>]"
388
389 sort.Sort(changes)
390 c.Assert(changes.String(), Equals, expected)
391 }
392
393 func (s *ChangeSuite) TestCancel(c *C) {
394 // Commit a5078b19f08f63e7948abd0a5e2fb7d319d3a565 of the go-git
395 // fixture inserted "examples/clone/main.go".
396 //
397 // On that commit, the "examples/clone" tree is
398 // 6efca3ff41cab651332f9ebc0c96bb26be809615
399 //
400 // and the "examples/clone/main.go" is
401 // f95dc8f7923add1a8b9f72ecb1e8db1402de601a
402
403 path := "examples/clone/main.go"
404 name := "main.go"
405 mode := filemode.Regular
406 blob := plumbing.NewHash("f95dc8f7923add1a8b9f72ecb1e8db1402de601a")
407 tree := plumbing.NewHash("6efca3ff41cab651332f9ebc0c96bb26be809615")
408
409 change := &Change{
410 From: empty,
411 To: ChangeEntry{
412 Name: path,
413 Tree: s.tree(c, tree),
414 TreeEntry: TreeEntry{
415 Name: name,
416 Mode: mode,
417 Hash: blob,
418 },
419 },
420 }
421
422 ctx, cancel := context.WithCancel(context.Background())
423 cancel()
424 p, err := change.PatchContext(ctx)
425 c.Assert(p, IsNil)
426 c.Assert(err, ErrorMatches, "operation canceled")
427 }