dulwich 4.9 KB

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