bundle.py 4.3 KB

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