dulwich 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  8. # General Public License as public by the Free Software Foundation; version 2.0
  9. # or (at your option) any later version. You can redistribute it and/or
  10. # modify it under the terms of either of these two licenses.
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. # You should have received a copy of the licenses; if not, see
  19. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  20. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  21. # License, Version 2.0.
  22. #
  23. """Simple command-line interface to Dulwich>
  24. This is a very simple command-line wrapper for Dulwich. It is by
  25. no means intended to be a full-blown Git command-line interface but just
  26. a way to test Dulwich.
  27. """
  28. import os
  29. import sys
  30. from getopt import getopt
  31. import optparse
  32. import signal
  33. def signal_int(signal, frame):
  34. sys.exit(1)
  35. signal.signal(signal.SIGINT, signal_int)
  36. from dulwich import porcelain
  37. from dulwich.client import get_transport_and_path
  38. from dulwich.errors import ApplyDeltaError
  39. from dulwich.index import Index
  40. from dulwich.pack import Pack, sha_to_hex
  41. from dulwich.patch import write_tree_diff
  42. from dulwich.repo import Repo
  43. def cmd_archive(args):
  44. opts, args = getopt(args, "", [])
  45. client, path = get_transport_and_path(args.pop(0))
  46. location = args.pop(0)
  47. committish = args.pop(0)
  48. porcelain.archive(location, committish, outstream=sys.stdout,
  49. errstream=sys.stderr)
  50. def cmd_add(args):
  51. opts, args = getopt(args, "", [])
  52. porcelain.add(".", paths=args)
  53. def cmd_rm(args):
  54. opts, args = getopt(args, "", [])
  55. porcelain.rm(".", paths=args)
  56. def cmd_fetch_pack(args):
  57. opts, args = getopt(args, "", ["all"])
  58. opts = dict(opts)
  59. client, path = get_transport_and_path(args.pop(0))
  60. r = Repo(".")
  61. if "--all" in opts:
  62. determine_wants = r.object_store.determine_wants_all
  63. else:
  64. determine_wants = lambda x: [y for y in args if not y in r.object_store]
  65. client.fetch(path, r, determine_wants)
  66. def cmd_fetch(args):
  67. opts, args = getopt(args, "", [])
  68. opts = dict(opts)
  69. client, path = get_transport_and_path(args.pop(0))
  70. r = Repo(".")
  71. if "--all" in opts:
  72. determine_wants = r.object_store.determine_wants_all
  73. refs = client.fetch(path, r, progress=sys.stdout.write)
  74. print("Remote refs:")
  75. for item in refs.items():
  76. print("%s -> %s" % item)
  77. def cmd_log(args):
  78. opts, args = getopt(args, "", [])
  79. porcelain.log(".", paths=args, outstream=sys.stdout)
  80. def cmd_diff(args):
  81. opts, args = getopt(args, "", [])
  82. if args == []:
  83. print("Usage: dulwich diff COMMITID")
  84. sys.exit(1)
  85. r = Repo(".")
  86. commit_id = args[0]
  87. commit = r[commit_id]
  88. parent_commit = r[commit.parents[0]]
  89. write_tree_diff(sys.stdout, r.object_store, parent_commit.tree, commit.tree)
  90. def cmd_dump_pack(args):
  91. opts, args = getopt(args, "", [])
  92. if args == []:
  93. print("Usage: dulwich dump-pack FILENAME")
  94. sys.exit(1)
  95. basename, _ = os.path.splitext(args[0])
  96. x = Pack(basename)
  97. print("Object names checksum: %s" % x.name())
  98. print("Checksum: %s" % sha_to_hex(x.get_stored_checksum()))
  99. if not x.check():
  100. print("CHECKSUM DOES NOT MATCH")
  101. print("Length: %d" % len(x))
  102. for name in x:
  103. try:
  104. print("\t%s" % x[name])
  105. except KeyError as k:
  106. print("\t%s: Unable to resolve base %s" % (name, k))
  107. except ApplyDeltaError as e:
  108. print("\t%s: Unable to apply delta: %r" % (name, e))
  109. def cmd_dump_index(args):
  110. opts, args = getopt(args, "", [])
  111. if args == []:
  112. print("Usage: dulwich dump-index FILENAME")
  113. sys.exit(1)
  114. filename = args[0]
  115. idx = Index(filename)
  116. for o in idx:
  117. print(o, idx[o])
  118. def cmd_init(args):
  119. opts, args = getopt(args, "", ["bare"])
  120. opts = dict(opts)
  121. if args == []:
  122. path = os.getcwd()
  123. else:
  124. path = args[0]
  125. porcelain.init(path, bare=("--bare" in opts))
  126. def cmd_clone(args):
  127. opts, args = getopt(args, "", ["bare"])
  128. opts = dict(opts)
  129. if args == []:
  130. print("usage: dulwich clone host:path [PATH]")
  131. sys.exit(1)
  132. source = args.pop(0)
  133. if len(args) > 0:
  134. target = args.pop(0)
  135. else:
  136. target = None
  137. porcelain.clone(source, target, bare=("--bare" in opts))
  138. def cmd_commit(args):
  139. opts, args = getopt(args, "", ["message"])
  140. opts = dict(opts)
  141. porcelain.commit(".", message=opts["--message"])
  142. def cmd_commit_tree(args):
  143. opts, args = getopt(args, "", ["message"])
  144. if args == []:
  145. print("usage: dulwich commit-tree tree")
  146. sys.exit(1)
  147. opts = dict(opts)
  148. porcelain.commit_tree(".", tree=args[0], message=opts["--message"])
  149. def cmd_update_server_info(args):
  150. porcelain.update_server_info(".")
  151. def cmd_symbolic_ref(args):
  152. opts, args = getopt(args, "", ["ref-name", "force"])
  153. if not args:
  154. print("Usage: dulwich symbolic-ref REF_NAME [--force]")
  155. sys.exit(1)
  156. ref_name = args.pop(0)
  157. porcelain.symbolic_ref(".", ref_name=ref_name, force='--force' in args)
  158. def cmd_show(args):
  159. opts, args = getopt(args, "", [])
  160. porcelain.show(".", args)
  161. def cmd_diff_tree(args):
  162. opts, args = getopt(args, "", [])
  163. if len(args) < 2:
  164. print("Usage: dulwich diff-tree OLD-TREE NEW-TREE")
  165. sys.exit(1)
  166. porcelain.diff_tree(".", args[0], args[1])
  167. def cmd_rev_list(args):
  168. opts, args = getopt(args, "", [])
  169. if len(args) < 1:
  170. print('Usage: dulwich rev-list COMMITID...')
  171. sys.exit(1)
  172. porcelain.rev_list('.', args)
  173. def cmd_tag(args):
  174. opts, args = getopt(args, '', [])
  175. if len(args) < 2:
  176. print('Usage: dulwich tag NAME')
  177. sys.exit(1)
  178. porcelain.tag('.', args[0])
  179. def cmd_repack(args):
  180. opts, args = getopt(args, "", [])
  181. opts = dict(opts)
  182. porcelain.repack('.')
  183. def cmd_reset(args):
  184. opts, args = getopt(args, "", ["hard", "soft", "mixed"])
  185. opts = dict(opts)
  186. mode = ""
  187. if "--hard" in opts:
  188. mode = "hard"
  189. elif "--soft" in opts:
  190. mode = "soft"
  191. elif "--mixed" in opts:
  192. mode = "mixed"
  193. porcelain.reset('.', mode=mode, *args)
  194. def cmd_daemon(args):
  195. from dulwich import log_utils
  196. from dulwich.protocol import TCP_GIT_PORT
  197. parser = optparse.OptionParser()
  198. parser.add_option("-l", "--listen_address", dest="listen_address",
  199. default="localhost",
  200. help="Binding IP address.")
  201. parser.add_option("-p", "--port", dest="port", type=int,
  202. default=TCP_GIT_PORT,
  203. help="Binding TCP port.")
  204. options, args = parser.parse_args(args)
  205. log_utils.default_logging_config()
  206. if len(args) >= 1:
  207. gitdir = args[0]
  208. else:
  209. gitdir = '.'
  210. from dulwich import porcelain
  211. porcelain.daemon(gitdir, address=options.listen_address,
  212. port=options.port)
  213. def cmd_web_daemon(args):
  214. from dulwich import log_utils
  215. parser = optparse.OptionParser()
  216. parser.add_option("-l", "--listen_address", dest="listen_address",
  217. default="",
  218. help="Binding IP address.")
  219. parser.add_option("-p", "--port", dest="port", type=int,
  220. default=8000,
  221. help="Binding TCP port.")
  222. options, args = parser.parse_args(args)
  223. log_utils.default_logging_config()
  224. if len(args) >= 1:
  225. gitdir = args[0]
  226. else:
  227. gitdir = '.'
  228. from dulwich import porcelain
  229. porcelain.web_daemon(gitdir, address=options.listen_address,
  230. port=options.port)
  231. def cmd_receive_pack(args):
  232. parser = optparse.OptionParser()
  233. options, args = parser.parse_args(args)
  234. if len(args) >= 1:
  235. gitdir = args[0]
  236. else:
  237. gitdir = '.'
  238. porcelain.receive_pack(gitdir)
  239. def cmd_upload_pack(args):
  240. parser = optparse.OptionParser()
  241. options, args = parser.parse_args(args)
  242. if len(args) >= 1:
  243. gitdir = args[0]
  244. else:
  245. gitdir = '.'
  246. porcelain.upload_pack(gitdir)
  247. def cmd_status(args):
  248. parser = optparse.OptionParser()
  249. options, args = parser.parse_args(args)
  250. if len(args) >= 1:
  251. gitdir = args[0]
  252. else:
  253. gitdir = '.'
  254. status = porcelain.status(gitdir)
  255. if status.staged:
  256. sys.stdout.write("Changes to be committed:\n\n")
  257. for kind, names in status.staged.items():
  258. for name in names:
  259. sys.stdout.write("\t%s: %s\n" % (kind, name))
  260. sys.stdout.write("\n")
  261. if status.unstaged:
  262. sys.stdout.write("Changes not staged for commit:\n\n")
  263. for name in status.unstaged:
  264. sys.stdout.write("\t%s\n" %
  265. name.decode(sys.getfilesystemencoding()))
  266. sys.stdout.write("\n")
  267. if status.untracked:
  268. sys.stdout.write("Untracked files:\n\n")
  269. for name in status.untracked:
  270. sys.stdout.write("\t%s\n" % name)
  271. sys.stdout.write("\n")
  272. def cmd_ls_remote(args):
  273. opts, args = getopt(args, '', [])
  274. if len(args) < 1:
  275. print('Usage: dulwich ls-remote URL')
  276. sys.exit(1)
  277. refs = porcelain.ls_remote(args[0])
  278. for ref in sorted(refs):
  279. sys.stdout.write("%s\t%s\n" % (ref, refs[ref]))
  280. def cmd_ls_tree(args):
  281. parser = optparse.OptionParser()
  282. parser.add_option("-r", "--recursive", action="store_true",
  283. help="Recusively list tree contents.")
  284. parser.add_option("--name-only", action="store_true",
  285. help="Only display name.")
  286. options, args = parser.parse_args(args)
  287. try:
  288. treeish = args.pop(0)
  289. except IndexError:
  290. treeish = None
  291. porcelain.ls_tree(
  292. '.', treeish, outstream=sys.stdout, recursive=options.recursive,
  293. name_only=options.name_only)
  294. def cmd_pack_objects(args):
  295. opts, args = getopt(args, '', ['stdout'])
  296. opts = dict(opts)
  297. if len(args) < 1 and not '--stdout' in args:
  298. print('Usage: dulwich pack-objects basename')
  299. sys.exit(1)
  300. object_ids = [l.strip() for l in sys.stdin.readlines()]
  301. basename = args[0]
  302. if '--stdout' in opts:
  303. packf = getattr(sys.stdout, 'buffer', sys.stdout)
  304. idxf = None
  305. close = []
  306. else:
  307. packf = open(basename + '.pack', 'w')
  308. idxf = open(basename + '.idx', 'w')
  309. close = [packf, idxf]
  310. porcelain.pack_objects('.', object_ids, packf, idxf)
  311. for f in close:
  312. f.close()
  313. def cmd_help(args):
  314. parser = optparse.OptionParser()
  315. parser.add_option("-a", "--all", dest="all",
  316. action="store_true",
  317. help="List all commands.")
  318. options, args = parser.parse_args(args)
  319. if options.all:
  320. print('Available commands:')
  321. for cmd in sorted(commands):
  322. print(' %s' % cmd)
  323. else:
  324. print("""\
  325. The dulwich command line tool is currently a very basic frontend for the
  326. Dulwich python module. For full functionality, please see the API reference.
  327. For a list of supported commands, see 'dulwich help -a'.
  328. """)
  329. commands = {
  330. "add": cmd_add,
  331. "archive": cmd_archive,
  332. "clone": cmd_clone,
  333. "commit": cmd_commit,
  334. "commit-tree": cmd_commit_tree,
  335. "daemon": cmd_daemon,
  336. "diff": cmd_diff,
  337. "diff-tree": cmd_diff_tree,
  338. "dump-pack": cmd_dump_pack,
  339. "dump-index": cmd_dump_index,
  340. "fetch-pack": cmd_fetch_pack,
  341. "fetch": cmd_fetch,
  342. "help": cmd_help,
  343. "init": cmd_init,
  344. "log": cmd_log,
  345. "ls-remote": cmd_ls_remote,
  346. "ls-tree": cmd_ls_tree,
  347. "pack-objects": cmd_pack_objects,
  348. "receive-pack": cmd_receive_pack,
  349. "repack": cmd_repack,
  350. "reset": cmd_reset,
  351. "rev-list": cmd_rev_list,
  352. "rm": cmd_rm,
  353. "show": cmd_show,
  354. "status": cmd_status,
  355. "symbolic-ref": cmd_symbolic_ref,
  356. "tag": cmd_tag,
  357. "update-server-info": cmd_update_server_info,
  358. "upload-pack": cmd_upload_pack,
  359. "web-daemon": cmd_web_daemon,
  360. }
  361. if len(sys.argv) < 2:
  362. print("Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys())))
  363. sys.exit(1)
  364. cmd = sys.argv[1]
  365. if not cmd in commands:
  366. print("No such subcommand: %s" % cmd)
  367. sys.exit(1)
  368. commands[cmd](sys.argv[2:])