superproject: support a new revision attribute.

Tested:
$ ./run_tests

Verified that a manifest that specified superproject revision would use
the specified revision, and superproject will use the default revision.

Note that this is a slight behavior change from earlier repo versions,
which would always use the branch name of the manifest itself. However,
the new behavior would be more consisitent with regular "project"
element and would allow superproject be used even if it is not enabled
for the particular manifest branch, so we have decided to make the
change as it would provide more flexibility and better matches what
other elements would do.

Bug: [google internal] b/187868160
Change-Id: I35255ee347aff6e65179f7879d52931f168b477e
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/317643
Tested-by: Xin Li <delphij@google.com>
Reviewed-by: Raman Tenneti <rtenneti@google.com>
Reviewed-by: Mike Frysinger <vapier@google.com>
This commit is contained in:
Xin Li 2021-09-26 23:20:32 -07:00
parent d669d2dee5
commit e0b16a22a0
5 changed files with 90 additions and 15 deletions

View File

@ -105,6 +105,7 @@ following DTD:
<!ELEMENT superproject EMPTY> <!ELEMENT superproject EMPTY>
<!ATTLIST superproject name CDATA #REQUIRED> <!ATTLIST superproject name CDATA #REQUIRED>
<!ATTLIST superproject remote IDREF #IMPLIED> <!ATTLIST superproject remote IDREF #IMPLIED>
<!ATTLIST superproject revision CDATA #IMPLIED>
<!ELEMENT contactinfo EMPTY> <!ELEMENT contactinfo EMPTY>
<!ATTLIST contactinfo bugurl CDATA #REQUIRED> <!ATTLIST contactinfo bugurl CDATA #REQUIRED>
@ -432,6 +433,11 @@ same meaning as project's name attribute. See the
Attribute `remote`: Name of a previously defined remote element. Attribute `remote`: Name of a previously defined remote element.
If not supplied the remote given by the default element is used. If not supplied the remote given by the default element is used.
Attribute `revision`: Name of the Git branch the manifest wants
to track for this superproject. If not supplied the revision given
by the remote element is used if applicable, else the default
element is used.
### Element contactinfo ### Element contactinfo
*** ***

View File

@ -90,7 +90,7 @@ class Superproject(object):
self._git_event_log = git_event_log self._git_event_log = git_event_log
self._quiet = quiet self._quiet = quiet
self._print_messages = print_messages self._print_messages = print_messages
self._branch = self._GetBranch() self._branch = manifest.branch
self._repodir = os.path.abspath(repodir) self._repodir = os.path.abspath(repodir)
self._superproject_dir = superproject_dir self._superproject_dir = superproject_dir
self._superproject_path = os.path.join(self._repodir, superproject_dir) self._superproject_path = os.path.join(self._repodir, superproject_dir)
@ -100,6 +100,7 @@ class Superproject(object):
if self._manifest.superproject: if self._manifest.superproject:
remote = self._manifest.superproject['remote'] remote = self._manifest.superproject['remote']
git_name = hashlib.md5(remote.name.encode('utf8')).hexdigest() + '-' git_name = hashlib.md5(remote.name.encode('utf8')).hexdigest() + '-'
self._branch = self._manifest.superproject['revision']
self._remote_url = remote.url self._remote_url = remote.url
else: else:
self._remote_url = None self._remote_url = None
@ -116,17 +117,6 @@ class Superproject(object):
"""Returns the manifest path if the path exists or None.""" """Returns the manifest path if the path exists or None."""
return self._manifest_path if os.path.exists(self._manifest_path) else None return self._manifest_path if os.path.exists(self._manifest_path) else None
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
def _LogMessage(self, message): def _LogMessage(self, message):
"""Logs message to stderr and _git_event_log.""" """Logs message to stderr and _git_event_log."""
if self._print_messages: if self._print_messages:

View File

