99import sys
1010
1111from pre_commit .logging_handler import LoggingHandler
12- from pre_commit .util import resource_filename
1312
1413
1514logger = logging .getLogger ('pre_commit' )
@@ -42,37 +41,55 @@ def make_executable(filename):
4241 )
4342
4443
45- def install (runner , overwrite = False , hooks = False ):
44+ def get_hook_path (runner , hook_type ):
45+ if hook_type == 'pre-commit' :
46+ hook_path = runner .pre_commit_path
47+ legacy_path = runner .pre_commit_legacy_path
48+ else :
49+ hook_path = runner .pre_push_path
50+ legacy_path = runner .pre_push_legacy_path
51+ return hook_path , legacy_path
52+
53+
54+ def install (runner , overwrite = False , hooks = False , hook_type = 'pre-commit' ):
4655 """Install the pre-commit hooks."""
47- pre_commit_file = resource_filename ( 'pre-commit-hook' )
56+ hook_path , legacy_path = get_hook_path ( runner , hook_type )
4857
4958 # If we have an existing hook, move it to pre-commit.legacy
5059 if (
51- os .path .exists (runner . pre_commit_path ) and
52- not is_our_pre_commit (runner . pre_commit_path ) and
53- not is_previous_pre_commit (runner . pre_commit_path )
60+ os .path .exists (hook_path ) and
61+ not is_our_pre_commit (hook_path ) and
62+ not is_previous_pre_commit (hook_path )
5463 ):
55- os .rename (runner . pre_commit_path , runner . pre_commit_legacy_path )
64+ os .rename (hook_path , legacy_path )
5665
5766 # If we specify overwrite, we simply delete the legacy file
58- if overwrite and os .path .exists (runner . pre_commit_legacy_path ):
59- os .remove (runner . pre_commit_legacy_path )
60- elif os .path .exists (runner . pre_commit_legacy_path ):
67+ if overwrite and os .path .exists (legacy_path ):
68+ os .remove (legacy_path )
69+ elif os .path .exists (legacy_path ):
6170 print (
6271 'Running in migration mode with existing hooks at {0}\n '
6372 'Use -f to use only pre-commit.' .format (
64- runner . pre_commit_legacy_path ,
73+ legacy_path ,
6574 )
6675 )
6776
68- with io .open (runner .pre_commit_path , 'w' ) as pre_commit_file_obj :
69- contents = io .open (pre_commit_file ).read ().format (
77+ with io .open (hook_path , 'w' ) as pre_commit_file_obj :
78+ if hook_type == 'pre-push' :
79+ with io .open (runner .pre_push_template ) as fp :
80+ pre_push_contents = fp .read ()
81+ else :
82+ pre_push_contents = ''
83+
84+ contents = io .open (runner .pre_template ).read ().format (
7085 sys_executable = sys .executable ,
86+ hook_type = hook_type ,
87+ pre_push = pre_push_contents ,
7188 )
7289 pre_commit_file_obj .write (contents )
73- make_executable (runner . pre_commit_path )
90+ make_executable (hook_path )
7491
75- print ('pre-commit installed at {0}' .format (runner . pre_commit_path ))
92+ print ('pre-commit installed at {0}' .format (hook_path ))
7693
7794 # If they requested we install all of the hooks, do so.
7895 if hooks :
@@ -85,22 +102,23 @@ def install(runner, overwrite=False, hooks=False):
85102 return 0
86103
87104
88- def uninstall (runner ):
105+ def uninstall (runner , hook_type = 'pre-commit' ):
89106 """Uninstall the pre-commit hooks."""
107+ hook_path , legacy_path = get_hook_path (runner , hook_type )
90108 # If our file doesn't exist or it isn't ours, gtfo.
91109 if (
92- not os .path .exists (runner . pre_commit_path ) or (
93- not is_our_pre_commit (runner . pre_commit_path ) and
94- not is_previous_pre_commit (runner . pre_commit_path )
110+ not os .path .exists (hook_path ) or (
111+ not is_our_pre_commit (hook_path ) and
112+ not is_previous_pre_commit (hook_path )
95113 )
96114 ):
97115 return 0
98116
99- os .remove (runner . pre_commit_path )
100- print ('pre-commit uninstalled' )
117+ os .remove (hook_path )
118+ print ('{0} uninstalled' . format ( hook_type ) )
101119
102- if os .path .exists (runner . pre_commit_legacy_path ):
103- os .rename (runner . pre_commit_legacy_path , runner . pre_commit_path )
104- print ('Restored previous hooks to {0}' .format (runner . pre_commit_path ))
120+ if os .path .exists (legacy_path ):
121+ os .rename (legacy_path , hook_path )
122+ print ('Restored previous hooks to {0}' .format (hook_path ))
105123
106124 return 0
0 commit comments