##// END OF EJS Templates
similar: delete extra newline at EOF...
Augie Fackler -
r29337:f72d0c21 default
parent child Browse files
Show More
@@ -1,109 +1,108
1 1 # similar.py - mechanisms for finding similar files
2 2 #
3 3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 from __future__ import absolute_import
9 9
10 10 from .i18n import _
11 11 from . import (
12 12 bdiff,
13 13 mdiff,
14 14 util,
15 15 )
16 16
17 17 def _findexactmatches(repo, added, removed):
18 18 '''find renamed files that have no changes
19 19
20 20 Takes a list of new filectxs and a list of removed filectxs, and yields
21 21 (before, after) tuples of exact matches.
22 22 '''
23 23 numfiles = len(added) + len(removed)
24 24
25 25 # Get hashes of removed files.
26 26 hashes = {}
27 27 for i, fctx in enumerate(removed):
28 28 repo.ui.progress(_('searching for exact renames'), i, total=numfiles,
29 29 unit=_('files'))
30 30 h = util.sha1(fctx.data()).digest()
31 31 hashes[h] = fctx
32 32
33 33 # For each added file, see if it corresponds to a removed file.
34 34 for i, fctx in enumerate(added):
35 35 repo.ui.progress(_('searching for exact renames'), i + len(removed),
36 36 total=numfiles, unit=_('files'))
37 37 h = util.sha1(fctx.data()).digest()
38 38 if h in hashes:
39 39 yield (hashes[h], fctx)
40 40
41 41 # Done
42 42 repo.ui.progress(_('searching for exact renames'), None)
43 43
44 44 def _findsimilarmatches(repo, added, removed, threshold):
45 45 '''find potentially renamed files based on similar file content
46 46
47 47 Takes a list of new filectxs and a list of removed filectxs, and yields
48 48 (before, after, score) tuples of partial matches.
49 49 '''
50 50 copies = {}
51 51 for i, r in enumerate(removed):
52 52 repo.ui.progress(_('searching for similar files'), i,
53 53 total=len(removed), unit=_('files'))
54 54
55 55 # lazily load text
56 56 @util.cachefunc
57 57 def data():
58 58 orig = r.data()
59 59 return orig, mdiff.splitnewlines(orig)
60 60
61 61 def score(text):
62 62 orig, lines = data()
63 63 # bdiff.blocks() returns blocks of matching lines
64 64 # count the number of bytes in each
65 65 equal = 0
66 66 matches = bdiff.blocks(text, orig)
67 67 for x1, x2, y1, y2 in matches:
68 68 for line in lines[y1:y2]:
69 69 equal += len(line)
70 70
71 71 lengths = len(text) + len(orig)
72 72 return equal * 2.0 / lengths
73 73
74 74 for a in added:
75 75 bestscore = copies.get(a, (None, threshold))[1]
76 76 myscore = score(a.data())
77 77 if myscore >= bestscore:
78 78 copies[a] = (r, myscore)
79 79 repo.ui.progress(_('searching'), None)
80 80
81 81 for dest, v in copies.iteritems():
82 82 source, score = v
83 83 yield source, dest, score
84 84
85 85 def findrenames(repo, added, removed, threshold):
86 86 '''find renamed files -- yields (before, after, score) tuples'''
87 87 parentctx = repo['.']
88 88 workingctx = repo[None]
89 89
90 90 # Zero length files will be frequently unrelated to each other, and
91 91 # tracking the deletion/addition of such a file will probably cause more
92 92 # harm than good. We strip them out here to avoid matching them later on.
93 93 addedfiles = set([workingctx[fp] for fp in added
94 94 if workingctx[fp].size() > 0])
95 95 removedfiles = set([parentctx[fp] for fp in removed
96 96 if fp in parentctx and parentctx[fp].size() > 0])
97 97
98 98 # Find exact matches.
99 99 for (a, b) in _findexactmatches(repo,
100 100 sorted(addedfiles), sorted(removedfiles)):
101 101 addedfiles.remove(b)
102 102 yield (a.path(), b.path(), 1.0)
103 103
104 104 # If the user requested similar files to be matched, search for them also.
105 105 if threshold < 1.0:
106 106 for (a, b, score) in _findsimilarmatches(repo,
107 107 sorted(addedfiles), sorted(removedfiles), threshold):
108 108 yield (a.path(), b.path(), score)
109
General Comments 0
You need to be logged in to leave comments. Login now