git-p4: use 'git hook' to run hooks

Instead of duplicating the behavior of run-command.h:run_hook_le() in Python, we can directly call 'git hook run'. We emulate the existence check with the --ignore-missing flag. We're dropping the "verbose" handling added in 9f59ca4d6af (git-p4: create new function run_git_hook, 2020-02-11), those who want diagnostic output about how hooks are run are now able to get that via e.g. the trace2 facility and GIT_TRACE=1. Signed-off-by: Emily Shaffer <emilyshaffer@google.com> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Acked-by: Emily Shaffer <emilyshaffer@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Emily Shaffer committed Dec 22, 2021 at 04:59 UTC 0c8ac06b53cf048dc5aae6f31123e93b62af7cfc
1 file changed +6 -64
git-p4.py
+6 -64
@@ -208,70 +208,12 @@ def decode_path(path):
208
209 def run_git_hook(cmd, param=[]):
210 """Execute a hook if the hook exists."""
211 - if verbose:
212 - sys.stderr.write("Looking for hook: %s\n" % cmd)
213 - sys.stderr.flush()
214 -
215 - hooks_path = gitConfig("core.hooksPath")
216 - if len(hooks_path) <= 0:
217 - hooks_path = os.path.join(os.environ["GIT_DIR"], "hooks")
218 -
219 - if not isinstance(param, list):
220 - param=[param]
221 -
222 - # resolve hook file name, OS depdenent
223 - hook_file = os.path.join(hooks_path, cmd)
224 - if platform.system() == 'Windows':
225 - if not os.path.isfile(hook_file):
226 - # look for the file with an extension
227 - files = glob.glob(hook_file + ".*")
228 - if not files:
229 - return True
230 - files.sort()
231 - hook_file = files.pop()
232 - while hook_file.upper().endswith(".SAMPLE"):
233 - # The file is a sample hook. We don't want it
234 - if len(files) > 0:
235 - hook_file = files.pop()
236 - else:
237 - return True
238 -
239 - if not os.path.isfile(hook_file) or not os.access(hook_file, os.X_OK):
240 - return True
241 -
242 - return run_hook_command(hook_file, param) == 0
243 -
244 -def run_hook_command(cmd, param):
245 - """Executes a git hook command
246 - cmd = the command line file to be executed. This can be
247 - a file that is run by OS association.
248 -
249 - param = a list of parameters to pass to the cmd command
250 -
251 - On windows, the extension is checked to see if it should
252 - be run with the Git for Windows Bash shell. If there
253 - is no file extension, the file is deemed a bash shell
254 - and will be handed off to sh.exe. Otherwise, Windows
255 - will be called with the shell to handle the file assocation.
256 -
257 - For non Windows operating systems, the file is called
258 - as an executable.
259 - """
260 - cli = [cmd] + param
261 - use_shell = False
262 - if platform.system() == 'Windows':
263 - (root,ext) = os.path.splitext(cmd)
264 - if ext == "":
265 - exe_path = os.environ.get("EXEPATH")
266 - if exe_path is None:
267 - exe_path = ""
268 - else:
269 - exe_path = os.path.join(exe_path, "bin")
270 - cli = [os.path.join(exe_path, "SH.EXE")] + cli
271 - else:
272 - use_shell = True
273 - return subprocess.call(cli, shell=use_shell)
274 -
211 + args = ['git', 'hook', 'run', '--ignore-missing', cmd]
212 + if param:
213 + args.append("--")
214 + for p in param:
215 + args.append(p)
216 + return subprocess.call(args) == 0
217
218 def write_pipe(c, stdin):
219 if verbose: