summaryrefslogtreecommitdiff
path: root/scripts/clang-format-diff.sh
blob: c1f1885b72769a7a66ebb04290b947c83cc93f39 (plain)
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
#!/bin/bash

set -o errexit
set -o pipefail

# When we are running on travis and *not* part of a pull request we don't
# have any upstream branch to compare against.
if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then
  echo "Skipping since not running on travis PR"
  exit 0
fi

if [ -n "$TRAVIS_BRANCH" ]; then
  BRANCH=$TRAVIS_BRANCH
else
  BRANCH=origin/main
fi

MERGE_BASE=$(git merge-base $BRANCH HEAD)
FORMAT_MSG=$(git clang-format $MERGE_BASE -q --diff -- src/)
if [ -n "$FORMAT_MSG" -a "$FORMAT_MSG" != "no modified files to format" ]
then
  echo "Please run git clang-format before committing, or apply this diff:"
  echo
  # Run git clang-format again, this time without capruting stdout.  This way
  # clang-format format the message nicely and add color.
  git clang-format $MERGE_BASE -q --diff -- src/
  exit 1
fi