dulwich 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/usr/bin/python
  2. # dul-daemon - Simple git smart server client
  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. import sys
  20. from getopt import getopt
  21. def get_transport_and_path(uri):
  22. from dulwich.client import TCPGitClient, SSHGitClient, SubprocessGitClient
  23. for handler, transport in (("git://", TCPGitClient), ("git+ssh://", SSHGitClient)):
  24. if uri.startswith(handler):
  25. host, path = uri[len(handler):].split("/", 1)
  26. return transport(host), "/"+path
  27. # if its not git or git+ssh, try a local url..
  28. return SubprocessGitClient(), uri
  29. def cmd_fetch_pack(args):
  30. from dulwich.repo import Repo
  31. opts, args = getopt(args, "", ["all"])
  32. opts = dict(opts)
  33. client, path = get_transport_and_path(args.pop(0))
  34. if "--all" in opts:
  35. determine_wants = r.object_store.determine_wants_all
  36. else:
  37. determine_wants = lambda x: [y for y in args if not y in r.object_store]
  38. r = Repo(".")
  39. graphwalker = r.get_graph_walker()
  40. f, commit = r.object_store.add_pack()
  41. try:
  42. client.fetch_pack(path, determine_wants, graphwalker, f.write, sys.stdout.write)
  43. finally:
  44. commit()
  45. def cmd_log(args):
  46. from dulwich.repo import Repo
  47. opts, args = getopt(args, "", [])
  48. r = Repo(".")
  49. todo = [r.head()]
  50. done = set()
  51. while todo:
  52. sha = todo.pop()
  53. assert isinstance(sha, str)
  54. if sha in done:
  55. continue
  56. done.add(sha)
  57. commit = r.commit(sha)
  58. print "-" * 50
  59. print "commit: %s" % sha
  60. if len(commit.parents) > 1:
  61. print "merge: %s" % "...".join(commit.parents[1:])
  62. print "author: %s" % commit.author
  63. print "committer: %s" % commit.committer
  64. print ""
  65. print commit.message
  66. print ""
  67. todo.extend([p for p in commit.parents if p not in done])
  68. def cmd_dump_pack(args):
  69. from dulwich.errors import ApplyDeltaError
  70. from dulwich.pack import Pack, sha_to_hex
  71. import os
  72. import sys
  73. opts, args = getopt(args, "", [])
  74. if args == []:
  75. print "Usage: dulwich dump-pack FILENAME"
  76. sys.exit(1)
  77. basename, _ = os.path.splitext(args[0])
  78. x = Pack(basename)
  79. print "Object names checksum: %s" % x.name()
  80. print "Checksum: %s" % sha_to_hex(x.get_stored_checksum())
  81. if not x.check():
  82. print "CHECKSUM DOES NOT MATCH"
  83. print "Length: %d" % len(x)
  84. for name in x:
  85. try:
  86. print "\t%s" % x[name]
  87. except KeyError, k:
  88. print "\t%s: Unable to resolve base %s" % (name, k)
  89. except ApplyDeltaError, e:
  90. print "\t%s: Unable to apply delta: %r" % (name, e)
  91. def cmd_dump_index(args):
  92. from dulwich.index import Index
  93. opts, args = getopt(args, "", [])
  94. if args == []:
  95. print "Usage: dulwich dump-index FILENAME"
  96. sys.exit(1)
  97. filename = args[0]
  98. idx = Index(filename)
  99. for o in idx:
  100. print o, idx[o]
  101. def cmd_init(args):
  102. from dulwich.repo import Repo
  103. import os
  104. import sys
  105. opts, args = getopt(args, "", ["--bare"])
  106. opts = dict(opts)
  107. if args == []:
  108. path = os.getcwd()
  109. else:
  110. path = args[0]
  111. if not os.path.exists(path):
  112. os.mkdir(path)
  113. if "--bare" in opts:
  114. Repo.init_bare(path)
  115. else:
  116. Repo.init(path)
  117. def cmd_clone(args):
  118. from dulwich.repo import Repo
  119. import os
  120. import sys
  121. opts, args = getopt(args, "", [])
  122. opts = dict(opts)
  123. if args == []:
  124. print "usage: dulwich clone host:path [PATH]"
  125. sys.exit(1)
  126. client, host_path = get_transport_and_path(args.pop(0))
  127. if len(args) > 0:
  128. path = args.pop(0)
  129. else:
  130. path = host_path.split("/")[-1]
  131. if not os.path.exists(path):
  132. os.mkdir(path)
  133. Repo.init(path)
  134. r = Repo(path)
  135. graphwalker = r.get_graph_walker()
  136. f, commit = r.object_store.add_pack()
  137. client.fetch_pack(host_path, r.object_store.determine_wants_all,
  138. graphwalker, f.write, sys.stdout.write)
  139. commit()
  140. commands = {
  141. "fetch-pack": cmd_fetch_pack,
  142. "dump-pack": cmd_dump_pack,
  143. "dump-index": cmd_dump_index,
  144. "init": cmd_init,
  145. "log": cmd_log,
  146. "clone": cmd_clone,
  147. }
  148. if len(sys.argv) < 2:
  149. print "Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys()))
  150. sys.exit(1)
  151. cmd = sys.argv[1]
  152. if not cmd in commands:
  153. print "No such subcommand: %s" % cmd
  154. sys.exit(1)
  155. commands[cmd](sys.argv[2:])