From 5561f8be123926d9af3ab8de901c158a8ae2aca8 Mon Sep 17 00:00:00 2001 From: Josip Sokcevic Date: Mon, 21 Aug 2023 16:00:42 +0000 Subject: [PATCH] [gclient] Fix setdep if relative path is not used R=aravindvasudev@google.com, jojwang@google.com Change-Id: Ic4f7046ac4e1a22a82db97466c43afee99c393de Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4795232 Reviewed-by: Aravind Vasudevan Auto-Submit: Josip Sokcevic Commit-Queue: Josip Sokcevic --- gclient.py | 33 +++++++++++++---- tests/gclient_git_smoketest.py | 66 ++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 6 deletions(-) diff --git a/gclient.py b/gclient.py index f6294ff7f0..e7e16ea758 100755 --- a/gclient.py +++ b/gclient.py @@ -115,7 +115,6 @@ from third_party.repo.progress import Progress import subcommand import subprocess2 import setup_color -import git_cl from third_party import six @@ -2722,11 +2721,14 @@ def CMDgitmodules(parser, args): if prefix_length: path = path[prefix_length:] - git_cl.RunGit( - ['update-index', '--add', '--cacheinfo', '160000', commit, path]) + subprocess2.call([ + 'git', 'update-index', '--add', '--cacheinfo', + f'160000,{commit},{path}' + ]) f.write(f'[submodule "{path}"]\n\tpath = {path}\n\turl = {url}\n') if 'condition' in dep: f.write(f'\tgclient-condition = {dep["condition"]}\n') + subprocess2.call(['git', 'add', '.gitmodules']) print('.gitmodules and gitlinks updated. Please check git diff and ' 'commit changes.') @@ -3536,22 +3538,41 @@ def CMDsetdep(parser, args): # Update git submodules when `git_dependencies` == SYNC or SUBMODULES. if git_modules and 'git_dependencies' in local_scope and local_scope[ 'git_dependencies'] in (gclient_eval.SUBMODULES, gclient_eval.SYNC): + git_module_name = name + if not 'use_relative_paths' in local_scope or \ + local_scope['use_relative_paths'] != True: + deps_dir = os.path.dirname(os.path.abspath(options.deps_file)) + gclient_path = gclient_paths.FindGclientRoot(deps_dir) + delta_path = None + if gclient_path: + delta_path = os.path.relpath(deps_dir, + os.path.abspath(gclient_path)) + if delta_path: + prefix_length = len(delta_path.replace(os.path.sep, '/')) + 1 + git_module_name = name[prefix_length:] # gclient setdep should update the revision, i.e., the gitlink only # when the submodule entry is already present within .gitmodules. - if name not in git_modules: + if git_module_name not in git_modules: raise KeyError( - 'Could not find any dependency called %s in .gitmodules.' % name) + f'Could not find any dependency called "{git_module_name}" in ' + f'.gitmodules.') # Update the gitlink for the submodule. subprocess2.call([ 'git', 'update-index', '--add', '--cacheinfo', - f'160000,{value},{name}' + f'160000,{value},{git_module_name}' ], cwd=cwd) with open(options.deps_file, 'wb') as f: f.write(gclient_eval.RenderDEPSFile(local_scope).encode('utf-8')) + if git_modules: + subprocess2.call(['git', 'add', options.deps_file], cwd=cwd) + print('Changes have been staged. See changes with `git status`.\n' + 'Use `git commit -m "Manual roll"` to commit your changes. \n' + 'Run gclient sync to update your local dependency checkout.') + @metrics.collector.collect_metrics('gclient verify') def CMDverify(parser, args): diff --git a/tests/gclient_git_smoketest.py b/tests/gclient_git_smoketest.py index c4415da2e8..1094878926 100755 --- a/tests/gclient_git_smoketest.py +++ b/tests/gclient_git_smoketest.py @@ -626,12 +626,77 @@ class GClientSmokeGIT(gclient_smoketest_base.GClientSmokeBase): ], contents) def testSetDep_Submodules(self): + self.gclient(['config', self.git_base + 'repo_1', '--name', 'src']) + with open(os.path.join(self.git_base, '.gclient'), 'w') as f: + f.write('') + + fake_deps = os.path.join(self.git_base, 'repo_1', 'DEPS') + gitmodules = os.path.join(self.git_base, 'repo_1', '.gitmodules') + with open(fake_deps, 'w') as f: + f.write('\n'.join([ + 'git_dependencies = "SYNC"', + 'vars = { ', + ' "foo_var": "foo_val",', + ' "foo_rev": "foo_rev",', + '}', + 'deps = { ', + ' "repo_1/foo": "https://foo" + Var("foo_rev"),', + ' "repo_1/bar": "https://bar@barrev",', + '}', + ])) + + with open(gitmodules, 'w') as f: + f.write('\n'.join([ + '[submodule "foo"]', ' url = https://foo', ' path = foo', + '[submodule "bar"]', ' url = https://bar', ' path = bar' + ])) + + subprocess2.call([ + 'git', 'update-index', '--add', '--cacheinfo', + '160000,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,foo' + ], + cwd=self.git_base + 'repo_1') + subprocess2.call([ + 'git', 'update-index', '--add', '--cacheinfo', + '160000,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,bar' + ], + cwd=self.git_base + 'repo_1') + + self.gclient([ + 'setdep', + '-r', + 'repo_1/foo@new_foo', + '--var', + 'foo_var=new_val', + '-r', + 'repo_1/bar@bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', + ], + cwd=self.git_base + 'repo_1') + + with open(fake_deps) as f: + contents = f.read().splitlines() + + self.assertEqual([ + 'git_dependencies = "SYNC"', + 'vars = { ', + ' "foo_var": "new_val",', + ' "foo_rev": "new_foo",', + '}', + 'deps = { ', + ' "repo_1/foo": "https://foo" + Var("foo_rev"),', + ' "repo_1/bar": ' + '"https://bar@bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",', + '}', + ], contents) + + def testSetDep_Submodules_relative(self): self.gclient(['config', self.git_base + 'repo_1', '--name', 'src']) fake_deps = os.path.join(self.git_base, 'repo_1', 'DEPS') gitmodules = os.path.join(self.git_base, 'repo_1', '.gitmodules') with open(fake_deps, 'w') as f: f.write('\n'.join([ 'git_dependencies = "SUBMODULES"', + 'use_relative_paths = True', 'vars = { ', ' "foo_var": "foo_val",', ' "foo_rev": "foo_rev",', @@ -659,6 +724,7 @@ class GClientSmokeGIT(gclient_smoketest_base.GClientSmokeBase): self.assertEqual([ 'git_dependencies = "SUBMODULES"', + 'use_relative_paths = True', 'vars = { ', ' "foo_var": "new_val",', ' "foo_rev": "foo_rev",',