'Call Githooks as standalone scripts

I've been given a git pre-push hook to include in some checking inside a VS Code extension.

The request made to me was to run this hook against some code, but not actually push anything, just return the result.

The file I've been given has no file type from what I can tell, so my question is --- In theory couldn't I just copy this as a separate file and make it a .sh script, and then call it with whatever arguments it accepts (in this case two)?



Solution 1:[1]

This will be officially supported with Git 2.36 (Q2 2022):

git hook run [--ignore-missing] <hook-name> [-- <hook-args>]

See commit 95ba86a, commit 306f445, commit dbb1c61, commit f443246, commit 0c8ac06, commit a755530, commit 1a3017d, commit 72ddf34, commit 67ad630, commit 432a50b, commit 25d4e02, commit 593ffdd, commit bad62a8, commit 96e7225 (22 Dec 2021) by Emily Shaffer (nasamuffin).
See commit 0d3979c, commit ab81cf2, commit 474c119 (22 Dec 2021) by Ævar Arnfjörð Bjarmason (avar).
(Merged by Junio C Hamano -- gitster -- in commit c70bc33, 09 Feb 2022)

hook: add 'run' subcommand

Signed-off-by: Emily Shaffer
Signed-off-by: Ævar Arnfjörð Bjarmason
Acked-by: Emily Shaffer

In order to enable hooks to be run as an external process, by a standalone Git command, or by tools which wrap Git, provide an external means to run all configured hook commands for a given hook event.

Most of our hooks require more complex functionality than this, but let's start with the bare minimum required to support our simplest hooks.

In terms of implementation the usage_with_options() and "goto usage" pattern here mirrors that of builtin/{commit-graph,multi-pack-index}.c.

Some of the implementation here, such as a function being named run_hooks_opt() when it's tasked with running one hook, to using the run_processes_parallel_tr2() API to run with jobs=1 is somewhere between a bit odd and and an overkill for the current features of this "hook run" command and the hook.[ch] API.

This code will eventually be able to run multiple hooks declared in config in parallel, by starting out with these names and APIs we reduce the later churn of renaming functions, switching from the run_command() to run_processes_parallel_tr2() API etc.

git hook now includes in its man page:

git-hook(1)

NAME

git-hook - Run git hooks

SYNOPSIS

[verse]

 'git hook' run <hook-name> [-- <hook-args>]

DESCRIPTION

A command interface to running git hooks (see linkgit:githooks[5]), for use by other scripted git commands.

SUBCOMMANDS

run

Run the <hook-name> hook.

Any positional arguments to the hook should be passed after a mandatory -- (or --end-of-options).

And:

git hook run: add an --ignore-missing flag

Signed-off-by: Ævar Arnfjörð Bjarmason
Reviewed-by: Emily Shaffer

For certain one-shot hooks we'd like to optimistically run them, and not complain if they don't exist.

This was already supported by the underlying hook.c library, but had not been exposed via "git hook"(man) run".
The command version of this will be used by send-email in a subsequent commit.

git hook now includes in its man page:

'git hook' run [--ignore-missing] <hook-name> [-- <hook-args>]

git hook now includes in its man page:

OPTIONS

--ignore-missing

Ignore any missing hook by quietly returning zero.

Used for tools that want to do a blind one-shot run of a hook that may or may not be present.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VonC