diff --git a/gclient.py b/gclient.py index 4042ac831..19f8d6a6f 100755 --- a/gclient.py +++ b/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': diff --git a/git_cl.py b/git_cl.py index 901c12e3a..e057e0456 100755 --- a/git_cl.py +++ b/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'): diff --git a/git_common.py b/git_common.py index 47e9583c5..6d6ac6c6f 100644 --- a/git_common.py +++ b/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. diff --git a/tests/gclient_git_smoketest.py b/tests/gclient_git_smoketest.py index 47ad0f16b..db0d738ba 100755 --- a/tests/gclient_git_smoketest.py +++ b/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: diff --git a/tests/git_common_test.py b/tests/git_common_test.py index f95df5a01..7609d9be6 100755 --- a/tests/git_common_test.py +++ b/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'