Show More
@@ -1,124 +1,126 | |||||
1 | # automv.py |
|
1 | # automv.py | |
2 | # |
|
2 | # | |
3 | # Copyright 2013-2016 Facebook, Inc. |
|
3 | # Copyright 2013-2016 Facebook, Inc. | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 | """check for unrecorded moves at commit time (EXPERIMENTAL) |
|
7 | """check for unrecorded moves at commit time (EXPERIMENTAL) | |
8 |
|
8 | |||
9 | This extension checks at commit/amend time if any of the committed files |
|
9 | This extension checks at commit/amend time if any of the committed files | |
10 | comes from an unrecorded mv. |
|
10 | comes from an unrecorded mv. | |
11 |
|
11 | |||
12 | The threshold at which a file is considered a move can be set with the |
|
12 | The threshold at which a file is considered a move can be set with the | |
13 | ``automv.similarity`` config option. This option takes a percentage between 0 |
|
13 | ``automv.similarity`` config option. This option takes a percentage between 0 | |
14 | (disabled) and 100 (files must be identical), the default is 95. |
|
14 | (disabled) and 100 (files must be identical), the default is 95. | |
15 |
|
15 | |||
16 | """ |
|
16 | """ | |
17 |
|
17 | |||
18 | # Using 95 as a default similarity is based on an analysis of the mercurial |
|
18 | # Using 95 as a default similarity is based on an analysis of the mercurial | |
19 | # repositories of the cpython, mozilla-central & mercurial repositories, as |
|
19 | # repositories of the cpython, mozilla-central & mercurial repositories, as | |
20 | # well as 2 very large facebook repositories. At 95 50% of all potential |
|
20 | # well as 2 very large facebook repositories. At 95 50% of all potential | |
21 | # missed moves would be caught, as well as correspond with 87% of all |
|
21 | # missed moves would be caught, as well as correspond with 87% of all | |
22 | # explicitly marked moves. Together, 80% of moved files are 95% similar or |
|
22 | # explicitly marked moves. Together, 80% of moved files are 95% similar or | |
23 | # more. |
|
23 | # more. | |
24 | # |
|
24 | # | |
25 | # See http://markmail.org/thread/5pxnljesvufvom57 for context. |
|
25 | # See http://markmail.org/thread/5pxnljesvufvom57 for context. | |
26 |
|
26 | |||
27 |
|
27 | |||
28 | from mercurial.i18n import _ |
|
28 | from mercurial.i18n import _ | |
29 | from mercurial import ( |
|
29 | from mercurial import ( | |
30 | commands, |
|
30 | commands, | |
31 | copies, |
|
31 | copies, | |
32 | error, |
|
32 | error, | |
33 | extensions, |
|
33 | extensions, | |
34 | pycompat, |
|
34 | pycompat, | |
35 | registrar, |
|
35 | registrar, | |
36 | scmutil, |
|
36 | scmutil, | |
37 | similar, |
|
37 | similar, | |
38 | ) |
|
38 | ) | |
39 |
|
39 | |||
40 | configtable = {} |
|
40 | configtable = {} | |
41 | configitem = registrar.configitem(configtable) |
|
41 | configitem = registrar.configitem(configtable) | |
42 |
|
42 | |||
43 | configitem( |
|
43 | configitem( | |
44 | b'automv', |
|
44 | b'automv', | |
45 | b'similarity', |
|
45 | b'similarity', | |
46 | default=95, |
|
46 | default=95, | |
47 | ) |
|
47 | ) | |
48 |
|
48 | |||
49 |
|
49 | |||
50 | def extsetup(ui): |
|
50 | def extsetup(ui): | |
51 | entry = extensions.wrapcommand(commands.table, b'commit', mvcheck) |
|
51 | entry = extensions.wrapcommand(commands.table, b'commit', mvcheck) | |
52 | entry[1].append( |
|
52 | entry[1].append( | |
53 | (b'', b'no-automv', None, _(b'disable automatic file move detection')) |
|
53 | (b'', b'no-automv', None, _(b'disable automatic file move detection')) | |
54 | ) |
|
54 | ) | |
55 |
|
55 | |||
56 |
|
56 | |||
57 | def mvcheck(orig, ui, repo, *pats, **opts): |
|
57 | def mvcheck(orig, ui, repo, *pats, **opts): | |
58 | """Hook to check for moves at commit time""" |
|
58 | """Hook to check for moves at commit time""" | |
59 | opts = pycompat.byteskwargs(opts) |
|
59 | opts = pycompat.byteskwargs(opts) | |
60 | renames = None |
|
60 | renames = None | |
61 | disabled = opts.pop(b'no_automv', False) |
|
61 | disabled = opts.pop(b'no_automv', False) | |
62 | if not disabled: |
|
62 | with repo.wlock(): | |
63 | threshold = ui.configint(b'automv', b'similarity') |
|
63 | if not disabled: | |
64 | if not 0 <= threshold <= 100: |
|
64 | threshold = ui.configint(b'automv', b'similarity') | |
65 | raise error.Abort(_(b'automv.similarity must be between 0 and 100')) |
|
65 | if not 0 <= threshold <= 100: | |
66 | if threshold > 0: |
|
66 | raise error.Abort( | |
67 | match = scmutil.match(repo[None], pats, opts) |
|
67 | _(b'automv.similarity must be between 0 and 100') | |
68 | added, removed = _interestingfiles(repo, match) |
|
68 | ) | |
69 | uipathfn = scmutil.getuipathfn(repo, legacyrelativevalue=True) |
|
69 | if threshold > 0: | |
70 | renames = _findrenames( |
|
70 | match = scmutil.match(repo[None], pats, opts) | |
71 | repo, uipathfn, added, removed, threshold / 100.0 |
|
71 | added, removed = _interestingfiles(repo, match) | |
72 | ) |
|
72 | uipathfn = scmutil.getuipathfn(repo, legacyrelativevalue=True) | |
|
73 | renames = _findrenames( | |||
|
74 | repo, uipathfn, added, removed, threshold / 100.0 | |||
|
75 | ) | |||
73 |
|
76 | |||
74 | with repo.wlock(): |
|
|||
75 | if renames is not None: |
|
77 | if renames is not None: | |
76 | with repo.dirstate.changing_files(repo): |
|
78 | with repo.dirstate.changing_files(repo): | |
77 | # XXX this should be wider and integrated with the commit |
|
79 | # XXX this should be wider and integrated with the commit | |
78 | # transaction. At the same time as we do the `addremove` logic |
|
80 | # transaction. At the same time as we do the `addremove` logic | |
79 | # for commit. However we can't really do better with the |
|
81 | # for commit. However we can't really do better with the | |
80 | # current extension structure, and this is not worse than what |
|
82 | # current extension structure, and this is not worse than what | |
81 | # happened before. |
|
83 | # happened before. | |
82 | scmutil._markchanges(repo, (), (), renames) |
|
84 | scmutil._markchanges(repo, (), (), renames) | |
83 | return orig(ui, repo, *pats, **pycompat.strkwargs(opts)) |
|
85 | return orig(ui, repo, *pats, **pycompat.strkwargs(opts)) | |
84 |
|
86 | |||
85 |
|
87 | |||
86 | def _interestingfiles(repo, matcher): |
|
88 | def _interestingfiles(repo, matcher): | |
87 | """Find what files were added or removed in this commit. |
|
89 | """Find what files were added or removed in this commit. | |
88 |
|
90 | |||
89 | Returns a tuple of two lists: (added, removed). Only files not *already* |
|
91 | Returns a tuple of two lists: (added, removed). Only files not *already* | |
90 | marked as moved are included in the added list. |
|
92 | marked as moved are included in the added list. | |
91 |
|
93 | |||
92 | """ |
|
94 | """ | |
93 | stat = repo.status(match=matcher) |
|
95 | stat = repo.status(match=matcher) | |
94 | added = stat.added |
|
96 | added = stat.added | |
95 | removed = stat.removed |
|
97 | removed = stat.removed | |
96 |
|
98 | |||
97 | copy = copies.pathcopies(repo[b'.'], repo[None], matcher) |
|
99 | copy = copies.pathcopies(repo[b'.'], repo[None], matcher) | |
98 | # remove the copy files for which we already have copy info |
|
100 | # remove the copy files for which we already have copy info | |
99 | added = [f for f in added if f not in copy] |
|
101 | added = [f for f in added if f not in copy] | |
100 |
|
102 | |||
101 | return added, removed |
|
103 | return added, removed | |
102 |
|
104 | |||
103 |
|
105 | |||
104 | def _findrenames(repo, uipathfn, added, removed, similarity): |
|
106 | def _findrenames(repo, uipathfn, added, removed, similarity): | |
105 | """Find what files in added are really moved files. |
|
107 | """Find what files in added are really moved files. | |
106 |
|
108 | |||
107 | Any file named in removed that is at least similarity% similar to a file |
|
109 | Any file named in removed that is at least similarity% similar to a file | |
108 | in added is seen as a rename. |
|
110 | in added is seen as a rename. | |
109 |
|
111 | |||
110 | """ |
|
112 | """ | |
111 | renames = {} |
|
113 | renames = {} | |
112 | if similarity > 0: |
|
114 | if similarity > 0: | |
113 | for src, dst, score in similar.findrenames( |
|
115 | for src, dst, score in similar.findrenames( | |
114 | repo, added, removed, similarity |
|
116 | repo, added, removed, similarity | |
115 | ): |
|
117 | ): | |
116 | if repo.ui.verbose: |
|
118 | if repo.ui.verbose: | |
117 | repo.ui.status( |
|
119 | repo.ui.status( | |
118 | _(b'detected move of %s as %s (%d%% similar)\n') |
|
120 | _(b'detected move of %s as %s (%d%% similar)\n') | |
119 | % (uipathfn(src), uipathfn(dst), score * 100) |
|
121 | % (uipathfn(src), uipathfn(dst), score * 100) | |
120 | ) |
|
122 | ) | |
121 | renames[dst] = src |
|
123 | renames[dst] = src | |
122 | if renames: |
|
124 | if renames: | |
123 | repo.ui.status(_(b'detected move of %d files\n') % len(renames)) |
|
125 | repo.ui.status(_(b'detected move of %d files\n') % len(renames)) | |
124 | return renames |
|
126 | return renames |
General Comments 0
You need to be logged in to leave comments.
Login now