Merge pull request #7822 from gilles-peskine-arm/code-style-since
code_style.py --since
This commit is contained in:
commit
38939f705a
1 changed files with 34 additions and 24 deletions
|
@ -22,7 +22,7 @@ import os
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from typing import FrozenSet, List
|
from typing import FrozenSet, List, Optional
|
||||||
|
|
||||||
UNCRUSTIFY_SUPPORTED_VERSION = "0.75.1"
|
UNCRUSTIFY_SUPPORTED_VERSION = "0.75.1"
|
||||||
CONFIG_FILE = ".uncrustify.cfg"
|
CONFIG_FILE = ".uncrustify.cfg"
|
||||||
|
@ -63,31 +63,38 @@ def list_generated_files() -> FrozenSet[str]:
|
||||||
checks = re.findall(CHECK_CALL_RE, content)
|
checks = re.findall(CHECK_CALL_RE, content)
|
||||||
return frozenset(word for s in checks for word in s.split())
|
return frozenset(word for s in checks for word in s.split())
|
||||||
|
|
||||||
def get_src_files() -> List[str]:
|
def get_src_files(since: Optional[str]) -> List[str]:
|
||||||
"""
|
"""
|
||||||
Use git ls-files to get a list of the source files
|
Use git to get a list of the source files.
|
||||||
|
|
||||||
|
The optional argument since is a commit, indicating to only list files
|
||||||
|
that have changed since that commit. Without this argument, list all
|
||||||
|
files known to git.
|
||||||
|
|
||||||
|
Only C files are included, and certain files (generated, or 3rdparty)
|
||||||
|
are excluded.
|
||||||
"""
|
"""
|
||||||
git_ls_files_cmd = ["git", "ls-files",
|
file_patterns = ["*.[hc]",
|
||||||
"*.[hc]",
|
"tests/suites/*.function",
|
||||||
"tests/suites/*.function",
|
"scripts/data_files/*.fmt"]
|
||||||
"scripts/data_files/*.fmt"]
|
output = subprocess.check_output(["git", "ls-files"] + file_patterns,
|
||||||
|
universal_newlines=True)
|
||||||
|
src_files = output.split()
|
||||||
|
if since:
|
||||||
|
output = subprocess.check_output(["git", "diff", "--name-only",
|
||||||
|
since, "--"] +
|
||||||
|
src_files,
|
||||||
|
universal_newlines=True)
|
||||||
|
src_files = output.split()
|
||||||
|
|
||||||
result = subprocess.run(git_ls_files_cmd, stdout=subprocess.PIPE,
|
generated_files = list_generated_files()
|
||||||
check=False)
|
# Don't correct style for third-party files (and, for simplicity,
|
||||||
|
# companion files in the same subtree), or for automatically
|
||||||
if result.returncode != 0:
|
# generated files (we're correcting the templates instead).
|
||||||
print_err("git ls-files returned: " + str(result.returncode))
|
src_files = [filename for filename in src_files
|
||||||
return []
|
if not (filename.startswith("3rdparty/") or
|
||||||
else:
|
filename in generated_files)]
|
||||||
generated_files = list_generated_files()
|
return src_files
|
||||||
src_files = str(result.stdout, "utf-8").split()
|
|
||||||
# Don't correct style for third-party files (and, for simplicity,
|
|
||||||
# companion files in the same subtree), or for automatically
|
|
||||||
# generated files (we're correcting the templates instead).
|
|
||||||
src_files = [filename for filename in src_files
|
|
||||||
if not (filename.startswith("3rdparty/") or
|
|
||||||
filename in generated_files)]
|
|
||||||
return src_files
|
|
||||||
|
|
||||||
def get_uncrustify_version() -> str:
|
def get_uncrustify_version() -> str:
|
||||||
"""
|
"""
|
||||||
|
@ -182,6 +189,9 @@ def main() -> int:
|
||||||
parser.add_argument('-f', '--fix', action='store_true',
|
parser.add_argument('-f', '--fix', action='store_true',
|
||||||
help=('modify source files to fix the code style '
|
help=('modify source files to fix the code style '
|
||||||
'(default: print diff, do not modify files)'))
|
'(default: print diff, do not modify files)'))
|
||||||
|
parser.add_argument('-s', '--since', metavar='COMMIT',
|
||||||
|
help=('only check files modified since the specified commit'
|
||||||
|
' (e.g. --since=HEAD~3 or --since=development)'))
|
||||||
# --subset is almost useless: it only matters if there are no files
|
# --subset is almost useless: it only matters if there are no files
|
||||||
# ('code_style.py' without arguments checks all files known to Git,
|
# ('code_style.py' without arguments checks all files known to Git,
|
||||||
# 'code_style.py --subset' does nothing). In particular,
|
# 'code_style.py --subset' does nothing). In particular,
|
||||||
|
@ -194,7 +204,7 @@ def main() -> int:
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
covered = frozenset(get_src_files())
|
covered = frozenset(get_src_files(args.since))
|
||||||
# We only check files that are known to git
|
# We only check files that are known to git
|
||||||
if args.subset or args.operands:
|
if args.subset or args.operands:
|
||||||
src_files = [f for f in args.operands if f in covered]
|
src_files = [f for f in args.operands if f in covered]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue