@@ -31,6 +31,10 @@ def is_dir_nonempty(directory) -> bool:
31
31
def is_first_build () -> bool :
32
32
return not is_dir_nonempty (BUILD_DIR )
33
33
34
+ def is_precommit_hook_configured (repo_path : str = REPO_DIR ) -> bool :
35
+ pre_commit_hook_path = os .path .join (repo_path , '.git' , 'hooks' , 'pre-commit' )
36
+ return os .path .isfile (pre_commit_hook_path ) and os .access (pre_commit_hook_path , os .X_OK )
37
+
34
38
def get_all_branch_tags () -> str :
35
39
cmd = ["git" , "tag" , "--merged" ]
36
40
tags = subprocess .check_output (cmd , stderr = subprocess .DEVNULL , text = True ).strip ()
@@ -118,6 +122,44 @@ def check_submodules(self) -> None:
118
122
except KeyboardInterrupt :
119
123
sys .exit (1 )
120
124
125
+ def check_pre_commit_hook (self ) -> None :
126
+ """
127
+ Check if the pre-commit hook is configured. If not, prompt the user to create it.
128
+ """
129
+
130
+ if is_precommit_hook_configured ():
131
+ logger .info ("The pre-commit hook is already configured." )
132
+ return
133
+
134
+ logger .info ("The pre-commit hook is not configured." )
135
+ if not self .prompt :
136
+ return
137
+
138
+ hook_path = os .path .join ('.git' , 'hooks' , 'pre-commit' )
139
+ hook_script = (
140
+ "#!/bin/bash\n "
141
+ "CRNT_DIR=$( cd -- \" $( dirname -- \" ${BASH_SOURCE[0]}\" )\" &> /dev/null && pwd )\n "
142
+ "GIT_DIR=\" $(dirname \" $CRNT_DIR\" )\" \n "
143
+ "REPO_DIR=\" $(dirname \" $GIT_DIR\" )\" \n "
144
+ "${REPO_DIR}/scripts/pre_commit.sh\n "
145
+ )
146
+ try :
147
+ user_input = input ("Do you want to create a pre-commit hook? [y/N]:\n " ).strip ().lower ()
148
+ if user_input != 'y' :
149
+ logger .info ("Skipped pre-commit hook setup." )
150
+ return
151
+
152
+ # Write the hook script
153
+ with open (hook_path , 'w' , encoding = "utf-8" ) as hook_file :
154
+ hook_file .write (hook_script )
155
+
156
+ # Make the hook executable
157
+ os .chmod (hook_path , 0o755 )
158
+ logger .info ("Pre-commit hook created successfully at '%s'." , hook_path )
159
+
160
+ except KeyboardInterrupt :
161
+ sys .exit (1 )
162
+
121
163
if __name__ == "__main__" :
122
164
logging .basicConfig (level = logging .ERROR )
123
165
logger .setLevel (logging .DEBUG )
@@ -131,3 +173,4 @@ def check_submodules(self) -> None:
131
173
checker .check_branch_tags ()
132
174
checker .check_python_requirements ()
133
175
checker .check_submodules ()
176
+ checker .check_pre_commit_hook ()
0 commit comments