瀏覽代碼

Add support for pretty printing objects.

Jelmer Vernooij 16 年之前
父節點
當前提交
86f5723ee2
共有 1 個文件被更改,包括 14 次插入0 次删除
  1. 14 0
      dulwich/objects.py

+ 14 - 0
dulwich/objects.py

@@ -24,6 +24,7 @@
 import mmap
 import os
 import sha
+import stat
 import zlib
 
 from dulwich.errors import (
@@ -112,6 +113,9 @@ class ShaFile(object):
             self.serialize()
         return self._text
 
+    def as_pretty_string(self):
+        return self.as_raw_string()
+
     def _ensure_parsed(self):
         if self._needs_parsing:
             self._parse_text()
@@ -436,6 +440,16 @@ class Tree(ShaFile):
             self._text += "%04o %s\0%s" % (mode, name, hex_to_sha(hexsha))
         self._needs_serialization = False
 
+    def as_pretty_string(self):
+        text = ""
+        for name, mode, hexsha in self.iteritems():
+            if mode & stat.S_IFDIR:
+                kind = "tree"
+            else:
+                kind = "blob"
+            text += "%04o %s %s\t%s\n" % (mode, kind, hexsha, name)
+        return text
+
 
 class Commit(ShaFile):
     """A git commit object"""