From a510fedb9ab6ec279773f10ff94d214a2fc8998b Mon Sep 17 00:00:00 2001 From: Roberto Alanis Date: Wed, 15 Dec 2021 07:18:13 +0000 Subject: [PATCH] patch-check: detect duplicated files Change-Id: Ia78425e7f8054d4b0b4e6b365ba0f5887cbcf2d1 Bug: b:185520507 --- PRESUBMIT.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/PRESUBMIT.py b/PRESUBMIT.py index 994473bc..91ad12e1 100644 --- a/PRESUBMIT.py +++ b/PRESUBMIT.py @@ -41,6 +41,7 @@ _BASH_INDENTATION = "2" _GIT_COMMIT_SUBJECT_LENGTH = 65 _INCLUDE_BASH_FILES_ONLY = [r".*\.sh$"] _INCLUDE_MAN_FILES_ONLY = [r"man/.+\.1$"] +_INCLUDE_SOURCE_FILES_ONLY = [r".*\.[ch]$"] _LIBWEBP_MAX_LINE_LENGTH = 80 @@ -72,6 +73,35 @@ def _CheckCommitSubjectLength(input_api, output_api): return output_api.PresubmitResult("%s\n (%4.2fs) success" % (name, duration)) +def _CheckDuplicateFiles(input_api, output_api): + """Ensures there are not repeated filenames.""" + all_files = [] + for f in input_api.change.AllFiles(): + for include_file in _INCLUDE_SOURCE_FILES_ONLY: + if re.match(include_file, f): + all_files.append(f) + break + + basename_to_path = {} + for f in all_files: + basename_file = input_api.basename(f) + if basename_file in basename_to_path: + basename_to_path[basename_file].append(f) + else: + basename_to_path[basename_file] = [f] + + dupes = [] + for files in basename_to_path.values(): + if len(files) > 1: + dupes.extend(files) + + if dupes: + return output_api.PresubmitError( + "Duplicate source files, rebase or rename some to make them unique:\n%s" + % dupes) + return output_api.PresubmitResult("No duplicates, success\n") + + def _GetFilesToSkip(input_api): return list(input_api.DEFAULT_FILES_TO_SKIP) + [ r"swig/.*\.py$", @@ -154,6 +184,7 @@ def _CommonChecks(input_api, output_api): input_api.canned_checks.CheckChangeHasNoStrayWhitespace( input_api, output_api)) results.append(_CheckCommitSubjectLength(input_api, output_api)) + results.append(_CheckDuplicateFiles(input_api, output_api)) source_file_filter = lambda x: input_api.FilterSourceFile( x, files_to_skip=_GetFilesToSkip(input_api))