| 1 |
package object |
| 2 |
|
| 3 |
import ( |
| 4 |
"path/filepath" |
| 5 |
"strings" |
| 6 |
|
| 7 |
"github.com/go-git/go-git/v5/plumbing" |
| 8 |
"github.com/go-git/go-git/v5/plumbing/filemode" |
| 9 |
"github.com/go-git/go-git/v5/storage/memory" |
| 10 |
. "gopkg.in/check.v1" |
| 11 |
) |
| 12 |
|
| 13 |
type RenameSuite struct { |
| 14 |
BaseObjectsSuite |
| 15 |
} |
| 16 |
|
| 17 |
var _ = Suite(&RenameSuite{}) |
| 18 |
|
| 19 |
func (s *RenameSuite) TestNameSimilarityScore(c *C) { |
| 20 |
testCases := []struct { |
| 21 |
a, b string |
| 22 |
score int |
| 23 |
}{ |
| 24 |
{"foo/bar.c", "foo/baz.c", 70}, |
| 25 |
{"src/utils/Foo.java", "tests/utils/Foo.java", 64}, |
| 26 |
{"foo/bar/baz.py", "README.md", 0}, |
| 27 |
{"src/utils/something/foo.py", "src/utils/something/other/foo.py", 69}, |
| 28 |
{"src/utils/something/foo.py", "src/utils/yada/foo.py", 63}, |
| 29 |
{"src/utils/something/foo.py", "src/utils/something/other/bar.py", 44}, |
| 30 |
{"src/utils/something/foo.py", "src/utils/something/foo.py", 100}, |
| 31 |
} |
| 32 |
|
| 33 |
for _, tt := range testCases { |
| 34 |
c.Assert(nameSimilarityScore(tt.a, tt.b), Equals, tt.score) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
const ( |
| 39 |
pathA = "src/A" |
| 40 |
pathB = "src/B" |
| 41 |
pathH = "src/H" |
| 42 |
pathQ = "src/Q" |
| 43 |
) |
| 44 |
|
| 45 |
func (s *RenameSuite) TestExactRename_OneRename(c *C) { |
| 46 |
a := makeAdd(c, makeFile(c, pathA, filemode.Regular, "foo")) |
| 47 |
b := makeDelete(c, makeFile(c, pathQ, filemode.Regular, "foo")) |
| 48 |
|
| 49 |
result := detectRenames(c, Changes{a, b}, nil, 1) |
| 50 |
assertRename(c, b, a, result[0]) |
| 51 |
} |
| 52 |
|
| 53 |
func (s *RenameSuite) TestExactRename_DifferentObjects(c *C) { |
| 54 |
a := makeAdd(c, makeFile(c, pathA, filemode.Regular, "foo")) |
| 55 |
h := makeAdd(c, makeFile(c, pathH, filemode.Regular, "foo")) |
| 56 |
q := makeDelete(c, makeFile(c, pathQ, filemode.Regular, "bar")) |
| 57 |
|
| 58 |
result := detectRenames(c, Changes{a, h, q}, nil, 3) |
| 59 |
c.Assert(result[0], DeepEquals, a) |
| 60 |
c.Assert(result[1], DeepEquals, h) |
| 61 |
c.Assert(result[2], DeepEquals, q) |
| 62 |
} |
| 63 |
|
| 64 |
func (s *RenameSuite) TestExactRename_OneRenameOneModify(c *C) { |
| 65 |
c1 := makeAdd(c, makeFile(c, pathA, filemode.Regular, "foo")) |
| 66 |
c2 := makeDelete(c, makeFile(c, pathQ, filemode.Regular, "foo")) |
| 67 |
c3 := makeChange(c, |
| 68 |
makeFile(c, pathH, filemode.Regular, "bar"), |
| 69 |
makeFile(c, pathH, filemode.Regular, "bar"), |
| 70 |
) |
| 71 |
|
| 72 |
result := detectRenames(c, Changes{c1, c2, c3}, nil, 2) |
| 73 |
c.Assert(result[0], DeepEquals, c3) |
| 74 |
assertRename(c, c2, c1, result[1]) |
| 75 |
} |
| 76 |
|
| 77 |
func (s *RenameSuite) TestExactRename_ManyRenames(c *C) { |
| 78 |
c1 := makeAdd(c, makeFile(c, pathA, filemode.Regular, "foo")) |
| 79 |
c2 := makeDelete(c, makeFile(c, pathQ, filemode.Regular, "foo")) |
| 80 |
c3 := makeAdd(c, makeFile(c, pathH, filemode.Regular, "bar")) |
| 81 |
c4 := makeDelete(c, makeFile(c, pathB, filemode.Regular, "bar")) |
| 82 |
|
| 83 |
result := detectRenames(c, Changes{c1, c2, c3, c4}, nil, 2) |
| 84 |
assertRename(c, c4, c3, result[0]) |
| 85 |
assertRename(c, c2, c1, result[1]) |
| 86 |
} |
| 87 |
|
| 88 |
func (s *RenameSuite) TestExactRename_MultipleIdenticalDeletes(c *C) { |
| 89 |
changes := Changes{ |
| 90 |
makeDelete(c, makeFile(c, pathA, filemode.Regular, "foo")), |
| 91 |
makeDelete(c, makeFile(c, pathB, filemode.Regular, "foo")), |
| 92 |
makeDelete(c, makeFile(c, pathH, filemode.Regular, "foo")), |
| 93 |
makeAdd(c, makeFile(c, pathQ, filemode.Regular, "foo")), |
| 94 |
} |
| 95 |
|
| 96 |
result := detectRenames(c, changes, nil, 3) |
| 97 |
assertRename(c, changes[0], changes[3], result[0]) |
| 98 |
c.Assert(result[1], DeepEquals, changes[1]) |
| 99 |
c.Assert(result[2], DeepEquals, changes[2]) |
| 100 |
} |
| 101 |
|
| 102 |
func (s *RenameSuite) TestRenameExact_PathBreaksTie(c *C) { |
| 103 |
changes := Changes{ |
| 104 |
makeAdd(c, makeFile(c, "src/com/foo/a.java", filemode.Regular, "foo")), |
| 105 |
makeDelete(c, makeFile(c, "src/com/foo/b.java", filemode.Regular, "foo")), |
| 106 |
makeAdd(c, makeFile(c, "c.txt", filemode.Regular, "foo")), |
| 107 |
makeDelete(c, makeFile(c, "d.txt", filemode.Regular, "foo")), |
| 108 |
makeAdd(c, makeFile(c, "the_e_file.txt", filemode.Regular, "foo")), |
| 109 |
} |
| 110 |
|
| 111 |
// Add out of order to avoid first-match succeeding |
| 112 |
result := detectRenames(c, Changes{ |
| 113 |
changes[0], |
| 114 |
changes[3], |
| 115 |
changes[4], |
| 116 |
changes[1], |
| 117 |
changes[2], |
| 118 |
}, nil, 3) |
| 119 |
assertRename(c, changes[3], changes[2], result[0]) |
| 120 |
assertRename(c, changes[1], changes[0], result[1]) |
| 121 |
c.Assert(result[2], DeepEquals, changes[4]) |
| 122 |
} |
| 123 |
|
| 124 |
func (s *RenameSuite) TestExactRename_OneDeleteManyAdds(c *C) { |
| 125 |
changes := Changes{ |
| 126 |
makeAdd(c, makeFile(c, "src/com/foo/a.java", filemode.Regular, "foo")), |
| 127 |
makeAdd(c, makeFile(c, "src/com/foo/b.java", filemode.Regular, "foo")), |
| 128 |
makeAdd(c, makeFile(c, "c.txt", filemode.Regular, "foo")), |
| 129 |
makeDelete(c, makeFile(c, "d.txt", filemode.Regular, "foo")), |
| 130 |
} |
| 131 |
|
| 132 |
result := detectRenames(c, changes, nil, 3) |
| 133 |
assertRename(c, changes[3], changes[2], result[0]) |
| 134 |
c.Assert(result[1], DeepEquals, changes[0]) |
| 135 |
c.Assert(result[2], DeepEquals, changes[1]) |
| 136 |
} |
| 137 |
|
| 138 |
func (s *RenameSuite) TestExactRename_UnstagedFile(c *C) { |
| 139 |
changes := Changes{ |
| 140 |
makeDelete(c, makeFile(c, pathA, filemode.Regular, "foo")), |
| 141 |
makeAdd(c, makeFile(c, pathB, filemode.Regular, "foo")), |
| 142 |
} |
| 143 |
result := detectRenames(c, changes, nil, 1) |
| 144 |
assertRename(c, changes[0], changes[1], result[0]) |
| 145 |
} |
| 146 |
|
| 147 |
func (s *RenameSuite) TestContentRename_OnePair(c *C) { |
| 148 |
changes := Changes{ |
| 149 |
makeAdd(c, makeFile(c, pathA, filemode.Regular, "foo\nbar\nbaz\nblarg\n")), |
| 150 |
makeDelete(c, makeFile(c, pathA, filemode.Regular, "foo\nbar\nbaz\nblah\n")), |
| 151 |
} |
| 152 |
|
| 153 |
result := detectRenames(c, changes, nil, 1) |
| 154 |
assertRename(c, changes[1], changes[0], result[0]) |
| 155 |
} |
| 156 |
|
| 157 |
func (s *RenameSuite) TestContentRename_OneRenameTwoUnrelatedFiles(c *C) { |
| 158 |
changes := Changes{ |
| 159 |
makeAdd(c, makeFile(c, pathA, filemode.Regular, "foo\nbar\nbaz\nblarg\n")), |
| 160 |
makeDelete(c, makeFile(c, pathQ, filemode.Regular, "foo\nbar\nbaz\nblah\n")), |
| 161 |
makeAdd(c, makeFile(c, pathB, filemode.Regular, "some\nsort\nof\ntext\n")), |
| 162 |
makeDelete(c, makeFile(c, pathH, filemode.Regular, "completely\nunrelated\ntext\n")), |
| 163 |
} |
| 164 |
|
| 165 |
result := detectRenames(c, changes, nil, 3) |
| 166 |
c.Assert(result[0], DeepEquals, changes[2]) |
| 167 |
c.Assert(result[1], DeepEquals, changes[3]) |
| 168 |
assertRename(c, changes[1], changes[0], result[2]) |
| 169 |
} |
| 170 |
|
| 171 |
func (s *RenameSuite) TestContentRename_LastByteDifferent(c *C) { |
| 172 |
changes := Changes{ |
| 173 |
makeAdd(c, makeFile(c, pathA, filemode.Regular, "foo\nbar\na")), |
| 174 |
makeDelete(c, makeFile(c, pathQ, filemode.Regular, "foo\nbar\nb")), |
| 175 |
} |
| 176 |
|
| 177 |
result := detectRenames(c, changes, nil, 1) |
| 178 |
assertRename(c, changes[1], changes[0], result[0]) |
| 179 |
} |
| 180 |
|
| 181 |
func (s *RenameSuite) TestContentRename_NewlinesOnly(c *C) { |
| 182 |
changes := Changes{ |
| 183 |
makeAdd(c, makeFile(c, pathA, filemode.Regular, strings.Repeat("\n", 3))), |
| 184 |
makeDelete(c, makeFile(c, pathQ, filemode.Regular, strings.Repeat("\n", 4))), |
| 185 |
} |
| 186 |
|
| 187 |
result := detectRenames(c, changes, nil, 1) |
| 188 |
assertRename(c, changes[1], changes[0], result[0]) |
| 189 |
} |
| 190 |
|
| 191 |
func (s *RenameSuite) TestContentRename_SameContentMultipleTimes(c *C) { |
| 192 |
changes := Changes{ |
| 193 |
makeAdd(c, makeFile(c, pathA, filemode.Regular, "a\na\na\na\n")), |
| 194 |
makeDelete(c, makeFile(c, pathQ, filemode.Regular, "a\na\na\n")), |
| 195 |
} |
| 196 |
|
| 197 |
result := detectRenames(c, changes, nil, 1) |
| 198 |
assertRename(c, changes[1], changes[0], result[0]) |
| 199 |
} |
| 200 |
|
| 201 |
func (s *RenameSuite) TestContentRename_OnePairRenameScore50(c *C) { |
| 202 |
changes := Changes{ |
| 203 |
makeAdd(c, makeFile(c, pathA, filemode.Regular, "ab\nab\nab\nac\nad\nae\n")), |
| 204 |
makeDelete(c, makeFile(c, pathQ, filemode.Regular, "ac\nab\nab\nab\naa\na0\na1\n")), |
| 205 |
} |
| 206 |
|
| 207 |
result := detectRenames(c, changes, &DiffTreeOptions{RenameScore: 50}, 1) |
| 208 |
assertRename(c, changes[1], changes[0], result[0]) |
| 209 |
} |
| 210 |
|
| 211 |
func (s *RenameSuite) TestNoRenames_SingleByteFiles(c *C) { |
| 212 |
changes := Changes{ |
| 213 |
makeAdd(c, makeFile(c, pathA, filemode.Regular, "a")), |
| 214 |
makeAdd(c, makeFile(c, pathQ, filemode.Regular, "b")), |
| 215 |
} |
| 216 |
|
| 217 |
result := detectRenames(c, changes, nil, 2) |
| 218 |
c.Assert(result[0], DeepEquals, changes[0]) |
| 219 |
c.Assert(result[1], DeepEquals, changes[1]) |
| 220 |
} |
| 221 |
|
| 222 |
func (s *RenameSuite) TestNoRenames_EmptyFile(c *C) { |
| 223 |
changes := Changes{ |
| 224 |
makeAdd(c, makeFile(c, pathA, filemode.Regular, "")), |
| 225 |
} |
| 226 |
result := detectRenames(c, changes, nil, 1) |
| 227 |
c.Assert(result[0], DeepEquals, changes[0]) |
| 228 |
} |
| 229 |
|
| 230 |
func (s *RenameSuite) TestNoRenames_EmptyFile2(c *C) { |
| 231 |
changes := Changes{ |
| 232 |
makeAdd(c, makeFile(c, pathA, filemode.Regular, "")), |
| 233 |
makeDelete(c, makeFile(c, pathQ, filemode.Regular, "blah")), |
| 234 |
} |
| 235 |
result := detectRenames(c, changes, nil, 2) |
| 236 |
c.Assert(result[0], DeepEquals, changes[0]) |
| 237 |
c.Assert(result[1], DeepEquals, changes[1]) |
| 238 |
} |
| 239 |
|
| 240 |
func (s *RenameSuite) TestNoRenames_SymlinkAndFile(c *C) { |
| 241 |
changes := Changes{ |
| 242 |
makeAdd(c, makeFile(c, pathA, filemode.Regular, "src/dest")), |
| 243 |
makeDelete(c, makeFile(c, pathQ, filemode.Symlink, "src/dest")), |
| 244 |
} |
| 245 |
result := detectRenames(c, changes, nil, 2) |
| 246 |
c.Assert(result[0], DeepEquals, changes[0]) |
| 247 |
c.Assert(result[1], DeepEquals, changes[1]) |
| 248 |
} |
| 249 |
|
| 250 |
func (s *RenameSuite) TestNoRenames_SymlinkAndFileSamePath(c *C) { |
| 251 |
changes := Changes{ |
| 252 |
makeAdd(c, makeFile(c, pathA, filemode.Regular, "src/dest")), |
| 253 |
makeDelete(c, makeFile(c, pathA, filemode.Symlink, "src/dest")), |
| 254 |
} |
| 255 |
result := detectRenames(c, changes, nil, 2) |
| 256 |
c.Assert(result[0], DeepEquals, changes[0]) |
| 257 |
c.Assert(result[1], DeepEquals, changes[1]) |
| 258 |
} |
| 259 |
|
| 260 |
func (s *RenameSuite) TestRenameLimit(c *C) { |
| 261 |
changes := Changes{ |
| 262 |
makeAdd(c, makeFile(c, pathA, filemode.Regular, "foo\nbar\nbaz\nblarg\n")), |
| 263 |
makeDelete(c, makeFile(c, pathB, filemode.Regular, "foo\nbar\nbaz\nblah\n")), |
| 264 |
makeAdd(c, makeFile(c, pathH, filemode.Regular, "a\nb\nc\nd\n")), |
| 265 |
makeDelete(c, makeFile(c, pathQ, filemode.Regular, "a\nb\nc\n")), |
| 266 |
} |
| 267 |
|
| 268 |
result := detectRenames(c, changes, &DiffTreeOptions{RenameLimit: 1}, 4) |
| 269 |
for i, res := range result { |
| 270 |
c.Assert(res, DeepEquals, changes[i]) |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
func (s *RenameSuite) TestRenameExactManyAddsManyDeletesNoGaps(c *C) { |
| 275 |
content := "a" |
| 276 |
detector := &renameDetector{ |
| 277 |
added: []*Change{ |
| 278 |
makeAdd(c, makeFile(c, pathA, filemode.Regular, content)), |
| 279 |
makeAdd(c, makeFile(c, pathQ, filemode.Regular, content)), |
| 280 |
makeAdd(c, makeFile(c, "something", filemode.Regular, content)), |
| 281 |
}, |
| 282 |
deleted: []*Change{ |
| 283 |
makeDelete(c, makeFile(c, pathA, filemode.Regular, content)), |
| 284 |
makeDelete(c, makeFile(c, pathB, filemode.Regular, content)), |
| 285 |
makeDelete(c, makeFile(c, "foo/bar/other", filemode.Regular, content)), |
| 286 |
}, |
| 287 |
} |
| 288 |
|
| 289 |
detector.detectExactRenames() |
| 290 |
|
| 291 |
for _, added := range detector.added { |
| 292 |
c.Assert(added, NotNil) |
| 293 |
} |
| 294 |
|
| 295 |
for _, deleted := range detector.deleted { |
| 296 |
c.Assert(deleted, NotNil) |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
func detectRenames(c *C, changes Changes, opts *DiffTreeOptions, expectedResults int) Changes { |
| 301 |
result, err := DetectRenames(changes, opts) |
| 302 |
c.Assert(err, IsNil) |
| 303 |
c.Assert(result, HasLen, expectedResults) |
| 304 |
return result |
| 305 |
} |
| 306 |
|
| 307 |
func assertRename(c *C, from, to *Change, rename *Change) { |
| 308 |
c.Assert(&Change{From: from.From, To: to.To}, DeepEquals, rename) |
| 309 |
} |
| 310 |
|
| 311 |
type SimilarityIndexSuite struct { |
| 312 |
BaseObjectsSuite |
| 313 |
} |
| 314 |
|
| 315 |
var _ = Suite(&SimilarityIndexSuite{}) |
| 316 |
|
| 317 |
func (s *SimilarityIndexSuite) TestScoreFiles(c *C) { |
| 318 |
tree := s.tree(c, plumbing.NewHash("a8d315b2b1c615d43042c3a62402b8a54288cf5c")) |
| 319 |
binary, err := tree.File("binary.jpg") |
| 320 |
c.Assert(err, IsNil) |
| 321 |
binIndex, err := fileSimilarityIndex(binary) |
| 322 |
c.Assert(err, IsNil) |
| 323 |
|
| 324 |
long, err := tree.File("json/long.json") |
| 325 |
c.Assert(err, IsNil) |
| 326 |
longIndex, err := fileSimilarityIndex(long) |
| 327 |
c.Assert(err, IsNil) |
| 328 |
|
| 329 |
short, err := tree.File("json/short.json") |
| 330 |
c.Assert(err, IsNil) |
| 331 |
shortIndex, err := fileSimilarityIndex(short) |
| 332 |
c.Assert(err, IsNil) |
| 333 |
|
| 334 |
php, err := tree.File("php/crappy.php") |
| 335 |
c.Assert(err, IsNil) |
| 336 |
phpIndex, err := fileSimilarityIndex(php) |
| 337 |
c.Assert(err, IsNil) |
| 338 |
|
| 339 |
testCases := []struct { |
| 340 |
src, dst *similarityIndex |
| 341 |
expectedScore int |
| 342 |
}{ |
| 343 |
{binIndex, binIndex, 10000}, // same file |
| 344 |
{shortIndex, longIndex, 32}, // slightly similar files |
| 345 |
{longIndex, shortIndex, 32}, // same as previous, diff order |
| 346 |
{shortIndex, phpIndex, 1}, // different files |
| 347 |
{longIndex, binIndex, 0}, // code vs binary file |
| 348 |
} |
| 349 |
|
| 350 |
for _, tt := range testCases { |
| 351 |
score := tt.src.score(tt.dst, 10000) |
| 352 |
c.Assert(score, Equals, tt.expectedScore) |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
func (s *SimilarityIndexSuite) TestHashContent(c *C) { |
| 357 |
idx := textIndex(c, "A\n"+ |
| 358 |
"B\n"+ |
| 359 |
"D\n"+ |
| 360 |
"B\n") |
| 361 |
|
| 362 |
keyA := keyFor(c, "A\n") |
| 363 |
keyB := keyFor(c, "B\n") |
| 364 |
keyD := keyFor(c, "D\n") |
| 365 |
|
| 366 |
c.Assert(keyA, Not(Equals), keyB) |
| 367 |
c.Assert(keyA, Not(Equals), keyD) |
| 368 |
c.Assert(keyD, Not(Equals), keyB) |
| 369 |
|
| 370 |
c.Assert(idx.numHashes, Equals, 3) |
| 371 |
c.Assert(idx.hashes[findIndex(idx, keyA)].count(), Equals, uint64(2)) |
| 372 |
c.Assert(idx.hashes[findIndex(idx, keyB)].count(), Equals, uint64(4)) |
| 373 |
c.Assert(idx.hashes[findIndex(idx, keyD)].count(), Equals, uint64(2)) |
| 374 |
} |
| 375 |
|
| 376 |
func (s *SimilarityIndexSuite) TestCommonSameFiles(c *C) { |
| 377 |
content := "A\n" + |
| 378 |
"B\n" + |
| 379 |
"D\n" + |
| 380 |
"B\n" |
| 381 |
|
| 382 |
src := textIndex(c, content) |
| 383 |
dst := textIndex(c, content) |
| 384 |
|
| 385 |
c.Assert(src.common(dst), Equals, uint64(8)) |
| 386 |
c.Assert(dst.common(src), Equals, uint64(8)) |
| 387 |
|
| 388 |
c.Assert(src.score(dst, 100), Equals, 100) |
| 389 |
c.Assert(dst.score(src, 100), Equals, 100) |
| 390 |
} |
| 391 |
|
| 392 |
func (s *SimilarityIndexSuite) TestCommonSameFilesCR(c *C) { |
| 393 |
content := "A\r\n" + |
| 394 |
"B\r\n" + |
| 395 |
"D\r\n" + |
| 396 |
"B\r\n" |
| 397 |
|
| 398 |
src := textIndex(c, content) |
| 399 |
dst := textIndex(c, strings.ReplaceAll(content, "\r", "")) |
| 400 |
|
| 401 |
c.Assert(src.common(dst), Equals, uint64(8)) |
| 402 |
c.Assert(dst.common(src), Equals, uint64(8)) |
| 403 |
|
| 404 |
c.Assert(src.score(dst, 100), Equals, 100) |
| 405 |
c.Assert(dst.score(src, 100), Equals, 100) |
| 406 |
} |
| 407 |
|
| 408 |
func (s *SimilarityIndexSuite) TestCommonEmptyFiles(c *C) { |
| 409 |
src := textIndex(c, "") |
| 410 |
dst := textIndex(c, "") |
| 411 |
|
| 412 |
c.Assert(src.common(dst), Equals, uint64(0)) |
| 413 |
c.Assert(dst.common(src), Equals, uint64(0)) |
| 414 |
} |
| 415 |
|
| 416 |
func (s *SimilarityIndexSuite) TestCommonTotallyDifferentFiles(c *C) { |
| 417 |
src := textIndex(c, "A\n") |
| 418 |
dst := textIndex(c, "D\n") |
| 419 |
|
| 420 |
c.Assert(src.common(dst), Equals, uint64(0)) |
| 421 |
c.Assert(dst.common(src), Equals, uint64(0)) |
| 422 |
} |
| 423 |
|
| 424 |
func (s *SimilarityIndexSuite) TestSimilarity75(c *C) { |
| 425 |
src := textIndex(c, "A\nB\nC\nD\n") |
| 426 |
dst := textIndex(c, "A\nB\nC\nQ\n") |
| 427 |
|
| 428 |
c.Assert(src.common(dst), Equals, uint64(6)) |
| 429 |
c.Assert(dst.common(src), Equals, uint64(6)) |
| 430 |
|
| 431 |
c.Assert(src.score(dst, 100), Equals, 75) |
| 432 |
c.Assert(dst.score(src, 100), Equals, 75) |
| 433 |
} |
| 434 |
|
| 435 |
func keyFor(c *C, line string) int { |
| 436 |
idx := newSimilarityIndex() |
| 437 |
err := idx.hashContent(strings.NewReader(line), int64(len(line)), false) |
| 438 |
c.Assert(err, IsNil) |
| 439 |
|
| 440 |
c.Assert(idx.numHashes, Equals, 1) |
| 441 |
for _, h := range idx.hashes { |
| 442 |
if h != 0 { |
| 443 |
return h.key() |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
return -1 |
| 448 |
} |
| 449 |
|
| 450 |
func textIndex(c *C, content string) *similarityIndex { |
| 451 |
idx := newSimilarityIndex() |
| 452 |
err := idx.hashContent(strings.NewReader(content), int64(len(content)), false) |
| 453 |
c.Assert(err, IsNil) |
| 454 |
return idx |
| 455 |
} |
| 456 |
|
| 457 |
func findIndex(idx *similarityIndex, key int) int { |
| 458 |
for i, h := range idx.hashes { |
| 459 |
if h.key() == key { |
| 460 |
return i |
| 461 |
} |
| 462 |
} |
| 463 |
return -1 |
| 464 |
} |
| 465 |
|
| 466 |
func makeFile(c *C, name string, mode filemode.FileMode, content string) *File { |
| 467 |
obj := new(plumbing.MemoryObject) |
| 468 |
obj.SetType(plumbing.BlobObject) |
| 469 |
_, err := obj.Write([]byte(content)) |
| 470 |
c.Assert(err, IsNil) |
| 471 |
return &File{ |
| 472 |
Name: name, |
| 473 |
Mode: mode, |
| 474 |
Blob: Blob{Hash: obj.Hash(), Size: obj.Size(), obj: obj}, |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
func makeChangeEntry(f *File) ChangeEntry { |
| 479 |
sto := memory.NewStorage() |
| 480 |
sto.SetEncodedObject(f.obj) |
| 481 |
tree := &Tree{s: sto} |
| 482 |
|
| 483 |
return ChangeEntry{ |
| 484 |
Name: f.Name, |
| 485 |
Tree: tree, |
| 486 |
TreeEntry: TreeEntry{ |
| 487 |
Name: filepath.Base(f.Name), |
| 488 |
Mode: f.Mode, |
| 489 |
Hash: f.Hash, |
| 490 |
}, |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
func makeAdd(c *C, f *File) *Change { |
| 495 |
return makeChange(c, nil, f) |
| 496 |
} |
| 497 |
|
| 498 |
func makeDelete(c *C, f *File) *Change { |
| 499 |
return makeChange(c, f, nil) |
| 500 |
} |
| 501 |
|
| 502 |
func makeChange(c *C, from *File, to *File) *Change { |
| 503 |
if from == nil { |
| 504 |
return &Change{To: makeChangeEntry(to)} |
| 505 |
} |
| 506 |
|
| 507 |
if to == nil { |
| 508 |
return &Change{From: makeChangeEntry(from)} |
| 509 |
} |
| 510 |
|
| 511 |
if from == nil && to == nil { |
| 512 |
c.Error("cannot make change without from or to") |
| 513 |
} |
| 514 |
|
| 515 |
return &Change{From: makeChangeEntry(from), To: makeChangeEntry(to)} |
| 516 |
} |