2
0

dulwich 12 KB

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