fastexport.py 2.3 KB

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