dulwich 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. import signal
  31. def signal_int(signal, frame):
  32. sys.exit(1)
  33. signal.signal(signal.SIGINT, signal_int)
  34. from dulwich import porcelain
  35. from dulwich.client import get_transport_and_path
  36. from dulwich.errors import ApplyDeltaError
  37. from dulwich.index import Index
  38. from dulwich.pack import Pack, sha_to_hex
  39. from dulwich.patch import write_tree_diff
  40. from dulwich.repo import Repo
  41. def cmd_archive(args):
  42. opts, args = getopt(args, "", [])
  43. client, path = get_transport_and_path(args.pop(0))
  44. location = args.pop(0)
  45. committish = args.pop(0)
  46. porcelain.archive(location, committish, outstream=sys.stdout,
  47. errstream=sys.stderr)
  48. def cmd_add(args):
  49. opts, args = getopt(args, "", [])
  50. porcelain.add(".", paths=args)
  51. def cmd_rm(args):
  52. opts, args = getopt(args, "", [])
  53. porcelain.rm(".", paths=args)
  54. def cmd_fetch_pack(args):
  55. opts, args = getopt(args, "", ["all"])
  56. opts = dict(opts)
  57. client, path = get_transport_and_path(args.pop(0))
  58. r = Repo(".")
  59. if "--all" in opts:
  60. determine_wants = r.object_store.determine_wants_all
  61. else:
  62. determine_wants = lambda x: [y for y in args if not y in r.object_store]
  63. client.fetch(path, r, determine_wants)
  64. def cmd_fetch(args):
  65. opts, args = getopt(args, "", [])
  66. opts = dict(opts)
  67. client, path = get_transport_and_path(args.pop(0))
  68. r = Repo(".")
  69. if "--all" in opts:
  70. determine_wants = r.object_store.determine_wants_all
  71. refs = client.fetch(path, r, progress=sys.stdout.write)
  72. print("Remote refs:")
  73. for item in refs.items():
  74. print("%s -> %s" % item)
  75. def cmd_log(args):
  76. opts, args = getopt(args, "", [])
  77. if len(args) > 0:
  78. path = args.pop(0)
  79. else:
  80. path = "."
  81. porcelain.log(repo=path, outstream=sys.stdout)
  82. def cmd_diff(args):
  83. opts, args = getopt(args, "", [])
  84. if args == []:
  85. print("Usage: dulwich diff COMMITID")
  86. sys.exit(1)
  87. r = Repo(".")
  88. commit_id = args[0]
  89. commit = r[commit_id]
  90. parent_commit = r[commit.parents[0]]
  91. write_tree_diff(sys.stdout, r.object_store, parent_commit.tree, commit.tree)
  92. def cmd_dump_pack(args):
  93. opts, args = getopt(args, "", [])
  94. if args == []:
  95. print("Usage: dulwich dump-pack FILENAME")
  96. sys.exit(1)
  97. basename, _ = os.path.splitext(args[0])
  98. x = Pack(basename)
  99. print("Object names checksum: %s" % x.name())
  100. print("Checksum: %s" % sha_to_hex(x.get_stored_checksum()))
  101. if not x.check():
  102. print("CHECKSUM DOES NOT MATCH")
  103. print("Length: %d" % len(x))
  104. for name in x:
  105. try:
  106. print("\t%s" % x[name])
  107. except KeyError as k:
  108. print("\t%s: Unable to resolve base %s" % (name, k))
  109. except ApplyDeltaError as e:
  110. print("\t%s: Unable to apply delta: %r" % (name, e))
  111. def cmd_dump_index(args):
  112. opts, args = getopt(args, "", [])
  113. if args == []:
  114. print("Usage: dulwich dump-index FILENAME")
  115. sys.exit(1)
  116. filename = args[0]
  117. idx = Index(filename)
  118. for o in idx:
  119. print(o, idx[o])
  120. def cmd_init(args):
  121. opts, args = getopt(args, "", ["bare"])
  122. opts = dict(opts)
  123. if args == []:
  124. path = os.getcwd()
  125. else:
  126. path = args[0]
  127. porcelain.init(path, bare=("--bare" in opts))
  128. def cmd_clone(args):
  129. opts, args = getopt(args, "", ["bare"])
  130. opts = dict(opts)
  131. if args == []:
  132. print("usage: dulwich clone host:path [PATH]")
  133. sys.exit(1)
  134. source = args.pop(0)
  135. if len(args) > 0:
  136. target = args.pop(0)
  137. else:
  138. target = None
  139. porcelain.clone(source, target, bare=("--bare" in opts))
  140. def cmd_commit(args):
  141. opts, args = getopt(args, "", ["message"])
  142. opts = dict(opts)
  143. porcelain.commit(".", message=opts["--message"])
  144. def cmd_commit_tree(args):
  145. opts, args = getopt(args, "", ["message"])
  146. if args == []:
  147. print("usage: dulwich commit-tree tree")
  148. sys.exit(1)
  149. opts = dict(opts)
  150. porcelain.commit_tree(".", tree=args[0], message=opts["--message"])
  151. def cmd_update_server_info(args):
  152. porcelain.update_server_info(".")
  153. def cmd_symbolic_ref(args):
  154. opts, args = getopt(args, "", ["ref-name", "force"])
  155. if not args:
  156. print("Usage: dulwich symbolic-ref REF_NAME [--force]")
  157. sys.exit(1)
  158. ref_name = args.pop(0)
  159. porcelain.symbolic_ref(".", ref_name=ref_name, force='--force' in args)
  160. def cmd_show(args):
  161. opts, args = getopt(args, "", [])
  162. porcelain.show(".", args)
  163. def cmd_diff_tree(args):
  164. opts, args = getopt(args, "", [])
  165. if len(args) < 2:
  166. print("Usage: dulwich diff-tree OLD-TREE NEW-TREE")
  167. sys.exit(1)
  168. porcelain.diff_tree(".", args[0], args[1])
  169. def cmd_rev_list(args):
  170. opts, args = getopt(args, "", [])
  171. if len(args) < 1:
  172. print('Usage: dulwich rev-list COMMITID...')
  173. sys.exit(1)
  174. porcelain.rev_list('.', args)
  175. def cmd_tag(args):
  176. opts, args = getopt(args, '', [])
  177. if len(args) < 2:
  178. print('Usage: dulwich tag NAME')
  179. sys.exit(1)
  180. porcelain.tag('.', args[0])
  181. def cmd_repack(args):
  182. opts, args = getopt(args, "", [])
  183. opts = dict(opts)
  184. porcelain.repack('.')
  185. def cmd_reset(args):
  186. opts, args = getopt(args, "", ["hard", "soft", "mixed"])
  187. opts = dict(opts)
  188. mode = ""
  189. if "--hard" in opts:
  190. mode = "hard"
  191. elif "--soft" in opts:
  192. mode = "soft"
  193. elif "--mixed" in opts:
  194. mode = "mixed"
  195. porcelain.reset('.', mode=mode, *args)
  196. def cmd_daemon(args):
  197. from dulwich import log_utils
  198. from dulwich.protocol import TCP_GIT_PORT
  199. parser = optparse.OptionParser()
  200. parser.add_option("-l", "--listen_address", dest="listen_address",
  201. default="localhost",
  202. help="Binding IP address.")
  203. parser.add_option("-p", "--port", dest="port", type=int,
  204. default=TCP_GIT_PORT,
  205. help="Binding TCP port.")
  206. options, args = parser.parse_args(args)
  207. log_utils.default_logging_config()
  208. if len(args) >= 1:
  209. gitdir = args[0]
  210. else:
  211. gitdir = '.'
  212. from dulwich import porcelain
  213. porcelain.daemon(gitdir, address=options.listen_address,
  214. port=options.port)
  215. def cmd_web_daemon(args):
  216. from dulwich import log_utils
  217. parser = optparse.OptionParser()
  218. parser.add_option("-l", "--listen_address", dest="listen_address",
  219. default="",
  220. help="Binding IP address.")
  221. parser.add_option("-p", "--port", dest="port", type=int,
  222. default=8000,
  223. help="Binding TCP port.")
  224. options, args = parser.parse_args(args)
  225. log_utils.default_logging_config()
  226. if len(args) >= 1:
  227. gitdir = args[0]
  228. else:
  229. gitdir = '.'
  230. from dulwich import porcelain
  231. porcelain.web_daemon(gitdir, address=options.listen_address,
  232. port=options.port)
  233. def cmd_receive_pack(args):
  234. parser = optparse.OptionParser()
  235. options, args = parser.parse_args(args)
  236. if len(args) >= 1:
  237. gitdir = args[0]
  238. else:
  239. gitdir = '.'
  240. porcelain.receive_pack(gitdir)
  241. def cmd_upload_pack(args):
  242. parser = optparse.OptionParser()
  243. options, args = parser.parse_args(args)
  244. if len(args) >= 1:
  245. gitdir = args[0]
  246. else:
  247. gitdir = '.'
  248. porcelain.upload_pack(gitdir)
  249. def cmd_status(args):
  250. parser = optparse.OptionParser()
  251. options, args = parser.parse_args(args)
  252. if len(args) >= 1:
  253. gitdir = args[0]
  254. else:
  255. gitdir = '.'
  256. status = porcelain.status(gitdir)
  257. if status.staged:
  258. sys.stdout.write("Changes to be committed:\n\n")
  259. for kind, names in status.staged.items():
  260. for name in names:
  261. sys.stdout.write("\t%s: %s\n" % (kind, name))
  262. sys.stdout.write("\n")
  263. if status.unstaged:
  264. sys.stdout.write("Changes not staged for commit:\n\n")
  265. for name in status.unstaged:
  266. sys.stdout.write("\t%s\n" %
  267. name.decode(sys.getfilesystemencoding()))
  268. sys.stdout.write("\n")
  269. if status.untracked:
  270. sys.stdout.write("Untracked files:\n\n")
  271. for name in status.untracked:
  272. sys.stdout.write("\t%s\n" % name)
  273. sys.stdout.write("\n")
  274. def cmd_ls_remote(args):
  275. opts, args = getopt(args, '', [])
  276. if len(args) < 1:
  277. print('Usage: dulwich ls-remote URL')
  278. sys.exit(1)
  279. refs = porcelain.ls_remote(args[0])
  280. for ref in sorted(refs):
  281. sys.stdout.write("%s\t%s\n" % (ref, refs[ref]))
  282. def cmd_pack_objects(args):
  283. opts, args = getopt(args, '', ['stdout'])
  284. opts = dict(opts)
  285. if len(args) < 1 and not '--stdout' in args:
  286. print('Usage: dulwich pack-objects basename')
  287. sys.exit(1)
  288. object_ids = [l.strip() for l in sys.stdin.readlines()]
  289. basename = args[0]
  290. if '--stdout' in opts:
  291. packf = getattr(sys.stdout, 'buffer', sys.stdout)
  292. idxf = None
  293. close = []
  294. else:
  295. packf = open(basename + '.pack', 'w')
  296. idxf = open(basename + '.idx', 'w')
  297. close = [packf, idxf]
  298. porcelain.pack_objects('.', object_ids, packf, idxf)
  299. for f in close:
  300. f.close()
  301. commands = {
  302. "add": cmd_add,
  303. "archive": cmd_archive,
  304. "clone": cmd_clone,
  305. "commit": cmd_commit,
  306. "commit-tree": cmd_commit_tree,
  307. "daemon": cmd_daemon,
  308. "diff": cmd_diff,
  309. "diff-tree": cmd_diff_tree,
  310. "dump-pack": cmd_dump_pack,
  311. "dump-index": cmd_dump_index,
  312. "fetch-pack": cmd_fetch_pack,
  313. "fetch": cmd_fetch,
  314. "init": cmd_init,
  315. "log": cmd_log,
  316. "ls-remote": cmd_ls_remote,
  317. "pack-objects": cmd_pack_objects,
  318. "receive-pack": cmd_receive_pack,
  319. "repack": cmd_repack,
  320. "reset": cmd_reset,
  321. "rev-list": cmd_rev_list,
  322. "rm": cmd_rm,
  323. "show": cmd_show,
  324. "status": cmd_status,
  325. "symbolic-ref": cmd_symbolic_ref,
  326. "tag": cmd_tag,
  327. "update-server-info": cmd_update_server_info,
  328. "upload-pack": cmd_upload_pack,
  329. "web-daemon": cmd_web_daemon,
  330. }
  331. if len(sys.argv) < 2:
  332. print("Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys())))
  333. sys.exit(1)
  334. cmd = sys.argv[1]
  335. if not cmd in commands:
  336. print("No such subcommand: %s" % cmd)
  337. sys.exit(1)
  338. commands[cmd](sys.argv[2:])