Add --diff to run_formatter and update_reference

When --diff is provided, they output nonzero statuses when changes are
required. This makes them suitable to run in CI

Change-Id: Iceda9895beae955e13059e74739810126a6a6964
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/22060
Reviewed-by: Takuto Ikuta <tikuta@google.com>
Commit-Queue: Matt Stark <msta@google.com>
diff --git a/docs/reference.md b/docs/reference.md
index 990b682..1742923 100644
--- a/docs/reference.md
+++ b/docs/reference.md
@@ -8583,4 +8583,3 @@
     *   -v: Verbose logging.
     *   --version: Prints the GN version number and exits.
 ```
-
diff --git a/tools/run_formatter.sh b/tools/run_formatter.sh
index d7197b9..a5f81a3 100755
--- a/tools/run_formatter.sh
+++ b/tools/run_formatter.sh
@@ -1,12 +1,20 @@
-#!/bin/sh
+#!/bin/bash -eu
 
 cd $(dirname $(dirname $0))
 
-ensure_file=$(mktemp)
+if [ "${1:-}" = "--diff" ]; then
+  opts="--dry-run -Werror"
+else
+  opts="-i"
+fi
 
-# https://chrome-infra-packages.appspot.com/p/fuchsia/third_party/clang
-echo 'fuchsia/third_party/clang/${platform} integration' > $ensure_file
-cipd ensure -ensure-file $ensure_file -root clang
+if [ -z "${CLANG_FORMAT:-}" ]; then
+  ensure_file=$(mktemp)
+  # https://chrome-infra-packages.appspot.com/p/fuchsia/third_party/clang
+  echo 'fuchsia/third_party/clang/${platform} integration' > $ensure_file
+  cipd ensure -ensure-file $ensure_file -root clang
+  CLANG_FORMAT="./clang/bin/clang-format"
+fi
 
 git ls-files | egrep '\.(h|cc)$' | fgrep -v 'third_party' |\
-    xargs ./clang/bin/clang-format -i
+    xargs "$CLANG_FORMAT" $opts
diff --git a/tools/update_reference.sh b/tools/update_reference.sh
index 50427e1..99bd0ee 100755
--- a/tools/update_reference.sh
+++ b/tools/update_reference.sh
@@ -1,4 +1,9 @@
-#!/bin/sh
+#!/bin/bash -eu
+
+check=false
+if [[ "$#" -ge 1 && "$1" == "--diff" ]]; then
+    check=true
+fi
 
 # Check for the existance of the AUTHORS file as an easy way to determine if
 # it's being run from the correct directory.
@@ -6,7 +11,14 @@
     echo Building gn...
     ninja -C out gn
     echo Generating new docs/reference.md...
-    out/gn help --markdown all > docs/reference.md
+    content=$(out/gn help --markdown all)
+
+    if "${check}"; then
+        diff -u docs/reference.md <(echo "$content")
+    else
+        echo "$content" > docs/reference.md
+    fi
 else
     echo Please run this command from the GN checkout root directory.
+    exit 1
 fi