Skip to content

Commit ac735e8

Browse files
committed
Make install -f / --overwrite work.
1 parent 1d8394a commit ac735e8

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

pre_commit/commands/install.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pkg_resources
88
import stat
99

10+
1011
# This is used to identify the hook file we install
1112
IDENTIFYING_HASH = 'd8ee923c46731b42cd95cc869add4062'
1213

@@ -23,7 +24,7 @@ def make_executable(filename):
2324
)
2425

2526

26-
def install(runner):
27+
def install(runner, overwrite=False):
2728
"""Install the pre-commit hooks."""
2829
pre_commit_file = pkg_resources.resource_filename(
2930
'pre_commit', 'resources/pre-commit-hook',
@@ -34,7 +35,18 @@ def install(runner):
3435
os.path.exists(runner.pre_commit_path) and
3536
not is_our_pre_commit(runner.pre_commit_path)
3637
):
37-
os.rename(runner.pre_commit_path, runner.pre_commit_path + '.legacy')
38+
os.rename(runner.pre_commit_path, runner.pre_commit_legacy_path)
39+
40+
# If we specify overwrite, we simply delete the legacy file
41+
if overwrite and os.path.exists(runner.pre_commit_legacy_path):
42+
os.remove(runner.pre_commit_legacy_path)
43+
elif os.path.exists(runner.pre_commit_legacy_path):
44+
print(
45+
'Running in migration mode with existing hooks at {0}\n'
46+
'Use -f to use only pre-commit.'.format(
47+
runner.pre_commit_legacy_path,
48+
)
49+
)
3850

3951
with open(runner.pre_commit_path, 'w') as pre_commit_file_obj:
4052
pre_commit_file_obj.write(open(pre_commit_file).read())

pre_commit/main.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ def main(argv):
2828

2929
subparsers = parser.add_subparsers(dest='command')
3030

31-
subparsers.add_parser('install', help='Intall the pre-commit script.')
31+
install_parser = subparsers.add_parser(
32+
'install', help='Intall the pre-commit script.',
33+
)
34+
install_parser.add_argument(
35+
'-f', '--overwrite', action='store_true',
36+
help='Overwrite existing hooks / remove migration mode.',
37+
)
3238

3339
subparsers.add_parser('uninstall', help='Uninstall the pre-commit script.')
3440

@@ -67,7 +73,7 @@ def main(argv):
6773
runner = Runner.create()
6874

6975
if args.command == 'install':
70-
return install(runner)
76+
return install(runner, overwrite=args.overwrite)
7177
elif args.command == 'uninstall':
7278
return uninstall(runner)
7379
elif args.command == 'clean':

pre_commit/runner.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ def repositories(self):
4141

4242
@cached_property
4343
def pre_commit_path(self):
44-
return os.path.join(self.git_root, '.git/hooks/pre-commit')
44+
return os.path.join(self.git_root, '.git', 'hooks', 'pre-commit')
45+
46+
@cached_property
47+
def pre_commit_legacy_path(self):
48+
"""The path in the 'hooks' directory representing the temporary
49+
storage for existing pre-commit hooks.
50+
"""
51+
return self.pre_commit_path + '.legacy'
4552

4653
@cached_property
4754
def cmd_runner(self):

tests/commands/install_test.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,28 @@ def test_install_existing_hooks_no_overwrite(tmpdir_factory):
156156
assert EXISTING_COMMIT_RUN.match(output)
157157

158158
# Now install pre-commit (no-overwrite)
159-
assert install(Runner(path)) == 0
159+
assert install(runner) == 0
160+
161+
# We should run both the legacy and pre-commit hooks
162+
ret, output = _get_commit_output(tmpdir_factory)
163+
assert ret == 0
164+
assert output.startswith('legacy hook\n')
165+
assert NORMAL_PRE_COMMIT_RUN.match(output[len('legacy hook\n'):])
166+
167+
168+
def test_install_existing_hook_no_overwrite_idempotent(tmpdir_factory):
169+
path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo')
170+
with local.cwd(path):
171+
runner = Runner(path)
172+
173+
# Write out an "old" hook
174+
with io.open(runner.pre_commit_path, 'w') as hook_file:
175+
hook_file.write('#!/usr/bin/env bash\necho "legacy hook"\n')
176+
make_executable(runner.pre_commit_path)
177+
178+
# Install twice
179+
assert install(runner) == 0
180+
assert install(runner) == 0
160181

161182
# We should run both the legacy and pre-commit hooks
162183
ret, output = _get_commit_output(tmpdir_factory)
@@ -184,9 +205,36 @@ def test_failing_existing_hook_returns_1(tmpdir_factory):
184205
hook_file.write('#!/usr/bin/env bash\necho "fail!"\nexit 1\n')
185206
make_executable(runner.pre_commit_path)
186207

187-
assert install(Runner(path)) == 0
208+
assert install(runner) == 0
188209

189210
# We should get a failure from the legacy hook
190211
ret, output = _get_commit_output(tmpdir_factory)
191212
assert ret == 1
192213
assert FAIL_OLD_HOOK.match(output)
214+
215+
216+
def test_install_overwrite_no_existing_hooks(tmpdir_factory):
217+
path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo')
218+
with local.cwd(path):
219+
assert install(Runner(path), overwrite=True) == 0
220+
221+
ret, output = _get_commit_output(tmpdir_factory)
222+
assert ret == 0
223+
assert NORMAL_PRE_COMMIT_RUN.match(output)
224+
225+
226+
def test_install_overwrite(tmpdir_factory):
227+
path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo')
228+
with local.cwd(path):
229+
runner = Runner(path)
230+
231+
# Write out the "old" hook
232+
with io.open(runner.pre_commit_path, 'w') as hook_file:
233+
hook_file.write('#!/usr/bin/env bash\necho "legacy hook"\n')
234+
make_executable(runner.pre_commit_path)
235+
236+
assert install(runner, overwrite=True) == 0
237+
238+
ret, output = _get_commit_output(tmpdir_factory)
239+
assert ret == 0
240+
assert NORMAL_PRE_COMMIT_RUN.match(output)

0 commit comments

Comments
 (0)