| 1 |
package object |
| 2 |
|
| 3 |
import ( |
| 4 |
"sort" |
| 5 |
|
| 6 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 7 |
"github.com/go-git/go-git/v5/plumbing" |
| 8 |
"github.com/go-git/go-git/v5/plumbing/cache" |
| 9 |
"github.com/go-git/go-git/v5/plumbing/filemode" |
| 10 |
"github.com/go-git/go-git/v5/plumbing/format/packfile" |
| 11 |
"github.com/go-git/go-git/v5/plumbing/storer" |
| 12 |
"github.com/go-git/go-git/v5/storage/filesystem" |
| 13 |
"github.com/go-git/go-git/v5/storage/memory" |
| 14 |
"github.com/go-git/go-git/v5/utils/merkletrie" |
| 15 |
|
| 16 |
. "gopkg.in/check.v1" |
| 17 |
) |
| 18 |
|
| 19 |
type DiffTreeSuite struct { |
| 20 |
fixtures.Suite |
| 21 |
Storer storer.EncodedObjectStorer |
| 22 |
Fixture *fixtures.Fixture |
| 23 |
cache map[string]storer.EncodedObjectStorer |
| 24 |
} |
| 25 |
|
| 26 |
func (s *DiffTreeSuite) SetUpSuite(c *C) { |
| 27 |
s.Fixture = fixtures.Basic().One() |
| 28 |
sto := filesystem.NewStorage(s.Fixture.DotGit(), cache.NewObjectLRUDefault()) |
| 29 |
s.Storer = sto |
| 30 |
s.cache = make(map[string]storer.EncodedObjectStorer) |
| 31 |
} |
| 32 |
|
| 33 |
func (s *DiffTreeSuite) commitFromStorer(c *C, sto storer.EncodedObjectStorer, |
| 34 |
h plumbing.Hash) *Commit { |
| 35 |
|
| 36 |
commit, err := GetCommit(sto, h) |
| 37 |
c.Assert(err, IsNil) |
| 38 |
return commit |
| 39 |
} |
| 40 |
|
| 41 |
func (s *DiffTreeSuite) storageFromPackfile(f *fixtures.Fixture) storer.EncodedObjectStorer { |
| 42 |
sto, ok := s.cache[f.URL] |
| 43 |
if ok { |
| 44 |
return sto |
| 45 |
} |
| 46 |
|
| 47 |
storer := memory.NewStorage() |
| 48 |
|
| 49 |
pf := f.Packfile() |
| 50 |
defer pf.Close() |
| 51 |
|
| 52 |
if err := packfile.UpdateObjectStorage(storer, pf); err != nil { |
| 53 |
panic(err) |
| 54 |
} |
| 55 |
|
| 56 |
s.cache[f.URL] = storer |
| 57 |
return storer |
| 58 |
} |
| 59 |
|
| 60 |
var _ = Suite(&DiffTreeSuite{}) |
| 61 |
|
| 62 |
type expectChange struct { |
| 63 |
Action merkletrie.Action |
| 64 |
Name string |
| 65 |
} |
| 66 |
|
| 67 |
func assertChanges(a Changes, c *C) { |
| 68 |
for _, changes := range a { |
| 69 |
action, err := changes.Action() |
| 70 |
c.Assert(err, IsNil) |
| 71 |
switch action { |
| 72 |
case merkletrie.Insert: |
| 73 |
c.Assert(changes.From.Tree, IsNil) |
| 74 |
c.Assert(changes.To.Tree, NotNil) |
| 75 |
case merkletrie.Delete: |
| 76 |
c.Assert(changes.From.Tree, NotNil) |
| 77 |
c.Assert(changes.To.Tree, IsNil) |
| 78 |
case merkletrie.Modify: |
| 79 |
c.Assert(changes.From.Tree, NotNil) |
| 80 |
c.Assert(changes.To.Tree, NotNil) |
| 81 |
default: |
| 82 |
c.Fatalf("unknown action: %d", action) |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func equalChanges(a Changes, b []expectChange, c *C) bool { |
| 88 |
if len(a) != len(b) { |
| 89 |
return false |
| 90 |
} |
| 91 |
|
| 92 |
sort.Sort(a) |
| 93 |
|
| 94 |
for i, va := range a { |
| 95 |
vb := b[i] |
| 96 |
action, err := va.Action() |
| 97 |
c.Assert(err, IsNil) |
| 98 |
if action != vb.Action || va.name() != vb.Name { |
| 99 |
return false |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return true |
| 104 |
} |
| 105 |
|
| 106 |
func (s *DiffTreeSuite) TestDiffTree(c *C) { |
| 107 |
for i, t := range []struct { |
| 108 |
repository string // the repo name as in localRepos |
| 109 |
commit1 string // the commit of the first tree |
| 110 |
commit2 string // the commit of the second tree |
| 111 |
expected []expectChange // the expected list of []changeExpect |
| 112 |
}{ |
| 113 |
{ |
| 114 |
"https://github.com/dezfowler/LiteMock.git", |
| 115 |
"", |
| 116 |
"", |
| 117 |
[]expectChange{}, |
| 118 |
}, |
| 119 |
{ |
| 120 |
"https://github.com/dezfowler/LiteMock.git", |
| 121 |
"b7965eaa2c4f245d07191fe0bcfe86da032d672a", |
| 122 |
"b7965eaa2c4f245d07191fe0bcfe86da032d672a", |
| 123 |
[]expectChange{}, |
| 124 |
}, |
| 125 |
{ |
| 126 |
"https://github.com/dezfowler/LiteMock.git", |
| 127 |
"", |
| 128 |
"b7965eaa2c4f245d07191fe0bcfe86da032d672a", |
| 129 |
[]expectChange{ |
| 130 |
{Action: merkletrie.Insert, Name: "README"}, |
| 131 |
}, |
| 132 |
}, |
| 133 |
{ |
| 134 |
"https://github.com/dezfowler/LiteMock.git", |
| 135 |
"b7965eaa2c4f245d07191fe0bcfe86da032d672a", |
| 136 |
"", |
| 137 |
[]expectChange{ |
| 138 |
{Action: merkletrie.Delete, Name: "README"}, |
| 139 |
}, |
| 140 |
}, |
| 141 |
{ |
| 142 |
"https://github.com/githubtraining/example-branches.git", |
| 143 |
"", |
| 144 |
"f0eb272cc8f77803478c6748103a1450aa1abd37", |
| 145 |
[]expectChange{ |
| 146 |
{Action: merkletrie.Insert, Name: "README.md"}, |
| 147 |
}, |
| 148 |
}, |
| 149 |
{ |
| 150 |
"https://github.com/githubtraining/example-branches.git", |
| 151 |
"f0eb272cc8f77803478c6748103a1450aa1abd37", |
| 152 |
"", |
| 153 |
[]expectChange{ |
| 154 |
{Action: merkletrie.Delete, Name: "README.md"}, |
| 155 |
}, |
| 156 |
}, |
| 157 |
{ |
| 158 |
"https://github.com/githubtraining/example-branches.git", |
| 159 |
"f0eb272cc8f77803478c6748103a1450aa1abd37", |
| 160 |
"f0eb272cc8f77803478c6748103a1450aa1abd37", |
| 161 |
[]expectChange{}, |
| 162 |
}, |
| 163 |
{ |
| 164 |
"https://github.com/github/gem-builder.git", |
| 165 |
"", |
| 166 |
"9608eed92b3839b06ebf72d5043da547de10ce85", |
| 167 |
[]expectChange{ |
| 168 |
{Action: merkletrie.Insert, Name: "README"}, |
| 169 |
{Action: merkletrie.Insert, Name: "gem_builder.rb"}, |
| 170 |
{Action: merkletrie.Insert, Name: "gem_eval.rb"}, |
| 171 |
}, |
| 172 |
}, |
| 173 |
{ |
| 174 |
"https://github.com/github/gem-builder.git", |
| 175 |
"9608eed92b3839b06ebf72d5043da547de10ce85", |
| 176 |
"", |
| 177 |
[]expectChange{ |
| 178 |
{Action: merkletrie.Delete, Name: "README"}, |
| 179 |
{Action: merkletrie.Delete, Name: "gem_builder.rb"}, |
| 180 |
{Action: merkletrie.Delete, Name: "gem_eval.rb"}, |
| 181 |
}, |
| 182 |
}, |
| 183 |
{ |
| 184 |
"https://github.com/github/gem-builder.git", |
| 185 |
"9608eed92b3839b06ebf72d5043da547de10ce85", |
| 186 |
"9608eed92b3839b06ebf72d5043da547de10ce85", |
| 187 |
[]expectChange{}, |
| 188 |
}, |
| 189 |
{ |
| 190 |
"https://github.com/toqueteos/ts3.git", |
| 191 |
"", |
| 192 |
"764e914b75d6d6df1fc5d832aa9840f590abf1bb", |
| 193 |
[]expectChange{ |
| 194 |
{Action: merkletrie.Insert, Name: "README.markdown"}, |
| 195 |
{Action: merkletrie.Insert, Name: "examples/bot.go"}, |
| 196 |
{Action: merkletrie.Insert, Name: "examples/raw_shell.go"}, |
| 197 |
{Action: merkletrie.Insert, Name: "helpers.go"}, |
| 198 |
{Action: merkletrie.Insert, Name: "ts3.go"}, |
| 199 |
}, |
| 200 |
}, |
| 201 |
{ |
| 202 |
"https://github.com/toqueteos/ts3.git", |
| 203 |
"764e914b75d6d6df1fc5d832aa9840f590abf1bb", |
| 204 |
"", |
| 205 |
[]expectChange{ |
| 206 |
{Action: merkletrie.Delete, Name: "README.markdown"}, |
| 207 |
{Action: merkletrie.Delete, Name: "examples/bot.go"}, |
| 208 |
{Action: merkletrie.Delete, Name: "examples/raw_shell.go"}, |
| 209 |
{Action: merkletrie.Delete, Name: "helpers.go"}, |
| 210 |
{Action: merkletrie.Delete, Name: "ts3.go"}, |
| 211 |
}, |
| 212 |
}, |
| 213 |
{ |
| 214 |
"https://github.com/toqueteos/ts3.git", |
| 215 |
"764e914b75d6d6df1fc5d832aa9840f590abf1bb", |
| 216 |
"764e914b75d6d6df1fc5d832aa9840f590abf1bb", |
| 217 |
[]expectChange{}, |
| 218 |
}, |
| 219 |
{ |
| 220 |
"https://github.com/github/gem-builder.git", |
| 221 |
"9608eed92b3839b06ebf72d5043da547de10ce85", |
| 222 |
"6c41e05a17e19805879689414026eb4e279f7de0", |
| 223 |
[]expectChange{ |
| 224 |
{Action: merkletrie.Modify, Name: "gem_eval.rb"}, |
| 225 |
}, |
| 226 |
}, |
| 227 |
{ |
| 228 |
"https://github.com/github/gem-builder.git", |
| 229 |
"6c41e05a17e19805879689414026eb4e279f7de0", |
| 230 |
"89be3aac2f178719c12953cc9eaa23441f8d9371", |
| 231 |
[]expectChange{ |
| 232 |
{Action: merkletrie.Modify, Name: "gem_eval.rb"}, |
| 233 |
{Action: merkletrie.Insert, Name: "gem_eval_test.rb"}, |
| 234 |
{Action: merkletrie.Insert, Name: "security.rb"}, |
| 235 |
{Action: merkletrie.Insert, Name: "security_test.rb"}, |
| 236 |
}, |
| 237 |
}, |
| 238 |
{ |
| 239 |
"https://github.com/github/gem-builder.git", |
| 240 |
"89be3aac2f178719c12953cc9eaa23441f8d9371", |
| 241 |
"597240b7da22d03ad555328f15abc480b820acc0", |
| 242 |
[]expectChange{ |
| 243 |
{Action: merkletrie.Modify, Name: "gem_eval.rb"}, |
| 244 |
}, |
| 245 |
}, |
| 246 |
{ |
| 247 |
"https://github.com/github/gem-builder.git", |
| 248 |
"597240b7da22d03ad555328f15abc480b820acc0", |
| 249 |
"0260380e375d2dd0e1a8fcab15f91ce56dbe778e", |
| 250 |
[]expectChange{ |
| 251 |
{Action: merkletrie.Modify, Name: "gem_eval.rb"}, |
| 252 |
{Action: merkletrie.Modify, Name: "gem_eval_test.rb"}, |
| 253 |
{Action: merkletrie.Insert, Name: "lazy_dir.rb"}, |
| 254 |
{Action: merkletrie.Insert, Name: "lazy_dir_test.rb"}, |
| 255 |
{Action: merkletrie.Modify, Name: "security.rb"}, |
| 256 |
{Action: merkletrie.Modify, Name: "security_test.rb"}, |
| 257 |
}, |
| 258 |
}, |
| 259 |
{ |
| 260 |
"https://github.com/github/gem-builder.git", |
| 261 |
"0260380e375d2dd0e1a8fcab15f91ce56dbe778e", |
| 262 |
"597240b7da22d03ad555328f15abc480b820acc0", |
| 263 |
[]expectChange{ |
| 264 |
{Action: merkletrie.Modify, Name: "gem_eval.rb"}, |
| 265 |
{Action: merkletrie.Modify, Name: "gem_eval_test.rb"}, |
| 266 |
{Action: merkletrie.Delete, Name: "lazy_dir.rb"}, |
| 267 |
{Action: merkletrie.Delete, Name: "lazy_dir_test.rb"}, |
| 268 |
{Action: merkletrie.Modify, Name: "security.rb"}, |
| 269 |
{Action: merkletrie.Modify, Name: "security_test.rb"}, |
| 270 |
}, |
| 271 |
}, |
| 272 |
{ |
| 273 |
"https://github.com/github/gem-builder.git", |
| 274 |
"0260380e375d2dd0e1a8fcab15f91ce56dbe778e", |
| 275 |
"ca9fd470bacb6262eb4ca23ee48bb2f43711c1ff", |
| 276 |
[]expectChange{ |
| 277 |
{Action: merkletrie.Modify, Name: "gem_eval.rb"}, |
| 278 |
{Action: merkletrie.Modify, Name: "security.rb"}, |
| 279 |
{Action: merkletrie.Modify, Name: "security_test.rb"}, |
| 280 |
}, |
| 281 |
}, |
| 282 |
{ |
| 283 |
"https://github.com/github/gem-builder.git", |
| 284 |
"fe3c86745f887c23a0d38c85cfd87ca957312f86", |
| 285 |
"b7e3f636febf7a0cd3ab473b6d30081786d2c5b6", |
| 286 |
[]expectChange{ |
| 287 |
{Action: merkletrie.Modify, Name: "gem_eval.rb"}, |
| 288 |
{Action: merkletrie.Modify, Name: "gem_eval_test.rb"}, |
| 289 |
{Action: merkletrie.Insert, Name: "git_mock"}, |
| 290 |
{Action: merkletrie.Modify, Name: "lazy_dir.rb"}, |
| 291 |
{Action: merkletrie.Modify, Name: "lazy_dir_test.rb"}, |
| 292 |
{Action: merkletrie.Modify, Name: "security.rb"}, |
| 293 |
}, |
| 294 |
}, |
| 295 |
{ |
| 296 |
"https://github.com/rumpkernel/rumprun-xen.git", |
| 297 |
"1831e47b0c6db750714cd0e4be97b5af17fb1eb0", |
| 298 |
"51d8515578ea0c88cc8fc1a057903675cf1fc16c", |
| 299 |
[]expectChange{ |
| 300 |
{Action: merkletrie.Modify, Name: "Makefile"}, |
| 301 |
{Action: merkletrie.Modify, Name: "netbsd_init.c"}, |
| 302 |
{Action: merkletrie.Modify, Name: "rumphyper_stubs.c"}, |
| 303 |
{Action: merkletrie.Delete, Name: "sysproxy.c"}, |
| 304 |
}, |
| 305 |
}, |
| 306 |
{ |
| 307 |
"https://github.com/rumpkernel/rumprun-xen.git", |
| 308 |
"1831e47b0c6db750714cd0e4be97b5af17fb1eb0", |
| 309 |
"e13e678f7ee9badd01b120889e0ec5fdc8ae3802", |
| 310 |
[]expectChange{ |
| 311 |
{Action: merkletrie.Modify, Name: "app-tools/rumprun"}, |
| 312 |
}, |
| 313 |
}, |
| 314 |
} { |
| 315 |
f := fixtures.ByURL(t.repository).One() |
| 316 |
sto := s.storageFromPackfile(f) |
| 317 |
|
| 318 |
var tree1, tree2 *Tree |
| 319 |
var err error |
| 320 |
if t.commit1 != "" { |
| 321 |
tree1, err = s.commitFromStorer(c, sto, |
| 322 |
plumbing.NewHash(t.commit1)).Tree() |
| 323 |
c.Assert(err, IsNil, |
| 324 |
Commentf("subtest %d: unable to retrieve tree from commit %s and repo %s: %s", i, t.commit1, t.repository, err)) |
| 325 |
} |
| 326 |
|
| 327 |
if t.commit2 != "" { |
| 328 |
tree2, err = s.commitFromStorer(c, sto, |
| 329 |
plumbing.NewHash(t.commit2)).Tree() |
| 330 |
c.Assert(err, IsNil, |
| 331 |
Commentf("subtest %d: unable to retrieve tree from commit %s and repo %s", i, t.commit2, t.repository, err)) |
| 332 |
} |
| 333 |
|
| 334 |
obtained, err := DiffTree(tree1, tree2) |
| 335 |
c.Assert(err, IsNil, |
| 336 |
Commentf("subtest %d: unable to calculate difftree: %s", i, err)) |
| 337 |
obtainedFromMethod, err := tree1.Diff(tree2) |
| 338 |
c.Assert(err, IsNil, |
| 339 |
Commentf("subtest %d: unable to calculate difftree: %s. Result calling Diff method from Tree object returns an error", i, err)) |
| 340 |
|
| 341 |
c.Assert(obtained, DeepEquals, obtainedFromMethod) |
| 342 |
|
| 343 |
c.Assert(equalChanges(obtained, t.expected, c), Equals, true, |
| 344 |
Commentf("subtest:%d\nrepo=%s\ncommit1=%s\ncommit2=%s\nexpected=%s\nobtained=%s", |
| 345 |
i, t.repository, t.commit1, t.commit2, t.expected, obtained)) |
| 346 |
|
| 347 |
assertChanges(obtained, c) |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
func (s *DiffTreeSuite) TestIssue279(c *C) { |
| 352 |
// treeNoders should have the same hash when their mode is |
| 353 |
// filemode.Deprecated and filemode.Regular. |
| 354 |
a := &treeNoder{ |
| 355 |
hash: plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), |
| 356 |
mode: filemode.Regular, |
| 357 |
} |
| 358 |
b := &treeNoder{ |
| 359 |
hash: plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), |
| 360 |
mode: filemode.Deprecated, |
| 361 |
} |
| 362 |
c.Assert(a.Hash(), DeepEquals, b.Hash()) |
| 363 |
|
| 364 |
// yet, they should have different hashes if their contents change. |
| 365 |
aa := &treeNoder{ |
| 366 |
hash: plumbing.NewHash("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"), |
| 367 |
mode: filemode.Regular, |
| 368 |
} |
| 369 |
c.Assert(a.Hash(), Not(DeepEquals), aa.Hash()) |
| 370 |
bb := &treeNoder{ |
| 371 |
hash: plumbing.NewHash("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"), |
| 372 |
mode: filemode.Deprecated, |
| 373 |
} |
| 374 |
c.Assert(b.Hash(), Not(DeepEquals), bb.Hash()) |
| 375 |
} |