dulwich 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/usr/bin/python
  2. # dulwich - Simple command-line interface to Dulwich
  3. # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; version 2
  8. # or (at your option) a later version of the License.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. # MA 02110-1301, USA.
  19. """Simple command-line interface to Dulwich>
  20. This is a very simple command-line wrapper for Dulwich. It is by
  21. no means intended to be a full-blown Git command-line interface but just
  22. a way to test Dulwich.
  23. """
  24. import os
  25. import sys
  26. from getopt import getopt
  27. from dulwich.client import get_transport_and_path
  28. from dulwich.errors import ApplyDeltaError
  29. from dulwich.index import Index
  30. from dulwich.pack import Pack, sha_to_hex
  31. from dulwich.repo import Repo
  32. def cmd_fetch_pack(args):
  33. opts, args = getopt(args, "", ["all"])
  34. opts = dict(opts)
  35. client, path = get_transport_and_path(args.pop(0))
  36. r = Repo(".")
  37. if "--all" in opts:
  38. determine_wants = r.object_store.determine_wants_all
  39. else:
  40. determine_wants = lambda x: [y for y in args if not y in r.object_store]
  41. graphwalker = r.get_graph_walker()
  42. client.fetch(path, r.object_store, determine_wants)
  43. def cmd_log(args):
  44. opts, args = getopt(args, "", [])
  45. if len(args) > 0:
  46. path = args.pop(0)
  47. else:
  48. path = "."
  49. r = Repo(path)
  50. todo = [r.head()]
  51. done = set()
  52. while todo:
  53. sha = todo.pop()
  54. assert isinstance(sha, str)
  55. if sha in done:
  56. continue
  57. done.add(sha)
  58. commit = r[sha]
  59. print "-" * 50
  60. print "commit: %s" % sha
  61. if len(commit.parents) > 1:
  62. print "merge: %s" % "...".join(commit.parents[1:])
  63. print "author: %s" % commit.author
  64. print "committer: %s" % commit.committer
  65. print ""
  66. print commit.message
  67. print ""
  68. todo.extend([p for p in commit.parents if p not in done])
  69. def cmd_dump_pack(args):
  70. opts, args = getopt(args, "", [])
  71. if args == []:
  72. print "Usage: dulwich dump-pack FILENAME"
  73. sys.exit(1)
  74. basename, _ = os.path.splitext(args[0])
  75. x = Pack(basename)
  76. print "Object names checksum: %s" % x.name()
  77. print "Checksum: %s" % sha_to_hex(x.get_stored_checksum())
  78. if not x.check():
  79. print "CHECKSUM DOES NOT MATCH"
  80. print "Length: %d" % len(x)
  81. for name in x:
  82. try:
  83. print "\t%s" % x[name]
  84. except KeyError, k:
  85. print "\t%s: Unable to resolve base %s" % (name, k)
  86. except ApplyDeltaError, e:
  87. print "\t%s: Unable to apply delta: %r" % (name, e)
  88. def cmd_dump_index(args):
  89. opts, args = getopt(args, "", [])
  90. if args == []:
  91. print "Usage: dulwich dump-index FILENAME"
  92. sys.exit(1)
  93. filename = args[0]
  94. idx = Index(filename)
  95. for o in idx:
  96. print o, idx[o]
  97. def cmd_init(args):
  98. opts, args = getopt(args, "", ["--bare"])
  99. opts = dict(opts)
  100. if args == []:
  101. path = os.getcwd()
  102. else:
  103. path = args[0]
  104. if not os.path.exists(path):
  105. os.mkdir(path)
  106. if "--bare" in opts:
  107. Repo.init_bare(path)
  108. else:
  109. Repo.init(path)
  110. def cmd_clone(args):
  111. opts, args = getopt(args, "", [])
  112. opts = dict(opts)
  113. if args == []:
  114. print "usage: dulwich clone host:path [PATH]"
  115. sys.exit(1)
  116. client, host_path = get_transport_and_path(args.pop(0))
  117. if len(args) > 0:
  118. path = args.pop(0)
  119. else:
  120. path = host_path.split("/")[-1]
  121. if not os.path.exists(path):
  122. os.mkdir(path)
  123. r = Repo.init(path)
  124. remote_refs = client.fetch(host_path, r,
  125. determine_wants=r.object_store.determine_wants_all,
  126. progress=sys.stdout.write)
  127. r["HEAD"] = remote_refs["HEAD"]
  128. def cmd_commit(args):
  129. opts, args = getopt(args, "", ["message"])
  130. opts = dict(opts)
  131. r = Repo(".")
  132. committer = "%s <%s>" % (os.getenv("GIT_COMMITTER_NAME"),
  133. os.getenv("GIT_COMMITTER_EMAIL"))
  134. author = "%s <%s>" % (os.getenv("GIT_AUTHOR_NAME"),
  135. os.getenv("GIT_AUTHOR_EMAIL"))
  136. r.do_commit(committer=committer, author=author, message=opts["--message"])
  137. commands = {
  138. "commit": cmd_commit,
  139. "fetch-pack": cmd_fetch_pack,
  140. "dump-pack": cmd_dump_pack,
  141. "dump-index": cmd_dump_index,
  142. "init": cmd_init,
  143. "log": cmd_log,
  144. "clone": cmd_clone,
  145. }
  146. if len(sys.argv) < 2:
  147. print "Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys()))
  148. sys.exit(1)
  149. cmd = sys.argv[1]
  150. if not cmd in commands:
  151. print "No such subcommand: %s" % cmd
  152. sys.exit(1)
  153. commands[cmd](sys.argv[2:])