Skip to content

Commit 1578cec

Browse files
authored
Improve error detection in clang-tidy/clang-format scripts (#2156)
- Error out of shell script if any command fails. - Don't try to run on travis builds that are not PRs (i.e. don't have any upstream). - Prefer origin/master over just master (some folks don't use master locally). - Don't hide stderr by piping it to dev/null.
1 parent 4e299e9 commit 1578cec

2 files changed

Lines changed: 37 additions & 5 deletions

File tree

clang-format-diff.sh

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
#!/bin/bash
2-
MERGE_BASE=$(git merge-base master HEAD)
2+
3+
set -o errexit
4+
5+
# When we are running on travis and *not* part of a pull request we don't
6+
# have any upstream branch to compare against.
7+
if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then
8+
echo "Skipping since not running on travis PR"
9+
exit 0
10+
fi
11+
12+
if [ -n "$TRAVIS_BRANCH" ]; then
13+
BRANCH=$TRAVIS_BRANCH
14+
else
15+
BRANCH=origin/master
16+
fi
17+
18+
MERGE_BASE=$(git merge-base $BRANCH HEAD)
319
FORMAT_MSG=$(git clang-format $MERGE_BASE -q --diff -- src/)
420
if [ -n "$FORMAT_MSG" -a "$FORMAT_MSG" != "no modified files to format" ]
521
then
622
echo "Run git clang-format before committing!"
723
echo
8-
# Run git clang-format once again to show the error
24+
# Run git clang-format again, this time without capruting stdout. This way
25+
# clang-format format the message nicely and add color.
926
git clang-format $MERGE_BASE -q --diff -- src/
1027
exit 1
1128
fi

clang-tidy-diff.sh

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
#!/bin/bash
2+
3+
set -o errexit
4+
5+
# When we are running on travis and *not* part of a pull request we don't
6+
# have any upstream branch to compare against.
7+
if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then
8+
echo "Skipping since not running on travis PR"
9+
exit 0
10+
fi
11+
12+
if [ -n "$TRAVIS_BRANCH" ]; then
13+
BRANCH=$TRAVIS_BRANCH
14+
else
15+
BRANCH=origin/master
16+
fi
17+
218
CLANG_DIR=$(dirname $(dirname $(which clang-tidy)))
319
CLANG_TIDY_DIFF=$CLANG_DIR/share/clang/clang-tidy-diff.py
4-
MERGE_BASE=$(git merge-base master HEAD)
5-
TIDY_MSG=$(git diff -U0 $MERGE_BASE | $CLANG_TIDY_DIFF -quiet -p1 2> /dev/null)
20+
TIDY_MSG=$(git diff -U0 $BRANCH... | $CLANG_TIDY_DIFF -quiet -p1 2> /dev/null)
621
if [ -n "$TIDY_MSG" -a "$TIDY_MSG" != "No relevant changes found." ]
722
then
823
echo "Fix clang-tidy errors before committing!"
924
echo
1025
# Run clang-tidy once again to show the error
11-
git diff -U0 $MERGE_BASE | $CLANG_TIDY_DIFF -quiet -p1 2> /dev/null
26+
git diff -U0 $BRANCH... | $CLANG_TIDY_DIFF -quiet -p1 2> /dev/null
1227
exit 1
1328
fi

0 commit comments

Comments
 (0)