Menu

mercurialhook-cppcheck-diff-hook

Daniel Marjamäki

Overview

Hook script to compare cppcheck before and after a mercurial commit, uses cppcheck-diff.sh. The usage for the script is described in detail at pre-commit hook.

Limitations

  • Assumes "safe" host system
  • Needs interactive terminal
  • Needs writeable $TMPDIR (which is hard-coded)
  • cpp/h filenames are hard-coded. Script needs to be altered to match your project

Script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
# Licence : GPLv3+, 2013
HG_ROOT=`hg root`

if [ x"$HG_ROOT" == x"" ] ; then
    echo "not an hg repo..."
    exit 1
fi

pushd ${HG_ROOT} > /dev/null

#Get a list of modified files
ALTERED_FILES=`hg status | egrep '^[MA]' | egrep '\.(cpp|h)$' | sed 's/[MA]\s//'`

TMPDIR=/tmp/cppcheck-newerrs/

mkdir -p "$TMPDIR"

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for i in $ALTERED_FILES
do
    echo "Checking for errors :  $i"
    hg diff "$i" > $TMPDIR/patch-file-dhje
    mkdir -p `dirname "$TMPDIR/$i"`
    cp "$i" "$TMPDIR/$i"
    cp "$i" "$TMPDIR/$i.orig"

    pushd $TMPDIR > /dev/null
    patch -Rs -p1 < patch-file-dhje
    cppcheck-diff.sh "$i" "$i.orig" >> "$TMPDIR/cppcheck-new-errs"
    rm "$i" "$i.orig" patch-file-dhje
    popd > /dev/null
done

popd > /dev/null

if [ ! -f $TMPDIR/cppcheck-new-errs ] ; then
    exit 0
fi

if [ `wc -l $TMPDIR/cppcheck-new-errs | awk '{print $1}' ` -ne 0 ] ; then
    echo "The following new cppcheck warnings were detected:"
    cat "$TMPDIR/cppcheck-new-errs"

    echo "abort? (y/n)"

    read wantAbort

    if [ x"$wantAbort" == x"y" ] ; then
        rm -rf "$TMPDIR"
        exit 1
    fi
fi
rm -rf "$TMPDIR"

Related

Wiki: mercurialhook-cppcheck-diff
Wiki: mercurialhook

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.