packfile: add repository argument to reprepare_packed_git
[gitweb.git] / object.c
index e680d881a45756eb234a80a9909c24a7b146c1f7..4c2cf7ff5d525b8559fc9099b1b957b0e61f3f97 100644 (file)
--- a/object.c
+++ b/object.c
@@ -4,6 +4,8 @@
 #include "tree.h"
 #include "commit.h"
 #include "tag.h"
+#include "object-store.h"
+#include "packfile.h"
 
 static struct object **obj_hash;
 static int nr_objs, obj_hash_size;
@@ -141,7 +143,6 @@ void *create_object(const unsigned char *sha1, void *o)
        struct object *obj = o;
 
        obj->parsed = 0;
-       obj->used = 0;
        obj->flags = 0;
        hashcpy(obj->oid.hash, sha1);
 
@@ -180,21 +181,21 @@ struct object *lookup_unknown_object(const unsigned char *sha1)
        return obj;
 }
 
-struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
+struct object *parse_object_buffer(const struct object_id *oid, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
 {
        struct object *obj;
        *eaten_p = 0;
 
        obj = NULL;
        if (type == OBJ_BLOB) {
-               struct blob *blob = lookup_blob(sha1);
+               struct blob *blob = lookup_blob(oid);
                if (blob) {
                        if (parse_blob_buffer(blob, buffer, size))
                                return NULL;
                        obj = &blob->object;
                }
        } else if (type == OBJ_TREE) {
-               struct tree *tree = lookup_tree(sha1);
+               struct tree *tree = lookup_tree(oid);
                if (tree) {
                        obj = &tree->object;
                        if (!tree->buffer)
@@ -206,7 +207,7 @@ struct object *parse_object_buffer(const unsigned char *sha1, enum object_type t
                        }
                }
        } else if (type == OBJ_COMMIT) {
-               struct commit *commit = lookup_commit(sha1);
+               struct commit *commit = lookup_commit(oid);
                if (commit) {
                        if (parse_commit_buffer(commit, buffer, size))
                                return NULL;
@@ -217,54 +218,54 @@ struct object *parse_object_buffer(const unsigned char *sha1, enum object_type t
                        obj = &commit->object;
                }
        } else if (type == OBJ_TAG) {
-               struct tag *tag = lookup_tag(sha1);
+               struct tag *tag = lookup_tag(oid);
                if (tag) {
                        if (parse_tag_buffer(tag, buffer, size))
                               return NULL;
                        obj = &tag->object;
                }
        } else {
-               warning("object %s has unknown type id %d", sha1_to_hex(sha1), type);
+               warning("object %s has unknown type id %d", oid_to_hex(oid), type);
                obj = NULL;
        }
        return obj;
 }
 
-struct object *parse_object_or_die(const unsigned char *sha1,
+struct object *parse_object_or_die(const struct object_id *oid,
                                   const char *name)
 {
-       struct object *o = parse_object(sha1);
+       struct object *o = parse_object(oid);
        if (o)
                return o;
 
-       die(_("unable to parse object: %s"), name ? name : sha1_to_hex(sha1));
+       die(_("unable to parse object: %s"), name ? name : oid_to_hex(oid));
 }
 
-struct object *parse_object(const unsigned char *sha1)
+struct object *parse_object(const struct object_id *oid)
 {
        unsigned long size;
        enum object_type type;
        int eaten;
-       const unsigned char *repl = lookup_replace_object(sha1);
+       const unsigned char *repl = lookup_replace_object(oid->hash);
        void *buffer;
        struct object *obj;
 
-       obj = lookup_object(sha1);
+       obj = lookup_object(oid->hash);
        if (obj && obj->parsed)
                return obj;
 
-       if ((obj && obj->type == OBJ_BLOB) ||
-           (!obj && has_sha1_file(sha1) &&
-            sha1_object_info(sha1, NULL) == OBJ_BLOB)) {
+       if ((obj && obj->type == OBJ_BLOB && has_object_file(oid)) ||
+           (!obj && has_object_file(oid) &&
+            sha1_object_info(oid->hash, NULL) == OBJ_BLOB)) {
                if (check_sha1_signature(repl, NULL, 0, NULL) < 0) {
-                       error("sha1 mismatch %s", sha1_to_hex(repl));
+                       error("sha1 mismatch %s", oid_to_hex(oid));
                        return NULL;
                }
-               parse_blob_buffer(lookup_blob(sha1), NULL, 0);
-               return lookup_object(sha1);
+               parse_blob_buffer(lookup_blob(oid), NULL, 0);
+               return lookup_object(oid->hash);
        }
 
-       buffer = read_sha1_file(sha1, &type, &size);
+       buffer = read_sha1_file(oid->hash, &type, &size);
        if (buffer) {
                if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) {
                        free(buffer);
@@ -272,7 +273,7 @@ struct object *parse_object(const unsigned char *sha1)
                        return NULL;
                }
 
-               obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
+               obj = parse_object_buffer(oid, type, size, buffer, &eaten);
                if (!eaten)
                        free(buffer);
                return obj;
@@ -354,6 +355,19 @@ static void object_array_release_entry(struct object_array_entry *ent)
        free(ent->path);
 }
 
+struct object *object_array_pop(struct object_array *array)
+{
+       struct object *ret;
+
+       if (!array->nr)
+               return NULL;
+
+       ret = array->objects[array->nr - 1].item;
+       object_array_release_entry(&array->objects[array->nr - 1]);
+       array->nr--;
+       return ret;
+}
+
 void object_array_filter(struct object_array *array,
                         object_array_each_func_t want, void *cb_data)
 {
@@ -377,8 +391,7 @@ void object_array_clear(struct object_array *array)
        int i;
        for (i = 0; i < array->nr; i++)
                object_array_release_entry(&array->objects[i]);
-       free(array->objects);
-       array->objects = NULL;
+       FREE_AND_NULL(array->objects);
        array->nr = array->alloc = 0;
 }
 
@@ -423,3 +436,54 @@ void clear_object_flags(unsigned flags)
                        obj->flags &= ~flags;
        }
 }
+
+void clear_commit_marks_all(unsigned int flags)
+{
+       int i;
+
+       for (i = 0; i < obj_hash_size; i++) {
+               struct object *obj = obj_hash[i];
+               if (obj && obj->type == OBJ_COMMIT)
+                       obj->flags &= ~flags;
+       }
+}
+
+struct raw_object_store *raw_object_store_new(void)
+{
+       struct raw_object_store *o = xmalloc(sizeof(*o));
+
+       memset(o, 0, sizeof(*o));
+       INIT_LIST_HEAD(&o->packed_git_mru);
+       return o;
+}
+
+static void free_alt_odb(struct alternate_object_database *alt)
+{
+       strbuf_release(&alt->scratch);
+       oid_array_clear(&alt->loose_objects_cache);
+       free(alt);
+}
+
+static void free_alt_odbs(struct raw_object_store *o)
+{
+       while (o->alt_odb_list) {
+               struct alternate_object_database *next;
+
+               next = o->alt_odb_list->next;
+               free_alt_odb(o->alt_odb_list);
+               o->alt_odb_list = next;
+       }
+}
+
+void raw_object_store_clear(struct raw_object_store *o)
+{
+       FREE_AND_NULL(o->objectdir);
+       FREE_AND_NULL(o->alternate_db);
+
+       free_alt_odbs(o);
+       o->alt_odb_tail = NULL;
+
+       INIT_LIST_HEAD(&o->packed_git_mru);
+       close_all_packs(o);
+       o->packed_git = NULL;
+}