From 9cb4018235ddb2c5e948e39e0193de9cc6853b2d Mon Sep 17 00:00:00 2001 From: KongQun Yang Date: Tue, 26 Sep 2017 15:05:39 -0700 Subject: [PATCH] Add check formatting script The script can be set as pre-commit hook to run clang-format check automatically on every commit. Change-Id: I05c8e5266ebc773c7949c64d7a885809070a58ec --- packager/tools/git/check_formatting.py | 50 ++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 packager/tools/git/check_formatting.py diff --git a/packager/tools/git/check_formatting.py b/packager/tools/git/check_formatting.py new file mode 100755 index 0000000000..429cc2aa73 --- /dev/null +++ b/packager/tools/git/check_formatting.py @@ -0,0 +1,50 @@ +#!/usr/bin/python +# +# Copyright 2017 Google Inc. All rights reserved. +# +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file or at +# https://developers.google.com/open-source/licenses/bsd +"""Presubmit script to check clang format. + +Install as pre-commit hook: + + cp packager/tools/git/check_formatting.py .git/hooks/pre-commit + +Steps to install clang-format on your system if you don't have it already: + +1. Install the standalone clang-format tool: + + Linux: sudo apt-get install clang-format + Mac: brew install clang-format + +2. Download git-clang-format from + https://llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/git-clang-format + +3. Move the script somewhere in your path, e.g. + sudo mv git-clang-format /usr/bin/ + +4. Make the script executable: sudo chmod +x /usr/bin/git-clang-format. + +5. Check it's been picked up by git: git clang-format -h. + +6. Try it out with: git clang-format --diff. + +""" + +import subprocess +import sys + +if __name__ == '__main__': + command = ['git', 'clang-format', '--style', 'Chromium'] + command += sys.argv[1:] + output = subprocess.check_output(command + ['--diff']) + + if output not in [ + 'no modified files to format\n', 'clang-format did not modify any files\n' + ]: + print output, '\n' + print 'Code style is not correct. Please run ', ' '.join(command), '\n' + sys.exit(1) + else: + sys.exit(0)