dulwich 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 cmd_fetch_pack(args):
  22. from dulwich.client import TCPGitClient, SimpleFetchGraphWalker
  23. from dulwich.repo import Repo
  24. opts, args = getopt(args, "", ["all"])
  25. opts = dict(opts)
  26. if not ":" in args[0]:
  27. print "Usage: dulwich fetch-pack [--all] host:path [REF...]"
  28. sys.exit(1)
  29. (host, path) = args.pop(0).split(":", 1)
  30. client = TCPGitClient(host)
  31. if "--all" in opts:
  32. determine_wants = lambda x: [y for y in x.values() if not y in r.object_store]
  33. else:
  34. determine_wants = lambda x: [y for y in args if not y in r.object_store]
  35. r = Repo(".")
  36. graphwalker = SimpleFetchGraphWalker(r.heads().values(), r.get_parents)
  37. f, commit = r.object_store.add_pack()
  38. try:
  39. client.fetch_pack(path, determine_wants, graphwalker, f.write, sys.stdout.write)
  40. f.close()
  41. commit()
  42. except:
  43. f.close()
  44. raise
  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. commands = {
  92. "fetch-pack": cmd_fetch_pack,
  93. "dump-pack": cmd_dump_pack,
  94. "log": cmd_log,
  95. }
  96. if len(sys.argv) < 2:
  97. print "Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys()))
  98. sys.exit(1)
  99. cmd = sys.argv[1]
  100. if not cmd in commands:
  101. print "No such subcommand: %s" % cmd
  102. sys.exit(1)
  103. commands[cmd](sys.argv[2:])