]> SALOME platform Git repositories - modules/shaper.git/blob - GitHooks/pre-commit.in
Salome HOME
52e778fdf80f80972af4f466a63b411b6e92c5d1
[modules/shaper.git] / GitHooks / pre-commit.in
1 #!/bin/bash
2
3 function isDOSFile
4 {
5   local FILENAME="$1"
6   file "$FILENAME" | grep -q "CRLF line terminators"
7 }
8
9 if git rev-parse --verify HEAD >/dev/null 2>&1
10 then
11   against=HEAD
12 else
13   # Initial commit: diff against an empty tree object
14   against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
15 fi
16
17 # If you want to allow non-ASCII filenames set this variable to true.
18 allownonascii=$(git config hooks.allownonascii)
19
20 # Redirect output to stderr.
21 exec 1>&2
22
23 # Cross platform projects tend to avoid non-ASCII filenames; prevent
24 # them from being added to the repository. We exploit the fact that the
25 # printable range starts at the space character and ends with tilde.
26 if [ "$allownonascii" != "true" ] &&
27   # Note that the use of brackets around a tr range is ok here, (it's
28   # even required, for portability to Solaris 10's /usr/bin/tr), since
29   # the square bracket bytes happen to fall in the designated range.
30   test $(git diff --cached --name-only --diff-filter=A -z $against |
31     LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
32 then
33   cat <<\EOF
34 Error: Attempt to add a non-ASCII file name.
35
36 This can cause problems if you want to work with people on other platforms.
37
38 To be portable it is advisable to rename the file.
39
40 If you know what you are doing you can disable this check using:
41
42   git config hooks.allownonascii true
43 EOF
44   exit 1
45 fi
46
47 FOUND=0
48 # If there are whitespace errors, print the offending file names and fail.
49 if [ "$(exec git diff-index --check --cached $against -- )" != "" ]
50 then
51   FOUND=1
52 fi
53
54 # Find files with DOS line endings
55 for FILE in $(exec git diff --name-only --cached $against -- | sed '/^[+-]/d' | sed -r 's/:[0-9]+:.*//' | uniq) ; do
56   isDOSFile "$FILE"
57   if (( $? == 0 ))
58   then
59     echo "ERROR: \"$FILE\" has DOS line endings" >&2
60     FOUND=1
61   fi
62 done
63
64 exit $FOUND