dulwich 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 import porcelain
  30. from dulwich.client import get_transport_and_path
  31. from dulwich.errors import ApplyDeltaError
  32. from dulwich.index import Index
  33. from dulwich.pack import Pack, sha_to_hex
  34. from dulwich.patch import write_tree_diff
  35. from dulwich.repo import Repo
  36. def cmd_archive(args):
  37. opts, args = getopt(args, "", [])
  38. client, path = get_transport_and_path(args.pop(0))
  39. location = args.pop(0)
  40. committish = args.pop(0)
  41. porcelain.archive(location, committish, outstream=sys.stdout,
  42. errstream=sys.stderr)
  43. def cmd_add(args):
  44. opts, args = getopt(args, "", [])
  45. porcelain.add(".", paths=args)
  46. def cmd_rm(args):
  47. opts, args = getopt(args, "", [])
  48. porcelain.rm(".", paths=args)
  49. def cmd_fetch_pack(args):
  50. opts, args = getopt(args, "", ["all"])
  51. opts = dict(opts)
  52. client, path = get_transport_and_path(args.pop(0))
  53. r = Repo(".")
  54. if "--all" in opts:
  55. determine_wants = r.object_store.determine_wants_all
  56. else:
  57. determine_wants = lambda x: [y for y in args if not y in r.object_store]
  58. client.fetch(path, r, determine_wants)
  59. def cmd_fetch(args):
  60. opts, args = getopt(args, "", [])
  61. opts = dict(opts)
  62. client, path = get_transport_and_path(args.pop(0))
  63. r = Repo(".")
  64. if "--all" in opts:
  65. determine_wants = r.object_store.determine_wants_all
  66. refs = client.fetch(path, r, progress=sys.stdout.write)
  67. print "Remote refs:"
  68. for item in refs.iteritems():
  69. print "%s -> %s" % item
  70. def cmd_log(args):
  71. opts, args = getopt(args, "", [])
  72. if len(args) > 0:
  73. path = args.pop(0)
  74. else:
  75. path = "."
  76. porcelain.log(repo=path, outstream=sys.stdout)
  77. def cmd_diff(args):
  78. opts, args = getopt(args, "", [])
  79. if args == []:
  80. print "Usage: dulwich diff COMMITID"
  81. sys.exit(1)
  82. r = Repo(".")
  83. commit_id = args[0]
  84. commit = r[commit_id]
  85. parent_commit = r[commit.parents[0]]
  86. write_tree_diff(sys.stdout, r.object_store, parent_commit.tree, commit.tree)
  87. def cmd_dump_pack(args):
  88. opts, args = getopt(args, "", [])
  89. if args == []:
  90. print "Usage: dulwich dump-pack FILENAME"
  91. sys.exit(1)
  92. basename, _ = os.path.splitext(args[0])
  93. x = Pack(basename)
  94. print "Object names checksum: %s" % x.name()
  95. print "Checksum: %s" % sha_to_hex(x.get_stored_checksum())
  96. if not x.check():
  97. print "CHECKSUM DOES NOT MATCH"
  98. print "Length: %d" % len(x)
  99. for name in x:
  100. try:
  101. print "\t%s" % x[name]
  102. except KeyError, k:
  103. print "\t%s: Unable to resolve base %s" % (name, k)
  104. except ApplyDeltaError, e:
  105. print "\t%s: Unable to apply delta: %r" % (name, e)
  106. def cmd_dump_index(args):
  107. opts, args = getopt(args, "", [])
  108. if args == []:
  109. print "Usage: dulwich dump-index FILENAME"
  110. sys.exit(1)
  111. filename = args[0]
  112. idx = Index(filename)
  113. for o in idx:
  114. print o, idx[o]
  115. def cmd_init(args):
  116. opts, args = getopt(args, "", ["bare"])
  117. opts = dict(opts)
  118. if args == []:
  119. path = os.getcwd()
  120. else:
  121. path = args[0]
  122. porcelain.init(path, bare=("--bare" in opts))
  123. def cmd_clone(args):
  124. opts, args = getopt(args, "", ["bare"])
  125. opts = dict(opts)
  126. if args == []:
  127. print "usage: dulwich clone host:path [PATH]"
  128. sys.exit(1)
  129. source = args.pop(0)
  130. if len(args) > 0:
  131. target = args.pop(0)
  132. else:
  133. target = None
  134. porcelain.clone(source, target, bare=("--bare" in opts))
  135. def cmd_commit(args):
  136. opts, args = getopt(args, "", ["message"])
  137. opts = dict(opts)
  138. porcelain.commit(".", message=opts["--message"])
  139. def cmd_commit_tree(args):
  140. opts, args = getopt(args, "", ["message"])
  141. if args == []:
  142. print "usage: dulwich commit-tree tree"
  143. sys.exit(1)
  144. opts = dict(opts)
  145. porcelain.commit_tree(".", tree=args[0], message=opts["--message"])
  146. def cmd_update_server_info(args):
  147. porcelain.update_server_info(".")
  148. def cmd_symbolic_ref(args):
  149. opts, args = getopt(args, "", ["ref-name", "force"])
  150. if not args:
  151. print "Usage: dulwich symbolic-ref REF_NAME [--force]"
  152. sys.exit(1)
  153. ref_name = args.pop(0)
  154. porcelain.symbolic_ref(".", ref_name=ref_name, force='--force' in args)
  155. def cmd_show(args):
  156. opts, args = getopt(args, "", [])
  157. porcelain.show(".", args)
  158. def cmd_diff_tree(args):
  159. opts, args = getopt(args, "", [])
  160. if len(args) < 2:
  161. print "Usage: dulwich diff-tree OLD-TREE NEW-TREE"
  162. sys.exit(1)
  163. porcelain.diff_tree(".", args[0], args[1])
  164. def cmd_rev_list(args):
  165. opts, args = getopt(args, "", [])
  166. if len(args) < 1:
  167. print 'Usage: dulwich rev-list COMMITID...'
  168. sys.exit(1)
  169. porcelain.rev_list('.', args)
  170. def cmd_tag(args):
  171. opts, args = getopt(args, '', [])
  172. if len(args) < 2:
  173. print 'Usage: dulwich tag NAME'
  174. sys.exit(1)
  175. porcelain.tag('.', args[0])
  176. def cmd_reset(args):
  177. opts, args = getopt(args, "", ["hard", "soft", "mixed"])
  178. opts = dict(opts)
  179. mode = ""
  180. if "--hard" in opts:
  181. mode = "hard"
  182. elif "--soft" in opts:
  183. mode = "soft"
  184. elif "--mixed" in opts:
  185. mode = "mixed"
  186. porcelain.reset('.', mode=mode, *args)
  187. commands = {
  188. "add": cmd_add,
  189. "archive": cmd_archive,
  190. "clone": cmd_clone,
  191. "commit": cmd_commit,
  192. "commit-tree": cmd_commit_tree,
  193. "diff": cmd_diff,
  194. "diff-tree": cmd_diff_tree,
  195. "dump-pack": cmd_dump_pack,
  196. "dump-index": cmd_dump_index,
  197. "fetch-pack": cmd_fetch_pack,
  198. "fetch": cmd_fetch,
  199. "init": cmd_init,
  200. "log": cmd_log,
  201. "reset": cmd_reset,
  202. "rev-list": cmd_rev_list,
  203. "rm": cmd_rm,
  204. "show": cmd_show,
  205. "symbolic-ref": cmd_symbolic_ref,
  206. "tag": cmd_tag,
  207. "update-server-info": cmd_update_server_info,
  208. }
  209. if len(sys.argv) < 2:
  210. print "Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys()))
  211. sys.exit(1)
  212. cmd = sys.argv[1]
  213. if not cmd in commands:
  214. print "No such subcommand: %s" % cmd
  215. sys.exit(1)
  216. commands[cmd](sys.argv[2:])