| 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 | if check_header $1 |
| 43 | then |
| 44 | echo "header failed" |
| 45 | FILE_FAILED=YES |
| 46 | GLOBAL_FAILED=YES |
| 47 | fi |
| 48 | |
| 49 | if grep -q -E "$TAB" $1 |
| 50 | then |
| 51 | echo "tabs failed:" |
| 52 | grep -n -E "$TAB" $1 |
| 53 | FILE_FAILED=YES |
| 54 | GLOBAL_FAILED=YES |
| 55 | fi |
| 56 | |
| 57 | if grep -q -E ' $' $1 |
| 58 | then |
| 59 | echo "trailing spaces failed:" |
| 60 | grep -n -E ' $' $1 |
| 61 | FILE_FAILED=YES |
| 62 | GLOBAL_FAILED=YES |
| 63 | fi |
| 64 | |
| 65 | if grep -q -E '.{81,}' $1 |
| 66 | then |
| 67 | echo "long lines failed:" |
| 68 | grep -n -E '.{81,}' $1 |
| 69 | if [ $GLOBAL_FAILED != YES ] |
| 70 | then |
| 71 | echo -n "Abort commit? (yes/no) " |
| 72 | read RESULT |
| 73 | if [ "$RESULT" != "no" ] |
| 74 | then |
| 75 | FILE_FAILED=YES |
| 76 | GLOBAL_FAILED=YES |
| 77 | fi |
| 78 | fi |
| 79 | fi |
| 80 | |
| 81 | if [[ $FILE_FAILED = YES ]]; |
| 82 | then |
| 83 | ((FAILED_FILES++)) |
| 84 | fi |
| 85 | } |
| 86 | |
| 87 | FAILED_FILES=0 |
| 88 | TOTAL_FILES=0 |
| 89 | GLOBAL_FAILED=NO |
| 90 | |
| 91 | function changed_files() { |
| 92 | { |
| 93 | if [ -n "$HG_PARENT1" ] |
| 94 | then |
| 95 | hg status --rev $HG_PARENT1:$HG_NODE -a -m |
| 96 | fi |
| 97 | if [ -n "$HG_PARENT2" ] |
| 98 | then |
| 99 | hg status --rev $HG_PARENT2:$HG_NODE -a -m |
| 100 | fi |
| 101 | } | cut -d ' ' -f 2 | grep -E '\.(cc|h|dox)$' | sort | uniq |
| 102 | } |
| 103 | |
| 104 | if [ $# == 0 ]; then |
| 105 | echo Check all modified files... |
| 106 | |
| 107 | while read FILENAME |
| 108 | do |
| 109 | check_file "$HGROOT/$FILENAME" |
| 110 | ((TOTAL_FILES++)) |
| 111 | done < <(changed_files) |
| 112 | echo ' done.' |
| 113 | else |
| 114 | for i in $@ |
| 115 | do |
| 116 | check_file $i |
| 117 | ((TOTAL_FILES++)) |
| 118 | done |
| 119 | fi |
| 120 | |
| 121 | echo $FAILED_FILES out of $TOTAL_FILES files has failed. |
| 122 | |
| 123 | [[ $GLOBAL_FAILED == NO ]] |