dulwich 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 os
  20. import sys
  21. from getopt import getopt
  22. from dulwich.client import get_transport_and_path
  23. from dulwich.errors import ApplyDeltaError
  24. from dulwich.index import Index
  25. from dulwich.pack import Pack, sha_to_hex
  26. from dulwich.repo import Repo
  27. def cmd_fetch_pack(args):
  28. opts, args = getopt(args, "", ["all"])
  29. opts = dict(opts)
  30. client, path = get_transport_and_path(args.pop(0))
  31. r = Repo(".")
  32. if "--all" in opts:
  33. determine_wants = r.object_store.determine_wants_all
  34. else:
  35. determine_wants = lambda x: [y for y in args if not y in r.object_store]
  36. graphwalker = r.get_graph_walker()
  37. client.fetch(path, r.object_store, determine_wants)
  38. def cmd_log(args):
  39. opts, args = getopt(args, "", [])
  40. if len(args) > 0:
  41. path = args.pop(0)
  42. else:
  43. path = "."
  44. r = Repo(path)
  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[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. opts, args = getopt(args, "", [])
  66. if args == []:
  67. print "Usage: dulwich dump-pack FILENAME"
  68. sys.exit(1)
  69. basename, _ = os.path.splitext(args[0])
  70. x = Pack(basename)
  71. print "Object names checksum: %s" % x.name()
  72. print "Checksum: %s" % sha_to_hex(x.get_stored_checksum())
  73. if not x.check():
  74. print "CHECKSUM DOES NOT MATCH"
  75. print "Length: %d" % len(x)
  76. for name in x:
  77. try:
  78. print "\t%s" % x[name]
  79. except KeyError, k:
  80. print "\t%s: Unable to resolve base %s" % (name, k)
  81. except ApplyDeltaError, e:
  82. print "\t%s: Unable to apply delta: %r" % (name, e)
  83. def cmd_dump_index(args):
  84. opts, args = getopt(args, "", [])
  85. if args == []:
  86. print "Usage: dulwich dump-index FILENAME"
  87. sys.exit(1)
  88. filename = args[0]
  89. idx = Index(filename)
  90. for o in idx:
  91. print o, idx[o]
  92. def cmd_init(args):
  93. opts, args = getopt(args, "", ["--bare"])
  94. opts = dict(opts)
  95. if args == []:
  96. path = os.getcwd()
  97. else:
  98. path = args[0]
  99. if not os.path.exists(path):
  100. os.mkdir(path)
  101. if "--bare" in opts:
  102. Repo.init_bare(path)
  103. else:
  104. Repo.init(path)
  105. def cmd_clone(args):
  106. opts, args = getopt(args, "", [])
  107. opts = dict(opts)
  108. if args == []:
  109. print "usage: dulwich clone host:path [PATH]"
  110. sys.exit(1)
  111. client, host_path = get_transport_and_path(args.pop(0))
  112. if len(args) > 0:
  113. path = args.pop(0)
  114. else:
  115. path = host_path.split("/")[-1]
  116. if not os.path.exists(path):
  117. os.mkdir(path)
  118. r = Repo.init(path)
  119. remote_refs = client.fetch(host_path, r,
  120. determine_wants=r.object_store.determine_wants_all,
  121. progress=sys.stdout.write)
  122. r["HEAD"] = remote_refs["HEAD"]
  123. def cmd_commit(args):
  124. opts, args = getopt(args, "", ["message"])
  125. opts = dict(opts)
  126. r = Repo(".")
  127. committer = "%s <%s>" % (os.getenv("GIT_COMMITTER_NAME"),
  128. os.getenv("GIT_COMMITTER_EMAIL"))
  129. author = "%s <%s>" % (os.getenv("GIT_AUTHOR_NAME"),
  130. os.getenv("GIT_AUTHOR_EMAIL"))
  131. r.do_commit(committer=committer, author=author, message=opts["--message"])
  132. commands = {
  133. "commit": cmd_commit,
  134. "fetch-pack": cmd_fetch_pack,
  135. "dump-pack": cmd_dump_pack,
  136. "dump-index": cmd_dump_index,
  137. "init": cmd_init,
  138. "log": cmd_log,
  139. "clone": cmd_clone,
  140. }
  141. if len(sys.argv) < 2:
  142. print "Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys()))
  143. sys.exit(1)
  144. cmd = sys.argv[1]
  145. if not cmd in commands:
  146. print "No such subcommand: %s" % cmd
  147. sys.exit(1)
  148. commands[cmd](sys.argv[2:])