@ -507,6 +507,9 @@ https://gerrit.googlesource.com/git-repo/+/HEAD/docs/manifest-format.md
if not d.remote or remote.orig_name != remoteName: if not d.remote or remote.orig_name != remoteName:
remoteName = remote.orig_name remoteName = remote.orig_name
e.setAttribute('remote', remoteName) e.setAttribute('remote', remoteName)
revision = remote.revision or d.revisionExpr
if not revision or revision != self._superproject['revision']:
e.setAttribute('revision', self._superproject['revision'])
root.appendChild(e) root.appendChild(e)
if self._contactinfo.bugurl != Wrapper().BUG_URL: if self._contactinfo.bugurl != Wrapper().BUG_URL:
@ -914,6 +917,13 @@ https://gerrit.googlesource.com/git-repo/+/HEAD/docs/manifest-format.md
raise ManifestParseError("no remote for superproject %s within %s" % raise ManifestParseError("no remote for superproject %s within %s" %
(name, self.manifestFile)) (name, self.manifestFile))
self._superproject['remote'] = remote.ToRemoteSpec(name) self._superproject['remote'] = remote.ToRemoteSpec(name)
revision = node.getAttribute('revision') or remote.revision
if not revision:
revision = self._default.revisionExpr
if not revision:
raise ManifestParseError('no revision for superproject %s within %s' %
(name, self.manifestFile))
self._superproject['revision'] = revision
if node.nodeName == 'contactinfo': if node.nodeName == 'contactinfo':
bugurl = self._reqatt(node, 'bugurl') bugurl = self._reqatt(node, 'bugurl')
# This element can be repeated, later entries will clobber earlier ones. # This element can be repeated, later entries will clobber earlier ones.

View File

@ -157,7 +157,7 @@ class SuperprojectTestCase(unittest.TestCase):
""") """)
self._superproject = git_superproject.Superproject(manifest, self.repodir, self._superproject = git_superproject.Superproject(manifest, self.repodir,
self.git_event_log) self.git_event_log)
with mock.patch.object(self._superproject, '_GetBranch', return_value='junk'): with mock.patch.object(self._superproject, '_branch', 'junk'):
sync_result = self._superproject.Sync() sync_result = self._superproject.Sync()
self.assertFalse(sync_result.success) self.assertFalse(sync_result.success)
self.assertTrue(sync_result.fatal) self.assertTrue(sync_result.fatal)

View File

