1#!/usr/bin/env python3
2#
3# Arm SCP/MCP Software
4# Copyright (c) 2024, Arm Limited and Contributors. All rights reserved.
5#
6# SPDX-License-Identifier: BSD-3-Clause
7#
8
9"""
10    Check for code style violations.
11"""
12
13import argparse
14import subprocess
15import sys
16from utils import banner, get_previous_commit
17
18#
19# Default output file
20#
21DEFAULT_OUTPUT_FILE = 'code-style.patch'
22
23
24def run(output_file=DEFAULT_OUTPUT_FILE, commit_hash=get_previous_commit()):
25    print(banner(f'Run coding style checks against {commit_hash[:8]}'))
26
27    # Run git clang-format with the previous commit hash and capture the patch
28    result = subprocess.run(
29        ['git', 'clang-format', '--quiet', '--diff', commit_hash],
30        stdout=subprocess.PIPE,
31        stderr=subprocess.PIPE,
32        text=True
33    )
34
35    patch = result.stdout
36
37    if patch and not patch.startswith('no modified files to format'):
38        # Write the patch to code-style.patch
39        with open(output_file, 'w') as patch_file:
40            patch_file.write(patch)
41
42        # Print messages
43        print('Code style deviations were identified.')
44        print('')
45        print('Please apply the supplied patch with:')
46        print(f'    patch -p1 < {output_file}')
47        return False
48
49    print('No code style deviations were identified.')
50    print('')
51    return True
52
53
54def parse_args(argv, prog_name):
55    parser = argparse.ArgumentParser(
56        prog=prog_name,
57        description='Perform code style check to SCP-Firmware')
58
59    parser.add_argument('-o', '--output-file', dest='output_file',
60                        required=False, default=DEFAULT_OUTPUT_FILE, type=str,
61                        action='store', help=f'Output file location, if it is\
62                        not specified, the default value will be\
63                        {DEFAULT_OUTPUT_FILE}')
64
65    parser.add_argument('-c', '--commit', dest='commit_hash',
66                        required=False, default=get_previous_commit(),
67                        type=str, action='store', help='Specify a commit \
68                        hash. If not specified, defaults to the previous \
69                        commit.')
70
71    return parser.parse_args(argv)
72
73
74def main(argv=[], prog_name=''):
75    args = parse_args(argv, prog_name)
76    return 0 if run(args.output_file, args.commit_hash) else 1
77
78
79if __name__ == '__main__':
80    sys.exit(main(sys.argv[1:], sys.argv[0]))
81