tools: add path support for checkpatch.sh

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
This commit is contained in:
Quentin Young 2018-01-03 16:30:56 -05:00
parent 33fe08f4b1
commit a162e6a599
No known key found for this signature in database
GPG Key ID: DAF48E0F57E0834F

View File

@ -1,69 +1,76 @@
#!/bin/bash #!/bin/bash
# Check a patch for style errors. # Check a patch for style errors.
# Usage: usage="./checkpatch.sh <patch> <tree>"
# ./checkpatch.sh <patch> patch=$1
checkpatch="./checkpatch.pl --no-tree -f" tree=$2
checkpatch="$tree/tools/checkpatch.pl --no-tree -f"
ignore="ldpd\|babeld" ignore="ldpd\|babeld"
cwd=${PWD##*/} cwd=${PWD##*/}
dirty=0 dirty=0
# check running from frr/tools/ if [[ -z "$1" || -z "$2" ]]; then
if [[ $cwd != *"tools"* ]]; then echo "$usage"
echo "[!] script must be run from tools/ directory" exit 0
exit 1
fi fi
# save working tree # save working tree
cd .. if git -C $tree status --porcelain | egrep --silent '^(\?\?|.[DM])'; then
if git status --porcelain | egrep --silent '^(\?\?|.[DM])'; then
echo "Detected dirty tree, caching state..." echo "Detected dirty tree, caching state..."
dirty=1 dirty=1
git config gc.auto 0; git -C $tree config gc.auto 0;
td=$(git status -z | grep -z "^[ARM]D" | cut -z -d' ' -f2- | tr '\0' '\n') td=$(git -C $tree status -z | grep -z "^[ARM]D" | cut -z -d' ' -f2- | tr '\0' '\n')
INDEX=`git write-tree` INDEX=`git -C $tree write-tree`
git add -f . git -C $tree add -f .
WORKTREE=`git write-tree` WORKTREE=`git -C $tree write-tree`
echo "Saved index to $INDEX" echo "Saved index to $INDEX"
echo "Saved working tree to $WORKTREE" echo "Saved working tree to $WORKTREE"
fi fi
# double check # double check
if git status --porcelain | egrep --silent '^(\?\?|.[DM])'; then if git -C $tree status --porcelain | egrep --silent '^(\?\?|.[DM])'; then
echo "[!] git working directory must be clean." echo "[!] git working directory must be clean."
exit 1 exit 1
fi fi
git reset --hard git -C $tree reset --hard
git apply $1 2> /dev/null git -C $tree apply < $patch
cd tools mkdir -p /tmp/f1 /tmp/f2
mkdir -p f1 f2 mod=$(git -C $tree ls-files -m | grep ".*\.[ch]" | grep -v $ignore)
mod=$(git ls-files -m .. | grep ".*\.[ch]" | grep -v $ignore) if [ -z "$mod" ]; then
cp $mod f1/ echo "There doesn't seem to be any changes."
git reset --hard else
cp $mod f2/ cp $tree/$mod /tmp/f1/
for file in f1/*; do git -C $tree reset --hard
cp $tree/$mod /tmp/f2/
echo "Running style checks..."
for file in /tmp/f1/*; do
echo "$checkpatch $file > $file _cp"
$checkpatch $file > "$file"_cp 2> /dev/null $checkpatch $file > "$file"_cp 2> /dev/null
done done
for file in f2/*; do for file in /tmp/f2/*; do
echo "$checkpatch $file > $file _cp"
$checkpatch $file > "$file"_cp 2> /dev/null $checkpatch $file > "$file"_cp 2> /dev/null
done done
for file in f1/*_cp; do echo "Done."
if [ -a f2/$(basename $file) ]; then for file in /tmp/f1/*_cp; do
diff $file f2/$(basename $file) | grep -A3 "ERROR\|WARNING" echo "Report for $(basename $file _cp)"
echo "==============================================="
if [ -a /tmp/f2/$(basename $file) ]; then
diff $file /tmp/f2/$(basename $file) | grep -A3 "ERROR\|WARNING"
else else
cat $file cat $file
fi fi
done done
rm -rf f1 f2 rm -rf /tmp/f1 /tmp/f2
cd .. fi
# restore working tree # restore working tree
if [ $dirty -eq 1 ]; then if [ $dirty -eq 1 ]; then
git read-tree $WORKTREE; git -C $tree read-tree $WORKTREE;
git checkout-index -af; git -C $tree checkout-index -af;
git read-tree $INDEX; git -C $tree read-tree $INDEX;
if [ -n "$td" ]; then if [ -n "$td" ]; then
rm $td rm $td
fi fi
git config --unset gc.auto; git -C $tree config --unset gc.auto;
fi fi