d308a81b5d384f587ef86cfb3ee60980e6372da9
[platform/upstream/cmake.git] / Utilities / Git / pre-commit
1 #!/usr/bin/env bash
2 #=============================================================================
3 # CMake - Cross Platform Makefile Generator
4 # Copyright 2000-2011 Kitware, Inc., Insight Software Consortium
5 #
6 # Distributed under the OSI-approved BSD License (the "License");
7 # see accompanying file Copyright.txt for details.
8 #
9 # This software is distributed WITHOUT ANY WARRANTY; without even the
10 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 # See the License for more information.
12 #=============================================================================
13
14 die() {
15   echo 'pre-commit hook failure' 1>&2
16   echo '-----------------------' 1>&2
17   echo '' 1>&2
18   echo "$@" 1>&2
19   exit 1
20 }
21
22 #-------------------------------------------------------------------------------
23 line_too_long=80
24 bad=$(regex=".{$line_too_long}" &&
25 git diff-index --cached HEAD --name-only --diff-filter=AM \
26     --pickaxe-regex -S"$regex" -- 'Source/*.h' 'Source/*.cxx' |
27 while read file; do
28   lines_too_long=$(git diff-index -p --cached HEAD \
29                    --pickaxe-regex -S"$regex" -- "$file")
30   if echo "$lines_too_long" | egrep -q '^\+'"$regex"; then
31     echo "$lines_too_long"
32   fi
33 done)
34 test -z "$bad" ||
35 die 'The following changes add lines too long for our C++ style:
36
37 '"$bad"'
38
39 Use lines strictly less than '"$line_too_long"' characters in C++ code.'
40
41 #-------------------------------------------------------------------------------
42 if test -z "$HOOKS_ALLOW_KWSYS"; then
43   # Disallow changes to KWSys
44   files=$(git diff-index --name-only --cached HEAD -- Source/kwsys) &&
45   if test -n "$files"; then
46     die 'Changes to KWSys files
47
48 '"$(echo "$files" | sed 's/^/  /')"'
49
50 cannot be committed through Git.  KWSys is kept in a CVS repository
51 shared by several projects.  A robot replays changes committed there
52 into the Source/kwsys directory in CMake.  Please send changes to
53 this directory separately.  Run
54
55   git reset HEAD -- Source/kwsys
56
57 to unstage these changes and then
58
59   git diff -- Source/kwsys > kwsys.patch
60
61 to construct the patch.  Alternatively, set environment variable
62
63   HOOKS_ALLOW_KWSYS=1
64
65 to disable this check and commit the changes locally.'
66   fi
67 fi