2
0

fastexport.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # __init__.py -- Fast export/import functionality
  2. # Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) any later version of
  8. # the License.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. # MA 02110-1301, USA.
  19. """Fast export/import functionality."""
  20. from dulwich.index import (
  21. commit_tree,
  22. )
  23. from dulwich.objects import (
  24. Blob,
  25. Commit,
  26. Tag,
  27. parse_timezone,
  28. )
  29. from fastimport import (
  30. commands,
  31. errors as fastimport_errors,
  32. parser,
  33. processor,
  34. )
  35. import stat
  36. def split_email(text):
  37. (name, email) = text.rsplit(" <", 1)
  38. return (name, email.rstrip(">"))
  39. class GitFastExporter(object):
  40. """Generate a fast-export output stream for Git objects."""
  41. def __init__(self, outf, store):
  42. self.outf = outf
  43. self.store = store
  44. self.markers = {}
  45. self._marker_idx = 0
  46. def print_cmd(self, cmd):
  47. self.outf.write("%r\n" % cmd)
  48. def _allocate_marker(self):
  49. self._marker_idx+=1
  50. return str(self._marker_idx)
  51. def _export_blob(self, blob):
  52. marker = self._allocate_marker()
  53. self.markers[marker] = blob.id
  54. return (commands.BlobCommand(marker, blob.data), marker)
  55. def emit_blob(self, blob):
  56. (cmd, marker) = self._export_blob(blob)
  57. self.print_cmd(cmd)
  58. return marker
  59. def _iter_files(self, base_tree, new_tree):
  60. for (old_path, new_path), (old_mode, new_mode), (old_hexsha, new_hexsha) in \
  61. self.store.tree_changes(base_tree, new_tree):
  62. if new_path is None:
  63. yield commands.FileDeleteCommand(old_path)
  64. continue
  65. if not stat.S_ISDIR(new_mode):
  66. blob = self.store[new_hexsha]
  67. marker = self.emit_blob(blob)
  68. if old_path != new_path and old_path is not None:
  69. yield commands.FileRenameCommand(old_path, new_path)
  70. if old_mode != new_mode or old_hexsha != new_hexsha:
  71. yield commands.FileModifyCommand(new_path, new_mode, marker, None)
  72. def _export_commit(self, commit, ref, base_tree=None):
  73. file_cmds = list(self._iter_files(base_tree, commit.tree))
  74. marker = self._allocate_marker()
  75. if commit.parents:
  76. from_ = commit.parents[0]
  77. merges = commit.parents[1:]
  78. else:
  79. from_ = None
  80. merges = []
  81. author, author_email = split_email(commit.author)
  82. committer, committer_email = split_email(commit.committer)
  83. cmd = commands.CommitCommand(ref, marker,
  84. (author, author_email, commit.author_time, commit.author_timezone),
  85. (committer, committer_email, commit.commit_time, commit.commit_timezone),
  86. commit.message, from_, merges, file_cmds)
  87. return (cmd, marker)
  88. def emit_commit(self, commit, ref, base_tree=None):
  89. cmd, marker = self._export_commit(commit, ref, base_tree)
  90. self.print_cmd(cmd)
  91. return marker
  92. class GitImportProcessor(processor.ImportProcessor):
  93. """An import processor that imports into a Git repository using Dulwich.
  94. """
  95. def __init__(self, repo, params=None, verbose=False, outf=None):
  96. processor.ImportProcessor.__init__(self, params, verbose)
  97. self.repo = repo
  98. self.last_commit = None
  99. def import_stream(self, stream):
  100. p = parser.ImportParser(stream)
  101. self.process(p.iter_commands)
  102. def blob_handler(self, cmd):
  103. """Process a BlobCommand."""
  104. self.repo.object_store.add_object(Blob.from_string(cmd.data))
  105. def checkpoint_handler(self, cmd):
  106. """Process a CheckpointCommand."""
  107. pass
  108. def commit_handler(self, cmd):
  109. """Process a CommitCommand."""
  110. commit = Commit()
  111. commit.author = cmd.author
  112. commit.committer = cmd.committer
  113. commit.message = cmd.message
  114. commit.parents = []
  115. contents = {}
  116. commit.tree = commit_tree(self.repo.object_store,
  117. ((path, hexsha, mode) for (path, (mode, hexsha)) in
  118. contents.iteritems()))
  119. if self.last_commit is not None:
  120. commit.parents.append(self.last_commit)
  121. commit.parents += cmd.merges
  122. self.repo.object_store.add_object(commit)
  123. self.repo[cmd.ref] = commit.id
  124. self.last_commit = commit.id
  125. def progress_handler(self, cmd):
  126. """Process a ProgressCommand."""
  127. pass
  128. def reset_handler(self, cmd):
  129. """Process a ResetCommand."""
  130. self.last_commit = cmd.from_
  131. self.rep.refs[cmd.from_] = cmd.id
  132. def tag_handler(self, cmd):
  133. """Process a TagCommand."""
  134. tag = Tag()
  135. tag.tagger = cmd.tagger
  136. tag.message = cmd.message
  137. tag.name = cmd.tag
  138. self.repo.add_object(tag)
  139. self.repo.refs["refs/tags/" + tag.name] = tag.id
  140. def feature_handler(self, cmd):
  141. """Process a FeatureCommand."""
  142. raise fastimport_errors.UnknownFeature(cmd.feature_name)