@ -572,6 +572,7 @@ class SuperProjectElementTests(ManifestParseTestCase):
self.assertEqual(manifest.superproject['name'], 'superproject') self.assertEqual(manifest.superproject['name'], 'superproject')
self.assertEqual(manifest.superproject['remote'].name, 'test-remote') self.assertEqual(manifest.superproject['remote'].name, 'test-remote')
self.assertEqual(manifest.superproject['remote'].url, 'http://localhost/superproject') self.assertEqual(manifest.superproject['remote'].url, 'http://localhost/superproject')
self.assertEqual(manifest.superproject['revision'], 'refs/heads/main')
self.assertEqual( self.assertEqual(
sort_attributes(manifest.ToXml().toxml()), sort_attributes(manifest.ToXml().toxml()),
'<?xml version="1.0" ?><manifest>' '<?xml version="1.0" ?><manifest>'
@ -580,6 +581,72 @@ class SuperProjectElementTests(ManifestParseTestCase):
'<superproject name="superproject"/>' '<superproject name="superproject"/>'
'</manifest>') '</manifest>')
def test_superproject_revision(self):
"""Check superproject settings with a different revision attribute"""
self.maxDiff = None
manifest = self.getXmlManifest("""
<manifest>
<remote name="test-remote" fetch="http://localhost" />
<default remote="test-remote" revision="refs/heads/main" />
<superproject name="superproject" revision="refs/heads/stable" />
</manifest>
""")
self.assertEqual(manifest.superproject['name'], 'superproject')
self.assertEqual(manifest.superproject['remote'].name, 'test-remote')
self.assertEqual(manifest.superproject['remote'].url, 'http://localhost/superproject')
self.assertEqual(manifest.superproject['revision'], 'refs/heads/stable')
self.assertEqual(
sort_attributes(manifest.ToXml().toxml()),
'<?xml version="1.0" ?><manifest>'
'<remote fetch="http://localhost" name="test-remote"/>'
'<default remote="test-remote" revision="refs/heads/main"/>'
'<superproject name="superproject" revision="refs/heads/stable"/>'
'</manifest>')
def test_superproject_revision_default_negative(self):
"""Check superproject settings with a same revision attribute"""
self.maxDiff = None
manifest = self.getXmlManifest("""
<manifest>
<remote name="test-remote" fetch="http://localhost" />
<default remote="test-remote" revision="refs/heads/stable" />
<superproject name="superproject" revision="refs/heads/stable" />
</manifest>
""")
self.assertEqual(manifest.superproject['name'], 'superproject')
self.assertEqual(manifest.superproject['remote'].name, 'test-remote')
self.assertEqual(manifest.superproject['remote'].url, 'http://localhost/superproject')
self.assertEqual(manifest.superproject['revision'], 'refs/heads/stable')
self.assertEqual(
sort_attributes(manifest.ToXml().toxml()),
'<?xml version="1.0" ?><manifest>'
'<remote fetch="http://localhost" name="test-remote"/>'
'<default remote="test-remote" revision="refs/heads/stable"/>'
'<superproject name="superproject"/>'
'</manifest>')
def test_superproject_revision_remote(self):
"""Check superproject settings with a same revision attribute"""
self.maxDiff = None
manifest = self.getXmlManifest("""
<manifest>
<remote name="test-remote" fetch="http://localhost" revision="refs/heads/main" />
<default remote="test-remote" />
<superproject name="superproject" revision="refs/heads/stable" />
</manifest>
""")
self.assertEqual(manifest.superproject['name'], 'superproject')
self.assertEqual(manifest.superproject['remote'].name, 'test-remote')
self.assertEqual(manifest.superproject['remote'].url, 'http://localhost/superproject')
self.assertEqual(manifest.superproject['revision'], 'refs/heads/stable')
self.assertEqual(
sort_attributes(manifest.ToXml().toxml()),
'<?xml version="1.0" ?><manifest>'
'<remote fetch="http://localhost" name="test-remote" revision="refs/heads/main"/>'
'<default remote="test-remote"/>'
'<superproject name="superproject" revision="refs/heads/stable"/>'
'</manifest>')
def test_remote(self): def test_remote(self):
"""Check superproject settings with a remote.""" """Check superproject settings with a remote."""
manifest = self.getXmlManifest(""" manifest = self.getXmlManifest("""
@ -593,6 +660,7 @@ class SuperProjectElementTests(ManifestParseTestCase):
self.assertEqual(manifest.superproject['name'], 'platform/superproject') self.assertEqual(manifest.superproject['name'], 'platform/superproject')
self.assertEqual(manifest.superproject['remote'].name, 'superproject-remote') self.assertEqual(manifest.superproject['remote'].name, 'superproject-remote')
self.assertEqual(manifest.superproject['remote'].url, 'http://localhost/platform/superproject') self.assertEqual(manifest.superproject['remote'].url, 'http://localhost/platform/superproject')
self.assertEqual(manifest.superproject['revision'], 'refs/heads/main')
self.assertEqual( self.assertEqual(
sort_attributes(manifest.ToXml().toxml()), sort_attributes(manifest.ToXml().toxml()),
'<?xml version="1.0" ?><manifest>' '<?xml version="1.0" ?><manifest>'
@ -613,6 +681,7 @@ class SuperProjectElementTests(ManifestParseTestCase):
""") """)
self.assertEqual(manifest.superproject['name'], 'superproject') self.assertEqual(manifest.superproject['name'], 'superproject')
self.assertEqual(manifest.superproject['remote'].name, 'default-remote') self.assertEqual(manifest.superproject['remote'].name, 'default-remote')
self.assertEqual(manifest.superproject['revision'], 'refs/heads/main')
self.assertEqual( self.assertEqual(
sort_attributes(manifest.ToXml().toxml()), sort_attributes(manifest.ToXml().toxml()),
'<?xml version="1.0" ?><manifest>' '<?xml version="1.0" ?><manifest>'