hooks: add opt-in cache update hook

CI now checks that plugin caches are up to date, for example
when adding a new property to an element.

This is something pretty easy to forget, and it can be checked
in a pre-commit hook provided the cache generator generates no
unnecessart diff on the developer's machine. This is now the
case for me in core, -base and -good, and eventually all the
repositories should behave appropriately, at least on my
development machine.

The new check in the pre-commit hook is only performed when
the user is in the development environment (it checks
$MESON_BUILD_ROOT to determine that), and when the developer
has opted-in by setting `GST_CACHE_HOOK=enabled`. That is
because the hook will actually rebuild the cache with ninja,
and modify it in the source directory, that's not a behaviour
we want to enable by default.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/546>
This commit is contained in:
Mathieu Duponchelle 2020-06-25 01:37:43 +02:00 committed by GStreamer Merge Bot
parent 57d29e7fcd
commit 65e89d45b8

View file

@ -81,3 +81,35 @@ echo "==========================================================================
fi
done
echo "--Checking style pass--"
# This is an opt-in check, and can only be run from gst-build's devenv,
# as outside of it MESON_BUILD_ROOT will not be set.
#
# The idea is to build the cache, and check if it has unstaged changes.
# To accomodate for the git add -p case, where the developer will break
# down a large change set into multiple commits, this hook will only
# fail when there are no unstaged changes left to commit, in other
# cases it will only print a reminder that the cache needs committing.
if [ -v MESON_BUILD_ROOT ] && [ "$GST_CACHE_HOOK" == "enabled" ]; then
echo "--Checking plugin cache--"
toplevel=`git rev-parse --show-toplevel`
repo_name=`basename $toplevel`
target_name=`ninja -C $MESON_BUILD_ROOT -t targets all | grep "\/\<$repo_name\>\/.*\/gst_plugins_cache.json" | cut -d ":" -f 1`
ninja -C $MESON_BUILD_ROOT $target_name
cache_path=`git ls-files $toplevel/**gst_plugins_cache.json`
git diff --quiet --exit-code -- $cache_path
has_unstaged_cache_changes=$?
git diff --quiet --exit-code -- ':!'$cache_path
has_unstaged_non_cache_changes=$?
if [ $has_unstaged_non_cache_changes != 0 ] ; then
if [ $has_unstaged_cache_changes != 0 ]; then
echo -e "\033[1;33mUnstaged cache changes, but working directory isn't clean, don't forget to commit the cache when you're done\033[0m"
fi
elif [ $has_unstaged_cache_changes != 0 ]; then
echo -e "\033[1;31mUnstaged cache changes, but the working directory is clean, you must commit the cache\033[0m"
exit 1
else
echo "--Checking plugin cache pass--"
fi
fi