dulwich 5.8 KB

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