Skip to content

Instantly share code, notes, and snippets.

@sergeyhush
Last active August 30, 2021 11:47
Show Gist options
  • Save sergeyhush/c0c09642ca76ddd67c53 to your computer and use it in GitHub Desktop.
Save sergeyhush/c0c09642ca76ddd67c53 to your computer and use it in GitHub Desktop.
Post-receive git hook to check if file changed
#!/bin/bash
WATCH_FILE="abc.123"
while read oldrev newrev refname; do
if [ "$refname" = "refs/heads/master" ]; then
if git diff-tree --name-only -r -z $oldrev $newrev | grep --quiet $WATCH_FILE ; then
# WATCH_FILE changed...
fi
fi
done
@loafer-mka
Copy link

loafer-mka commented Dec 11, 2019

  1. git diff-tree --name-only -r -z $oldrev $newrev | grep --quiet $WATCH_FILE
    will search any substring which may be part of filename, soo it return true for 'foo${WATCH_FILE}bar'

! git diff-tree --exit-code --name-only -r -z $oldrev $newrev -- "$WATCH_FILE"
note: exit code will be 1 if $WATCH_FILE changed

  1. $oldrev is 0000000000000000000000000000000000000000 for new branch, so git diff-tree will fail for each new branch. As long as you are limited only by existing branches, then everything is correct. But in general, this does not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment