fastexport.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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.objects import format_timezone
  21. import stat
  22. class FastExporter(object):
  23. def __init__(self, outf, store):
  24. self.outf = outf
  25. self.store = store
  26. self.markers = {}
  27. def export_blob(self, blob, i):
  28. self.outf.write("blob\nmark :%s\n" % i)
  29. self.outf.write("data %s\n" % blob.raw_length())
  30. self.outf.write(blob.as_raw_string())
  31. self.outf.write("\n")
  32. def export_commit(self, commit, branchname):
  33. file_changes = []
  34. for path, mode, hexsha in tree_changes(commit.tree):
  35. if stat.S_ISDIR(mode):
  36. file_changes.extend(self.dump_file_tree(self.store[hexsha]))
  37. else:
  38. self.dump_file_blob(self.store[hexsha], i)
  39. file_changes.append("M %o :%s %s" % (mode, i, path))
  40. return file_changes
  41. self.write("commit refs/heads/%s\n" % branchname)
  42. self.write("committer %s %s %s\n" % (commit.committer, commit.commit_time, format_timezone(commit.commit_timezone)))
  43. self.write("author %s %s %s\n" % (commit.author, commit.author_time, format_timezone(commit.author_timezone)))
  44. self.write("data %s\n" % len(commit.message))
  45. self.write(commit.message)
  46. self.write("\n")
  47. file_changes = self.export_tree(self.store[commit.tree])
  48. self.write('\n'.join(file_changes))
  49. self.write("\n\n")