Make gclient getdep work with submodules.

Bug: 1475770
Change-Id: I341910e75195d87b91defd98f2c6ba25262995b1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4828423
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
Commit-Queue: Joanna Wang <jojwang@chromium.org>
changes/23/4828423/5
Joanna Wang 2 years ago committed by LUCI CQ
parent 6308837868
commit e36c6bba14

@ -105,6 +105,7 @@ import gclient_utils
import git_cache import git_cache
import metrics import metrics
import metrics_utils import metrics_utils
import scm as scm_git
import setup_color import setup_color
import subcommand import subcommand
import subprocess2 import subprocess2
@ -945,17 +946,7 @@ class Dependency(gclient_utils.WorkItem, DependencySettings):
gitmodules[submodule][sub_key] = value gitmodules[submodule][sub_key] = value
paths = [module['path'] for module in gitmodules.values()] paths = [module['path'] for module in gitmodules.values()]
# Get submodule commit hashes. We use `gitt ls-files -s` to ensure commit_hashes = scm_git.GIT.GetSubmoduleCommits(cwd, paths)
# we capture gitlink changes from staged applied patches.
# Output Format: `<mode> SP <type> SP <object> TAB <file>`.
result = subprocess2.check_output(['git', 'ls-files', '-s'] + paths,
cwd=cwd).decode('utf-8')
commit_hashes = {}
for r in result.splitlines():
# ['<mode>', '<commit_hash>', '<stage_number>', '<path>'].
record = r.strip().split(maxsplit=3) # path can contain spaces.
assert record[0] == '160000', 'file is not a gitlink: %s' % record
commit_hashes[record[3]] = record[1]
# Structure git submodules into a dict of DEPS git url entries. # Structure git submodules into a dict of DEPS git url entries.
submodules = {} submodules = {}
@ -3428,6 +3419,14 @@ def CMDgetdep(parser, args):
for var in options.vars: for var in options.vars:
print(gclient_eval.GetVar(local_scope, var)) print(gclient_eval.GetVar(local_scope, var))
commits = {}
if local_scope.get('git_dependencies'
) == gclient_eval.SUBMODULES and options.getdep_revisions:
commits.update(
scm_git.GIT.GetSubmoduleCommits(
os.getcwd(),
[path for path in options.getdep_revisions if ':' not in path]))
for name in options.getdep_revisions: for name in options.getdep_revisions:
if ':' in name: if ':' in name:
name, _, package = name.partition(':') name, _, package = name.partition(':')
@ -3436,6 +3435,8 @@ def CMDgetdep(parser, args):
'Wrong CIPD format: %s:%s should be of the form path:pkg.' 'Wrong CIPD format: %s:%s should be of the form path:pkg.'
% (name, package)) % (name, package))
print(gclient_eval.GetCIPD(local_scope, name, package)) print(gclient_eval.GetCIPD(local_scope, name, package))
elif commits:
print(commits[name])
else: else:
try: try:
print(gclient_eval.GetRevision(local_scope, name)) print(gclient_eval.GetRevision(local_scope, name))

@ -387,6 +387,23 @@ class GIT(object):
# return only files # return only files
return [f.split(maxsplit=3)[-1] for f in files if f.startswith('100')] return [f.split(maxsplit=3)[-1] for f in files if f.startswith('100')]
@staticmethod
def GetSubmoduleCommits(cwd, submodules):
# type: (string, List[string]) => Mapping[string][string]
"""Returns a mapping of staged or committed new commits for submodules."""
if not submodules:
return {}
result = subprocess2.check_output(['git', 'ls-files', '-s', '--'] +
submodules,
cwd=cwd).decode('utf-8')
commit_hashes = {}
for r in result.splitlines():
# ['<mode>', '<commit_hash>', '<stage_number>', '<path>'].
record = r.strip().split(maxsplit=3) # path can contain spaces.
assert record[0] == '160000', 'file is not a gitlink: %s' % record
commit_hashes[record[3]] = record[1]
return commit_hashes
@staticmethod @staticmethod
def GetPatchName(cwd): def GetPatchName(cwd):
"""Constructs a name for this patch.""" """Constructs a name for this patch."""

@ -810,6 +810,22 @@ class GClientSmokeGIT(gclient_smoketest_base.GClientSmokeBase):
'bar_rev', 'bar_rev',
], results[0].splitlines()) ], results[0].splitlines())
def testGetDep_Submodule(self):
self.gclient(['config', self.git_base + 'repo_20', '--name', 'src'])
subprocess2.call([
'git', 'update-index', '--add', '--cacheinfo',
'160000,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,bar'
],
cwd=self.git_base + 'repo_20')
results = self.gclient(
['getdep', '-r', 'foo/bar:lemur', '-r', 'bar', '--var', 'foo_checkout'],
cwd=self.git_base + 'repo_20')
self.assertEqual(
['True', 'version:1234', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'],
results[0].splitlines())
def testGetDep_BuiltinVariables(self): def testGetDep_BuiltinVariables(self):
self.gclient(['config', self.git_base + 'repo_1', '--name', 'src']) self.gclient(['config', self.git_base + 'repo_1', '--name', 'src'])
fake_deps = os.path.join(self.root_dir, 'DEPS.fake') fake_deps = os.path.join(self.root_dir, 'DEPS.fake')

@ -1500,8 +1500,8 @@ class GclientTest(trial_dir.TestCase):
subprocess2_check_output_mock.assert_has_calls([ subprocess2_check_output_mock.assert_has_calls([
mock.call(['git', 'config', '--file', mock.ANY, '-l']), mock.call(['git', 'config', '--file', mock.ANY, '-l']),
mock.call([ mock.call([
'git', 'ls-files', '-s', 'foo/bar', 'aaaaaa', 'a.a.a/a', 'a_b/c', 'git', 'ls-files', '-s', '--', 'foo/bar', 'aaaaaa', 'a.a.a/a',
'a b/c' 'a_b/c', 'a b/c'
], ],
cwd=mock.ANY) cwd=mock.ANY)
]) ])
@ -1584,8 +1584,8 @@ class GclientTest(trial_dir.TestCase):
subprocess2_check_output_mock.assert_has_calls([ subprocess2_check_output_mock.assert_has_calls([
mock.call(['git', 'config', '--file', mock.ANY, '-l']), mock.call(['git', 'config', '--file', mock.ANY, '-l']),
mock.call([ mock.call([
'git', 'ls-files', '-s', 'foo/bar', 'aaaaaa', 'a.a.a/a', 'a_b/c', 'git', 'ls-files', '-s', '--', 'foo/bar', 'aaaaaa', 'a.a.a/a',
'a b/c' 'a_b/c', 'a b/c'
], ],
cwd=mock.ANY) cwd=mock.ANY)
]) ])

Loading…
Cancel
Save