dulwich 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. #!/usr/bin/python -u
  2. #
  3. # dulwich - Simple command-line interface to Dulwich
  4. # Copyright (C) 2008-2011 Jelmer Vernooij <jelmer@jelmer.uk>
  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. def signal_quit(signal, frame):
  36. import pdb
  37. pdb.set_trace()
  38. if 'DULWICH_PDB' in os.environ:
  39. signal.signal(signal.SIGQUIT, signal_quit)
  40. signal.signal(signal.SIGINT, signal_int)
  41. from dulwich import porcelain
  42. from dulwich.client import get_transport_and_path
  43. from dulwich.errors import ApplyDeltaError
  44. from dulwich.index import Index
  45. from dulwich.pack import Pack, sha_to_hex
  46. from dulwich.patch import write_tree_diff
  47. from dulwich.repo import Repo
  48. class Command(object):
  49. """A Dulwich subcommand."""
  50. def run(self, args):
  51. """Run the command."""
  52. raise NotImplementedError(self.run)
  53. class cmd_archive(Command):
  54. def run(self, args):
  55. parser = optparse.OptionParser()
  56. parser.add_option("--remote", type=str,
  57. help="Retrieve archive from specified remote repo")
  58. options, args = parser.parse_args(args)
  59. committish = args.pop(0)
  60. if options.remote:
  61. client, path = get_transport_and_path(options.remote)
  62. client.archive(path, committish, sys.stdout.write,
  63. write_error=sys.stderr.write)
  64. else:
  65. porcelain.archive('.', committish, outstream=sys.stdout,
  66. errstream=sys.stderr)
  67. class cmd_add(Command):
  68. def run(self, args):
  69. opts, args = getopt(args, "", [])
  70. porcelain.add(".", paths=args)
  71. class cmd_rm(Command):
  72. def run(self, args):
  73. opts, args = getopt(args, "", [])
  74. porcelain.rm(".", paths=args)
  75. class cmd_fetch_pack(Command):
  76. def run(self, args):
  77. opts, args = getopt(args, "", ["all"])
  78. opts = dict(opts)
  79. client, path = get_transport_and_path(args.pop(0))
  80. r = Repo(".")
  81. if "--all" in opts:
  82. determine_wants = r.object_store.determine_wants_all
  83. else:
  84. determine_wants = lambda x: [y for y in args if not y in r.object_store]
  85. client.fetch(path, r, determine_wants)
  86. class cmd_fetch(Command):
  87. def run(self, args):
  88. opts, args = getopt(args, "", [])
  89. opts = dict(opts)
  90. client, path = get_transport_and_path(args.pop(0))
  91. r = Repo(".")
  92. if "--all" in opts:
  93. determine_wants = r.object_store.determine_wants_all
  94. refs = client.fetch(path, r, progress=sys.stdout.write)
  95. print("Remote refs:")
  96. for item in refs.items():
  97. print("%s -> %s" % item)
  98. class cmd_fsck(Command):
  99. def run(self, args):
  100. opts, args = getopt(args, "", [])
  101. opts = dict(opts)
  102. for (obj, msg) in porcelain.fsck('.'):
  103. print("%s: %s" % (obj, msg))
  104. class cmd_log(Command):
  105. def run(self, args):
  106. parser = optparse.OptionParser()
  107. parser.add_option("--reverse", dest="reverse", action="store_true",
  108. help="Reverse order in which entries are printed")
  109. parser.add_option("--name-status", dest="name_status", action="store_true",
  110. help="Print name/status for each changed file")
  111. options, args = parser.parse_args(args)
  112. porcelain.log(".", paths=args, reverse=options.reverse,
  113. name_status=options.name_status,
  114. outstream=sys.stdout)
  115. class cmd_diff(Command):
  116. def run(self, args):
  117. opts, args = getopt(args, "", [])
  118. if args == []:
  119. print("Usage: dulwich diff COMMITID")
  120. sys.exit(1)
  121. r = Repo(".")
  122. commit_id = args[0]
  123. commit = r[commit_id]
  124. parent_commit = r[commit.parents[0]]
  125. write_tree_diff(sys.stdout, r.object_store, parent_commit.tree, commit.tree)
  126. class cmd_dump_pack(Command):
  127. def run(self, args):
  128. opts, args = getopt(args, "", [])
  129. if args == []:
  130. print("Usage: dulwich dump-pack FILENAME")
  131. sys.exit(1)
  132. basename, _ = os.path.splitext(args[0])
  133. x = Pack(basename)
  134. print("Object names checksum: %s" % x.name())
  135. print("Checksum: %s" % sha_to_hex(x.get_stored_checksum()))
  136. if not x.check():
  137. print("CHECKSUM DOES NOT MATCH")
  138. print("Length: %d" % len(x))
  139. for name in x:
  140. try:
  141. print("\t%s" % x[name])
  142. except KeyError as k:
  143. print("\t%s: Unable to resolve base %s" % (name, k))
  144. except ApplyDeltaError as e:
  145. print("\t%s: Unable to apply delta: %r" % (name, e))
  146. class cmd_dump_index(Command):
  147. def run(self, args):
  148. opts, args = getopt(args, "", [])
  149. if args == []:
  150. print("Usage: dulwich dump-index FILENAME")
  151. sys.exit(1)
  152. filename = args[0]
  153. idx = Index(filename)
  154. for o in idx:
  155. print(o, idx[o])
  156. class cmd_init(Command):
  157. def run(self, args):
  158. opts, args = getopt(args, "", ["bare"])
  159. opts = dict(opts)
  160. if args == []:
  161. path = os.getcwd()
  162. else:
  163. path = args[0]
  164. porcelain.init(path, bare=("--bare" in opts))
  165. class cmd_clone(Command):
  166. def run(self, args):
  167. parser = optparse.OptionParser()
  168. parser.add_option("--bare", dest="bare",
  169. help="Whether to create a bare repository.",
  170. action="store_true")
  171. parser.add_option("--depth", dest="depth",
  172. type=int, help="Depth at which to fetch")
  173. options, args = parser.parse_args(args)
  174. if args == []:
  175. print("usage: dulwich clone host:path [PATH]")
  176. sys.exit(1)
  177. source = args.pop(0)
  178. if len(args) > 0:
  179. target = args.pop(0)
  180. else:
  181. target = None
  182. porcelain.clone(source, target, bare=options.bare, depth=options.depth)
  183. class cmd_commit(Command):
  184. def run(self, args):
  185. opts, args = getopt(args, "", ["message"])
  186. opts = dict(opts)
  187. porcelain.commit(".", message=opts["--message"])
  188. class cmd_commit_tree(Command):
  189. def run(self, args):
  190. opts, args = getopt(args, "", ["message"])
  191. if args == []:
  192. print("usage: dulwich commit-tree tree")
  193. sys.exit(1)
  194. opts = dict(opts)
  195. porcelain.commit_tree(".", tree=args[0], message=opts["--message"])
  196. class cmd_update_server_info(Command):
  197. def run(self, args):
  198. porcelain.update_server_info(".")
  199. class cmd_symbolic_ref(Command):
  200. def run(self, args):
  201. opts, args = getopt(args, "", ["ref-name", "force"])
  202. if not args:
  203. print("Usage: dulwich symbolic-ref REF_NAME [--force]")
  204. sys.exit(1)
  205. ref_name = args.pop(0)
  206. porcelain.symbolic_ref(".", ref_name=ref_name, force='--force' in args)
  207. class cmd_show(Command):
  208. def run(self, args):
  209. opts, args = getopt(args, "", [])
  210. porcelain.show(".", args)
  211. class cmd_diff_tree(Command):
  212. def run(self, args):
  213. opts, args = getopt(args, "", [])
  214. if len(args) < 2:
  215. print("Usage: dulwich diff-tree OLD-TREE NEW-TREE")
  216. sys.exit(1)
  217. porcelain.diff_tree(".", args[0], args[1])
  218. class cmd_rev_list(Command):
  219. def run(self, args):
  220. opts, args = getopt(args, "", [])
  221. if len(args) < 1:
  222. print('Usage: dulwich rev-list COMMITID...')
  223. sys.exit(1)
  224. porcelain.rev_list('.', args)
  225. class cmd_tag(Command):
  226. def run(self, args):
  227. parser = optparse.OptionParser()
  228. parser.add_option("-a", "--annotated", help="Create an annotated tag.", action="store_true")
  229. parser.add_option("-s", "--sign", help="Sign the annotated tag.", action="store_true")
  230. options, args = parser.parse_args(args)
  231. porcelain.tag_create(
  232. '.', args[0], annotated=options.annotated,
  233. sign=options.sign)
  234. class cmd_repack(Command):
  235. def run(self, args):
  236. opts, args = getopt(args, "", [])
  237. opts = dict(opts)
  238. porcelain.repack('.')
  239. class cmd_reset(Command):
  240. def run(self, args):
  241. opts, args = getopt(args, "", ["hard", "soft", "mixed"])
  242. opts = dict(opts)
  243. mode = ""
  244. if "--hard" in opts:
  245. mode = "hard"
  246. elif "--soft" in opts:
  247. mode = "soft"
  248. elif "--mixed" in opts:
  249. mode = "mixed"
  250. porcelain.reset('.', mode=mode, *args)
  251. class cmd_daemon(Command):
  252. def run(self, args):
  253. from dulwich import log_utils
  254. from dulwich.protocol import TCP_GIT_PORT
  255. parser = optparse.OptionParser()
  256. parser.add_option("-l", "--listen_address", dest="listen_address",
  257. default="localhost",
  258. help="Binding IP address.")
  259. parser.add_option("-p", "--port", dest="port", type=int,
  260. default=TCP_GIT_PORT,
  261. help="Binding TCP port.")
  262. options, args = parser.parse_args(args)
  263. log_utils.default_logging_config()
  264. if len(args) >= 1:
  265. gitdir = args[0]
  266. else:
  267. gitdir = '.'
  268. from dulwich import porcelain
  269. porcelain.daemon(gitdir, address=options.listen_address,
  270. port=options.port)
  271. class cmd_web_daemon(Command):
  272. def run(self, args):
  273. from dulwich import log_utils
  274. parser = optparse.OptionParser()
  275. parser.add_option("-l", "--listen_address", dest="listen_address",
  276. default="",
  277. help="Binding IP address.")
  278. parser.add_option("-p", "--port", dest="port", type=int,
  279. default=8000,
  280. help="Binding TCP port.")
  281. options, args = parser.parse_args(args)
  282. log_utils.default_logging_config()
  283. if len(args) >= 1:
  284. gitdir = args[0]
  285. else:
  286. gitdir = '.'
  287. from dulwich import porcelain
  288. porcelain.web_daemon(gitdir, address=options.listen_address,
  289. port=options.port)
  290. class cmd_write_tree(Command):
  291. def run(self, args):
  292. parser = optparse.OptionParser()
  293. options, args = parser.parse_args(args)
  294. sys.stdout.write('%s\n' % porcelain.write_tree('.'))
  295. class cmd_receive_pack(Command):
  296. def run(self, args):
  297. parser = optparse.OptionParser()
  298. options, args = parser.parse_args(args)
  299. if len(args) >= 1:
  300. gitdir = args[0]
  301. else:
  302. gitdir = '.'
  303. porcelain.receive_pack(gitdir)
  304. class cmd_upload_pack(Command):
  305. def run(self, args):
  306. parser = optparse.OptionParser()
  307. options, args = parser.parse_args(args)
  308. if len(args) >= 1:
  309. gitdir = args[0]
  310. else:
  311. gitdir = '.'
  312. porcelain.upload_pack(gitdir)
  313. class cmd_status(Command):
  314. def run(self, args):
  315. parser = optparse.OptionParser()
  316. options, args = parser.parse_args(args)
  317. if len(args) >= 1:
  318. gitdir = args[0]
  319. else:
  320. gitdir = '.'
  321. status = porcelain.status(gitdir)
  322. if any(names for (kind, names) in status.staged.items()):
  323. sys.stdout.write("Changes to be committed:\n\n")
  324. for kind, names in status.staged.items():
  325. for name in names:
  326. sys.stdout.write("\t%s: %s\n" % (
  327. kind, name.decode(sys.getfilesystemencoding())))
  328. sys.stdout.write("\n")
  329. if status.unstaged:
  330. sys.stdout.write("Changes not staged for commit:\n\n")
  331. for name in status.unstaged:
  332. sys.stdout.write("\t%s\n" %
  333. name.decode(sys.getfilesystemencoding()))
  334. sys.stdout.write("\n")
  335. if status.untracked:
  336. sys.stdout.write("Untracked files:\n\n")
  337. for name in status.untracked:
  338. sys.stdout.write("\t%s\n" % name)
  339. sys.stdout.write("\n")
  340. class cmd_ls_remote(Command):
  341. def run(self, args):
  342. opts, args = getopt(args, '', [])
  343. if len(args) < 1:
  344. print('Usage: dulwich ls-remote URL')
  345. sys.exit(1)
  346. refs = porcelain.ls_remote(args[0])
  347. for ref in sorted(refs):
  348. sys.stdout.write("%s\t%s\n" % (ref, refs[ref]))
  349. class cmd_ls_tree(Command):
  350. def run(self, args):
  351. parser = optparse.OptionParser()
  352. parser.add_option("-r", "--recursive", action="store_true",
  353. help="Recusively list tree contents.")
  354. parser.add_option("--name-only", action="store_true",
  355. help="Only display name.")
  356. options, args = parser.parse_args(args)
  357. try:
  358. treeish = args.pop(0)
  359. except IndexError:
  360. treeish = None
  361. porcelain.ls_tree(
  362. '.', treeish, outstream=sys.stdout, recursive=options.recursive,
  363. name_only=options.name_only)
  364. class cmd_pack_objects(Command):
  365. def run(self, args):
  366. opts, args = getopt(args, '', ['stdout'])
  367. opts = dict(opts)
  368. if len(args) < 1 and not '--stdout' in args:
  369. print('Usage: dulwich pack-objects basename')
  370. sys.exit(1)
  371. object_ids = [l.strip() for l in sys.stdin.readlines()]
  372. basename = args[0]
  373. if '--stdout' in opts:
  374. packf = getattr(sys.stdout, 'buffer', sys.stdout)
  375. idxf = None
  376. close = []
  377. else:
  378. packf = open(basename + '.pack', 'w')
  379. idxf = open(basename + '.idx', 'w')
  380. close = [packf, idxf]
  381. porcelain.pack_objects('.', object_ids, packf, idxf)
  382. for f in close:
  383. f.close()
  384. class cmd_pull(Command):
  385. def run(self, args):
  386. parser = optparse.OptionParser()
  387. options, args = parser.parse_args(args)
  388. try:
  389. from_location = args[0]
  390. except IndexError:
  391. from_location = None
  392. porcelain.pull('.', from_location)
  393. class cmd_push(Command):
  394. def run(self, args):
  395. parser = optparse.OptionParser()
  396. options, args = parser.parse_args(args)
  397. if len(args) < 2:
  398. print("Usage: dulwich push TO-LOCATION REFSPEC..")
  399. sys.exit(1)
  400. to_location = args[0]
  401. refspecs = args[1:]
  402. porcelain.push('.', to_location, refspecs)
  403. class cmd_remote_add(Command):
  404. def run(self, args):
  405. parser = optparse.OptionParser()
  406. options, args = parser.parse_args(args)
  407. porcelain.remote_add('.', args[0], args[1])
  408. class SuperCommand(Command):
  409. subcommands = {}
  410. def run(self, args):
  411. if not args:
  412. print("Supported subcommands: %s" % ', '.join(self.subcommands.keys()))
  413. return False
  414. cmd = args[0]
  415. try:
  416. cmd_kls = self.subcommands[cmd]
  417. except KeyError:
  418. print('No such subcommand: %s' % args[0])
  419. return False
  420. return cmd_kls().run(args[1:])
  421. class cmd_remote(SuperCommand):
  422. subcommands = {
  423. "add": cmd_remote_add,
  424. }
  425. class cmd_check_ignore(Command):
  426. def run(self, args):
  427. parser = optparse.OptionParser()
  428. options, args = parser.parse_args(args)
  429. ret = 1
  430. for path in porcelain.check_ignore('.', args):
  431. print(path)
  432. ret = 0
  433. return ret
  434. class cmd_check_mailmap(Command):
  435. def run(self, args):
  436. parser = optparse.OptionParser()
  437. options, args = parser.parse_args(args)
  438. for arg in args:
  439. canonical_identity = porcelain.check_mailmap('.', arg)
  440. print(canonical_identity)
  441. class cmd_stash_list(Command):
  442. def run(self, args):
  443. parser = optparse.OptionParser()
  444. options, args = parser.parse_args(args)
  445. for i, entry in porcelain.stash_list('.'):
  446. print("stash@{%d}: %s" % (i, entry.message.rstrip('\n')))
  447. class cmd_stash_push(Command):
  448. def run(self, args):
  449. parser = optparse.OptionParser()
  450. options, args = parser.parse_args(args)
  451. porcelain.stash_push('.')
  452. print("Saved working directory and index state")
  453. class cmd_stash_pop(Command):
  454. def run(self, args):
  455. parser = optparse.OptionParser()
  456. options, args = parser.parse_args(args)
  457. porcelain.stash_pop('.')
  458. print("Restrored working directory and index state")
  459. class cmd_stash(SuperCommand):
  460. subcommands = {
  461. "list": cmd_stash_list,
  462. "pop": cmd_stash_pop,
  463. "push": cmd_stash_push,
  464. }
  465. class cmd_ls_files(Command):
  466. def run(self, args):
  467. parser = optparse.OptionParser()
  468. options, args = parser.parse_args(args)
  469. for name in porcelain.ls_files('.'):
  470. print(name)
  471. class cmd_describe(Command):
  472. def run(self, args):
  473. parser = optparse.OptionParser()
  474. options, args = parser.parse_args(args)
  475. print(porcelain.describe('.'))
  476. class cmd_help(Command):
  477. def run(self, args):
  478. parser = optparse.OptionParser()
  479. parser.add_option("-a", "--all", dest="all",
  480. action="store_true",
  481. help="List all commands.")
  482. options, args = parser.parse_args(args)
  483. if options.all:
  484. print('Available commands:')
  485. for cmd in sorted(commands):
  486. print(' %s' % cmd)
  487. else:
  488. print("""\
  489. The dulwich command line tool is currently a very basic frontend for the
  490. Dulwich python module. For full functionality, please see the API reference.
  491. For a list of supported commands, see 'dulwich help -a'.
  492. """)
  493. commands = {
  494. "add": cmd_add,
  495. "archive": cmd_archive,
  496. "check-ignore": cmd_check_ignore,
  497. "check-mailmap": cmd_check_mailmap,
  498. "clone": cmd_clone,
  499. "commit": cmd_commit,
  500. "commit-tree": cmd_commit_tree,
  501. "describe": cmd_describe,
  502. "daemon": cmd_daemon,
  503. "diff": cmd_diff,
  504. "diff-tree": cmd_diff_tree,
  505. "dump-pack": cmd_dump_pack,
  506. "dump-index": cmd_dump_index,
  507. "fetch-pack": cmd_fetch_pack,
  508. "fetch": cmd_fetch,
  509. "fsck": cmd_fsck,
  510. "help": cmd_help,
  511. "init": cmd_init,
  512. "log": cmd_log,
  513. "ls-files": cmd_ls_files,
  514. "ls-remote": cmd_ls_remote,
  515. "ls-tree": cmd_ls_tree,
  516. "pack-objects": cmd_pack_objects,
  517. "pull": cmd_pull,
  518. "push": cmd_push,
  519. "receive-pack": cmd_receive_pack,
  520. "remote": cmd_remote,
  521. "repack": cmd_repack,
  522. "reset": cmd_reset,
  523. "rev-list": cmd_rev_list,
  524. "rm": cmd_rm,
  525. "show": cmd_show,
  526. "stash": cmd_stash,
  527. "status": cmd_status,
  528. "symbolic-ref": cmd_symbolic_ref,
  529. "tag": cmd_tag,
  530. "update-server-info": cmd_update_server_info,
  531. "upload-pack": cmd_upload_pack,
  532. "web-daemon": cmd_web_daemon,
  533. "write-tree": cmd_write_tree,
  534. }
  535. if len(sys.argv) < 2:
  536. print("Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys())))
  537. sys.exit(1)
  538. cmd = sys.argv[1]
  539. try:
  540. cmd_kls = commands[cmd]
  541. except KeyError:
  542. print("No such subcommand: %s" % cmd)
  543. sys.exit(1)
  544. # TODO(jelmer): Return non-0 on errors
  545. cmd_kls().run(sys.argv[2:])