From 3920ba0d327eabd7f52ff212b303ebce72cc31c4 Mon Sep 17 00:00:00 2001 From: Dragorn421 Date: Wed, 31 Jan 2024 19:37:09 +0100 Subject: [PATCH] Check format: only check modified files, v2 --- Jenkinsfile | 4 +-- format.py | 22 ++----------- tools/check_format.py | 77 +++++++++++++++++++++++++++++++++++++++++++ tools/check_format.sh | 50 ---------------------------- 4 files changed, 81 insertions(+), 72 deletions(-) create mode 100644 tools/check_format.py delete mode 100755 tools/check_format.sh diff --git a/Jenkinsfile b/Jenkinsfile index 40aaba4498..423c865f4c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -10,7 +10,7 @@ pipeline { } steps { echo 'Checking formatting on all files...' - sh 'tools/check_format.sh full' + sh 'python3 tools/check_format.py' } } stage('Check formatting (modified)') { @@ -21,7 +21,7 @@ pipeline { } steps { echo 'Checking formatting on modified files...' - sh 'tools/check_format.sh modified' + sh 'python3 tools/check_format.py --verbose --compare-to origin/main' } } stage('Setup') { diff --git a/format.py b/format.py index 023ddfd7ab..520a2d8078 100755 --- a/format.py +++ b/format.py @@ -9,7 +9,6 @@ import shutil import subprocess import sys import tempfile -from pathlib import Path from functools import partial from typing import List @@ -151,15 +150,6 @@ def main(): action="store_true", help="Print the paths to the clang-* binaries used", ) - parser.add_argument( - "--files-list", - dest="files_list", - help="A file listing the files to format", - ) - parser.add_argument( - "--verbose", - action="store_true", - ) parser.add_argument("files", metavar="file", nargs="*") parser.add_argument( "-j", @@ -185,18 +175,10 @@ def main(): f"Error: neither clang-apply-replacements nor clang-apply-replacements-{CLANG_VER} found (required to use -j)" ) - if args.files or args.files_list: - files = [] - files.extend(args.files) - if args.files_list: - files.extend(Path(args.files_list).read_text().split()) - files = list(files) + if args.files: + files = args.files extra_files = [] - if args.verbose: - print("Formatting specific files:", len(files), files) else: - if args.verbose: - print("Formatting all files") files = glob.glob("src/**/*.c", recursive=True) extra_files = glob.glob("assets/**/*.xml", recursive=True) diff --git a/tools/check_format.py b/tools/check_format.py new file mode 100644 index 0000000000..c2d6d7d2a0 --- /dev/null +++ b/tools/check_format.py @@ -0,0 +1,77 @@ +# SPDX-FileCopyrightText: 2024 zeldaret +# SPDX-License-Identifier: CC0-1.0 + +import subprocess +import argparse +import difflib +import multiprocessing +import glob +import os.path +import sys + +sys.path.insert(0, os.curdir) +import format + +sys.path.pop(0) + + +def get_git_status(): + return subprocess.check_output("git status --porcelain".split(), text=True) + + +def get_modified_files_to_format(compare_to): + modified_files_str = subprocess.check_output( + ["git", "diff", "--name-only", compare_to], text=True + ) + modified_files = modified_files_str.splitlines() + modified_c_files = [ + f + for f in modified_files + if f.startswith("src" + os.path.sep) and f.endswith(".c") + ] + modified_c_files_existing = [f for f in modified_c_files if os.path.exists(f)] + return modified_c_files_existing + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--verbose", action="store_true") + parser.add_argument("--compare-to", dest="compare_to") + args = parser.parse_args() + + if args.compare_to: + src_files = get_modified_files_to_format(args.compare_to) + if args.verbose: + print("Formatting specific files:", len(src_files), src_files) + if not src_files: + if args.verbose: + print("Nothing to format") + exit(0) + else: + src_files = glob.glob("src/**/*.c", recursive=True) + + nb_jobs = multiprocessing.cpu_count() + + git_status_pre = get_git_status() + + format.format_files(src_files, [], nb_jobs) + + git_status_post = get_git_status() + + if git_status_pre != git_status_post: + print( + "Misformatted files found." + " Run ./format.py and verify codegen is not impacted." + ) + for l in difflib.unified_diff( + git_status_pre.splitlines(), + git_status_post.splitlines(), + "Old git status", + "New git status", + ): + print(l) + exit(1) + + +if __name__ == "__main__": + main() diff --git a/tools/check_format.sh b/tools/check_format.sh deleted file mode 100755 index a803fcbea8..0000000000 --- a/tools/check_format.sh +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/bash - -if [ $# -ne 1 ] -then - echo "Usage: $0 . Exiting." - exit -1 -fi - -STATUSOLD=`git status --porcelain` - -if [ $1 = 'modified' ] -then - FILES_LIST=.tmp_format_modified_files_list.txt - git show --name-only --format='' origin/main.. | grep 'src/.*\.c' | sort | uniq > $FILES_LIST - if [ ${PIPESTATUS[0]} -ne 0 ] # git show failed - then - echo "Listing modified files failed. Exiting." - exit -1 - fi - ./format.py --verbose -j`nproc` --files-list $FILES_LIST - FORMAT_EXIT_CODE=$? - rm $FILES_LIST -elif [ $1 = 'full' ] -then - ./format.py -j - FORMAT_EXIT_CODE=$? -else - echo "Unknown argument $1. Exiting." - exit -1 -fi - -if [ $FORMAT_EXIT_CODE -ne 0 ] -then - echo "Formatter failed. Exiting." - exit -1 -fi -STATUSNEW=`git status --porcelain` - -if [ "${STATUSOLD}" != "${STATUSNEW}" ]; -then - echo "" - echo "Misformatted files found. Run ./format.py and verify codegen is not impacted." - echo "" - diff --unified=0 --label "Old git status" <(echo "${STATUSOLD}") --label "New git status" <(echo "${STATUSNEW}") - echo "" - echo "Exiting." - exit 1 -fi - -exit 0