comparison hgext/automv.py @ 43076:2372284d9457

formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:45:02 -0400
parents 2702dfc7e029
children 687b865b95ad
comparison
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
33 error, 33 error,
34 extensions, 34 extensions,
35 pycompat, 35 pycompat,
36 registrar, 36 registrar,
37 scmutil, 37 scmutil,
38 similar 38 similar,
39 ) 39 )
40 40
41 configtable = {} 41 configtable = {}
42 configitem = registrar.configitem(configtable) 42 configitem = registrar.configitem(configtable)
43 43
44 configitem('automv', 'similarity', 44 configitem(
45 default=95, 45 'automv', 'similarity', default=95,
46 ) 46 )
47 47
48
48 def extsetup(ui): 49 def extsetup(ui):
49 entry = extensions.wrapcommand( 50 entry = extensions.wrapcommand(commands.table, 'commit', mvcheck)
50 commands.table, 'commit', mvcheck)
51 entry[1].append( 51 entry[1].append(
52 ('', 'no-automv', None, 52 ('', 'no-automv', None, _('disable automatic file move detection'))
53 _('disable automatic file move detection'))) 53 )
54
54 55
55 def mvcheck(orig, ui, repo, *pats, **opts): 56 def mvcheck(orig, ui, repo, *pats, **opts):
56 """Hook to check for moves at commit time""" 57 """Hook to check for moves at commit time"""
57 opts = pycompat.byteskwargs(opts) 58 opts = pycompat.byteskwargs(opts)
58 renames = None 59 renames = None
63 raise error.Abort(_('automv.similarity must be between 0 and 100')) 64 raise error.Abort(_('automv.similarity must be between 0 and 100'))
64 if threshold > 0: 65 if threshold > 0:
65 match = scmutil.match(repo[None], pats, opts) 66 match = scmutil.match(repo[None], pats, opts)
66 added, removed = _interestingfiles(repo, match) 67 added, removed = _interestingfiles(repo, match)
67 uipathfn = scmutil.getuipathfn(repo, legacyrelativevalue=True) 68 uipathfn = scmutil.getuipathfn(repo, legacyrelativevalue=True)
68 renames = _findrenames(repo, uipathfn, added, removed, 69 renames = _findrenames(
69 threshold / 100.0) 70 repo, uipathfn, added, removed, threshold / 100.0
71 )
70 72
71 with repo.wlock(): 73 with repo.wlock():
72 if renames is not None: 74 if renames is not None:
73 scmutil._markchanges(repo, (), (), renames) 75 scmutil._markchanges(repo, (), (), renames)
74 return orig(ui, repo, *pats, **pycompat.strkwargs(opts)) 76 return orig(ui, repo, *pats, **pycompat.strkwargs(opts))
77
75 78
76 def _interestingfiles(repo, matcher): 79 def _interestingfiles(repo, matcher):
77 """Find what files were added or removed in this commit. 80 """Find what files were added or removed in this commit.
78 81
79 Returns a tuple of two lists: (added, removed). Only files not *already* 82 Returns a tuple of two lists: (added, removed). Only files not *already*
88 # remove the copy files for which we already have copy info 91 # remove the copy files for which we already have copy info
89 added = [f for f in added if f not in copy] 92 added = [f for f in added if f not in copy]
90 93
91 return added, removed 94 return added, removed
92 95
96
93 def _findrenames(repo, uipathfn, added, removed, similarity): 97 def _findrenames(repo, uipathfn, added, removed, similarity):
94 """Find what files in added are really moved files. 98 """Find what files in added are really moved files.
95 99
96 Any file named in removed that is at least similarity% similar to a file 100 Any file named in removed that is at least similarity% similar to a file
97 in added is seen as a rename. 101 in added is seen as a rename.
98 102
99 """ 103 """
100 renames = {} 104 renames = {}
101 if similarity > 0: 105 if similarity > 0:
102 for src, dst, score in similar.findrenames( 106 for src, dst, score in similar.findrenames(
103 repo, added, removed, similarity): 107 repo, added, removed, similarity
108 ):
104 if repo.ui.verbose: 109 if repo.ui.verbose:
105 repo.ui.status( 110 repo.ui.status(
106 _('detected move of %s as %s (%d%% similar)\n') % ( 111 _('detected move of %s as %s (%d%% similar)\n')
107 uipathfn(src), uipathfn(dst), score * 100)) 112 % (uipathfn(src), uipathfn(dst), score * 100)
113 )
108 renames[dst] = src 114 renames[dst] = src
109 if renames: 115 if renames:
110 repo.ui.status(_('detected move of %d files\n') % len(renames)) 116 repo.ui.status(_('detected move of %d files\n') % len(renames))
111 return renames 117 return renames