| 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 list_files() { |
| 96 | hg status -a -m -c | |
| 97 | cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' | sort | uniq |
| 98 | } |
| 99 | |
| 100 | function changed_files() { |
| 101 | { |
| 102 | if [ -n "$HG_PARENT1" ] |
| 103 | then |
| 104 | hg status --rev $HG_PARENT1:$HG_NODE -a -m |
| 105 | fi |
| 106 | if [ -n "$HG_PARENT2" ] |
| 107 | then |
| 108 | hg status --rev $HG_PARENT2:$HG_NODE -a -m |
| 109 | fi |
| 110 | } | cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' | sort | uniq |
| 111 | } |
| 112 | |
| 113 | if [ $# == 0 ]; then |
| 114 | echo Check all modified files... |
| 115 | while read FILENAME |
| 116 | do |
| 117 | check_file "$HGROOT/$FILENAME" |
| 118 | ((TOTAL_FILES++)) |
| 119 | done < <(changed_files) |
| 120 | echo ' done.' |
| 121 | elif [[ ($# == 1) && (($1 == "--all") || ($1 == "-a")) ]]; then |
| 122 | echo Check all source files... |
| 123 | while read FILENAME |
| 124 | do |
| 125 | check_file "$HGROOT/$FILENAME" |
| 126 | ((TOTAL_FILES++)) |
| 127 | done < <(list_files) |
| 128 | echo ' done.' |
| 129 | else |
| 130 | for i in $@ |
| 131 | do |
| 132 | check_file $i |
| 133 | ((TOTAL_FILES++)) |
| 134 | done |
| 135 | fi |
| 136 | |
| 137 | echo $FAILED_FILES out of $TOTAL_FILES files has failed. |
| 138 | |
| 139 | [[ $GLOBAL_FAILED == NO ]] |