Debian Codegraph @

.githooks/pre-commit

raw
1 #!/bin/sh
2
3 # git pre-commit hook that runs an Uncrustify stylecheck.
4 # Features:
5 # - abort commit when commit does not comply with the style guidelines
6 # - create a patch of the proposed style changes
7 #
8 # More info on Uncrustify: http://uncrustify.sourceforge.net/
9
10 # This file is part of a set of unofficial pre-commit hooks available
11 # at github.
12 # Link: https://github.com/githubbrowser/Pre-commit-hooks
13 # Contact: David Martin, [email protected]
14
15
16 ##################################################################
17 # CONFIGURATION
18 # set uncrustify path or executable
19 # UNCRUSTIFY="/usr/bin/uncrustify"
20 UNCRUSTIFY="uncrustify"
21
22 # set uncrustify config location
23 # CONFIG="/home/user/.config/uncrustify.cfg"
24 CONFIG=".uncrustify.cfg"
25
26 # the source language: C, CPP, D, CS, JAVA, PAWN, VALA, OC, OC+
27 # use AUTO to let Uncrustify decide which language a given file uses.
28 # the detected language is printed to the console when Uncrustify is called.
29 # override if the automatic detection seems off.
30 # SOURCE_LANGUAGE="AUTO"
31 SOURCE_LANGUAGE="AUTO"
32
33 # remove any older patches from previous commits. Set to true or false.
34 # DELETE_OLD_PATCHES=false
35 DELETE_OLD_PATCHES=false
36
37 # only parse files with the extensions in FILE_EXTS. Set to true or false.
38 # if false every changed file in the commit will be parsed with Uncrustify.
39 # if true only files matching one of the extensions are parsed with Uncrustify.
40 # PARSE_EXTS=true
41 PARSE_EXTS=true
42
43 # file types to parse. Only effective when PARSE_EXTS is true.
44 # FILE_EXTS=".c .h .cpp .hpp"
45 FILE_EXTS=".c .h .cc .hh .cpp .hpp .hxx"
46
47 ##################################################################
48 # There should be no need to change anything below this line.
49
50 . "$(dirname -- "$0")/canonicalize_filename.sh"
51
52 # exit on error
53 set -e
54
55 # check whether the given file matches any of the set extensions
56 matches_extension() {
57 local filename="$(basename -- "$1")"
58 local extension=".${filename##*.}"
59 local ext
60
61 for ext in $FILE_EXTS; do [ "$ext" = "$extension" ] && return 0; done
62
63 return 1
64 }
65
66 # necessary check for initial commit
67 if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
68 against=HEAD
69 else
70 # Initial commit: diff against an empty tree object
71 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
72 fi
73
74 # make sure the config file and executable are correctly set
75 if [ ! -f "$CONFIG" ] ; then
76 printf "Error: uncrustify config file not found.\n"
77 printf "Set the correct path in $(canonicalize_filename "$0").\n"
78 exit 1
79 fi
80
81 if ! command -v "$UNCRUSTIFY" > /dev/null ; then
82 printf "Error: uncrustify executable not found.\n"
83 printf "Set the correct path in $(canonicalize_filename "$0").\n"
84 exit 1
85 fi
86
87 # create a filename to store our generated patch
88 prefix="pre-commit-uncrustify"
89 suffix="$(date +%C%y-%m-%d_%Hh%Mm%Ss)"
90 patch="/tmp/$prefix-$suffix.patch"
91 diff="/tmp/$prefix-$suffix.diff"
92
93 # clean up any older uncrustify patches
94 $DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch
95
96 # create one patch containing all changes to the files
97 # sed to remove quotes around the filename, if inserted by the system
98 # (done sometimes, if the filename contains special characters, like the quote itself)
99 git diff-index --cached --diff-filter=ACMR --name-only $against -- | \
100 sed -e 's/^"\(.*\)"$/\1/' | \
101 while read file
102 do
103 # ignore file if we do check for file extensions and the file
104 # does not match any of the extensions specified in $FILE_EXTS
105 if $PARSE_EXTS && ! matches_extension "$file"; then
106 continue;
107 fi
108
109 case "$file" in
110 src/protocol/*.pb.*) continue;;
111 include/*) continue;;
112 samples/*) continue;;
113 esac
114
115 # escape special characters in the source filename:
116 # - '\': backslash needs to be escaped
117 # - '*': used as matching string => '*' would mean expansion
118 # (curiously, '?' must not be escaped)
119 # - '[': used as matching string => '[' would mean start of set
120 # - '|': used as sed split char instead of '/', so it needs to be escaped
121 # in the filename
122 # printf %s particularly important if the filename contains the % character
123 file_escaped_source=$(printf "%s" "$file" | sed -e 's/[\*[|]/\\&/g')
124
125 # escape special characters in the target filename:
126 # phase 1 (characters escaped in the output diff):
127 # - '\': backslash needs to be escaped in the output diff
128 # - '"': quote needs to be escaped in the output diff if present inside
129 # of the filename, as it used to bracket the entire filename part
130 # phase 2 (characters escaped in the match replacement):
131 # - '\': backslash needs to be escaped again for sed itself
132 # (i.e. double escaping after phase 1)
133 # - '&': would expand to matched string
134 # - '|': used as sed split char instead of '/'
135 # printf %s particularly important if the filename contains the % character
136 file_escaped_target=$(printf "%s" "$file" | sed -e 's/[\"]/\\&/g' -e 's/[\&|]/\\&/g')
137
138 # Uncrustify detects the language automatically if it is not specified
139 language_option=""
140 if [ "$SOURCE_LANGUAGE" != "AUTO" ] ; then
141 language_option="-l $SOURCE_LANGUAGE"
142 fi
143
144 # uncrustify our sourcefile, create a patch with diff and append it to our $patch
145 # The sed call is necessary to transform the patch from
146 # --- $file timestamp
147 # +++ - timestamp
148 # to both lines working on the same file and having a a/ and b/ prefix.
149 # Else it can not be applied with 'git apply'.
150 "$UNCRUSTIFY" -q -c "$CONFIG" -f "$file" $language_option | \
151 git --no-pager diff --color=always --no-index -- "$file" - | \
152 tail -n +3 | \
153 sed -e "1s|--- a/$file_escaped_source|--- \"a/$file_escaped_target\"|" \
154 -e "2s|+++ b/-|+++ \"b/$file_escaped_target\"|" | \
155 tee -a "$diff" | \
156 sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" >> "$patch"
157 done
158
159 # if no patch has been generated all is ok, clean up the file stub and exit
160 if [ ! -s "$patch" ] ; then
161 printf "Files in this commit comply with the uncrustify rules.\n"
162 rm -f "$patch"
163 exit 0
164 fi
165
166 # a patch has been created, notify the user and exit
167 printf "\nThe following differences were found between the code to commit "
168 printf "and the uncrustify rules:\n\n"
169 cat "$diff"
170 rm -f "$diff"
171
172 printf "\nYou can apply these changes with:\n git apply $patch\n"
173 printf "(may need to be called from the root directory of your repository)\n"
174 printf "Aborting commit. Apply changes and commit again or skip checking with"
175 printf " --no-verify (not recommended).\n"
176
177 exit 1