dulwich 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. r = Repo(".")
  35. if "--all" in opts:
  36. determine_wants = r.object_store.determine_wants_all
  37. else:
  38. determine_wants = lambda x: [y for y in args if not y in r.object_store]
  39. graphwalker = r.get_graph_walker()
  40. client.fetch(path, r.object_store, determine_wants)
  41. def cmd_log(args):
  42. from dulwich.repo import Repo
  43. opts, args = getopt(args, "", [])
  44. if len(args) > 0:
  45. path = args.pop(0)
  46. else:
  47. path = "."
  48. r = Repo(path)
  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. opts, args = getopt(args, "", ["--bare"])
  105. opts = dict(opts)
  106. if args == []:
  107. path = os.getcwd()
  108. else:
  109. path = args[0]
  110. if not os.path.exists(path):
  111. os.mkdir(path)
  112. if "--bare" in opts:
  113. Repo.init_bare(path)
  114. else:
  115. Repo.init(path)
  116. def cmd_clone(args):
  117. from dulwich.repo import Repo
  118. import os
  119. import sys
  120. opts, args = getopt(args, "", [])
  121. opts = dict(opts)
  122. if args == []:
  123. print "usage: dulwich clone host:path [PATH]"
  124. sys.exit(1)
  125. client, host_path = get_transport_and_path(args.pop(0))
  126. if len(args) > 0:
  127. path = args.pop(0)
  128. else:
  129. path = host_path.split("/")[-1]
  130. if not os.path.exists(path):
  131. os.mkdir(path)
  132. r = Repo.init(path)
  133. remote_refs = client.fetch(host_path, r,
  134. determine_wants=r.object_store.determine_wants_all,
  135. progress=sys.stdout.write)
  136. r["HEAD"] = remote_refs["HEAD"]
  137. def cmd_commit(args):
  138. from dulwich.repo import Repo
  139. import os
  140. opts, args = getopt(args, "", ["message"])
  141. opts = dict(opts)
  142. r = Repo(".")
  143. committer = "%s <%s>" % (os.getenv("GIT_COMMITTER_NAME"),
  144. os.getenv("GIT_COMMITTER_EMAIL"))
  145. author = "%s <%s>" % (os.getenv("GIT_AUTHOR_NAME"),
  146. os.getenv("GIT_AUTHOR_EMAIL"))
  147. r.do_commit(committer=committer, author=author, message=opts["--message"])
  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. }
  157. if len(sys.argv) < 2:
  158. print "Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys()))
  159. sys.exit(1)
  160. cmd = sys.argv[1]
  161. if not cmd in commands:
  162. print "No such subcommand: %s" % cmd
  163. sys.exit(1)
  164. commands[cmd](sys.argv[2:])