Selaa lähdekoodia

Check git version

On `git cl upload` and when running a command on all deps
using `gclient` in a non-Cog env, prints/logs a warning if git
should be updated. Non-blocking; command should still be
attempted as usual.

Bug: b/360206460
Change-Id: I8ebef4cb8b520124bc34ade7f0f2179874b470a4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5874690
Reviewed-by: Yiwei Zhang <yiwzhang@google.com>
Commit-Queue: Anne Redulla <aredulla@google.com>
Anne Redulla 11 kuukautta sitten
vanhempi
commit
1291794f5e
5 muutettua tiedostoa jossa 43 lisäystä ja 4 poistoa
  1. 10 0
      gclient.py
  2. 7 0
      git_cl.py
  3. 6 0
      git_common.py
  4. 1 0
      tests/gclient_git_smoketest.py
  5. 19 4
      tests/git_common_test.py

+ 10 - 0
gclient.py

@@ -2453,6 +2453,16 @@ it or fix the checkout.
                 % ('\n'.join(patch_repo + '@' + patch_ref
                              for patch_repo, patch_ref in patch_refs.items())))
 
+        # Check whether git should be updated.
+        recommendation = git_common.check_git_version()
+        if (recommendation and
+                os.environ.get('GCLIENT_SUPPRESS_GIT_VERSION_WARNING') != '1'):
+            message = (f'{recommendation}\n'
+                       'Disable this warning by setting the '
+                       'GCLIENT_SUPPRESS_GIT_VERSION_WARNING\n'
+                       'environment variable to 1.')
+            gclient_utils.AddWarning(message)
+
         # TODO(crbug.com/1475405): Warn users if the project uses submodules and
         # they have fsmonitor enabled.
         if command == 'update':

+ 7 - 0
git_cl.py

@@ -5293,6 +5293,13 @@ def CMDupload(parser, args):
     # authentication is not working properly.
     gerrit_util.GetAccountDetails(cl.GetGerritHost())
 
+    # Check whether git should be updated.
+    recommendation = git_common.check_git_version()
+    if recommendation:
+        print(colorama.Fore.RED)
+        print(f'WARNING: {recommendation}')
+        print(colorama.Style.RESET_ALL)
+
     # TODO(crbug.com/1475405): Warn users if the project uses submodules and
     # they have fsmonitor enabled.
     if os.path.isfile('.gitmodules'):

+ 6 - 0
git_common.py

@@ -48,6 +48,7 @@ from typing import ContextManager
 from typing import Optional
 from typing import Tuple
 
+import gclient_utils
 import scm
 import subprocess2
 
@@ -1200,6 +1201,7 @@ def upstream(branch):
         return None
 
 
+@functools.lru_cache
 def check_git_version(
         min_version: Tuple[int] = GIT_MIN_VERSION) -> Optional[str]:
     """Checks whether git is installed, and its version meets the recommended
@@ -1208,6 +1210,10 @@ def check_git_version(
     Returns:
         - the remediation action to take.
     """
+    if gclient_utils.IsEnvCog():
+        # No remediation action required in a non-git environment.
+        return None
+
     min_tag = '.'.join(str(x) for x in min_version)
     if shutil.which(GIT_EXE) is None:
         # git command was not found.

+ 1 - 0
tests/gclient_git_smoketest.py

@@ -30,6 +30,7 @@ class GClientSmokeGIT(gclient_smoketest_base.GClientSmokeBase):
         super(GClientSmokeGIT, self).setUp()
         self.env['PATH'] = (os.path.join(ROOT_DIR, 'testing_support') +
                             os.pathsep + self.env['PATH'])
+        self.env['GCLIENT_SUPPRESS_GIT_VERSION_WARNING'] = '1'
         self.env['GCLIENT_SUPPRESS_SUBMODULE_WARNING'] = '1'
         self.enabled = self.FAKE_REPOS.set_up_git()
         if not self.enabled:

+ 19 - 4
tests/git_common_test.py

@@ -1144,10 +1144,19 @@ class GitTestUtilsTest(git_test_utils.GitRepoReadOnlyTestBase):
 class CheckGitVersionTest(GitCommonTestBase):
 
     def setUp(self):
+        self.addCleanup(self.gc.check_git_version.cache_clear)
         self.addCleanup(self.gc.get_git_version.cache_clear)
 
+    @mock.patch('gclient_utils.IsEnvCog')
+    def testNonGitEnv(self, mockCog):
+        mockCog.return_value = True
+
+        self.assertIsNone(self.gc.check_git_version())
+
+    @mock.patch('gclient_utils.IsEnvCog')
     @mock.patch('shutil.which')
-    def testGitNotInstalled(self, mockWhich):
+    def testGitNotInstalled(self, mockWhich, mockCog):
+        mockCog.return_value = False
         mockWhich.return_value = None
 
         recommendation = self.gc.check_git_version()
@@ -1156,9 +1165,11 @@ class CheckGitVersionTest(GitCommonTestBase):
 
         mockWhich.assert_called_once()
 
+    @mock.patch('gclient_utils.IsEnvCog')
     @mock.patch('shutil.which')
     @mock.patch('git_common.run')
-    def testGitOldVersion(self, mockRun, mockWhich):
+    def testGitOldVersion(self, mockRun, mockWhich, mockCog):
+        mockCog.return_value = False
         mockWhich.return_value = '/example/bin/git'
         mockRun.return_value = 'git version 2.2.40-abc'
 
@@ -1169,9 +1180,11 @@ class CheckGitVersionTest(GitCommonTestBase):
         mockWhich.assert_called_once()
         mockRun.assert_called_once()
 
+    @mock.patch('gclient_utils.IsEnvCog')
     @mock.patch('shutil.which')
     @mock.patch('git_common.run')
-    def testGitSufficientVersion(self, mockRun, mockWhich):
+    def testGitSufficientVersion(self, mockRun, mockWhich, mockCog):
+        mockCog.return_value = False
         mockWhich.return_value = '/example/bin/git'
         mockRun.return_value = 'git version 2.30.1.456'
 
@@ -1180,9 +1193,11 @@ class CheckGitVersionTest(GitCommonTestBase):
         mockWhich.assert_called_once()
         mockRun.assert_called_once()
 
+    @mock.patch('gclient_utils.IsEnvCog')
     @mock.patch('shutil.which')
     @mock.patch('git_common.run')
-    def testHandlesErrorGettingVersion(self, mockRun, mockWhich):
+    def testHandlesErrorGettingVersion(self, mockRun, mockWhich, mockCog):
+        mockCog.return_value = False
         mockWhich.return_value = '/example/bin/git'
         mockRun.return_value = 'Error running git version'