use get_cached_commit_buffer where appropriate
[gitweb.git] / commit.c
index bd3d5afcd691dec674f6bc53b9c216fa75ee00d9..b6b0e0d6fbe3628c39e4bc14f8301b89b6b4e834 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -17,7 +17,6 @@ static struct commit_extra_header *read_commit_extra_header_lines(const char *bu
 int save_commit_buffer = 1;
 
 const char *commit_type = "commit";
-static int commit_count;
 
 static struct commit *check_commit(struct object *obj,
                                   const unsigned char *sha1,
@@ -64,7 +63,6 @@ struct commit *lookup_commit(const unsigned char *sha1)
        struct object *obj = lookup_object(sha1);
        if (!obj) {
                struct commit *c = alloc_commit_node();
-               c->index = commit_count++;
                return create_object(sha1, OBJ_COMMIT, c);
        }
        if (!obj->type)
@@ -247,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;
@@ -324,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);