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:
#!/bin/bash
while read old_sha new_sha refname
do
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
fi
done
exit 0