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