From 9fd46c2b6b86e1f13ba0d94e113bb636b374d8e6 Mon Sep 17 00:00:00 2001 From: Josip Sokcevic Date: Thu, 30 Jan 2025 14:38:36 -0800 Subject: [PATCH] [presubmit] Check if .gitmodules diverges from DEPS Raise presubmit error if .gitmodules file contains entry that doesn't have gitlink, nor DEPS entry. This is desired as some tooling started to depend on .gitmodules to learn about git dependencies. Tested manually on chromium/src, based on the bug report test case. R=ddoman@google.com Bug: 392766700 Change-Id: I9dd8e03503b151f75e548acd0af098f342fc77a6 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6219822 Reviewed-by: Scott Lee Commit-Queue: Josip Sokcevic --- presubmit_canned_checks.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/presubmit_canned_checks.py b/presubmit_canned_checks.py index 8c2914955..fbf465ab6 100644 --- a/presubmit_canned_checks.py +++ b/presubmit_canned_checks.py @@ -2061,8 +2061,17 @@ def CheckForCommitObjects(input_api, output_api): gitmodules_file = input_api.os_path.join(input_api.PresubmitLocalPath(), '.gitmodules') - with open(gitmodules_file) as f: - gitmodules_content = f.read() + import configparser + config = configparser.ConfigParser() + config.read(gitmodules_file) + gitmodule_paths = set([]) + for name, section in config.items(): + if not name.startswith('submodule '): + continue + if 'path' not in section: + continue + gitmodule_paths.add(section['path']) + mismatch_entries = [] deps_msg = "" @@ -2094,7 +2103,9 @@ def CheckForCommitObjects(input_api, output_api): 'Make sure DEPS paths match those in .gitmodules \n' f'and a gitlink exists at {dep_path}.') ] - if f'path = {submodule_path}' not in gitmodules_content: + try: + gitmodule_paths.remove(submodule_path) + except KeyError: return [ output_api.PresubmitError( f'No submodule with path {submodule_path} in ' @@ -2123,6 +2134,18 @@ def CheckForCommitObjects(input_api, output_api): 'The following entries diverged: ' + deps_msg) ] + if len(gitmodule_paths) > 0: + return [ + output_api.PresubmitError( + '.gitmodules file contains submodules that no longer exist\n' + 'in DEPS or Git (gitlink).\n' + 'Remove the following entries from .gitmodules:\n' + '\n\t' + '\n\t'.join(gitmodule_paths) + + '\n\nor run the following command:\n' + ' gclient gitmodules\n') + ] + + return []