2
0

dulwich 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #!/usr/bin/python -u
  2. #
  3. # dulwich - Simple command-line interface to Dulwich
  4. # Copyright (C) 2008-2011 Jelmer Vernooij <jelmer@samba.org>
  5. # vim: expandtab
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; version 2
  10. # or (at your option) a later version of the License.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20. # MA 02110-1301, USA.
  21. """Simple command-line interface to Dulwich>
  22. This is a very simple command-line wrapper for Dulwich. It is by
  23. no means intended to be a full-blown Git command-line interface but just
  24. a way to test Dulwich.
  25. """
  26. import os
  27. import sys
  28. from getopt import getopt
  29. from dulwich.client import get_transport_and_path
  30. from dulwich.errors import ApplyDeltaError
  31. from dulwich.index import Index
  32. from dulwich.pack import Pack, sha_to_hex
  33. from dulwich.patch import write_tree_diff
  34. from dulwich.repo import Repo
  35. from dulwich.server import update_server_info
  36. def cmd_archive(args):
  37. opts, args = getopt(args, "", [])
  38. client, path = get_transport_and_path(args.pop(0))
  39. committish = args.pop(0)
  40. client.archive(path, committish, sys.stdout.write, sys.stderr.write)
  41. def cmd_fetch_pack(args):
  42. opts, args = getopt(args, "", ["all"])
  43. opts = dict(opts)
  44. client, path = get_transport_and_path(args.pop(0))
  45. r = Repo(".")
  46. if "--all" in opts:
  47. determine_wants = r.object_store.determine_wants_all
  48. else:
  49. determine_wants = lambda x: [y for y in args if not y in r.object_store]
  50. client.fetch(path, r, determine_wants)
  51. def cmd_fetch(args):
  52. opts, args = getopt(args, "", [])
  53. opts = dict(opts)
  54. client, path = get_transport_and_path(args.pop(0))
  55. r = Repo(".")
  56. if "--all" in opts:
  57. determine_wants = r.object_store.determine_wants_all
  58. refs = client.fetch(path, r, progress=sys.stdout.write)
  59. print "Remote refs:"
  60. for item in refs.iteritems():
  61. print "%s -> %s" % item
  62. def cmd_log(args):
  63. opts, args = getopt(args, "", [])
  64. if len(args) > 0:
  65. path = args.pop(0)
  66. else:
  67. path = "."
  68. r = Repo(path)
  69. todo = [r.head()]
  70. done = set()
  71. while todo:
  72. sha = todo.pop()
  73. assert isinstance(sha, str)
  74. if sha in done:
  75. continue
  76. done.add(sha)
  77. commit = r[sha]
  78. print "-" * 50
  79. print "commit: %s" % sha
  80. if len(commit.parents) > 1:
  81. print "merge: %s" % "...".join(commit.parents[1:])
  82. print "author: %s" % commit.author
  83. print "committer: %s" % commit.committer
  84. print ""
  85. print commit.message
  86. print ""
  87. todo.extend([p for p in commit.parents if p not in done])
  88. def cmd_diff(args):
  89. opts, args = getopt(args, "", [])
  90. if args == []:
  91. print "Usage: dulwich diff COMMITID"
  92. sys.exit(1)
  93. r = Repo(".")
  94. commit_id = args[0]
  95. commit = r[commit_id]
  96. parent_commit = r[commit.parents[0]]
  97. write_tree_diff(sys.stdout, r.object_store, parent_commit.tree, commit.tree)
  98. def cmd_dump_pack(args):
  99. opts, args = getopt(args, "", [])
  100. if args == []:
  101. print "Usage: dulwich dump-pack FILENAME"
  102. sys.exit(1)
  103. basename, _ = os.path.splitext(args[0])
  104. x = Pack(basename)
  105. print "Object names checksum: %s" % x.name()
  106. print "Checksum: %s" % sha_to_hex(x.get_stored_checksum())
  107. if not x.check():
  108. print "CHECKSUM DOES NOT MATCH"
  109. print "Length: %d" % len(x)
  110. for name in x:
  111. try:
  112. print "\t%s" % x[name]
  113. except KeyError, k:
  114. print "\t%s: Unable to resolve base %s" % (name, k)
  115. except ApplyDeltaError, e:
  116. print "\t%s: Unable to apply delta: %r" % (name, e)
  117. def cmd_dump_index(args):
  118. opts, args = getopt(args, "", [])
  119. if args == []:
  120. print "Usage: dulwich dump-index FILENAME"
  121. sys.exit(1)
  122. filename = args[0]
  123. idx = Index(filename)
  124. for o in idx:
  125. print o, idx[o]
  126. def cmd_init(args):
  127. opts, args = getopt(args, "", ["bare"])
  128. opts = dict(opts)
  129. if args == []:
  130. path = os.getcwd()
  131. else:
  132. path = args[0]
  133. if not os.path.exists(path):
  134. os.mkdir(path)
  135. if "--bare" in opts:
  136. Repo.init_bare(path)
  137. else:
  138. Repo.init(path)
  139. def cmd_clone(args):
  140. opts, args = getopt(args, "", [])
  141. opts = dict(opts)
  142. if args == []:
  143. print "usage: dulwich clone host:path [PATH]"
  144. sys.exit(1)
  145. client, host_path = get_transport_and_path(args.pop(0))
  146. if len(args) > 0:
  147. path = args.pop(0)
  148. else:
  149. path = host_path.split("/")[-1]
  150. if not os.path.exists(path):
  151. os.mkdir(path)
  152. r = Repo.init(path)
  153. remote_refs = client.fetch(host_path, r,
  154. determine_wants=r.object_store.determine_wants_all,
  155. progress=sys.stdout.write)
  156. r["HEAD"] = remote_refs["HEAD"]
  157. def cmd_commit(args):
  158. opts, args = getopt(args, "", ["message"])
  159. opts = dict(opts)
  160. r = Repo(".")
  161. committer = "%s <%s>" % (os.getenv("GIT_COMMITTER_NAME"),
  162. os.getenv("GIT_COMMITTER_EMAIL"))
  163. author = "%s <%s>" % (os.getenv("GIT_AUTHOR_NAME"),
  164. os.getenv("GIT_AUTHOR_EMAIL"))
  165. r.do_commit(committer=committer, author=author, message=opts["--message"])
  166. def cmd_update_server_info(args):
  167. r = Repo(".")
  168. update_server_info(r)
  169. commands = {
  170. "commit": cmd_commit,
  171. "fetch-pack": cmd_fetch_pack,
  172. "fetch": cmd_fetch,
  173. "dump-pack": cmd_dump_pack,
  174. "dump-index": cmd_dump_index,
  175. "init": cmd_init,
  176. "log": cmd_log,
  177. "clone": cmd_clone,
  178. "archive": cmd_archive,
  179. "update-server-info": cmd_update_server_info,
  180. "diff": cmd_diff,
  181. }
  182. if len(sys.argv) < 2:
  183. print "Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys()))
  184. sys.exit(1)
  185. cmd = sys.argv[1]
  186. if not cmd in commands:
  187. print "No such subcommand: %s" % cmd
  188. sys.exit(1)
  189. commands[cmd](sys.argv[2:])