toolchain-utils: add a `cros lint` git hook

This adds a git hook to run `cros lint` on all of the files we're
committing. It also adds a wrapper around both check-lint and
check-format that runs the two in parallel without making the output
interleave. Better names for check-style are appreciated.

BUG=chromium:918755
TEST=Ran ./check-style on a few Python files. Got the expected
     output/lint errors/etc. Works both outside and inside of the
     chroot.
CQ-DEPEND=CL:1394285

Change-Id: I30c74662759f27abdfd663083d04002a05260bf7
Reviewed-on: https://chromium-review.googlesource.com/1395807
Commit-Ready: George Burgess <gbiv@chromium.org>
Tested-by: George Burgess <gbiv@chromium.org>
Reviewed-by: Manoj Gupta <manojgupta@chromium.org>
diff --git a/toolchain_utils_githooks/check-lint b/toolchain_utils_githooks/check-lint
new file mode 100755
index 0000000..5b9ade3
--- /dev/null
+++ b/toolchain_utils_githooks/check-lint
@@ -0,0 +1,22 @@
+#!/bin/bash -u
+# Copyright 2019 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+#
+# This script runs `cros lint` on everything it's handed.
+
+if test $# -eq 0; then
+  echo "No files were given to lint." >&2
+  echo "Usage: $0 file1 file2 ..." >&2
+  exit 1
+fi
+
+cros=cros
+
+if ! type "${cros}" >&/dev/null; then
+  echo "${cros} isn't on your \$PATH. Please either enter a chroot, or place" \
+    "depot_tools on your \$PATH." >&2
+  exit 1
+fi
+
+"${cros}" lint -- $@
diff --git a/toolchain_utils_githooks/check-style b/toolchain_utils_githooks/check-style
new file mode 100755
index 0000000..dbc38a7
--- /dev/null
+++ b/toolchain_utils_githooks/check-style
@@ -0,0 +1,44 @@
+#!/bin/bash -eu
+# Copyright 2019 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+#
+# Convenient wrapper to run format and lint checks in parallel without
+# interleaving output/etc.
+
+if test $# -eq 0; then
+  echo "No files were given to check the format of." >&2
+  echo "Usage: $0 file1 file2 ..." >&2
+  exit 1
+fi
+
+mydir="$(dirname "$(readlink -m "$0")")"
+
+tempfile="$(mktemp)"
+rm_tempfile() {
+  rm -f "${tempfile}"
+}
+
+trap rm_tempfile EXIT
+
+success=true
+
+# Since we only have two things to do in parallel, we just output the
+# stdout/stderr of this to a temp file. When the other check is done, we `cat`
+# the temp file, and we're good.
+#
+# It appears that check-lint will emit colorful output regardless of whether or
+# not it's writing to a tty, so we don't lose the nice red "ERROR" text from
+# it.
+"${mydir}/check-lint" "$@" >"${tempfile}" 2>&1 &
+pid="$!"
+
+"${mydir}/check-format" "$@" || success=false
+wait "${pid}" || success=false
+
+lint_output="$(cat "${tempfile}")"
+if ! test -z "${lint_output}"; then
+  echo "${lint_output}"
+fi
+
+$success
diff --git a/toolchain_utils_githooks/pre-push.real b/toolchain_utils_githooks/pre-push.real
index 7b65b17..c05a35b 100755
--- a/toolchain_utils_githooks/pre-push.real
+++ b/toolchain_utils_githooks/pre-push.real
@@ -46,7 +46,7 @@
     all_files="$(git show --pretty="format:" --name-only "${range}")"
     # Note that ${all_files} may include files that were deleted. Hence, we
     # ignore any complaints about missing files.
-    IGNORE_MISSING=1 "${mydir}/check-format" ${all_files} || exit 1
+    IGNORE_MISSING=1 "${mydir}/check-style" ${all_files} || exit 1
   fi
 done