From cd61269389bd4925d58054995a5b3e06cf367efc Mon Sep 17 00:00:00 2001 From: Tristan Carel Date: Wed, 27 Mar 2019 06:24:47 +0100 Subject: [PATCH 1/3] Do not run legacy script again when this is the one being executed --- pre_commit/resources/hook-tmpl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pre_commit/resources/hook-tmpl b/pre_commit/resources/hook-tmpl index f455ca35..3703b9b9 100755 --- a/pre_commit/resources/hook-tmpl +++ b/pre_commit/resources/hook-tmpl @@ -54,8 +54,10 @@ def _run_legacy(): else: stdin = None - legacy_hook = os.path.join(HERE, '{}.legacy'.format(HOOK_TYPE)) - if os.access(legacy_hook, os.X_OK): + legacy_script = HOOK_TYPE + '.legacy' + is_legacy_executed = os.path.basename(__file__) == legacy_script + legacy_hook = os.path.join(HERE, legacy_script) + if not is_legacy_executed and os.access(legacy_hook, os.X_OK): cmd = _norm_exe(legacy_hook) + (legacy_hook,) + tuple(sys.argv[1:]) proc = subprocess.Popen(cmd, stdin=subprocess.PIPE if stdin else None) proc.communicate(stdin) From ec72cb7260b0822afd0f6a869bc5a28e6ebcd9b5 Mon Sep 17 00:00:00 2001 From: Tristan Carel Date: Fri, 29 Mar 2019 13:55:04 +0100 Subject: [PATCH 2/3] assert that the pre-commit script being executed is not the legacy --- pre_commit/resources/hook-tmpl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pre_commit/resources/hook-tmpl b/pre_commit/resources/hook-tmpl index 3703b9b9..4bfb2398 100755 --- a/pre_commit/resources/hook-tmpl +++ b/pre_commit/resources/hook-tmpl @@ -57,7 +57,8 @@ def _run_legacy(): legacy_script = HOOK_TYPE + '.legacy' is_legacy_executed = os.path.basename(__file__) == legacy_script legacy_hook = os.path.join(HERE, legacy_script) - if not is_legacy_executed and os.access(legacy_hook, os.X_OK): + assert not is_legacy_executed, __file__ + if os.access(legacy_hook, os.X_OK): cmd = _norm_exe(legacy_hook) + (legacy_hook,) + tuple(sys.argv[1:]) proc = subprocess.Popen(cmd, stdin=subprocess.PIPE if stdin else None) proc.communicate(stdin) From bbc3130af224d0d812f25aaed5bda5dbedbe0f55 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 30 Mar 2019 13:24:53 -0700 Subject: [PATCH 3/3] Produce slightly more helpful message --- pre_commit/resources/hook-tmpl | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/pre_commit/resources/hook-tmpl b/pre_commit/resources/hook-tmpl index 4bfb2398..b706d5ae 100755 --- a/pre_commit/resources/hook-tmpl +++ b/pre_commit/resources/hook-tmpl @@ -49,15 +49,22 @@ def _norm_exe(exe): def _run_legacy(): + if __file__.endswith('.legacy'): + raise SystemExit( + "bug: pre-commit's script is installed in migration mode\n" + 'run `pre-commit install -f --hook-type {}` to fix this\n\n' + 'Please report this bug at ' + 'https://github.com/pre-commit/pre-commit/issues'.format( + HOOK_TYPE, + ), + ) + if HOOK_TYPE == 'pre-push': stdin = getattr(sys.stdin, 'buffer', sys.stdin).read() else: stdin = None - legacy_script = HOOK_TYPE + '.legacy' - is_legacy_executed = os.path.basename(__file__) == legacy_script - legacy_hook = os.path.join(HERE, legacy_script) - assert not is_legacy_executed, __file__ + legacy_hook = os.path.join(HERE, '{}.legacy'.format(HOOK_TYPE)) if os.access(legacy_hook, os.X_OK): cmd = _norm_exe(legacy_hook) + (legacy_hook,) + tuple(sys.argv[1:]) proc = subprocess.Popen(cmd, stdin=subprocess.PIPE if stdin else None)