2
0

dulwich 5.9 KB

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