File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+
2+ import os
3+ import pkg_resources
4+ import stat
5+
6+
7+ def install (runner ):
8+ """Install the pre-commit hooks."""
9+ pre_commit_file = pkg_resources .resource_filename ('pre_commit' , 'resources/pre-commit.sh' )
10+ with open (runner .pre_commit_path , 'w' ) as pre_commit_file_obj :
11+ pre_commit_file_obj .write (open (pre_commit_file ).read ())
12+
13+ original_mode = os .stat (runner .pre_commit_path ).st_mode
14+ os .chmod (
15+ runner .pre_commit_path ,
16+ original_mode | stat .S_IXUSR | stat .S_IXGRP | stat .S_IXOTH ,
17+ )
18+ return 0
19+
20+
21+ def uninstall (runner ):
22+ """Uninstall the pre-commit hooks."""
23+ if os .path .exists (runner .pre_commit_path ):
24+ os .remove (runner .pre_commit_path )
25+ return 0
Original file line number Diff line number Diff line change 11import functools
22import os
33import os .path
4- import pkg_resources
54import re
6- import stat
75from plumbum import local
86
97from pre_commit .util import memoize_by_cwd
@@ -24,23 +22,6 @@ def get_root():
2422 return _get_root_new ()
2523
2624
27- @memoize_by_cwd
28- def get_pre_commit_path ():
29- return os .path .join (get_root (), '.git/hooks/pre-commit' )
30-
31-
32- def create_pre_commit ():
33- path = get_pre_commit_path ()
34- pre_commit_file = pkg_resources .resource_filename ('pre_commit' , 'resources/pre-commit.sh' )
35- local .path (path ).write (local .path (pre_commit_file ).read ())
36- original_mode = os .stat (path ).st_mode
37- os .chmod (path , original_mode | stat .S_IXUSR | stat .S_IXGRP | stat .S_IXOTH )
38-
39-
40- def remove_pre_commit ():
41- local .path (get_pre_commit_path ()).delete ()
42-
43-
4425def get_head_sha (git_repo_path ):
4526 with local .cwd (git_repo_path ):
4627 return local ['git' ]['rev-parse' , 'HEAD' ]().strip ()
Original file line number Diff line number Diff line change 44import subprocess
55import sys
66
7+ from pre_commit import commands
78from pre_commit import git
89from pre_commit .runner import Runner
910from pre_commit .util import entry
@@ -126,13 +127,13 @@ def run(argv):
126127 runner = Runner .create ()
127128
128129 if args .command == 'install' :
129- git . create_pre_commit ( )
130- print 'pre-commit installed at {0}' .format (git . get_pre_commit_path () )
131- return 0
130+ retval = commands . install ( runner )
131+ print 'pre-commit installed at {0}' .format (runner . pre_commit_path )
132+ return retval
132133 elif args .command == 'uninstall' :
133- git . remove_pre_commit ( )
134+ retval = commands . uninstall ( runner )
134135 print 'pre-commit uninstalled'
135- return 0
136+ return retval
136137 elif args .command == 'run' :
137138 if args .hook :
138139 return run_single_hook (runner , args .hook , all_files = args .all_files )
@@ -148,6 +149,10 @@ def run(argv):
148149 'Command {0} not implemented.' .format (args .command )
149150 )
150151
152+ raise AssertionError (
153+ 'Command {0} failed to exit with a returncode' .format (args .command )
154+ )
155+
151156
152157if __name__ == '__main__' :
153158 sys .exit (run ())
Original file line number Diff line number Diff line change @@ -40,3 +40,7 @@ def repositories(self):
4040 """Returns a tuple of the configured repositories."""
4141 config = load_config (self .config_file_path )
4242 return tuple (map (Repository , config ))
43+
44+ @cached_property
45+ def pre_commit_path (self ):
46+ return os .path .join (self .git_root , '.git/hooks/pre-commit' )
Original file line number Diff line number Diff line change 1+
2+ import os
3+ import os .path
4+ import pkg_resources
5+ import stat
6+
7+ from pre_commit .commands import install
8+ from pre_commit .commands import uninstall
9+ from pre_commit .runner import Runner
10+
11+
12+ def test_install_pre_commit (empty_git_dir ):
13+ runner = Runner (empty_git_dir )
14+ ret = install (runner )
15+ assert ret == 0
16+ assert os .path .exists (runner .pre_commit_path )
17+ pre_commit_contents = open (runner .pre_commit_path ).read ()
18+ pre_commit_sh = pkg_resources .resource_filename ('pre_commit' , 'resources/pre-commit.sh' )
19+ expected_contents = open (pre_commit_sh ).read ()
20+ assert pre_commit_contents == expected_contents
21+ stat_result = os .stat (runner .pre_commit_path )
22+ assert stat_result .st_mode & (stat .S_IXUSR | stat .S_IXGRP | stat .S_IXOTH )
23+
24+
25+ def test_uninstall_pre_commit_does_not_blow_up_when_not_there (empty_git_dir ):
26+ runner = Runner (empty_git_dir )
27+ ret = uninstall (runner )
28+ assert ret == 0
29+
30+
31+ def test_uninstall (empty_git_dir ):
32+ runner = Runner (empty_git_dir )
33+ assert not os .path .exists (runner .pre_commit_path )
34+ install (runner )
35+ assert os .path .exists (runner .pre_commit_path )
36+ uninstall (runner )
37+ assert not os .path .exists (runner .pre_commit_path )
Original file line number Diff line number Diff line change 11
2- import os
32import pytest
4- import stat
53from plumbum import local
64
75from pre_commit import git
@@ -17,32 +15,6 @@ def test_get_root(empty_git_dir):
1715 assert git .get_root () == empty_git_dir
1816
1917
20- def test_get_pre_commit_path (empty_git_dir ):
21- assert git .get_pre_commit_path () == '{0}/.git/hooks/pre-commit' .format (
22- empty_git_dir ,
23- )
24-
25-
26- def test_create_pre_commit (empty_git_dir ):
27- git .create_pre_commit ()
28- assert len (open (git .get_pre_commit_path (), 'r' ).read ()) > 0
29- stat_result = os .stat (git .get_pre_commit_path ())
30- assert stat_result .st_mode & stat .S_IXUSR
31- assert stat_result .st_mode & stat .S_IXGRP
32- assert stat_result .st_mode & stat .S_IXOTH
33-
34-
35- def test_remove_pre_commit (empty_git_dir ):
36- git .remove_pre_commit ()
37-
38- assert not os .path .exists (git .get_pre_commit_path ())
39-
40- git .create_pre_commit ()
41- git .remove_pre_commit ()
42-
43- assert not os .path .exists (git .get_pre_commit_path ())
44-
45-
4618@pytest .fixture
4719def get_files_matching_func ():
4820 def get_filenames ():
Original file line number Diff line number Diff line change @@ -53,3 +53,9 @@ def test_repositories(consumer_repo):
5353 '[email protected] :pre-commit/pre-commit-hooks' ,
5454 '[email protected] :pre-commit/pre-commit' ,
5555 ]
56+
57+
58+ def test_pre_commit_path ():
59+ runner = Runner ('foo/bar' )
60+ expected_path = os .path .join ('foo/bar' , '.git/hooks/pre-commit' )
61+ assert runner .pre_commit_path == expected_path
You can’t perform that action at this time.
0 commit comments