Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3867394

Browse files
committed
Merge pull request #119 from pre-commit/migration_mode
Migration mode
2 parents 332e8b4 + 0cde0fd commit 3867394

14 files changed

Lines changed: 412 additions & 107 deletions

File tree

pre_commit/commands/install.py

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from __future__ import print_function
2+
from __future__ import unicode_literals
3+
4+
import io
5+
import os
6+
import os.path
7+
import pkg_resources
8+
import stat
9+
10+
11+
# This is used to identify the hook file we install
12+
IDENTIFYING_HASH = 'd8ee923c46731b42cd95cc869add4062'
13+
14+
15+
def is_our_pre_commit(filename):
16+
return IDENTIFYING_HASH in io.open(filename).read()
17+
18+
19+
def make_executable(filename):
20+
original_mode = os.stat(filename).st_mode
21+
os.chmod(
22+
filename,
23+
original_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH,
24+
)
25+
26+
27+
def install(runner, overwrite=False):
28+
"""Install the pre-commit hooks."""
29+
pre_commit_file = pkg_resources.resource_filename(
30+
'pre_commit', 'resources/pre-commit-hook',
31+
)
32+
33+
# If we have an existing hook, move it to pre-commit.legacy
34+
if (
35+
os.path.exists(runner.pre_commit_path) and
36+
not is_our_pre_commit(runner.pre_commit_path)
37+
):
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+
)
50+
51+
with open(runner.pre_commit_path, 'w') as pre_commit_file_obj:
52+
pre_commit_file_obj.write(open(pre_commit_file).read())
53+
make_executable(runner.pre_commit_path)
54+
55+
print('pre-commit installed at {0}'.format(runner.pre_commit_path))
56+
return 0
57+
58+
59+
def uninstall(runner):
60+
"""Uninstall the pre-commit hooks."""
61+
if os.path.exists(runner.pre_commit_path):
62+
os.remove(runner.pre_commit_path)
63+
print('pre-commit uninstalled')
64+
65+
if os.path.exists(runner.pre_commit_legacy_path):
66+
os.rename(runner.pre_commit_legacy_path, runner.pre_commit_path)
67+
print('Restored previous hooks to {0}'.format(runner.pre_commit_path))
68+
69+
return 0

pre_commit/commands/uninstall.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

pre_commit/main.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from pre_commit import color
77
from pre_commit.commands.autoupdate import autoupdate
88
from pre_commit.commands.clean import clean
9-
from pre_commit.commands.install import install
9+
from pre_commit.commands.install_uninstall import install
10+
from pre_commit.commands.install_uninstall import uninstall
1011
from pre_commit.commands.run import run
11-
from pre_commit.commands.uninstall import uninstall
1212
from pre_commit.runner import Runner
1313
from pre_commit.util import entry
1414

@@ -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':
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
# This is a randomish md5 to identify this script
3+
# d8ee923c46731b42cd95cc869add4062
4+
5+
HERE=$(dirname $(readlink -f "$0"))
6+
7+
retv=0
8+
9+
which pre-commit > /dev/null
10+
if [ $? -ne 0 ]; then
11+
echo '`pre-commit` not found. Did you forget to activate your virtualenv?'
12+
exit 1
13+
fi
14+
15+
16+
# Run the legacy pre-commit if it exists
17+
if [ -x "$HERE"/pre-commit.legacy ]; then
18+
"$HERE"/pre-commit.legacy
19+
if [ $? -ne 0 ]; then
20+
retv=1
21+
fi
22+
fi
23+
24+
25+
# Run pre-commit
26+
pre-commit
27+
if [ $? -ne 0 ]; then
28+
retv=1
29+
fi
30+
31+
exit $retv

pre_commit/resources/pre-commit.sh

Lines changed: 0 additions & 9 deletions
This file was deleted.

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):

pre_commit/store.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ def _get_default_directory():
2222
`Store.get_default_directory` can be mocked in tests and
2323
`_get_default_directory` can be tested.
2424
"""
25-
return os.path.join(os.environ['HOME'], '.pre-commit')
25+
return os.environ.get(
26+
'PRE_COMMIT_HOME',
27+
os.path.join(os.environ['HOME'], '.pre-commit'),
28+
)
2629

2730

2831
class Store(object):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
packages=find_packages('.', exclude=('tests*', 'testing*')),
2828
package_data={
2929
'pre_commit': [
30-
'resources/pre-commit.sh'
30+
'resources/pre-commit-hook'
3131
]
3232
},
3333
install_requires=[

testing/fixtures.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,7 @@ def make_consuming_repo(tmpdir_factory, repo_source):
6666
config = make_config_from_repo(path)
6767
git_path = git_dir(tmpdir_factory)
6868
write_config(git_path, config)
69+
with local.cwd(git_path):
70+
git('add', C.CONFIG_FILE)
71+
git('commit', '-m', 'Add hooks config')
6972
return git_path

0 commit comments

Comments
 (0)