bundle.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 .pack import PackData
  23. from typing import Dict, List, Tuple
  24. class Bundle(object):
  25. version: int
  26. capabilities: Dict[str, str]
  27. prerequisites: List[Tuple[bytes, str]]
  28. references: Dict[str, bytes]
  29. pack_data: PackData
  30. def _read_bundle(f, version):
  31. capabilities = {}
  32. prerequisites = []
  33. references = {}
  34. line = f.readline()
  35. if version >= 3:
  36. while line.startswith(b'@'):
  37. line = line[1:].rstrip(b'\n')
  38. try:
  39. key, value = line.split(b'=', 1)
  40. except IndexError:
  41. key = line
  42. value = None
  43. capabilities[key] = value
  44. line = f.readline()
  45. while line.startswith(b'-'):
  46. (obj_id, comment) = line[1:].split(b' ', 1)
  47. prerequisites.append((obj_id, comment.decode('utf-8')))
  48. line = f.readline()
  49. while line != b'\n':
  50. (obj_id, ref) = line.rstrip(b'\n').split(b' ', 1)
  51. references[ref] = obj_id
  52. line = f.readline()
  53. pack_data = PackData.from_file(f)
  54. ret = Bundle()
  55. ret.references = references
  56. ret.capabilities = capabilities
  57. ret.prerequisites = prerequisites
  58. ret.pack_data = pack_data
  59. return ret
  60. def read_bundle(f):
  61. """Read a bundle file."""
  62. firstline = f.readline()
  63. if firstline == b'# v2 git bundle\n':
  64. return _read_bundle(f, 2)
  65. if firstline == b'# v3 git bundle\n':
  66. return _read_bundle(f, 3)
  67. raise AssertionError(
  68. 'unsupported bundle format header: %r' % firstline)