bundle.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. """
  22. from typing import Dict, List, Tuple, Optional, Union, Sequence
  23. from .pack import PackData, write_pack_data
  24. class Bundle(object):
  25. version = None # type: Optional[int]
  26. capabilities = {} # type: Dict[str, str]
  27. prerequisites = [] # type: List[Tuple[bytes, str]]
  28. references = {} # type: Dict[str, bytes]
  29. pack_data = [] # type: Union[PackData, Sequence[bytes]]
  30. def __eq__(self, other):
  31. if not isinstance(other, type(self)):
  32. return False
  33. if self.version != other.version:
  34. return False
  35. if self.capabilities != other.capabilities:
  36. return False
  37. if self.prerequisites != other.prerequisites:
  38. return False
  39. if self.references != other.references:
  40. return False
  41. if self.pack_data != other.pack_data:
  42. return False
  43. return True
  44. def _read_bundle(f, version):
  45. capabilities = {}
  46. prerequisites = []
  47. references = {}
  48. line = f.readline()
  49. if version >= 3:
  50. while line.startswith(b"@"):
  51. line = line[1:].rstrip(b"\n")
  52. try:
  53. key, value = line.split(b"=", 1)
  54. except ValueError:
  55. key = line
  56. value = None
  57. else:
  58. value = value.decode("utf-8")
  59. capabilities[key.decode("utf-8")] = value
  60. line = f.readline()
  61. while line.startswith(b"-"):
  62. (obj_id, comment) = line[1:].rstrip(b"\n").split(b" ", 1)
  63. prerequisites.append((obj_id, comment.decode("utf-8")))
  64. line = f.readline()
  65. while line != b"\n":
  66. (obj_id, ref) = line.rstrip(b"\n").split(b" ", 1)
  67. references[ref] = obj_id
  68. line = f.readline()
  69. pack_data = PackData.from_file(f)
  70. ret = Bundle()
  71. ret.references = references
  72. ret.capabilities = capabilities
  73. ret.prerequisites = prerequisites
  74. ret.pack_data = pack_data
  75. ret.version = version
  76. return ret
  77. def read_bundle(f):
  78. """Read a bundle file."""
  79. firstline = f.readline()
  80. if firstline == b"# v2 git bundle\n":
  81. return _read_bundle(f, 2)
  82. if firstline == b"# v3 git bundle\n":
  83. return _read_bundle(f, 3)
  84. raise AssertionError("unsupported bundle format header: %r" % firstline)
  85. def write_bundle(f, bundle):
  86. version = bundle.version
  87. if version is None:
  88. if bundle.capabilities:
  89. version = 3
  90. else:
  91. version = 2
  92. if version == 2:
  93. f.write(b"# v2 git bundle\n")
  94. elif version == 3:
  95. f.write(b"# v3 git bundle\n")
  96. else:
  97. raise AssertionError("unknown version %d" % version)
  98. if version == 3:
  99. for key, value in bundle.capabilities.items():
  100. f.write(b"@" + key.encode("utf-8"))
  101. if value is not None:
  102. f.write(b"=" + value.encode("utf-8"))
  103. f.write(b"\n")
  104. for (obj_id, comment) in bundle.prerequisites:
  105. f.write(b"-%s %s\n" % (obj_id, comment.encode("utf-8")))
  106. for ref, obj_id in bundle.references.items():
  107. f.write(b"%s %s\n" % (obj_id, ref))
  108. f.write(b"\n")
  109. write_pack_data(f, records=bundle.pack_data)