Browse Source

[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 <ddoman@chromium.org>
Commit-Queue: Josip Sokcevic <sokcevic@chromium.org>
Josip Sokcevic 6 tháng trước cách đây
mục cha
commit
9fd46c2b6b
1 tập tin đã thay đổi với 26 bổ sung3 xóa
  1. 26 3
      presubmit_canned_checks.py

+ 26 - 3
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_file = input_api.os_path.join(input_api.PresubmitLocalPath(),
                                              '.gitmodules')
                                              '.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 = []
     mismatch_entries = []
     deps_msg = ""
     deps_msg = ""
@@ -2094,7 +2103,9 @@ def CheckForCommitObjects(input_api, output_api):
                         'Make sure DEPS paths match those in .gitmodules \n'
                         'Make sure DEPS paths match those in .gitmodules \n'
                         f'and a gitlink exists at {dep_path}.')
                         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 [
                 return [
                     output_api.PresubmitError(
                     output_api.PresubmitError(
                         f'No submodule with path {submodule_path} in '
                         f'No submodule with path {submodule_path} in '
@@ -2123,6 +2134,18 @@ def CheckForCommitObjects(input_api, output_api):
                 'The following entries diverged: ' + deps_msg)
                 '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 []
     return []