Skip to content
Snippets Groups Projects
Commit 0be753d5 authored by vlorentz's avatar vlorentz
Browse files

Add parameter 'ignore_unresolved' to snapshot_git_object

It allows building a snapshot manifest despite some branches being
unresolved (instead of raising an error).

This feature was removed in 57ae405d
but in the end, Snapshot.compute_hash() will need to use it so that
swh-scrubber does not crash when checksum such snapshots.
parent 79ed5505
No related branches found
No related tags found
No related merge requests found
......@@ -412,7 +412,9 @@ def release_git_object(release: Union[Dict, model.Release]) -> bytes:
return format_git_object_from_headers("tag", headers, release.message)
def snapshot_git_object(snapshot: Union[Dict, model.Snapshot]) -> bytes:
def snapshot_git_object(
snapshot: Union[Dict, model.Snapshot], *, ignore_unresolved: bool = False
) -> bytes:
"""Formats a snapshot as a git-like object.
Snapshots are a set of named branches, which are pointers to objects at any
......@@ -456,6 +458,10 @@ def snapshot_git_object(snapshot: Union[Dict, model.Snapshot]) -> bytes:
Note that, akin to directory manifests, there is no separator between
entries. Because of symbolic branches, identifiers are of arbitrary
length but are length-encoded to avoid ambiguity.
Args:
ignore_unresolved: if False (the default), raises an exception when
alias branches point to non-existing branches cause
"""
if isinstance(snapshot, dict):
# For backward compatibility
......@@ -495,7 +501,7 @@ def snapshot_git_object(snapshot: Union[Dict, model.Snapshot]) -> bytes:
]
)
if unresolved:
if unresolved and not ignore_unresolved:
raise ValueError(
"Branch aliases unresolved: %s"
% ", ".join("%r -> %r" % x for x in unresolved),
......
......@@ -758,6 +758,11 @@ class SnapshotIdentifier(unittest.TestCase):
with self.assertRaisesRegex(ValueError, "b'foo' -> b'bar'"):
Snapshot.from_dict(remove_id(self.unresolved))
def test_git_object_unresolved(self):
with self.assertRaisesRegex(ValueError, "b'foo' -> b'bar'"):
git_objects.snapshot_git_object(self.unresolved)
git_objects.snapshot_git_object(self.unresolved, ignore_unresolved=True)
def test_all_types(self):
self.assertEqual(
Snapshot.from_dict(remove_id(self.all_types)).id,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment