2021-05-03 03:30:36 +00:00
|
|
|
#!/usr/bin/python3
|
2017-09-26 22:05:39 +00:00
|
|
|
#
|
2023-12-01 17:32:19 +00:00
|
|
|
# Copyright 2017 Google LLC. All rights reserved.
|
2017-09-26 22:05:39 +00:00
|
|
|
#
|
|
|
|
# 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
|
2021-05-07 17:21:40 +00:00
|
|
|
https://github.com/llvm-mirror/clang/blob/master/tools/clang-format/git-clang-format
|
2017-09-26 22:05:39 +00:00
|
|
|
|
|
|
|
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__':
|
2018-05-05 00:35:15 +00:00
|
|
|
is_pre_commit_hook = len(sys.argv) == 1
|
|
|
|
if not is_pre_commit_hook:
|
|
|
|
output = subprocess.check_output(['git', 'log', '--pretty=full', '-1'])
|
2021-05-03 03:30:36 +00:00
|
|
|
if b'disable-clang-format' in output:
|
2018-05-05 00:35:15 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
2017-09-26 22:05:39 +00:00
|
|
|
command = ['git', 'clang-format', '--style', 'Chromium']
|
|
|
|
command += sys.argv[1:]
|
|
|
|
output = subprocess.check_output(command + ['--diff'])
|
|
|
|
|
|
|
|
if output not in [
|
2021-05-03 03:30:36 +00:00
|
|
|
b'no modified files to format\n',
|
|
|
|
b'clang-format did not modify any files\n'
|
2017-09-26 22:05:39 +00:00
|
|
|
]:
|
2021-06-18 18:08:36 +00:00
|
|
|
print(output.decode('utf-8'))
|
2020-06-02 07:22:36 +00:00
|
|
|
print()
|
|
|
|
print('Code style is not correct. Please run {}.'.format(' '.join(command)))
|
|
|
|
print()
|
2017-09-26 22:05:39 +00:00
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
sys.exit(0)
|