dulwich 7.9 KB

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