Merge branch 'move_subdir' into tizen_gst_1.19.2_mono
[platform/upstream/gstreamer.git] / subprojects / gstreamer / hooks / pre-commit.hook
1 #!/bin/sh
2 #
3 # Check that the code follows a consistant code style
4 #
5
6 # Check for existence of indent, and error out if not present.
7 # On some *bsd systems the binary seems to be called gnunindent,
8 # so check for that first.
9
10 version=`gnuindent --version 2>/dev/null`
11 if test "x$version" = "x"; then
12   version=`gindent --version 2>/dev/null`
13   if test "x$version" = "x"; then
14     version=`indent --version 2>/dev/null`
15     if test "x$version" = "x"; then
16       echo "GStreamer git pre-commit hook:"
17       echo "Did not find GNU indent, please install it before continuing."
18       exit 1
19     else
20       INDENT=indent
21     fi
22   else
23     INDENT=gindent
24   fi
25 else
26   INDENT=gnuindent
27 fi
28
29 case `$INDENT --version` in
30   GNU*)
31       ;;
32   default)
33       echo "GStreamer git pre-commit hook:"
34       echo "Did not find GNU indent, please install it before continuing."
35       echo "(Found $INDENT, but it doesn't seem to be GNU indent)"
36       exit 1
37       ;;
38 esac
39
40 INDENT_PARAMETERS="--braces-on-if-line \
41         --case-brace-indentation0 \
42         --case-indentation2 \
43         --braces-after-struct-decl-line \
44         --line-length80 \
45         --no-tabs \
46         --cuddle-else \
47         --dont-line-up-parentheses \
48         --continuation-indentation4 \
49         --honour-newlines \
50         --tab-size8 \
51         --indent-level2 \
52         --leave-preprocessor-space"
53
54 echo "--Checking style--"
55 for file in `git diff-index --cached --name-only HEAD --diff-filter=ACMR| grep "\.c$"` ; do
56     # nf is the temporary checkout. This makes sure we check against the
57     # revision in the index (and not the checked out version).
58     nf=`git checkout-index --temp ${file} | cut -f 1`
59     newfile=`mktemp /tmp/${nf}.XXXXXX` || exit 1
60     $INDENT ${INDENT_PARAMETERS} \
61         $nf -o $newfile 2>> /dev/null
62     # FIXME: Call indent twice as it tends to do line-breaks
63     # different for every second call.
64     $INDENT ${INDENT_PARAMETERS} \
65         $newfile 2>> /dev/null
66     diff -u -p "${nf}" "${newfile}"
67     r=$?
68     rm "${newfile}"
69     rm "${nf}"
70     if [ $r != 0 ] ; then
71 echo "================================================================================================="
72 echo " Code style error in: $file                                                                      "
73 echo "                                                                                                 "
74 echo " Please fix before committing. Don't forget to run git add before trying to commit again.        "
75 echo " If the whole file is to be committed, this should work (run from the top-level directory):      "
76 echo "                                                                                                 "
77 echo "   gst-indent $file; git add $file; git commit"
78 echo "                                                                                                 "
79 echo "================================================================================================="
80         exit 1
81     fi
82 done
83 echo "--Checking style pass--"
84
85 # This is an opt-in check, and can only be run from gst-build's devenv,
86 # as outside of it MESON_BUILD_ROOT will not be set.
87 #
88 # The idea is to build the cache, and check if it has unstaged changes.
89 # To accomodate for the git add -p case, where the developer will break
90 # down a large change set into multiple commits, this hook will only
91 # fail when there are no unstaged changes left to commit, in other
92 # cases it will only print a reminder that the cache needs committing.
93 if [ -v MESON_BUILD_ROOT ] && [ "$GST_CACHE_HOOK" == "enabled" ]; then
94     echo "--Checking plugin cache--"
95     toplevel=`git rev-parse --show-toplevel`
96     repo_name=`basename $toplevel`
97     target_name=`ninja -C $MESON_BUILD_ROOT -t targets all | grep "\/\<$repo_name\>\/.*\/gst_plugins_cache.json" | cut -d ":" -f 1`
98     ninja -C $MESON_BUILD_ROOT $target_name
99     cache_path=`git ls-files $toplevel/**gst_plugins_cache.json`
100     git diff --quiet --exit-code -- $cache_path
101     has_unstaged_cache_changes=$?
102     git diff --quiet --exit-code -- ':!'$cache_path
103     has_unstaged_non_cache_changes=$?
104
105     if [ $has_unstaged_non_cache_changes != 0 ] ; then
106         if [ $has_unstaged_cache_changes != 0 ]; then
107             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"
108         fi
109     elif [ $has_unstaged_cache_changes != 0 ]; then
110         echo -e "\033[1;31mUnstaged cache changes, but the working directory is clean, you must commit the cache\033[0m"
111         exit 1
112     else
113     echo "--Checking plugin cache pass--"
114     fi
115 fi