use get_cached_commit_buffer where appropriate
[gitweb.git] / commit.c
index fbdc480ccf17b4d241e0a950e8dd44dfe9d684ce..b6b0e0d6fbe3628c39e4bc14f8301b89b6b4e834 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -245,6 +245,52 @@ int unregister_shallow(const unsigned char *sha1)
        return 0;
 }
 
+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);
+       commit->buffer = NULL;
+}
+
+const void *detach_commit_buffer(struct commit *commit)
+{
+       void *ret = commit->buffer;
+       commit->buffer = NULL;
+       return ret;
+}
+
 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
 {
        const char *tail = buffer;
@@ -322,7 +368,7 @@ int parse_commit(struct commit *item)
        }
        ret = parse_commit_buffer(item, buffer, size);
        if (save_commit_buffer && !ret) {
-               item->buffer = buffer;
+               set_commit_buffer(item, buffer);
                return 0;
        }
        free(buffer);