diff --git a/swh/model/git_objects.py b/swh/model/git_objects.py index 41be6f2ea77ed6304e9435ed72b47a7281f8d9df..2eb48d8d706cadcd834e18874f48912305d5e071 100644 --- a/swh/model/git_objects.py +++ b/swh/model/git_objects.py @@ -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), diff --git a/swh/model/tests/test_identifiers.py b/swh/model/tests/test_identifiers.py index 793e6d5b3aa6f931a1afc8ca17f7b3be61c4e90d..bbbdc8c4cc321548c4673dc8bef489350fcc14dd 100644 --- a/swh/model/tests/test_identifiers.py +++ b/swh/model/tests/test_identifiers.py @@ -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,