diff --git a/swh/model/git_objects.py b/swh/model/git_objects.py --- a/swh/model/git_objects.py +++ b/swh/model/git_objects.py @@ -412,7 +412,9 @@ 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 @@ 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 @@ ] ) - if unresolved: + if unresolved and not ignore_unresolved: raise ValueError( "Branch aliases unresolved: %s" % ", ".join("%r -> %r" % x for x in unresolved), diff --git a/swh/model/tests/test_identifiers.py b/swh/model/tests/test_identifiers.py --- a/swh/model/tests/test_identifiers.py +++ b/swh/model/tests/test_identifiers.py @@ -758,6 +758,11 @@ 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,