summaryrefslogtreecommitdiff
path: root/scripts/git-hooks/pre-commit.hook
blob: 3b9b5c37084c87204f3f31ad580e348ec7825271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/sh
#
# Check that the code follows a consistant code style
#

# Check for existence of indent, and error out if not present.
# On some *bsd systems the binary seems to be called gnunindent,
# so check for that first.

version=`gnuindent --version 2>/dev/null`
if test "x$version" = "x"; then
  version=`gindent --version 2>/dev/null`
  if test "x$version" = "x"; then
    version=`indent --version 2>/dev/null`
    if test "x$version" = "x"; then
      echo "GStreamer git pre-commit hook:"
      echo "Did not find GNU indent, please install it before continuing."
      exit 1
    else
      INDENT=indent
    fi
  else
    INDENT=gindent
  fi
else
  INDENT=gnuindent
fi

case `$INDENT --version` in
  GNU*)
      ;;
  default)
      echo "GStreamer git pre-commit hook:"
      echo "Did not find GNU indent, please install it before continuing."
      echo "(Found $INDENT, but it doesn't seem to be GNU indent)"
      exit 1
      ;;
esac

INDENT_PARAMETERS="--braces-on-if-line \
	--case-brace-indentation0 \
	--case-indentation2 \
	--braces-after-struct-decl-line \
	--line-length80 \
	--no-tabs \
	--cuddle-else \
	--dont-line-up-parentheses \
	--continuation-indentation4 \
	--honour-newlines \
	--tab-size8 \
	--indent-level2 \
	--leave-preprocessor-space"

echo "--Checking style--"
for file in `git diff-index --cached --name-only HEAD --diff-filter=ACMR| grep "\.c$"` ; do
    # nf is the temporary checkout. This makes sure we check against the
    # revision in the index (and not the checked out version).
    nf=`git checkout-index --temp ${file} | cut -f 1`
    newfile=`mktemp /tmp/${nf}.XXXXXX` || exit 1
    $INDENT ${INDENT_PARAMETERS} \
	$nf -o $newfile 2>> /dev/null
    # FIXME: Call indent twice as it tends to do line-breaks
    # different for every second call.
    $INDENT ${INDENT_PARAMETERS} \
        $newfile 2>> /dev/null
    diff -u -p "${nf}" "${newfile}"
    r=$?
    rm "${newfile}"
    rm "${nf}"
    if [ $r != 0 ] ; then
        ERROR_FILES="$ERROR_FILES $file"
echo "================================================================================================="
echo " Code style error in: $file                                                                      "
echo "================================================================================================="
echo ""
    fi
done

if [ -n "$ERROR_FILES" ];then
echo "================================================================================================="
echo " Code style error in:                                                                            "
for file in $ERROR_FILES ; do
echo "   $file"
done
echo "                                                                                                 "
echo " Please fix before committing. Don't forget to run git add before trying to commit again.        "
echo " If the whole file is to be committed, this should work (run from the top-level directory):      "
echo "   scripts/gst-indent$ERROR_FILES ; git add$ERROR_FILES ; git commit"
echo "                                                                                                 "
echo "================================================================================================="
  exit 1
fi

csharp_files=` git diff-index --cached --name-only HEAD --diff-filter=ACMR| grep "^subprojects/gstreamer-sharp/.*cs$" `
if test "x$csharp_files" != "x"; then
  version=`dotnet-format --version 2>/dev/null`
  if test "x$version" = "x"; then
      echo "GStreamer git pre-commit hook:"
      echo "Did not find dotnet-format required to format C# files, please install it before continuing."
      exit 1
  fi
  scripts/format-csharp --check
  r=$?
  if [ $r != 0 ] ; then
echo "================================================================================================="
echo " Code style error:                                                                               "
echo "                                                                                                 "
echo " Please fix before committing, running from the top-level directory:                             "
echo "   scripts/format-chsarp                                                                         "
echo "                                                                                                 "
echo " Don't forget to run git add before trying to commit again.                                      "
echo "================================================================================================="
    exit 1
  fi
fi

echo "--Checking style pass--"