aboutsummaryrefslogtreecommitdiff
path: root/PRESUBMIT.sh
blob: 14fc8e9f53ab08c2c5977a41e11d40c54d31f1e0 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env bash
# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Check that:
#  - all C++ and header files conform to clang-format style.
#  - all header files have appropriate include guards.
#  - all GN files are formatted with `gn format`.
fail=0

function check_clang_format() {
  if ! cmp -s <(clang-format -style=file "$1") "$1"; then
    echo "Needs format: $1"
    fail=1
  fi
}

function check_include_guard() {
  # Replace all folder slashes with underscores, and add "_" suffix.
  guard_name=${1//[\/\.]/_}_

  # This to-uppercase syntax is available in bash 4.0+
  guard_name=${guard_name^^}

  ifndef_count=$(grep -E "^#ifndef $guard_name\$" "$1" | wc -l)
  define_count=$(grep -E "^#define $guard_name\$" "$1" | wc -l)
  endif_count=$(grep -E "^#endif  // $guard_name\$" "$1" | wc -l)
  if [ $ifndef_count -ne 1 -o $define_count -ne 1 -o \
       $endif_count -ne 1 ]; then
    echo "Include guard missing/incorrect: $1"
    fail=1
  fi
}

function check_gn_format() {
  if ! which gn &>/dev/null; then
    echo "Please add gn to your PATH or manually check $1 for format errors."
  else
    if ! cmp -s <(cat "$1" | gn format --stdin) "$1"; then
      echo "Needs format: $1"
      fail=1
    fi
  fi
}

if [[ "${BASH_VERSION:0:1}" -lt 4 ]]; then
  echo "This script requires at least bash version 4.0, please upgrade!"
  echo "Your version: " $BASH_VERSION
  exit $fail
fi

for f in $(git diff --name-only --diff-filter=d @{u}); do
  # Skip third party files, except our custom BUILD.gns
  if [[ $f =~ third_party/[^\/]*/src ]]; then
    continue;
  fi

  # Skip statically copied Chromium QUIC build files.
  if [[ $f =~ third_party/chromium_quic/build ]]; then
    continue;
  fi

  if [[ $f =~ \.(cc|h)$ ]]; then
    # clang-format check.
    check_clang_format "$f"
    # Include guard check.
    if [[ $f =~ \.h$ ]]; then
      check_include_guard "$f"
    fi
  elif [[ $f =~ \.gn(i)?$ ]]; then
    check_gn_format "$f"
  fi
done

exit $fail