| 1 | #!/bin/bash |
| 2 | |
| 3 | # Add to your .hg/hgrc the following section. |
| 4 | # [hooks] |
| 5 | # pretxncommit.checksources = scripts/check-sources.sh |
| 6 | |
| 7 | YEAR=`date +2003-%Y` |
| 8 | HGROOT=`hg root` |
| 9 | |
| 10 | TAB=`echo -e '\t'` |
| 11 | |
| 12 | function check_header() { |
| 13 | HEADER=\ |
| 14 | "/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 15 | * |
| 16 | * This file is a part of LEMON, a generic C++ optimization library. |
| 17 | * |
| 18 | * Copyright (C) "$YEAR" |
| 19 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 20 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 21 | * |
| 22 | * Permission to use, modify and distribute this software is granted |
| 23 | * provided that this copyright notice appears in all copies. For |
| 24 | * precise terms see the accompanying LICENSE file. |
| 25 | * |
| 26 | * This software is provided \"AS IS\" with no warranty of any kind, |
| 27 | * express or implied, and with no claim as to its suitability for any |
| 28 | * purpose. |
| 29 | * |
| 30 | */" |
| 31 | |
| 32 | HEADER_LENGTH=$(echo "$HEADER" | wc -l) |
| 33 | FILE_HEADER=$(cat $1 | head -n $HEADER_LENGTH) |
| 34 | |
| 35 | [[ "$FILE_HEADER" != "$HEADER" ]] |
| 36 | } |
| 37 | |
| 38 | function check_file() { |
| 39 | echo ' check' $1 ... |
| 40 | |
| 41 | FILE_FAILED=NO |
| 42 | |
| 43 | if echo $1 | grep -q -v -E 'Makefile\.am$' |
| 44 | then |
| 45 | if check_header $1 |
| 46 | then |
| 47 | echo "header failed" |
| 48 | FILE_FAILED=YES |
| 49 | GLOBAL_FAILED=YES |
| 50 | fi |
| 51 | fi |
| 52 | |
| 53 | if grep -q -E "$TAB" $1 |
| 54 | then |
| 55 | echo "tabs failed:" |
| 56 | grep -n -E "$TAB" $1 |
| 57 | FILE_FAILED=YES |
| 58 | GLOBAL_FAILED=YES |
| 59 | fi |
| 60 | |
| 61 | if grep -q -E ' $' $1 |
| 62 | then |
| 63 | echo "trailing spaces failed:" |
| 64 | grep -n -E ' $' $1 |
| 65 | FILE_FAILED=YES |
| 66 | GLOBAL_FAILED=YES |
| 67 | fi |
| 68 | |
| 69 | if grep -q -E '.{81,}' $1 |
| 70 | then |
| 71 | echo "long lines failed:" |
| 72 | grep -n -E '.{81,}' $1 |
| 73 | if [ $GLOBAL_FAILED != YES ] |
| 74 | then |
| 75 | echo -n "Abort commit? (yes/no) " |
| 76 | read RESULT |
| 77 | if [ "$RESULT" != "no" ] |
| 78 | then |
| 79 | FILE_FAILED=YES |
| 80 | GLOBAL_FAILED=YES |
| 81 | fi |
| 82 | fi |
| 83 | fi |
| 84 | |
| 85 | if [[ $FILE_FAILED = YES ]]; |
| 86 | then |
| 87 | ((FAILED_FILES++)) |
| 88 | fi |
| 89 | } |
| 90 | |
| 91 | FAILED_FILES=0 |
| 92 | TOTAL_FILES=0 |
| 93 | GLOBAL_FAILED=NO |
| 94 | |
| 95 | function changed_files() { |
| 96 | { |
| 97 | if [ -n "$HG_PARENT1" ] |
| 98 | then |
| 99 | hg status --rev $HG_PARENT1:$HG_NODE -a -m |
| 100 | fi |
| 101 | if [ -n "$HG_PARENT2" ] |
| 102 | then |
| 103 | hg status --rev $HG_PARENT2:$HG_NODE -a -m |
| 104 | fi |
| 105 | } | cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' | sort | uniq |
| 106 | } |
| 107 | |
| 108 | if [ $# == 0 ]; then |
| 109 | echo Check all modified files... |
| 110 | |
| 111 | while read FILENAME |
| 112 | do |
| 113 | check_file "$HGROOT/$FILENAME" |
| 114 | ((TOTAL_FILES++)) |
| 115 | done < <(changed_files) |
| 116 | echo ' done.' |
| 117 | else |
| 118 | for i in $@ |
| 119 | do |
| 120 | check_file $i |
| 121 | ((TOTAL_FILES++)) |
| 122 | done |
| 123 | fi |
| 124 | |
| 125 | echo $FAILED_FILES out of $TOTAL_FILES files has failed. |
| 126 | |
| 127 | [[ $GLOBAL_FAILED == NO ]] |