From 44408ce219a0a84f4b2491c20eb4bc80b1959a3d Mon Sep 17 00:00:00 2001 From: Dragorn421 Date: Wed, 31 Jan 2024 04:25:24 +0100 Subject: [PATCH] Only check formatting on modified files (#1673) * only check formatting on modified files (attempt 1) * foreshadow maximum command string length being an issue and pass files list using a file * rm temp file list otherwise it shows in git status and counts as a "formatting diff"... * cheeky z_play modif * format --- Jenkinsfile | 20 +++++++++++++++++--- format.py | 22 ++++++++++++++++++++-- tools/check_format.sh | 31 +++++++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 7 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 24988b6812..40aaba4498 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -4,10 +4,24 @@ pipeline { } stages { - stage('Check formatting') { + stage('Check formatting (full)') { + when { + branch 'main' + } steps { - echo 'Checking formatting...' - sh 'tools/check_format.sh' + echo 'Checking formatting on all files...' + sh 'tools/check_format.sh full' + } + } + stage('Check formatting (modified)') { + when { + not { + branch 'main' + } + } + steps { + echo 'Checking formatting on modified files...' + sh 'tools/check_format.sh modified' } } stage('Setup') { diff --git a/format.py b/format.py index 520a2d8078..023ddfd7ab 100755 --- a/format.py +++ b/format.py @@ -9,6 +9,7 @@ import shutil import subprocess import sys import tempfile +from pathlib import Path from functools import partial from typing import List @@ -150,6 +151,15 @@ 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", @@ -175,10 +185,18 @@ def main(): f"Error: neither clang-apply-replacements nor clang-apply-replacements-{CLANG_VER} found (required to use -j)" ) - if args.files: - files = args.files + 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) 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.sh b/tools/check_format.sh index 1195a9c593..a803fcbea8 100755 --- a/tools/check_format.sh +++ b/tools/check_format.sh @@ -1,8 +1,35 @@ #!/bin/bash +if [ $# -ne 1 ] +then + echo "Usage: $0 . Exiting." + exit -1 +fi + STATUSOLD=`git status --porcelain` -./format.py -j -if [ $? -ne 0 ] + +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