Debian Codegraph @

.githooks/pre-commit

raw symbols SCIP
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
92 # clean up any older uncrustify patches
93 $DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch
94
95 # create one patch containing all changes to the files
96 # sed to remove quotes around the filename, if inserted by the system
97 # (done sometimes, if the filename contains special characters, like the quote itself)
98 git diff-index --cached --diff-filter=ACMR --name-only $against -- | \
99 sed -e 's/^"\(.*\)"$/\1/' | \
100 while read file
101 do
102 # ignore file if we do check for file extensions and the file
103 # does not match any of the extensions specified in $FILE_EXTS
104 if $PARSE_EXTS && ! matches_extension "$file"; then
105 continue;
106 fi
107
108 # escape special characters in the source filename:
109 # - '\': backslash needs to be escaped
110 # - '*': used as matching string => '*' would mean expansion
111 # (curiously, '?' must not be escaped)
112 # - '[': used as matching string => '[' would mean start of set
113 # - '|': used as sed split char instead of '/', so it needs to be escaped
114 # in the filename
115 # printf %s particularly important if the filename contains the % character
116 file_escaped_source=$(printf "%s" "$file" | sed -e 's/[\*[|]/\\&/g')
117
118 # escape special characters in the target filename:
119 # phase 1 (characters escaped in the output diff):
120 # - '\': backslash needs to be escaped in the output diff
121 # - '"': quote needs to be escaped in the output diff if present inside
122 # of the filename, as it used to bracket the entire filename part
123 # phase 2 (characters escaped in the match replacement):
124 # - '\': backslash needs to be escaped again for sed itself
125 # (i.e. double escaping after phase 1)
126 # - '&': would expand to matched string
127 # - '|': used as sed split char instead of '/'
128 # printf %s particularly important if the filename contains the % character
129 file_escaped_target=$(printf "%s" "$file" | sed -e 's/[\"]/\\&/g' -e 's/[\&|]/\\&/g')
130
131 # Uncrustify detects the language automatically if it is not specified
132 language_option=""
133 if [ "$SOURCE_LANGUAGE" != "AUTO" ] ; then
134 language_option="-l $SOURCE_LANGUAGE"
135 fi
136
137 # uncrustify our sourcefile, create a patch with diff and append it to our $patch
138 # The sed call is necessary to transform the patch from
139 # --- $file timestamp
140 # +++ - timestamp
141 # to both lines working on the same file and having a a/ and b/ prefix.
142 # Else it can not be applied with 'git apply'.
143 "$UNCRUSTIFY" -c "$CONFIG" -f "$file" $language_option | \
144 git --no-pager diff --color=always --no-index -- "$file" - | \
145 sed -e "1s|--- $file_escaped_source|--- \"a/$file_escaped_target\"|" -e "2s|+++ -|+++ \"b/$file_escaped_target\"|" >> "$patch"
146 done
147
148 # if no patch has been generated all is ok, clean up the file stub and exit
149 if [ ! -s "$patch" ] ; then
150 printf "Files in this commit comply with the uncrustify rules.\n"
151 rm -f "$patch"
152 exit 0
153 fi
154
155 # a patch has been created, notify the user and exit
156 printf "\nThe following differences were found between the code to commit "
157 printf "and the uncrustify rules:\n\n"
158 cat "$patch"
159
160 printf "\nYou can apply these changes with:\n git apply $patch\n"
161 printf "(may need to be called from the root directory of your repository)\n"
162 printf "Aborting commit. Apply changes and commit again or skip checking with"
163 printf " --no-verify (not recommended).\n"
164
165 exit 1