Accessing data in a server-hook from new commit

Hi everyone!

I’m trying to implement a server-sided hook for update , using this guide: Git server hooks | GitLab I found my repo-path, created subfolders named custom_hooks/update.d and a script which is executable. Everything works as expected so far. Now, what the hook should do is: It should check the content of the newly commited files for some properties; if a specific string is in there, for example. This one is where I’m stuck. Can someone please explain to me how I get the newly pushed content?

Git Hooks get different parameters passed by default, and require them to be read, and used in git commands, e.g. git diff to search for specific file content.

The hook shared in git - How to setup server side pre-receive hook? - Stack Overflow looks like a good example. I think this could be modeled for your case like this, untested:


while read old_sha new_sha refname

if git diff "$old_sha" "$new_sha" | grep -qE 'PATTERN'; then
    echo "Saw disallowed pattern in $(basename "$refname")."
    git diff "$old_sha" "$new_sha" | grep -nE 'PATTERN'
    exit 1


exit 0