dulwich 5.5 KB

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