dulwich 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.porcelain import archive
  33. from dulwich.pack import Pack, sha_to_hex
  34. from dulwich.patch import write_tree_diff
  35. from dulwich.repo import Repo
  36. from dulwich.server import update_server_info
  37. def cmd_archive(args):
  38. opts, args = getopt(args, "", [])
  39. client, path = get_transport_and_path(args.pop(0))
  40. location = args.pop(0)
  41. committish = args.pop(0)
  42. archive(location, committish, outstream=sys.stdout, errstream=sys.stderr)
  43. def cmd_fetch_pack(args):
  44. opts, args = getopt(args, "", ["all"])
  45. opts = dict(opts)
  46. client, path = get_transport_and_path(args.pop(0))
  47. r = Repo(".")
  48. if "--all" in opts:
  49. determine_wants = r.object_store.determine_wants_all
  50. else:
  51. determine_wants = lambda x: [y for y in args if not y in r.object_store]
  52. client.fetch(path, r, determine_wants)
  53. def cmd_fetch(args):
  54. opts, args = getopt(args, "", [])
  55. opts = dict(opts)
  56. client, path = get_transport_and_path(args.pop(0))
  57. r = Repo(".")
  58. if "--all" in opts:
  59. determine_wants = r.object_store.determine_wants_all
  60. refs = client.fetch(path, r, progress=sys.stdout.write)
  61. print "Remote refs:"
  62. for item in refs.iteritems():
  63. print "%s -> %s" % item
  64. def cmd_log(args):
  65. opts, args = getopt(args, "", [])
  66. if len(args) > 0:
  67. path = args.pop(0)
  68. else:
  69. path = "."
  70. r = Repo(path)
  71. todo = [r.head()]
  72. done = set()
  73. while todo:
  74. sha = todo.pop()
  75. assert isinstance(sha, str)
  76. if sha in done:
  77. continue
  78. done.add(sha)
  79. commit = r[sha]
  80. print "-" * 50
  81. print "commit: %s" % sha
  82. if len(commit.parents) > 1:
  83. print "merge: %s" % "...".join(commit.parents[1:])
  84. print "author: %s" % commit.author
  85. print "committer: %s" % commit.committer
  86. print ""
  87. print commit.message
  88. print ""
  89. todo.extend([p for p in commit.parents if p not in done])
  90. def cmd_diff(args):
  91. opts, args = getopt(args, "", [])
  92. if args == []:
  93. print "Usage: dulwich diff COMMITID"
  94. sys.exit(1)
  95. r = Repo(".")
  96. commit_id = args[0]
  97. commit = r[commit_id]
  98. parent_commit = r[commit.parents[0]]
  99. write_tree_diff(sys.stdout, r.object_store, parent_commit.tree, commit.tree)
  100. def cmd_dump_pack(args):
  101. opts, args = getopt(args, "", [])
  102. if args == []:
  103. print "Usage: dulwich dump-pack FILENAME"
  104. sys.exit(1)
  105. basename, _ = os.path.splitext(args[0])
  106. x = Pack(basename)
  107. print "Object names checksum: %s" % x.name()
  108. print "Checksum: %s" % sha_to_hex(x.get_stored_checksum())
  109. if not x.check():
  110. print "CHECKSUM DOES NOT MATCH"
  111. print "Length: %d" % len(x)
  112. for name in x:
  113. try:
  114. print "\t%s" % x[name]
  115. except KeyError, k:
  116. print "\t%s: Unable to resolve base %s" % (name, k)
  117. except ApplyDeltaError, e:
  118. print "\t%s: Unable to apply delta: %r" % (name, e)
  119. def cmd_dump_index(args):
  120. opts, args = getopt(args, "", [])
  121. if args == []:
  122. print "Usage: dulwich dump-index FILENAME"
  123. sys.exit(1)
  124. filename = args[0]
  125. idx = Index(filename)
  126. for o in idx:
  127. print o, idx[o]
  128. def cmd_init(args):
  129. opts, args = getopt(args, "", ["bare"])
  130. opts = dict(opts)
  131. if args == []:
  132. path = os.getcwd()
  133. else:
  134. path = args[0]
  135. if not os.path.exists(path):
  136. os.mkdir(path)
  137. if "--bare" in opts:
  138. Repo.init_bare(path)
  139. else:
  140. Repo.init(path)
  141. def cmd_clone(args):
  142. opts, args = getopt(args, "", [])
  143. opts = dict(opts)
  144. if args == []:
  145. print "usage: dulwich clone host:path [PATH]"
  146. sys.exit(1)
  147. client, host_path = get_transport_and_path(args.pop(0))
  148. if len(args) > 0:
  149. path = args.pop(0)
  150. else:
  151. path = host_path.split("/")[-1]
  152. if not os.path.exists(path):
  153. os.mkdir(path)
  154. r = Repo.init(path)
  155. remote_refs = client.fetch(host_path, r,
  156. determine_wants=r.object_store.determine_wants_all,
  157. progress=sys.stdout.write)
  158. r["HEAD"] = remote_refs["HEAD"]
  159. def cmd_commit(args):
  160. opts, args = getopt(args, "", ["message"])
  161. opts = dict(opts)
  162. r = Repo(".")
  163. committer = "%s <%s>" % (os.getenv("GIT_COMMITTER_NAME"),
  164. os.getenv("GIT_COMMITTER_EMAIL"))
  165. author = "%s <%s>" % (os.getenv("GIT_AUTHOR_NAME"),
  166. os.getenv("GIT_AUTHOR_EMAIL"))
  167. r.do_commit(committer=committer, author=author, message=opts["--message"])
  168. def cmd_update_server_info(args):
  169. r = Repo(".")
  170. update_server_info(r)
  171. commands = {
  172. "commit": cmd_commit,
  173. "fetch-pack": cmd_fetch_pack,
  174. "fetch": cmd_fetch,
  175. "dump-pack": cmd_dump_pack,
  176. "dump-index": cmd_dump_index,
  177. "init": cmd_init,
  178. "log": cmd_log,
  179. "clone": cmd_clone,
  180. "archive": cmd_archive,
  181. "update-server-info": cmd_update_server_info,
  182. "diff": cmd_diff,
  183. }
  184. if len(sys.argv) < 2:
  185. print "Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys()))
  186. sys.exit(1)
  187. cmd = sys.argv[1]
  188. if not cmd in commands:
  189. print "No such subcommand: %s" % cmd
  190. sys.exit(1)
  191. commands[cmd](sys.argv[2:])