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
This commit is contained in:
KongQun Yang 2017-09-26 15:05:39 -07:00
parent 23a2893a35
commit 9cb4018235
1 changed files with 50 additions and 0 deletions

View File

@ -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)