use get_cached_commit_buffer where appropriate
[gitweb.git] / commit.c
index fc8b4e287d7e0219d13569d40ddb3c90a7a4b96b..b6b0e0d6fbe3628c39e4bc14f8301b89b6b4e834 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -250,6 +250,34 @@ void set_commit_buffer(struct commit *commit, void *buffer)
        commit->buffer = buffer;
 }
 
+const void *get_cached_commit_buffer(const struct commit *commit)
+{
+       return commit->buffer;
+}
+
+const void *get_commit_buffer(const struct commit *commit)
+{
+       const void *ret = get_cached_commit_buffer(commit);
+       if (!ret) {
+               enum object_type type;
+               unsigned long size;
+               ret = read_sha1_file(commit->object.sha1, &type, &size);
+               if (!ret)
+                       die("cannot read commit object %s",
+                           sha1_to_hex(commit->object.sha1));
+               if (type != OBJ_COMMIT)
+                       die("expected commit for %s, got %s",
+                           sha1_to_hex(commit->object.sha1), typename(type));
+       }
+       return ret;
+}
+
+void unuse_commit_buffer(const struct commit *commit, const void *buffer)
+{
+       if (commit->buffer != buffer)
+               free((void *)buffer);
+}
+
 void free_commit_buffer(struct commit *commit)
 {
        free(commit->buffer);