From 4da31aa521fe0c66c2577f49cb7002a8d438bfbf Mon Sep 17 00:00:00 2001 From: adammhaile Date: Wed, 16 Dec 2015 22:40:57 -0500 Subject: [PATCH 1/2] Fix checking for repo dir without git/hg --- flake8/hooks.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/flake8/hooks.py b/flake8/hooks.py index 2d1db8d..c25b9aa 100644 --- a/flake8/hooks.py +++ b/flake8/hooks.py @@ -155,14 +155,20 @@ def _get_files(repo, **kwargs): def find_vcs(): - _, git_dir, _ = run('git rev-parse --git-dir') - if git_dir and os.path.isdir(git_dir[0]): - if not os.path.isdir(os.path.join(git_dir[0], 'hooks')): - os.mkdir(os.path.join(git_dir[0], 'hooks')) - return os.path.join(git_dir[0], 'hooks', 'pre-commit') - _, hg_dir, _ = run('hg root') - if hg_dir and os.path.isdir(hg_dir[0]): - return os.path.join(hg_dir[0], '.hg', 'hgrc') + try: + _, git_dir, _ = run('git rev-parse --git-dir') + if git_dir and os.path.isdir(git_dir[0]): + if not os.path.isdir(os.path.join(git_dir[0], 'hooks')): + os.mkdir(os.path.join(git_dir[0], 'hooks')) + return os.path.join(git_dir[0], 'hooks', 'pre-commit') + except OSError: + pass + try: + _, hg_dir, _ = run('hg root') + if hg_dir and os.path.isdir(hg_dir[0]): + return os.path.join(hg_dir[0], '.hg', 'hgrc') + except OSError: + pass return '' @@ -269,7 +275,7 @@ def install_hook(): p = get_parser()[0] sys.stderr.write('Error: could not find either a git or mercurial ' 'directory. Please re-run this in a proper ' - 'repository.') + 'repository.\n') p.print_help() sys.exit(1) From fa1105aaac3f00cb2b7b7dea092f3b6541769753 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Thu, 17 Dec 2015 08:35:50 -0600 Subject: [PATCH 2/2] Isolate the commands that raise OSError Right now, running the git and hg commands could easily raise an OSError. Let's only catch it in those cases and then use else to install hooks when those are successful. Otherwise, pass on to the next supported VCS --- flake8/hooks.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/flake8/hooks.py b/flake8/hooks.py index c25b9aa..14f844e 100644 --- a/flake8/hooks.py +++ b/flake8/hooks.py @@ -157,18 +157,20 @@ def _get_files(repo, **kwargs): def find_vcs(): try: _, git_dir, _ = run('git rev-parse --git-dir') + except OSError: + pass + else: if git_dir and os.path.isdir(git_dir[0]): if not os.path.isdir(os.path.join(git_dir[0], 'hooks')): os.mkdir(os.path.join(git_dir[0], 'hooks')) return os.path.join(git_dir[0], 'hooks', 'pre-commit') - except OSError: - pass try: _, hg_dir, _ = run('hg root') - if hg_dir and os.path.isdir(hg_dir[0]): - return os.path.join(hg_dir[0], '.hg', 'hgrc') except OSError: pass + else: + if hg_dir and os.path.isdir(hg_dir[0]): + return os.path.join(hg_dir[0], '.hg', 'hgrc') return ''