dulwich 5.5 KB

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