5454import os .path as osp
5555from git .cmd import Git
5656
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+
5774
5875class TestIndex (TestBase ):
5976
@@ -834,25 +851,21 @@ def test_add_a_file_with_wildcard_chars(self, rw_dir):
834851@with_rw_repo ('HEAD' ,bare = True )
835852def test_pre_commit_hook_success (self ,rw_repo ):
836853index = 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+ )
844859index .commit ("This should not fail" )
845860
846861@with_rw_repo ('HEAD' ,bare = True )
847862def test_pre_commit_hook_fail (self ,rw_repo ):
848863index = 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+ )
856869try :
857870index .commit ("This should fail" )
858871except HookExecutionError as err :
@@ -869,35 +882,29 @@ def test_pre_commit_hook_fail(self, rw_repo):
869882self .assertEqual (err .stderr ,"\n stderr: 'stderr\n '" )
870883assert str (err )
871884else :
872- raise AssertionError ("Should havecought a HookExecutionError" )
885+ raise AssertionError ("Should havecaught a HookExecutionError" )
873886
874887@with_rw_repo ('HEAD' ,bare = True )
875888def test_commit_msg_hook_success (self ,rw_repo ):
876- index = rw_repo .index
877889commit_message = u"commit default head by Frèderic Çaufl€"
878890from_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+ )
888897new_commit = index .commit (commit_message )
889898self .assertEqual (new_commit .message ,u"{0} {1}" .format (commit_message ,from_hook_message ))
890899
891900@with_rw_repo ('HEAD' ,bare = True )
892901def test_commit_msg_hook_fail (self ,rw_repo ):
893902index = 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+ )
901908try :
902909index .commit ("This should fail" )
903910except HookExecutionError as err :