From 39c10c7c42bd7f025b1ac01cba85377c95ae53c5 Mon Sep 17 00:00:00 2001 From: Marek Michalowski Date: Wed, 16 Oct 2024 14:09:51 +0100 Subject: [PATCH 1/2] tools: move get_changed_files to utils.py Moved get_changed_files to utils.py as it is now being shared by check_style.py and check_copyright.py Signed-off-by: Marek Michalowski --- tools/check_copyright.py | 42 ++++++---------------------------------- tools/utils.py | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/tools/check_copyright.py b/tools/check_copyright.py index 21715cbb4..326261d84 100755 --- a/tools/check_copyright.py +++ b/tools/check_copyright.py @@ -24,7 +24,12 @@ import subprocess import glob from dataclasses import dataclass, asdict from itertools import islice -from utils import banner, get_filtered_files, get_previous_commit +from utils import ( + banner, + get_filtered_files, + get_previous_commit, + get_changed_files +) # # Directories to exclude @@ -131,41 +136,6 @@ class Analysis: return f'- {count} {name}.\n' -def get_changed_files(commit_range): - """ - Get a list of changed files for the given commit range. - """ - # - # git command using diff-filter to include Added (A), Copied (C), - # Modified (M), Renamed (R), type changed (T), Unmerged (U), Unknown (X) - # files Deleted files (D) are not included - # - diff_cmd = f'git diff --name-status {commit_range} --diff-filter=ACMRTUX' - - try: - result = subprocess.Popen( - diff_cmd, - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - except subprocess.CalledProcessError as e: - print('ERROR ' + e.returncode + ': Failed to get changed files') - return -1 - - result_out, _ = result.communicate() - result_out = result_out.decode('utf-8').strip().split('\n') - result_out = list(map(lambda diff: diff.split('\t'), result_out)) - - # Remove files renamed with 100% similarity. R100 denotes this. - result_out = list(filter(lambda diff: diff[0] != 'R100', result_out)) - - # Get modified file and convert to absolute path - current_directory = os.getcwd() - return list(map( - lambda diff: os.path.abspath(os.path.join(current_directory, - diff[-1])), result_out)) - - def check_copyright(filename, pattern, analysis): with open(filename, encoding='utf-8') as file: # Read just the first HEAD_LINE_COUNT lines of a file diff --git a/tools/utils.py b/tools/utils.py index 9a356e343..445afc404 100644 --- a/tools/utils.py +++ b/tools/utils.py @@ -144,3 +144,38 @@ def get_filtered_files(exclude_dir_patterns, file_types): files))) return filtered_files + + +def get_changed_files(commit_range): + """ + Get a list of changed files for the given commit range. + """ + # + # git command using diff-filter to include Added (A), Copied (C), + # Modified (M), Renamed (R), type changed (T), Unmerged (U), Unknown (X) + # files Deleted files (D) are not included + # + diff_cmd = f'git diff --name-status {commit_range} --diff-filter=ACMRTUX' + + try: + result = subprocess.Popen( + diff_cmd, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + except subprocess.CalledProcessError as e: + print('ERROR ' + e.returncode + ': Failed to get changed files') + return -1 + + result_out, _ = result.communicate() + result_out = result_out.decode('utf-8').strip().split('\n') + result_out = list(map(lambda diff: diff.split('\t'), result_out)) + + # Remove files renamed with 100% similarity. R100 denotes this. + result_out = list(filter(lambda diff: diff[0] != 'R100', result_out)) + + # Get modified file and convert to absolute path + current_directory = os.getcwd() + return list(map( + lambda diff: os.path.abspath(os.path.join(current_directory, + diff[-1])), result_out)) -- GitLab From d80d8de928facbbc4657510486c1b9756c5989c6 Mon Sep 17 00:00:00 2001 From: Marek Michalowski Date: Wed, 16 Oct 2024 14:10:22 +0100 Subject: [PATCH 2/2] tools: check_style.py to ignore renamed-only files Now uses the get_changed_files to skip checking style on files which were renamed only Signed-off-by: Marek Michalowski --- tools/check_style.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tools/check_style.py b/tools/check_style.py index b9e628494..18dbe6223 100755 --- a/tools/check_style.py +++ b/tools/check_style.py @@ -13,7 +13,11 @@ import argparse import subprocess import sys -from utils import banner, get_previous_commit +from utils import ( + banner, + get_previous_commit, + get_changed_files +) # # Default output file @@ -24,9 +28,17 @@ DEFAULT_OUTPUT_FILE = 'code-style.patch' def run(output_file=DEFAULT_OUTPUT_FILE, commit_hash=get_previous_commit()): print(banner(f'Run coding style checks against {commit_hash[:8]}')) + files = get_changed_files(commit_hash) + if files == -1: + return False + elif not files: + print('No added/modified files were found.') + print('') + return True + # Run git clang-format with the previous commit hash and capture the patch result = subprocess.run( - ['git', 'clang-format', '--quiet', '--diff', commit_hash], + ['git', 'clang-format', '--quiet', '--diff', commit_hash] + files, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True -- GitLab