2021-01-15 03:17:50 +00:00
|
|
|
# Copyright (C) 2021 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2021-02-09 08:26:31 +00:00
|
|
|
"""Provide functionality to get all projects and their commit ids from Superproject.
|
2021-01-15 03:17:50 +00:00
|
|
|
|
|
|
|
For more information on superproject, check out:
|
|
|
|
https://en.wikibooks.org/wiki/Git/Submodules_and_Superprojects
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
superproject = Superproject()
|
2021-02-09 08:26:31 +00:00
|
|
|
project_commit_ids = superproject.UpdateProjectsRevisionId(projects)
|
2021-01-15 03:17:50 +00:00
|
|
|
"""
|
|
|
|
|
2021-02-23 00:54:56 +00:00
|
|
|
import hashlib
|
2021-01-15 03:17:50 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2021-06-02 17:05:54 +00:00
|
|
|
from git_command import git_require, GitCommand
|
2021-02-09 08:26:31 +00:00
|
|
|
from git_refs import R_HEADS
|
2021-01-15 03:17:50 +00:00
|
|
|
|
2021-02-08 00:30:27 +00:00
|
|
|
_SUPERPROJECT_GIT_NAME = 'superproject.git'
|
|
|
|
_SUPERPROJECT_MANIFEST_NAME = 'superproject_override.xml'
|
|
|
|
|
2021-01-15 03:17:50 +00:00
|
|
|
|
|
|
|
class Superproject(object):
|
2021-02-09 08:26:31 +00:00
|
|
|
"""Get commit ids from superproject.
|
2021-01-15 03:17:50 +00:00
|
|
|
|
2021-02-23 00:54:56 +00:00
|
|
|
Initializes a local copy of a superproject for the manifest. This allows
|
|
|
|
lookup of commit ids for all projects. It contains _project_commit_ids which
|
|
|
|
is a dictionary with project/commit id entries.
|
2021-01-15 03:17:50 +00:00
|
|
|
"""
|
2021-03-04 18:29:40 +00:00
|
|
|
def __init__(self, manifest, repodir, superproject_dir='exp-superproject',
|
|
|
|
quiet=False):
|
2021-01-15 03:17:50 +00:00
|
|
|
"""Initializes superproject.
|
|
|
|
|
|
|
|
Args:
|
2021-02-09 08:26:31 +00:00
|
|
|
manifest: A Manifest object that is to be written to a file.
|
2021-01-15 03:17:50 +00:00
|
|
|
repodir: Path to the .repo/ dir for holding all internal checkout state.
|
2021-02-09 08:26:31 +00:00
|
|
|
It must be in the top directory of the repo client checkout.
|
2021-01-15 03:17:50 +00:00
|
|
|
superproject_dir: Relative path under |repodir| to checkout superproject.
|
2021-03-04 18:29:40 +00:00
|
|
|
quiet: If True then only print the progress messages.
|
2021-01-15 03:17:50 +00:00
|
|
|
"""
|
2021-02-09 08:26:31 +00:00
|
|
|
self._project_commit_ids = None
|
|
|
|
self._manifest = manifest
|
2021-03-04 18:29:40 +00:00
|
|
|
self._quiet = quiet
|
2021-02-09 08:26:31 +00:00
|
|
|
self._branch = self._GetBranch()
|
2021-01-15 03:17:50 +00:00
|
|
|
self._repodir = os.path.abspath(repodir)
|
|
|
|
self._superproject_dir = superproject_dir
|
|
|
|
self._superproject_path = os.path.join(self._repodir, superproject_dir)
|
2021-02-04 22:39:38 +00:00
|
|
|
self._manifest_path = os.path.join(self._superproject_path,
|
2021-02-08 00:30:27 +00:00
|
|
|
_SUPERPROJECT_MANIFEST_NAME)
|
2021-02-23 00:54:56 +00:00
|
|
|
git_name = ''
|
|
|
|
if self._manifest.superproject:
|
|
|
|
remote_name = self._manifest.superproject['remote'].name
|
|
|
|
git_name = hashlib.md5(remote_name.encode('utf8')).hexdigest() + '-'
|
|
|
|
self._work_git_name = git_name + _SUPERPROJECT_GIT_NAME
|
|
|
|
self._work_git = os.path.join(self._superproject_path, self._work_git_name)
|
2021-01-15 03:17:50 +00:00
|
|
|
|
|
|
|
@property
|
2021-02-09 08:26:31 +00:00
|
|
|
def project_commit_ids(self):
|
|
|
|
"""Returns a dictionary of projects and their commit ids."""
|
|
|
|
return self._project_commit_ids
|
|
|
|
|
|
|
|
def _GetBranch(self):
|
|
|
|
"""Returns the branch name for getting the approved manifest."""
|
|
|
|
p = self._manifest.manifestProject
|
|
|
|
b = p.GetBranch(p.CurrentBranch)
|
|
|
|
if not b:
|
|
|
|
return None
|
|
|
|
branch = b.merge
|
|
|
|
if branch and branch.startswith(R_HEADS):
|
|
|
|
branch = branch[len(R_HEADS):]
|
|
|
|
return branch
|
2021-01-15 03:17:50 +00:00
|
|
|
|
2021-02-23 00:54:56 +00:00
|
|
|
def _Init(self):
|
|
|
|
"""Sets up a local Git repository to get a copy of a superproject.
|
2021-01-15 03:17:50 +00:00
|
|
|
|
|
|
|
Returns:
|
2021-02-23 00:54:56 +00:00
|
|
|
True if initialization is successful, or False.
|
2021-01-15 03:17:50 +00:00
|
|
|
"""
|
2021-02-08 00:30:27 +00:00
|
|
|
if not os.path.exists(self._superproject_path):
|
|
|
|
os.mkdir(self._superproject_path)
|
2021-03-04 18:29:40 +00:00
|
|
|
if not self._quiet and not os.path.exists(self._work_git):
|
|
|
|
print('%s: Performing initial setup for superproject; this might take '
|
|
|
|
'several minutes.' % self._work_git)
|
2021-02-23 00:54:56 +00:00
|
|
|
cmd = ['init', '--bare', self._work_git_name]
|
2021-01-15 03:17:50 +00:00
|
|
|
p = GitCommand(None,
|
|
|
|
cmd,
|
|
|
|
cwd=self._superproject_path,
|
|
|
|
capture_stdout=True,
|
|
|
|
capture_stderr=True)
|
|
|
|
retval = p.Wait()
|
|
|
|
if retval:
|
2021-02-23 00:54:56 +00:00
|
|
|
print('repo: error: git init call failed with return code: %r, stderr: %r' %
|
2021-01-15 03:17:50 +00:00
|
|
|
(retval, p.stderr), file=sys.stderr)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2021-02-23 00:54:56 +00:00
|
|
|
def _Fetch(self, url):
|
|
|
|
"""Fetches a local copy of a superproject for the manifest based on url.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
url: superproject's url.
|
2021-02-01 19:47:06 +00:00
|
|
|
|
|
|
|
Returns:
|
2021-02-23 00:54:56 +00:00
|
|
|
True if fetch is successful, or False.
|
2021-02-01 19:47:06 +00:00
|
|
|
"""
|
2021-02-04 22:39:38 +00:00
|
|
|
if not os.path.exists(self._work_git):
|
2021-02-08 00:30:27 +00:00
|
|
|
print('git fetch missing drectory: %s' % self._work_git,
|
|
|
|
file=sys.stderr)
|
|
|
|
return False
|
2021-06-02 17:05:54 +00:00
|
|
|
if not git_require((2, 28, 0)):
|
|
|
|
print('superproject requires a git version 2.28 or later', file=sys.stderr)
|
|
|
|
return False
|
2021-03-19 20:53:43 +00:00
|
|
|
cmd = ['fetch', url, '--depth', '1', '--force', '--no-tags', '--filter', 'blob:none']
|
2021-02-23 00:54:56 +00:00
|
|
|
if self._branch:
|
|
|
|
cmd += [self._branch + ':' + self._branch]
|
2021-02-01 19:47:06 +00:00
|
|
|
p = GitCommand(None,
|
|
|
|
cmd,
|
2021-02-04 22:39:38 +00:00
|
|
|
cwd=self._work_git,
|
2021-02-01 19:47:06 +00:00
|
|
|
capture_stdout=True,
|
|
|
|
capture_stderr=True)
|
|
|
|
retval = p.Wait()
|
|
|
|
if retval:
|
2021-02-08 00:30:27 +00:00
|
|
|
print('repo: error: git fetch call failed with return code: %r, stderr: %r' %
|
2021-02-01 19:47:06 +00:00
|
|
|
(retval, p.stderr), file=sys.stderr)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2021-02-09 08:26:31 +00:00
|
|
|
def _LsTree(self):
|
2021-02-23 00:54:56 +00:00
|
|
|
"""Gets the commit ids for all projects.
|
2021-01-15 03:17:50 +00:00
|
|
|
|
|
|
|
Works only in git repositories.
|
|
|
|
|
|
|
|
Returns:
|
2021-02-09 08:26:31 +00:00
|
|
|
data: data returned from 'git ls-tree ...' instead of None.
|
2021-01-15 03:17:50 +00:00
|
|
|
"""
|
2021-02-04 22:39:38 +00:00
|
|
|
if not os.path.exists(self._work_git):
|
2021-02-08 00:30:27 +00:00
|
|
|
print('git ls-tree missing drectory: %s' % self._work_git,
|
|
|
|
file=sys.stderr)
|
|
|
|
return None
|
2021-01-15 03:17:50 +00:00
|
|
|
data = None
|
2021-02-09 08:26:31 +00:00
|
|
|
branch = 'HEAD' if not self._branch else self._branch
|
2021-02-08 21:27:41 +00:00
|
|
|
cmd = ['ls-tree', '-z', '-r', branch]
|
|
|
|
|
2021-01-15 03:17:50 +00:00
|
|
|
p = GitCommand(None,
|
|
|
|
cmd,
|
2021-02-04 22:39:38 +00:00
|
|
|
cwd=self._work_git,
|
2021-01-15 03:17:50 +00:00
|
|
|
capture_stdout=True,
|
|
|
|
capture_stderr=True)
|
|
|
|
retval = p.Wait()
|
|
|
|
if retval == 0:
|
|
|
|
data = p.stdout
|
|
|
|
else:
|
|
|
|
print('repo: error: git ls-tree call failed with return code: %r, stderr: %r' % (
|
|
|
|
retval, p.stderr), file=sys.stderr)
|
|
|
|
return data
|
|
|
|
|
2021-02-09 08:26:31 +00:00
|
|
|
def Sync(self):
|
2021-02-23 00:54:56 +00:00
|
|
|
"""Gets a local copy of a superproject for the manifest.
|
2021-01-15 03:17:50 +00:00
|
|
|
|
|
|
|
Returns:
|
2021-02-09 08:26:31 +00:00
|
|
|
True if sync of superproject is successful, or False.
|
2021-01-15 03:17:50 +00:00
|
|
|
"""
|
2021-06-03 00:46:25 +00:00
|
|
|
print('NOTICE: --use-superproject is in beta; report any issues to the '
|
|
|
|
'address described in `repo version`', file=sys.stderr)
|
2021-02-09 08:26:31 +00:00
|
|
|
|
|
|
|
if not self._manifest.superproject:
|
|
|
|
print('error: superproject tag is not defined in manifest',
|
|
|
|
file=sys.stderr)
|
|
|
|
return False
|
|
|
|
|
|
|
|
url = self._manifest.superproject['remote'].url
|
2021-01-15 03:17:50 +00:00
|
|
|
if not url:
|
2021-02-09 08:26:31 +00:00
|
|
|
print('error: superproject URL is not defined in manifest',
|
|
|
|
file=sys.stderr)
|
|
|
|
return False
|
2021-02-08 00:30:27 +00:00
|
|
|
|
2021-02-23 00:54:56 +00:00
|
|
|
if not self._Init():
|
|
|
|
return False
|
|
|
|
if not self._Fetch(url):
|
|
|
|
return False
|
2021-03-04 18:29:40 +00:00
|
|
|
if not self._quiet:
|
|
|
|
print('%s: Initial setup for superproject completed.' % self._work_git)
|
2021-02-09 08:26:31 +00:00
|
|
|
return True
|
2021-01-15 03:17:50 +00:00
|
|
|
|
2021-02-09 08:26:31 +00:00
|
|
|
def _GetAllProjectsCommitIds(self):
|
|
|
|
"""Get commit ids for all projects from superproject and save them in _project_commit_ids.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A dictionary with the projects/commit ids on success, otherwise None.
|
|
|
|
"""
|
|
|
|
if not self.Sync():
|
|
|
|
return None
|
|
|
|
|
|
|
|
data = self._LsTree()
|
2021-01-15 03:17:50 +00:00
|
|
|
if not data:
|
2021-02-23 00:54:56 +00:00
|
|
|
print('error: git ls-tree failed to return data for superproject',
|
|
|
|
file=sys.stderr)
|
2021-02-09 08:26:31 +00:00
|
|
|
return None
|
2021-01-15 03:17:50 +00:00
|
|
|
|
|
|
|
# Parse lines like the following to select lines starting with '160000' and
|
2021-02-09 08:26:31 +00:00
|
|
|
# build a dictionary with project path (last element) and its commit id (3rd element).
|
2021-01-15 03:17:50 +00:00
|
|
|
#
|
|
|
|
# 160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00
|
|
|
|
# 120000 blob acc2cbdf438f9d2141f0ae424cec1d8fc4b5d97f\tbootstrap.bash\x00
|
2021-02-09 08:26:31 +00:00
|
|
|
commit_ids = {}
|
2021-01-15 03:17:50 +00:00
|
|
|
for line in data.split('\x00'):
|
|
|
|
ls_data = line.split(None, 3)
|
|
|
|
if not ls_data:
|
|
|
|
break
|
|
|
|
if ls_data[0] == '160000':
|
2021-02-09 08:26:31 +00:00
|
|
|
commit_ids[ls_data[3]] = ls_data[2]
|
2021-01-15 03:17:50 +00:00
|
|
|
|
2021-02-09 08:26:31 +00:00
|
|
|
self._project_commit_ids = commit_ids
|
|
|
|
return commit_ids
|
2021-02-04 22:39:38 +00:00
|
|
|
|
2021-02-09 08:26:31 +00:00
|
|
|
def _WriteManfiestFile(self):
|
2021-02-04 22:39:38 +00:00
|
|
|
"""Writes manifest to a file.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
manifest_path: Path name of the file into which manifest is written instead of None.
|
|
|
|
"""
|
|
|
|
if not os.path.exists(self._superproject_path):
|
|
|
|
print('error: missing superproject directory %s' %
|
|
|
|
self._superproject_path,
|
|
|
|
file=sys.stderr)
|
|
|
|
return None
|
2021-03-09 23:19:06 +00:00
|
|
|
manifest_str = self._manifest.ToXml(groups=self._manifest.GetGroupsStr()).toxml()
|
2021-02-04 22:39:38 +00:00
|
|
|
manifest_path = self._manifest_path
|
|
|
|
try:
|
|
|
|
with open(manifest_path, 'w', encoding='utf-8') as fp:
|
|
|
|
fp.write(manifest_str)
|
|
|
|
except IOError as e:
|
|
|
|
print('error: cannot write manifest to %s:\n%s'
|
|
|
|
% (manifest_path, e),
|
|
|
|
file=sys.stderr)
|
|
|
|
return None
|
|
|
|
return manifest_path
|
|
|
|
|
2021-02-09 08:26:31 +00:00
|
|
|
def UpdateProjectsRevisionId(self, projects):
|
|
|
|
"""Update revisionId of every project in projects with the commit id.
|
2021-02-04 22:39:38 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
projects: List of projects whose revisionId needs to be updated.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
manifest_path: Path name of the overriding manfiest file instead of None.
|
|
|
|
"""
|
2021-02-09 08:26:31 +00:00
|
|
|
commit_ids = self._GetAllProjectsCommitIds()
|
|
|
|
if not commit_ids:
|
|
|
|
print('error: Cannot get project commit ids from manifest', file=sys.stderr)
|
2021-02-04 22:39:38 +00:00
|
|
|
return None
|
|
|
|
|
2021-02-09 08:26:31 +00:00
|
|
|
projects_missing_commit_ids = []
|
2021-02-04 22:39:38 +00:00
|
|
|
for project in projects:
|
|
|
|
path = project.relpath
|
|
|
|
if not path:
|
|
|
|
continue
|
2021-02-09 08:26:31 +00:00
|
|
|
commit_id = commit_ids.get(path)
|
|
|
|
if commit_id:
|
|
|
|
project.SetRevisionId(commit_id)
|
2021-02-04 22:39:38 +00:00
|
|
|
else:
|
2021-02-09 08:26:31 +00:00
|
|
|
projects_missing_commit_ids.append(path)
|
|
|
|
if projects_missing_commit_ids:
|
|
|
|
print('error: please file a bug using %s to report missing commit_ids for: %s' %
|
2021-05-12 19:00:31 +00:00
|
|
|
(self._manifest.contactinfo.bugurl, projects_missing_commit_ids), file=sys.stderr)
|
2021-02-04 22:39:38 +00:00
|
|
|
return None
|
|
|
|
|
2021-02-09 08:26:31 +00:00
|
|
|
manifest_path = self._WriteManfiestFile()
|
2021-02-04 22:39:38 +00:00
|
|
|
return manifest_path
|