From 023de21fe209818282a188edc38c0d4e97f97e6b Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Thu, 16 Jun 2016 07:20:09 -0500 Subject: [PATCH] Find filenames for mercurial hook Extract the files changed in a particular commit or set of commits for the commit and qrefresh mercurial hooks. --- flake8/main/mercurial.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/flake8/main/mercurial.py b/flake8/main/mercurial.py index 10e5c06..d067612 100644 --- a/flake8/main/mercurial.py +++ b/flake8/main/mercurial.py @@ -32,8 +32,10 @@ def hook(ui, repo, **kwargs): hgconfig = configparser_for(hgrc) strict = hgconfig.get('flake8', 'strict', fallback=True) + filenames = list(get_filenames_from(repo, kwargs)) + app = application.Application() - app.run() + app.run(filenames) if strict: return app.result_count @@ -78,6 +80,22 @@ def install(): return True +def get_filenames_from(repository, kwargs): + seen_filenames = set() + node = kwargs['node'] + for revision in range(repository[node], len(repository)): + for filename in repository[revision].files(): + full_filename = os.path.join(repository.root, filename) + have_seen_filename = full_filename in seen_filenames + filename_does_not_exist = not os.path.exists(full_filename) + if have_seen_filename or filename_does_not_exist: + continue + + seen_filenames.add(full_filename) + if full_filename.endswith('.py'): + yield full_filename + + def find_hgrc(create_if_missing=False): root = subprocess.Popen( ['hg', 'root'],