dulwich 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. from dulwich.client import get_transport_and_path
  30. from dulwich.errors import ApplyDeltaError
  31. from dulwich.index import Index
  32. from dulwich.porcelain import (
  33. archive,
  34. clone,
  35. commit,
  36. init,
  37. update_server_info,
  38. )
  39. from dulwich.pack import Pack, sha_to_hex
  40. from dulwich.patch import write_tree_diff
  41. from dulwich.repo import Repo
  42. def cmd_archive(args):
  43. opts, args = getopt(args, "", [])
  44. client, path = get_transport_and_path(args.pop(0))
  45. location = args.pop(0)
  46. committish = args.pop(0)
  47. archive(location, committish, outstream=sys.stdout, errstream=sys.stderr)
  48. def cmd_fetch_pack(args):
  49. opts, args = getopt(args, "", ["all"])
  50. opts = dict(opts)
  51. client, path = get_transport_and_path(args.pop(0))
  52. r = Repo(".")
  53. if "--all" in opts:
  54. determine_wants = r.object_store.determine_wants_all
  55. else:
  56. determine_wants = lambda x: [y for y in args if not y in r.object_store]
  57. client.fetch(path, r, determine_wants)
  58. def cmd_fetch(args):
  59. opts, args = getopt(args, "", [])
  60. opts = dict(opts)
  61. client, path = get_transport_and_path(args.pop(0))
  62. r = Repo(".")
  63. if "--all" in opts:
  64. determine_wants = r.object_store.determine_wants_all
  65. refs = client.fetch(path, r, progress=sys.stdout.write)
  66. print "Remote refs:"
  67. for item in refs.iteritems():
  68. print "%s -> %s" % item
  69. def cmd_log(args):
  70. opts, args = getopt(args, "", [])
  71. if len(args) > 0:
  72. path = args.pop(0)
  73. else:
  74. path = "."
  75. r = Repo(path)
  76. todo = [r.head()]
  77. done = set()
  78. while todo:
  79. sha = todo.pop()
  80. assert isinstance(sha, str)
  81. if sha in done:
  82. continue
  83. done.add(sha)
  84. commit = r[sha]
  85. print "-" * 50
  86. print "commit: %s" % sha
  87. if len(commit.parents) > 1:
  88. print "merge: %s" % "...".join(commit.parents[1:])
  89. print "author: %s" % commit.author
  90. print "committer: %s" % commit.committer
  91. print ""
  92. print commit.message
  93. print ""
  94. todo.extend([p for p in commit.parents if p not in done])
  95. def cmd_diff(args):
  96. opts, args = getopt(args, "", [])
  97. if args == []:
  98. print "Usage: dulwich diff COMMITID"
  99. sys.exit(1)
  100. r = Repo(".")
  101. commit_id = args[0]
  102. commit = r[commit_id]
  103. parent_commit = r[commit.parents[0]]
  104. write_tree_diff(sys.stdout, r.object_store, parent_commit.tree, commit.tree)
  105. def cmd_dump_pack(args):
  106. opts, args = getopt(args, "", [])
  107. if args == []:
  108. print "Usage: dulwich dump-pack FILENAME"
  109. sys.exit(1)
  110. basename, _ = os.path.splitext(args[0])
  111. x = Pack(basename)
  112. print "Object names checksum: %s" % x.name()
  113. print "Checksum: %s" % sha_to_hex(x.get_stored_checksum())
  114. if not x.check():
  115. print "CHECKSUM DOES NOT MATCH"
  116. print "Length: %d" % len(x)
  117. for name in x:
  118. try:
  119. print "\t%s" % x[name]
  120. except KeyError, k:
  121. print "\t%s: Unable to resolve base %s" % (name, k)
  122. except ApplyDeltaError, e:
  123. print "\t%s: Unable to apply delta: %r" % (name, e)
  124. def cmd_dump_index(args):
  125. opts, args = getopt(args, "", [])
  126. if args == []:
  127. print "Usage: dulwich dump-index FILENAME"
  128. sys.exit(1)
  129. filename = args[0]
  130. idx = Index(filename)
  131. for o in idx:
  132. print o, idx[o]
  133. def cmd_init(args):
  134. opts, args = getopt(args, "", ["bare"])
  135. opts = dict(opts)
  136. if args == []:
  137. path = os.getcwd()
  138. else:
  139. path = args[0]
  140. init(path, bare=("--bare" in opts))
  141. def cmd_clone(args):
  142. opts, args = getopt(args, "", ["bare"])
  143. opts = dict(opts)
  144. if args == []:
  145. print "usage: dulwich clone host:path [PATH]"
  146. sys.exit(1)
  147. source = args.pop(0)
  148. if len(args) > 0:
  149. target = args.pop(0)
  150. else:
  151. target = None
  152. clone(source, target, bare=("--bare" in opts))
  153. def cmd_commit(args):
  154. opts, args = getopt(args, "", ["message"])
  155. opts = dict(opts)
  156. commit(".", message=opts["--message"])
  157. def cmd_update_server_info(args):
  158. update_server_info(".")
  159. commands = {
  160. "commit": cmd_commit,
  161. "fetch-pack": cmd_fetch_pack,
  162. "fetch": cmd_fetch,
  163. "dump-pack": cmd_dump_pack,
  164. "dump-index": cmd_dump_index,
  165. "init": cmd_init,
  166. "log": cmd_log,
  167. "clone": cmd_clone,
  168. "archive": cmd_archive,
  169. "update-server-info": cmd_update_server_info,
  170. "diff": cmd_diff,
  171. }
  172. if len(sys.argv) < 2:
  173. print "Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys()))
  174. sys.exit(1)
  175. cmd = sys.argv[1]
  176. if not cmd in commands:
  177. print "No such subcommand: %s" % cmd
  178. sys.exit(1)
  179. commands[cmd](sys.argv[2:])