dulwich 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 get_transport_and_path(uri):
  22. from dulwich.client import TCPGitClient, SSHGitClient, SubprocessGitClient
  23. for handler, transport in (("git://", TCPGitClient), ("git+ssh://", SSHGitClient)):
  24. if uri.startswith(handler):
  25. host, path = uri[len(handler):].split("/", 1)
  26. return transport(host), "/"+path
  27. # if its not git or git+ssh, try a local url..
  28. return SubprocessGitClient(), uri
  29. def cmd_fetch_pack(args):
  30. from dulwich.client import SimpleFetchGraphWalker
  31. from dulwich.repo import Repo
  32. opts, args = getopt(args, "", ["all"])
  33. opts = dict(opts)
  34. if not ":" in args[0]:
  35. print "Usage: dulwich fetch-pack [--all] host:path [REF...]"
  36. sys.exit(1)
  37. client, path = get_transport_and_path(args.pop(0))
  38. if not client:
  39. print "Must be git:// or git+ssh://"
  40. sys.exit(1)
  41. if "--all" in opts:
  42. determine_wants = lambda x: [y for y in x.values() if not y in r.object_store]
  43. else:
  44. determine_wants = lambda x: [y for y in args if not y in r.object_store]
  45. r = Repo(".")
  46. graphwalker = SimpleFetchGraphWalker(r.heads().values(), r.get_parents)
  47. f, commit = r.object_store.add_pack()
  48. try:
  49. client.fetch_pack(path, determine_wants, graphwalker, f.write, sys.stdout.write)
  50. f.close()
  51. commit()
  52. except:
  53. f.close()
  54. raise
  55. def cmd_log(args):
  56. from dulwich.repo import Repo
  57. opts, args = getopt(args, "", [])
  58. r = Repo(".")
  59. todo = [r.head()]
  60. done = set()
  61. while todo:
  62. sha = todo.pop()
  63. assert isinstance(sha, str)
  64. if sha in done:
  65. continue
  66. done.add(sha)
  67. commit = r.commit(sha)
  68. print "-" * 50
  69. print "commit: %s" % sha
  70. if len(commit.parents) > 1:
  71. print "merge: %s" % "...".join(commit.parents[1:])
  72. print "author: %s" % commit.author
  73. print "committer: %s" % commit.committer
  74. print ""
  75. print commit.message
  76. print ""
  77. todo.extend([p for p in commit.parents if p not in done])
  78. def cmd_dump_pack(args):
  79. from dulwich.errors import ApplyDeltaError
  80. from dulwich.pack import Pack, sha_to_hex
  81. import os
  82. import sys
  83. opts, args = getopt(args, "", [])
  84. if args == []:
  85. print "Usage: dulwich dump-pack FILENAME"
  86. sys.exit(1)
  87. basename, _ = os.path.splitext(args[0])
  88. x = Pack(basename)
  89. print "Object names checksum: %s" % x.name()
  90. print "Checksum: %s" % sha_to_hex(x.get_stored_checksum())
  91. if not x.check():
  92. print "CHECKSUM DOES NOT MATCH"
  93. print "Length: %d" % len(x)
  94. for name in x:
  95. try:
  96. print "\t%s" % x[name]
  97. except KeyError, k:
  98. print "\t%s: Unable to resolve base %s" % (name, k)
  99. except ApplyDeltaError, e:
  100. print "\t%s: Unable to apply delta: %r" % (name, e)
  101. def cmd_dump_index(args):
  102. from dulwich.index import Index
  103. opts, args = getopt(args, "", [])
  104. if args == []:
  105. print "Usage: dulwich dump-pack FILENAME"
  106. sys.exit(1)
  107. filename = args[0]
  108. idx = Index(filename)
  109. for o in idx:
  110. print o[0]
  111. def cmd_init(args):
  112. from dulwich.repo import Repo
  113. import os
  114. import sys
  115. opts, args = getopt(args, "", ["--bare"])
  116. opts = dict(opts)
  117. if args == []:
  118. path = os.getcwd()
  119. else:
  120. path = args[0]
  121. if not os.path.exists(path):
  122. os.mkdir(path)
  123. if "--bare" in opts:
  124. Repo.init_bare(path)
  125. else:
  126. Repo.init(path)
  127. def cmd_clone(args):
  128. from dulwich.client import SimpleFetchGraphWalker
  129. from dulwich.repo import Repo
  130. import os
  131. import sys
  132. opts, args = getopt(args, "", [])
  133. opts = dict(opts)
  134. if args == []:
  135. print "usage: dulwich clone host:path [PATH]"
  136. sys.exit(1)
  137. if not ":" in args[0]:
  138. print "Usage: dulwich clone host:path [PATH]"
  139. sys.exit(1)
  140. client, host_path = get_transport_and_path(args.pop(0))
  141. if not client:
  142. print "Must be git:// or git+ssh://"
  143. sys.exit(1)
  144. if len(args) > 0:
  145. path = args.pop(0)
  146. else:
  147. path = host_path.split("/")[-1]
  148. if not os.path.exists(path):
  149. os.mkdir(path)
  150. Repo.init(path)
  151. r = Repo(path)
  152. determine_wants = lambda x: [y for y in x.values() if not y in r.object_store]
  153. graphwalker = SimpleFetchGraphWalker(r.heads().values(), r.get_parents)
  154. f, commit = r.object_store.add_pack()
  155. try:
  156. client.fetch_pack(host_path, determine_wants, graphwalker, f.write,
  157. sys.stdout.write)
  158. f.close()
  159. commit()
  160. except:
  161. f.close()
  162. raise
  163. commands = {
  164. "fetch-pack": cmd_fetch_pack,
  165. "dump-pack": cmd_dump_pack,
  166. "dump-index": cmd_dump_index,
  167. "init": cmd_init,
  168. "log": cmd_log,
  169. "clone": cmd_clone,
  170. }
  171. if len(sys.argv) < 2:
  172. print "Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys()))
  173. sys.exit(1)
  174. cmd = sys.argv[1]
  175. if not cmd in commands:
  176. print "No such subcommand: %s" % cmd
  177. sys.exit(1)
  178. commands[cmd](sys.argv[2:])