dulwich 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. class Command(object):
  44. """A Dulwich subcommand."""
  45. def run(self, args):
  46. """Run the command."""
  47. raise NotImplementedError(self.run)
  48. class cmd_archive(Command):
  49. def run(self, args):
  50. opts, args = getopt(args, "", [])
  51. client, path = get_transport_and_path(args.pop(0))
  52. location = args.pop(0)
  53. committish = args.pop(0)
  54. porcelain.archive(location, committish, outstream=sys.stdout,
  55. errstream=sys.stderr)
  56. class cmd_add(Command):
  57. def run(self, args):
  58. opts, args = getopt(args, "", [])
  59. porcelain.add(".", paths=args)
  60. class cmd_rm(Command):
  61. def run(self, args):
  62. opts, args = getopt(args, "", [])
  63. porcelain.rm(".", paths=args)
  64. class cmd_fetch_pack(Command):
  65. def run(self, args):
  66. opts, args = getopt(args, "", ["all"])
  67. opts = dict(opts)
  68. client, path = get_transport_and_path(args.pop(0))
  69. r = Repo(".")
  70. if "--all" in opts:
  71. determine_wants = r.object_store.determine_wants_all
  72. else:
  73. determine_wants = lambda x: [y for y in args if not y in r.object_store]
  74. client.fetch(path, r, determine_wants)
  75. class cmd_fetch(Command):
  76. def run(self, args):
  77. opts, args = getopt(args, "", [])
  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. refs = client.fetch(path, r, progress=sys.stdout.write)
  84. print("Remote refs:")
  85. for item in refs.items():
  86. print("%s -> %s" % item)
  87. class cmd_log(Command):
  88. def run(self, args):
  89. parser = optparse.OptionParser()
  90. parser.add_option("--reverse", dest="reverse", action="store_true",
  91. help="Reverse order in which entries are printed")
  92. parser.add_option("--name-status", dest="name_status", action="store_true",
  93. help="Print name/status for each changed file")
  94. options, args = parser.parse_args(args)
  95. porcelain.log(".", paths=args, reverse=options.reverse,
  96. name_status=options.name_status,
  97. outstream=sys.stdout)
  98. class cmd_diff(Command):
  99. def run(self, args):
  100. opts, args = getopt(args, "", [])
  101. if args == []:
  102. print("Usage: dulwich diff COMMITID")
  103. sys.exit(1)
  104. r = Repo(".")
  105. commit_id = args[0]
  106. commit = r[commit_id]
  107. parent_commit = r[commit.parents[0]]
  108. write_tree_diff(sys.stdout, r.object_store, parent_commit.tree, commit.tree)
  109. class cmd_dump_pack(Command):
  110. def run(self, args):
  111. opts, args = getopt(args, "", [])
  112. if args == []:
  113. print("Usage: dulwich dump-pack FILENAME")
  114. sys.exit(1)
  115. basename, _ = os.path.splitext(args[0])
  116. x = Pack(basename)
  117. print("Object names checksum: %s" % x.name())
  118. print("Checksum: %s" % sha_to_hex(x.get_stored_checksum()))
  119. if not x.check():
  120. print("CHECKSUM DOES NOT MATCH")
  121. print("Length: %d" % len(x))
  122. for name in x:
  123. try:
  124. print("\t%s" % x[name])
  125. except KeyError as k:
  126. print("\t%s: Unable to resolve base %s" % (name, k))
  127. except ApplyDeltaError as e:
  128. print("\t%s: Unable to apply delta: %r" % (name, e))
  129. class cmd_dump_index(Command):
  130. def run(self, args):
  131. opts, args = getopt(args, "", [])
  132. if args == []:
  133. print("Usage: dulwich dump-index FILENAME")
  134. sys.exit(1)
  135. filename = args[0]
  136. idx = Index(filename)
  137. for o in idx:
  138. print(o, idx[o])
  139. class cmd_init(Command):
  140. def run(self, args):
  141. opts, args = getopt(args, "", ["bare"])
  142. opts = dict(opts)
  143. if args == []:
  144. path = os.getcwd()
  145. else:
  146. path = args[0]
  147. porcelain.init(path, bare=("--bare" in opts))
  148. class cmd_clone(Command):
  149. def run(self, args):
  150. opts, args = getopt(args, "", ["bare"])
  151. opts = dict(opts)
  152. if args == []:
  153. print("usage: dulwich clone host:path [PATH]")
  154. sys.exit(1)
  155. source = args.pop(0)
  156. if len(args) > 0:
  157. target = args.pop(0)
  158. else:
  159. target = None
  160. porcelain.clone(source, target, bare=("--bare" in opts))
  161. class cmd_commit(Command):
  162. def run(self, args):
  163. opts, args = getopt(args, "", ["message"])
  164. opts = dict(opts)
  165. porcelain.commit(".", message=opts["--message"])
  166. class cmd_commit_tree(Command):
  167. def run(self, args):
  168. opts, args = getopt(args, "", ["message"])
  169. if args == []:
  170. print("usage: dulwich commit-tree tree")
  171. sys.exit(1)
  172. opts = dict(opts)
  173. porcelain.commit_tree(".", tree=args[0], message=opts["--message"])
  174. class cmd_update_server_info(Command):
  175. def run(self, args):
  176. porcelain.update_server_info(".")
  177. class cmd_symbolic_ref(Command):
  178. def run(self, args):
  179. opts, args = getopt(args, "", ["ref-name", "force"])
  180. if not args:
  181. print("Usage: dulwich symbolic-ref REF_NAME [--force]")
  182. sys.exit(1)
  183. ref_name = args.pop(0)
  184. porcelain.symbolic_ref(".", ref_name=ref_name, force='--force' in args)
  185. class cmd_show(Command):
  186. def run(self, args):
  187. opts, args = getopt(args, "", [])
  188. porcelain.show(".", args)
  189. class cmd_diff_tree(Command):
  190. def run(self, args):
  191. opts, args = getopt(args, "", [])
  192. if len(args) < 2:
  193. print("Usage: dulwich diff-tree OLD-TREE NEW-TREE")
  194. sys.exit(1)
  195. porcelain.diff_tree(".", args[0], args[1])
  196. class cmd_rev_list(Command):
  197. def run(self, args):
  198. opts, args = getopt(args, "", [])
  199. if len(args) < 1:
  200. print('Usage: dulwich rev-list COMMITID...')
  201. sys.exit(1)
  202. porcelain.rev_list('.', args)
  203. class cmd_tag(Command):
  204. def run(self, args):
  205. opts, args = getopt(args, '', [])
  206. if len(args) < 2:
  207. print('Usage: dulwich tag NAME')
  208. sys.exit(1)
  209. porcelain.tag('.', args[0])
  210. class cmd_repack(Command):
  211. def run(self, args):
  212. opts, args = getopt(args, "", [])
  213. opts = dict(opts)
  214. porcelain.repack('.')
  215. class cmd_reset(Command):
  216. def run(self, args):
  217. opts, args = getopt(args, "", ["hard", "soft", "mixed"])
  218. opts = dict(opts)
  219. mode = ""
  220. if "--hard" in opts:
  221. mode = "hard"
  222. elif "--soft" in opts:
  223. mode = "soft"
  224. elif "--mixed" in opts:
  225. mode = "mixed"
  226. porcelain.reset('.', mode=mode, *args)
  227. class cmd_daemon(Command):
  228. def run(self, args):
  229. from dulwich import log_utils
  230. from dulwich.protocol import TCP_GIT_PORT
  231. parser = optparse.OptionParser()
  232. parser.add_option("-l", "--listen_address", dest="listen_address",
  233. default="localhost",
  234. help="Binding IP address.")
  235. parser.add_option("-p", "--port", dest="port", type=int,
  236. default=TCP_GIT_PORT,
  237. help="Binding TCP port.")
  238. options, args = parser.parse_args(args)
  239. log_utils.default_logging_config()
  240. if len(args) >= 1:
  241. gitdir = args[0]
  242. else:
  243. gitdir = '.'
  244. from dulwich import porcelain
  245. porcelain.daemon(gitdir, address=options.listen_address,
  246. port=options.port)
  247. class cmd_web_daemon(Command):
  248. def run(self, args):
  249. from dulwich import log_utils
  250. parser = optparse.OptionParser()
  251. parser.add_option("-l", "--listen_address", dest="listen_address",
  252. default="",
  253. help="Binding IP address.")
  254. parser.add_option("-p", "--port", dest="port", type=int,
  255. default=8000,
  256. help="Binding TCP port.")
  257. options, args = parser.parse_args(args)
  258. log_utils.default_logging_config()
  259. if len(args) >= 1:
  260. gitdir = args[0]
  261. else:
  262. gitdir = '.'
  263. from dulwich import porcelain
  264. porcelain.web_daemon(gitdir, address=options.listen_address,
  265. port=options.port)
  266. class cmd_receive_pack(Command):
  267. def run(self, args):
  268. parser = optparse.OptionParser()
  269. options, args = parser.parse_args(args)
  270. if len(args) >= 1:
  271. gitdir = args[0]
  272. else:
  273. gitdir = '.'
  274. porcelain.receive_pack(gitdir)
  275. class cmd_upload_pack(Command):
  276. def run(self, args):
  277. parser = optparse.OptionParser()
  278. options, args = parser.parse_args(args)
  279. if len(args) >= 1:
  280. gitdir = args[0]
  281. else:
  282. gitdir = '.'
  283. porcelain.upload_pack(gitdir)
  284. class cmd_status(Command):
  285. def run(self, args):
  286. parser = optparse.OptionParser()
  287. options, args = parser.parse_args(args)
  288. if len(args) >= 1:
  289. gitdir = args[0]
  290. else:
  291. gitdir = '.'
  292. status = porcelain.status(gitdir)
  293. if any(names for (kind, names) in status.staged.items()):
  294. sys.stdout.write("Changes to be committed:\n\n")
  295. for kind, names in status.staged.items():
  296. for name in names:
  297. sys.stdout.write("\t%s: %s\n" % (
  298. kind, name.decode(sys.getfilesystemencoding())))
  299. sys.stdout.write("\n")
  300. if status.unstaged:
  301. sys.stdout.write("Changes not staged for commit:\n\n")
  302. for name in status.unstaged:
  303. sys.stdout.write("\t%s\n" %
  304. name.decode(sys.getfilesystemencoding()))
  305. sys.stdout.write("\n")
  306. if status.untracked:
  307. sys.stdout.write("Untracked files:\n\n")
  308. for name in status.untracked:
  309. sys.stdout.write("\t%s\n" % name)
  310. sys.stdout.write("\n")
  311. class cmd_ls_remote(Command):
  312. def run(self, args):
  313. opts, args = getopt(args, '', [])
  314. if len(args) < 1:
  315. print('Usage: dulwich ls-remote URL')
  316. sys.exit(1)
  317. refs = porcelain.ls_remote(args[0])
  318. for ref in sorted(refs):
  319. sys.stdout.write("%s\t%s\n" % (ref, refs[ref]))
  320. class cmd_ls_tree(Command):
  321. def run(self, args):
  322. parser = optparse.OptionParser()
  323. parser.add_option("-r", "--recursive", action="store_true",
  324. help="Recusively list tree contents.")
  325. parser.add_option("--name-only", action="store_true",
  326. help="Only display name.")
  327. options, args = parser.parse_args(args)
  328. try:
  329. treeish = args.pop(0)
  330. except IndexError:
  331. treeish = None
  332. porcelain.ls_tree(
  333. '.', treeish, outstream=sys.stdout, recursive=options.recursive,
  334. name_only=options.name_only)
  335. class cmd_pack_objects(Command):
  336. def run(self, args):
  337. opts, args = getopt(args, '', ['stdout'])
  338. opts = dict(opts)
  339. if len(args) < 1 and not '--stdout' in args:
  340. print('Usage: dulwich pack-objects basename')
  341. sys.exit(1)
  342. object_ids = [l.strip() for l in sys.stdin.readlines()]
  343. basename = args[0]
  344. if '--stdout' in opts:
  345. packf = getattr(sys.stdout, 'buffer', sys.stdout)
  346. idxf = None
  347. close = []
  348. else:
  349. packf = open(basename + '.pack', 'w')
  350. idxf = open(basename + '.idx', 'w')
  351. close = [packf, idxf]
  352. porcelain.pack_objects('.', object_ids, packf, idxf)
  353. for f in close:
  354. f.close()
  355. class cmd_pull(Command):
  356. def run(self, args):
  357. parser = optparse.OptionParser()
  358. options, args = parser.parse_args(args)
  359. try:
  360. from_location = args[0]
  361. except IndexError:
  362. from_location = None
  363. porcelain.pull('.', from_location)
  364. class cmd_remote_add(Command):
  365. def run(self, args):
  366. parser = optparse.OptionParser()
  367. options, args = parser.parse_args(args)
  368. porcelain.remote_add('.', args[0], args[1])
  369. class cmd_remote(Command):
  370. subcommands = {
  371. "add": cmd_remote_add,
  372. }
  373. def run(self, args):
  374. if not args:
  375. print("Supported subcommands: %s" % ', '.join(self.subcommands.keys()))
  376. return False
  377. cmd = args[0]
  378. try:
  379. cmd_kls = self.subcommands[cmd]
  380. except KeyError:
  381. print('No such subcommand: %s' % args[0])
  382. return False
  383. return cmd_kls(args[1:])
  384. class cmd_check_ignore(Command):
  385. def run(self, args):
  386. parser = optparse.OptionParser()
  387. options, args = parser.parse_args(args)
  388. ret = 1
  389. for path in porcelain.check_ignore('.', args):
  390. print(path)
  391. ret = 0
  392. return ret
  393. class cmd_help(Command):
  394. def run(self, args):
  395. parser = optparse.OptionParser()
  396. parser.add_option("-a", "--all", dest="all",
  397. action="store_true",
  398. help="List all commands.")
  399. options, args = parser.parse_args(args)
  400. if options.all:
  401. print('Available commands:')
  402. for cmd in sorted(commands):
  403. print(' %s' % cmd)
  404. else:
  405. print("""\
  406. The dulwich command line tool is currently a very basic frontend for the
  407. Dulwich python module. For full functionality, please see the API reference.
  408. For a list of supported commands, see 'dulwich help -a'.
  409. """)
  410. commands = {
  411. "add": cmd_add,
  412. "archive": cmd_archive,
  413. "check-ignore": cmd_check_ignore,
  414. "clone": cmd_clone,
  415. "commit": cmd_commit,
  416. "commit-tree": cmd_commit_tree,
  417. "daemon": cmd_daemon,
  418. "diff": cmd_diff,
  419. "diff-tree": cmd_diff_tree,
  420. "dump-pack": cmd_dump_pack,
  421. "dump-index": cmd_dump_index,
  422. "fetch-pack": cmd_fetch_pack,
  423. "fetch": cmd_fetch,
  424. "help": cmd_help,
  425. "init": cmd_init,
  426. "log": cmd_log,
  427. "ls-remote": cmd_ls_remote,
  428. "ls-tree": cmd_ls_tree,
  429. "pack-objects": cmd_pack_objects,
  430. "pull": cmd_pull,
  431. "receive-pack": cmd_receive_pack,
  432. "remote": cmd_remote,
  433. "repack": cmd_repack,
  434. "reset": cmd_reset,
  435. "rev-list": cmd_rev_list,
  436. "rm": cmd_rm,
  437. "show": cmd_show,
  438. "status": cmd_status,
  439. "symbolic-ref": cmd_symbolic_ref,
  440. "tag": cmd_tag,
  441. "update-server-info": cmd_update_server_info,
  442. "upload-pack": cmd_upload_pack,
  443. "web-daemon": cmd_web_daemon,
  444. }
  445. if len(sys.argv) < 2:
  446. print("Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys())))
  447. sys.exit(1)
  448. cmd = sys.argv[1]
  449. try:
  450. cmd_kls = commands[cmd]
  451. except KeyError:
  452. print("No such subcommand: %s" % cmd)
  453. sys.exit(1)
  454. # TODO(jelmer): Return non-0 on errors
  455. cmd_kls().run(sys.argv[2:])