54
54
import os .path as osp
55
55
from git .cmd import Git
56
56
57
+ HOOKS_SHEBANG = \
58
+ "!C:/Program\ Files/Git/usr/bin/sh.exe\n " if is_win \
59
+ else "#!/usr/bin/env sh\n "
60
+
61
+
62
+ def _make_hook (git_dir , name , content , make_exec = True ):
63
+ """A helper to create a hook"""
64
+ hp = hook_path (name , git_dir )
65
+ hpd = osp .dirname (hp )
66
+ if not osp .isdir (hpd ):
67
+ os .mkdir (hpd )
68
+ with open (hp , "wt" ) as fp :
69
+ fp .write (HOOKS_SHEBANG + content )
70
+ if make_exec :
71
+ os .chmod (hp , 0o744 )
72
+ return hp
73
+
57
74
58
75
class TestIndex (TestBase ):
59
76
@@ -834,25 +851,21 @@ def test_add_a_file_with_wildcard_chars(self, rw_dir):
834
851
@with_rw_repo ('HEAD' , bare = True )
835
852
def test_pre_commit_hook_success (self , rw_repo ):
836
853
index = rw_repo .index
837
- hp = hook_path ('pre-commit' , index .repo .git_dir )
838
- hpd = osp .dirname (hp )
839
- if not osp .isdir (hpd ):
840
- os .mkdir (hpd )
841
- with open (hp , "wt" ) as fp :
842
- fp .write ("#!/usr/bin/env sh\n exit 0" )
843
- os .chmod (hp , 0o744 )
854
+ _make_hook (
855
+ index .repo .git_dir ,
856
+ 'pre-commit' ,
857
+ "exit 0"
858
+ )
844
859
index .commit ("This should not fail" )
845
860
846
861
@with_rw_repo ('HEAD' , bare = True )
847
862
def test_pre_commit_hook_fail (self , rw_repo ):
848
863
index = rw_repo .index
849
- hp = hook_path ('pre-commit' , index .repo .git_dir )
850
- hpd = osp .dirname (hp )
851
- if not osp .isdir (hpd ):
852
- os .mkdir (hpd )
853
- with open (hp , "wt" ) as fp :
854
- fp .write ("#!/usr/bin/env sh\n echo stdout; echo stderr 1>&2; exit 1" )
855
- os .chmod (hp , 0o744 )
864
+ hp = _make_hook (
865
+ index .repo .git_dir ,
866
+ 'pre-commit' ,
867
+ "echo stdout; echo stderr 1>&2; exit 1"
868
+ )
856
869
try :
857
870
index .commit ("This should fail" )
858
871
except HookExecutionError as err :
@@ -869,35 +882,29 @@ def test_pre_commit_hook_fail(self, rw_repo):
869
882
self .assertEqual (err .stderr , "\n stderr: 'stderr\n '" )
870
883
assert str (err )
871
884
else :
872
- raise AssertionError ("Should have cought a HookExecutionError" )
885
+ raise AssertionError ("Should have caught a HookExecutionError" )
873
886
874
887
@with_rw_repo ('HEAD' , bare = True )
875
888
def test_commit_msg_hook_success (self , rw_repo ):
876
- index = rw_repo .index
877
889
commit_message = u"commit default head by Frèderic Çaufl€"
878
890
from_hook_message = u"from commit-msg"
879
-
880
- hp = hook_path ('commit-msg' , index .repo .git_dir )
881
- hpd = osp .dirname (hp )
882
- if not osp .isdir (hpd ):
883
- os .mkdir (hpd )
884
- with open (hp , "wt" ) as fp :
885
- fp .write ('#!/usr/bin/env sh\n echo -n " {0}" >> "$1"' .format (from_hook_message ))
886
- os .chmod (hp , 0o744 )
887
-
891
+ index = rw_repo .index
892
+ _make_hook (
893
+ index .repo .git_dir ,
894
+ 'commit-msg' ,
895
+ 'echo -n " {0}" >> "$1"' .format (from_hook_message )
896
+ )
888
897
new_commit = index .commit (commit_message )
889
898
self .assertEqual (new_commit .message , u"{0} {1}" .format (commit_message , from_hook_message ))
890
899
891
900
@with_rw_repo ('HEAD' , bare = True )
892
901
def test_commit_msg_hook_fail (self , rw_repo ):
893
902
index = rw_repo .index
894
- hp = hook_path ('commit-msg' , index .repo .git_dir )
895
- hpd = osp .dirname (hp )
896
- if not osp .isdir (hpd ):
897
- os .mkdir (hpd )
898
- with open (hp , "wt" ) as fp :
899
- fp .write ("#!/usr/bin/env sh\n echo stdout; echo stderr 1>&2; exit 1" )
900
- os .chmod (hp , 0o744 )
903
+ hp = _make_hook (
904
+ index .repo .git_dir ,
905
+ 'commit-msg' ,
906
+ "echo stdout; echo stderr 1>&2; exit 1"
907
+ )
901
908
try :
902
909
index .commit ("This should fail" )
903
910
except HookExecutionError as err :
0 commit comments