bundle.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # bundle.py -- Bundle format support
  2. # Copyright (C) 2020 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Bundle format support."""
  22. from typing import BinaryIO, Optional
  23. from .pack import PackData, write_pack_data
  24. class Bundle:
  25. version: Optional[int]
  26. capabilities: dict[str, Optional[str]]
  27. prerequisites: list[tuple[bytes, str]]
  28. references: dict[bytes, bytes]
  29. pack_data: PackData
  30. def __repr__(self) -> str:
  31. return (
  32. f"<{type(self).__name__}(version={self.version}, "
  33. f"capabilities={self.capabilities}, "
  34. f"prerequisites={self.prerequisites}, "
  35. f"references={self.references})>"
  36. )
  37. def __eq__(self, other: object) -> bool:
  38. if not isinstance(other, type(self)):
  39. return False
  40. if self.version != other.version:
  41. return False
  42. if self.capabilities != other.capabilities:
  43. return False
  44. if self.prerequisites != other.prerequisites:
  45. return False
  46. if self.references != other.references:
  47. return False
  48. if self.pack_data != other.pack_data:
  49. return False
  50. return True
  51. def _read_bundle(f: BinaryIO, version: int) -> Bundle:
  52. capabilities = {}
  53. prerequisites = []
  54. references = {}
  55. line = f.readline()
  56. if version >= 3:
  57. while line.startswith(b"@"):
  58. line = line[1:].rstrip(b"\n")
  59. try:
  60. key, value_bytes = line.split(b"=", 1)
  61. value = value_bytes.decode("utf-8")
  62. except ValueError:
  63. key = line
  64. value = None
  65. capabilities[key.decode("utf-8")] = value
  66. line = f.readline()
  67. while line.startswith(b"-"):
  68. (obj_id, comment) = line[1:].rstrip(b"\n").split(b" ", 1)
  69. prerequisites.append((obj_id, comment.decode("utf-8")))
  70. line = f.readline()
  71. while line != b"\n":
  72. (obj_id, ref) = line.rstrip(b"\n").split(b" ", 1)
  73. references[ref] = obj_id
  74. line = f.readline()
  75. pack_data = PackData.from_file(f)
  76. ret = Bundle()
  77. ret.references = references
  78. ret.capabilities = capabilities
  79. ret.prerequisites = prerequisites
  80. ret.pack_data = pack_data
  81. ret.version = version
  82. return ret
  83. def read_bundle(f: BinaryIO) -> Bundle:
  84. """Read a bundle file."""
  85. firstline = f.readline()
  86. if firstline == b"# v2 git bundle\n":
  87. return _read_bundle(f, 2)
  88. if firstline == b"# v3 git bundle\n":
  89. return _read_bundle(f, 3)
  90. raise AssertionError(f"unsupported bundle format header: {firstline!r}")
  91. def write_bundle(f: BinaryIO, bundle: Bundle) -> None:
  92. version = bundle.version
  93. if version is None:
  94. if bundle.capabilities:
  95. version = 3
  96. else:
  97. version = 2
  98. if version == 2:
  99. f.write(b"# v2 git bundle\n")
  100. elif version == 3:
  101. f.write(b"# v3 git bundle\n")
  102. else:
  103. raise AssertionError(f"unknown version {version}")
  104. if version == 3:
  105. for key, value in bundle.capabilities.items():
  106. f.write(b"@" + key.encode("utf-8"))
  107. if value is not None:
  108. f.write(b"=" + value.encode("utf-8"))
  109. f.write(b"\n")
  110. for obj_id, comment in bundle.prerequisites:
  111. f.write(b"-%s %s\n" % (obj_id, comment.encode("utf-8")))
  112. for ref, obj_id in bundle.references.items():
  113. f.write(b"%s %s\n" % (obj_id, ref))
  114. f.write(b"\n")
  115. write_pack_data(
  116. f.write,
  117. num_records=len(bundle.pack_data),
  118. records=bundle.pack_data.iter_unpacked(),
  119. )