~ubuntu-branches/debian/sid/simpleitk/sid

« back to all changes in this revision

Viewing changes to Utilities/Hooks/pre-commit

  • Committer: Package Import Robot
  • Author(s): Ghislain Antony Vaillant
  • Date: 2017-11-02 08:49:18 UTC
  • Revision ID: package-import@ubuntu.com-20171102084918-7hs09ih668xq87ej
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env bash
 
2
#==========================================================================
 
3
#
 
4
#   Copyright Insight Software Consortium
 
5
#
 
6
#   Licensed under the Apache License, Version 2.0 (the "License");
 
7
#   you may not use this file except in compliance with the License.
 
8
#   You may obtain a copy of the License at
 
9
#
 
10
#          http://www.apache.org/licenses/LICENSE-2.0.txt
 
11
#
 
12
#   Unless required by applicable law or agreed to in writing, software
 
13
#   distributed under the License is distributed on an "AS IS" BASIS,
 
14
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
15
#   See the License for the specific language governing permissions and
 
16
#   limitations under the License.
 
17
#
 
18
#==========================================================================*/
 
19
 
 
20
egrep-q() {
 
21
  egrep "$@" >/dev/null 2>/dev/null
 
22
}
 
23
 
 
24
die() {
 
25
  echo 'pre-commit hook failure' 1>&2
 
26
  echo '-----------------------' 1>&2
 
27
  echo '' 1>&2
 
28
  echo "$@" 1>&2
 
29
  exit 1
 
30
}
 
31
 
 
32
#-----------------------------------------------------------------------------
 
33
 
 
34
# Check that developmer setup is up-to-date.
 
35
lastSetupForDevelopment=$(git config --get hooks.SetupForDevelopment || echo 0)
 
36
eval $(grep '^SetupForDevelopment_VERSION=' "${BASH_SOURCE%/*}/../SetupForDevelopment.sh")
 
37
test -n "$SetupForDevelopment_VERSION" || SetupForDevelopment_VERSION=0
 
38
if test $lastSetupForDevelopment -lt $SetupForDevelopment_VERSION; then
 
39
  die 'Developer setup in this work tree is out of date.  Please re-run
 
40
 
 
41
  Utilities/SetupForDevelopment.sh
 
42
'
 
43
fi
 
44
 
 
45
#-----------------------------------------------------------------------------
 
46
# Check files added by commit
 
47
 
 
48
added=$(git diff-index --diff-filter=A --cached HEAD --)
 
49
added_normal=$(echo "$added" | grep -v '^:...... 160000')
 
50
added_module=$(echo "$added" | grep    '^:...... 160000')
 
51
 
 
52
# Do not allow adding of files with .txx extension.
 
53
added_txx=$(echo "$added_normal" | grep '\.txx$')
 
54
bad=$(
 
55
  test -n "$added_txx" &&
 
56
  echo "Files with the .txx extension are deprecated -- please use .hxx instead:" &&
 
57
  echo "$added_txx" | awk '{printf("  %s\n",$6)}'
 
58
)
 
59
test -z "$bad" || die "$bad"
 
60
 
 
61
# Do not allow addition of submodules.
 
62
bad=$(
 
63
  test -n "$added_module" &&
 
64
  echo "Submodules may not be added to SimpleITK at this time:" &&
 
65
  echo "$added_module" | awk '{printf("  %s\n",$6)}'
 
66
)
 
67
test -z "$bad" || die "$bad"
 
68
 
 
69
#-----------------------------------------------------------------------------
 
70
# Style hooks.
 
71
. "${BASH_SOURCE%/*}/pre-commit-style.bash"
 
72
 
 
73
# Validate json files
 
74
validate_json() {
 
75
    changes=$(git diff-files -- "$1") &&
 
76
    if test -n "$changes"; then
 
77
        die "Cannot validate '$1' with work tree changes."
 
78
    fi &&
 
79
    out=$($python_exe -c "import sys, json; json.load( open( sys.argv[1], 'r' ) )" "$1" 2>&1) || die "JSON Validation error with \"$file\".
 
80
$out"
 
81
}
 
82
 
 
83
json_files=$(git diff-index --cached HEAD --diff-filter=AM ) &&
 
84
if test -n "$json_files"; then
 
85
    validate=$(git config --get --bool hooks.ValidateJSON || echo true) &&
 
86
    if test "$validate" = "true"; then
 
87
        python_exe=$(git config hooks.python || type -p python) &&
 
88
        if test -z "$python_exe"; then
 
89
            die 'Cannont validate SimpleITK JSON files with out Python.
 
90
Configure the Python location with:
 
91
 
 
92
  git config hooks.python "/path/to/python"
 
93
 
 
94
It is NOT recommended to disable this check with:
 
95
 
 
96
  git config hooks.validatejson false
 
97
'
 
98
        fi
 
99
        echo "$json_files" |
 
100
        while read src_mode dst_mode src_obj dst_obj status file; do
 
101
            if echo "$file" | egrep-q '\.json$'; then
 
102
                echo "validating $file"
 
103
                validate_json "$file"
 
104
            fi
 
105
        done
 
106
    fi
 
107
fi