dulwich 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/python
  2. # dul-daemon - Simple git smart server client
  3. # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; version 2
  8. # or (at your option) a later version of 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. import sys
  20. from getopt import getopt
  21. def cmd_fetch_pack(args):
  22. from dulwich.client import TCPGitClient, SimpleFetchGraphWalker
  23. from dulwich.repo import Repo
  24. opts, args = getopt(args, "", ["all"])
  25. opts = dict(opts)
  26. if not ":" in args[0]:
  27. print "Usage: dulwich fetch-pack [--all] host:path [REF...]"
  28. sys.exit(1)
  29. (host, path) = args.pop(0).split(":", 1)
  30. client = TCPGitClient(host)
  31. if "--all" in opts:
  32. determine_wants = lambda x: [y for y in x.values() if not y in r.object_store]
  33. else:
  34. determine_wants = lambda x: [y for y in args if not y in r.object_store]
  35. r = Repo(".")
  36. graphwalker = SimpleFetchGraphWalker(r.heads().values(), r.get_parents)
  37. f, commit = r.object_store.add_pack()
  38. try:
  39. client.fetch_pack(path, determine_wants, graphwalker, f.write, sys.stdout.write)
  40. f.close()
  41. commit()
  42. except:
  43. f.close()
  44. raise
  45. def cmd_log(args):
  46. from dulwich.repo import Repo
  47. opts, args = getopt(args, "", [])
  48. r = Repo(".")
  49. todo = [r.head()]
  50. done = set()
  51. while todo:
  52. sha = todo.pop()
  53. assert isinstance(sha, str)
  54. if sha in done:
  55. continue
  56. done.add(sha)
  57. commit = r.commit(sha)
  58. print "-" * 50
  59. print "commit: %s" % sha
  60. if len(commit.parents) > 1:
  61. print "merge: %s" % "...".join(commit.parents[1:])
  62. print "author: %s" % commit.author
  63. print "committer: %s" % commit.committer
  64. print ""
  65. print commit.message
  66. print ""
  67. todo.extend([p for p in commit.parents if p not in done])
  68. def cmd_dump_pack(args):
  69. from dulwich.errors import ApplyDeltaError
  70. from dulwich.pack import Pack, sha_to_hex
  71. import os
  72. import sys
  73. opts, args = getopt(args, "", [])
  74. if args == []:
  75. print "Usage: dulwich dump-pack FILENAME"
  76. sys.exit(1)
  77. basename, _ = os.path.splitext(args[0])
  78. x = Pack(basename)
  79. print "Object names checksum: %s" % x.name()
  80. print "Checksum: %s" % sha_to_hex(x.get_stored_checksum())
  81. if not x.check():
  82. print "CHECKSUM DOES NOT MATCH"
  83. print "Length: %d" % len(x)
  84. for name in x:
  85. try:
  86. print "\t%s" % x[name]
  87. except KeyError, k:
  88. print "\t%s: Unable to resolve base %s" % (name, k)
  89. except ApplyDeltaError, e:
  90. print "\t%s: Unable to apply delta: %r" % (name, e)
  91. def cmd_init(args):
  92. from dulwich.repo import Repo
  93. import os
  94. import sys
  95. opts, args = getopt(args, "", ["--bare"])
  96. opts = dict(opts)
  97. if args == []:
  98. path = os.getcwd()
  99. else:
  100. path = args[0]
  101. if not os.path.exists(path):
  102. os.mkdir(path)
  103. if "--bare" in opts:
  104. Repo.init_bare(path)
  105. else:
  106. Repo.init(path)
  107. def cmd_clone(args):
  108. from dulwich.client import TCPGitClient, SimpleFetchGraphWalker
  109. from dulwich.repo import Repo
  110. import os
  111. import sys
  112. opts, args = getopt(args, "", [])
  113. opts = dict(opts)
  114. if args == []:
  115. print "usage: dulwich clone host:path [PATH]"
  116. sys.exit(1)
  117. if not ":" in args[0]:
  118. print "Usage: dulwich clone host:path [PATH]"
  119. sys.exit(1)
  120. (host, host_path) = args.pop(0).split(":", 1)
  121. client = TCPGitClient(host)
  122. if len(args) > 0:
  123. path = args.pop(0)
  124. else:
  125. path = host_path.split("/")[-1]
  126. if not os.path.exists(path):
  127. os.mkdir(path)
  128. Repo.init(path)
  129. r = Repo(path)
  130. determine_wants = lambda x: [y for y in x.values() if not y in r.object_store]
  131. graphwalker = SimpleFetchGraphWalker(r.heads().values(), r.get_parents)
  132. f, commit = r.object_store.add_pack()
  133. try:
  134. client.fetch_pack(host_path, determine_wants, graphwalker, f.write,
  135. sys.stdout.write)
  136. f.close()
  137. commit()
  138. except:
  139. f.close()
  140. raise
  141. commands = {
  142. "fetch-pack": cmd_fetch_pack,
  143. "dump-pack": cmd_dump_pack,
  144. "init": cmd_init,
  145. "log": cmd_log,
  146. "clone": cmd_clone,
  147. }
  148. if len(sys.argv) < 2:
  149. print "Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys()))
  150. sys.exit(1)
  151. cmd = sys.argv[1]
  152. if not cmd in commands:
  153. print "No such subcommand: %s" % cmd
  154. sys.exit(1)
  155. commands[cmd](sys.argv[2:])