summaryrefslogtreecommitdiff
path: root/object.c
diff options
context:
space:
mode:
Diffstat (limited to 'object.c')
-rw-r--r--object.c121
1 files changed, 90 insertions, 31 deletions
diff --git a/object.c b/object.c
index 794c866..51e3848 100644
--- a/object.c
+++ b/object.c
@@ -1,14 +1,19 @@
-#include "cache.h"
+#include "git-compat-util.h"
+#include "gettext.h"
+#include "hex.h"
#include "object.h"
#include "replace-object.h"
+#include "object-file.h"
#include "object-store.h"
#include "blob.h"
+#include "statinfo.h"
#include "tree.h"
#include "commit.h"
#include "tag.h"
#include "alloc.h"
#include "packfile.h"
#include "commit-graph.h"
+#include "loose.h"
unsigned int get_max_object_index(void)
{
@@ -43,8 +48,7 @@ int type_from_string_gently(const char *str, ssize_t len, int gentle)
len = strlen(str);
for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
- if (!strncmp(str, object_type_strings[i], len) &&
- object_type_strings[i][len] == '\0')
+ if (!xstrncmpz(object_type_strings[i], str, len))
return i;
if (gentle)
@@ -127,7 +131,7 @@ static void grow_object_hash(struct repository *r)
int new_hash_size = r->parsed_objects->obj_hash_size < 32 ? 32 : 2 * r->parsed_objects->obj_hash_size;
struct object **new_hash;
- new_hash = xcalloc(new_hash_size, sizeof(struct object *));
+ CALLOC_ARRAY(new_hash, new_hash_size);
for (i = 0; i < r->parsed_objects->obj_hash_size; i++) {
struct object *obj = r->parsed_objects->obj_hash[i];
@@ -157,13 +161,13 @@ void *create_object(struct repository *r, const struct object_id *oid, void *o)
return obj;
}
-void *object_as_type(struct repository *r, struct object *obj, enum object_type type, int quiet)
+void *object_as_type(struct object *obj, enum object_type type, int quiet)
{
if (obj->type == type)
return obj;
else if (obj->type == OBJ_NONE) {
if (type == OBJ_COMMIT)
- init_commit_node(r, (struct commit *) obj);
+ init_commit_node((struct commit *) obj);
else
obj->type = type;
return obj;
@@ -177,15 +181,32 @@ void *object_as_type(struct repository *r, struct object *obj, enum object_type
}
}
-struct object *lookup_unknown_object(const struct object_id *oid)
+struct object *lookup_unknown_object(struct repository *r, const struct object_id *oid)
{
- struct object *obj = lookup_object(the_repository, oid);
+ struct object *obj = lookup_object(r, oid);
if (!obj)
- obj = create_object(the_repository, oid,
- alloc_object_node(the_repository));
+ obj = create_object(r, oid, alloc_object_node(r));
return obj;
}
+struct object *lookup_object_by_type(struct repository *r,
+ const struct object_id *oid,
+ enum object_type type)
+{
+ switch (type) {
+ case OBJ_COMMIT:
+ return (struct object *)lookup_commit(r, oid);
+ case OBJ_TREE:
+ return (struct object *)lookup_tree(r, oid);
+ case OBJ_TAG:
+ return (struct object *)lookup_tag(r, oid);
+ case OBJ_BLOB:
+ return (struct object *)lookup_blob(r, oid);
+ default:
+ BUG("unknown object type %d", type);
+ }
+}
+
struct object *parse_object_buffer(struct repository *r, const struct object_id *oid, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
{
struct object *obj;
@@ -195,8 +216,7 @@ struct object *parse_object_buffer(struct repository *r, const struct object_id
if (type == OBJ_BLOB) {
struct blob *blob = lookup_blob(r, oid);
if (blob) {
- if (parse_blob_buffer(blob, buffer, size))
- return NULL;
+ parse_blob_buffer(blob);
obj = &blob->object;
}
} else if (type == OBJ_TREE) {
@@ -216,7 +236,8 @@ struct object *parse_object_buffer(struct repository *r, const struct object_id
if (commit) {
if (parse_commit_buffer(r, commit, buffer, size, 1))
return NULL;
- if (!get_cached_commit_buffer(r, commit, NULL)) {
+ if (save_commit_buffer &&
+ !get_cached_commit_buffer(r, commit, NULL)) {
set_commit_buffer(r, commit, buffer, size);
*eaten_p = 1;
}
@@ -246,8 +267,12 @@ struct object *parse_object_or_die(const struct object_id *oid,
die(_("unable to parse object: %s"), name ? name : oid_to_hex(oid));
}
-struct object *parse_object(struct repository *r, const struct object_id *oid)
+struct object *parse_object_with_flags(struct repository *r,
+ const struct object_id *oid,
+ enum parse_object_flags flags)
{
+ int skip_hash = !!(flags & PARSE_OBJECT_SKIP_HASH_CHECK);
+ int discard_tree = !!(flags & PARSE_OBJECT_DISCARD_TREE);
unsigned long size;
enum object_type type;
int eaten;
@@ -259,21 +284,37 @@ struct object *parse_object(struct repository *r, const struct object_id *oid)
if (obj && obj->parsed)
return obj;
- if ((obj && obj->type == OBJ_BLOB && repo_has_object_file(r, oid)) ||
- (!obj && repo_has_object_file(r, oid) &&
- oid_object_info(r, oid, NULL) == OBJ_BLOB)) {
- if (check_object_signature(r, repl, NULL, 0, NULL) < 0) {
+ if (skip_hash) {
+ struct commit *commit = lookup_commit_in_graph(r, repl);
+ if (commit)
+ return &commit->object;
+ }
+
+ if ((!obj || obj->type == OBJ_BLOB) &&
+ oid_object_info(r, oid, NULL) == OBJ_BLOB) {
+ if (!skip_hash && stream_object_signature(r, repl) < 0) {
error(_("hash mismatch %s"), oid_to_hex(oid));
return NULL;
}
- parse_blob_buffer(lookup_blob(r, oid), NULL, 0);
+ parse_blob_buffer(lookup_blob(r, oid));
return lookup_object(r, oid);
}
+ /*
+ * If the caller does not care about the tree buffer and does not
+ * care about checking the hash, we can simply verify that we
+ * have the on-disk object with the correct type.
+ */
+ if (skip_hash && discard_tree &&
+ (!obj || obj->type == OBJ_TREE) &&
+ oid_object_info(r, oid, NULL) == OBJ_TREE) {
+ return &lookup_tree(r, oid)->object;
+ }
+
buffer = repo_read_object_file(r, oid, &type, &size);
if (buffer) {
- if (check_object_signature(r, repl, buffer, size,
- type_name(type)) < 0) {
+ if (!skip_hash &&
+ check_object_signature(r, repl, buffer, size, type) < 0) {
free(buffer);
error(_("hash mismatch %s"), oid_to_hex(repl));
return NULL;
@@ -283,11 +324,18 @@ struct object *parse_object(struct repository *r, const struct object_id *oid)
buffer, &eaten);
if (!eaten)
free(buffer);
+ if (discard_tree && type == OBJ_TREE)
+ free_tree_buffer((struct tree *)obj);
return obj;
}
return NULL;
}
+struct object *parse_object(struct repository *r, const struct object_id *oid)
+{
+ return parse_object_with_flags(r, oid, 0);
+}
+
struct object_list *object_list_insert(struct object *item,
struct object_list **list_p)
{
@@ -323,6 +371,12 @@ void object_list_free(struct object_list **list)
*/
static char object_array_slopbuf[1];
+void object_array_init(struct object_array *array)
+{
+ struct object_array blank = OBJECT_ARRAY_INIT;
+ memcpy(array, &blank, sizeof(*array));
+}
+
void add_object_array_with_path(struct object *obj, const char *name,
struct object_array *array,
unsigned mode, const char *path)
@@ -412,15 +466,16 @@ void object_array_clear(struct object_array *array)
}
/*
- * Return true iff array already contains an entry with name.
+ * Return true if array already contains an entry.
*/
-static int contains_name(struct object_array *array, const char *name)
+static int contains_object(struct object_array *array,
+ const struct object *item, const char *name)
{
unsigned nr = array->nr, i;
struct object_array_entry *object = array->objects;
for (i = 0; i < nr; i++, object++)
- if (!strcmp(object->name, name))
+ if (item == object->item && !strcmp(object->name, name))
return 1;
return 0;
}
@@ -432,7 +487,8 @@ void object_array_remove_duplicates(struct object_array *array)
array->nr = 0;
for (src = 0; src < nr; src++) {
- if (!contains_name(array, objects[src].name)) {
+ if (!contains_object(array, objects[src].item,
+ objects[src].name)) {
if (src != array->nr)
objects[array->nr] = objects[src];
array->nr++;
@@ -453,12 +509,12 @@ void clear_object_flags(unsigned flags)
}
}
-void clear_commit_marks_all(unsigned int flags)
+void repo_clear_commit_marks(struct repository *r, unsigned int flags)
{
int i;
- for (i = 0; i < the_repository->parsed_objects->obj_hash_size; i++) {
- struct object *obj = the_repository->parsed_objects->obj_hash[i];
+ for (i = 0; i < r->parsed_objects->obj_hash_size; i++) {
+ struct object *obj = r->parsed_objects->obj_hash[i];
if (obj && obj->type == OBJ_COMMIT)
obj->flags &= ~flags;
}
@@ -476,7 +532,7 @@ struct parsed_object_pool *parsed_object_pool_new(void)
o->object_state = allocate_alloc_state();
o->is_shallow = -1;
- o->shallow_stat = xcalloc(1, sizeof(*o->shallow_stat));
+ CALLOC_ARRAY(o->shallow_stat, 1);
o->buffer_slab = allocate_commit_buffer_slab();
@@ -494,10 +550,11 @@ struct raw_object_store *raw_object_store_new(void)
return o;
}
-static void free_object_directory(struct object_directory *odb)
+void free_object_directory(struct object_directory *odb)
{
free(odb->path);
odb_clear_loose_cache(odb);
+ loose_object_map_clear(&odb->loose_map);
free(odb);
}
@@ -510,6 +567,8 @@ static void free_object_directories(struct raw_object_store *o)
free_object_directory(o->odb);
o->odb = next;
}
+ kh_destroy_odb_path_map(o->odb_by_path);
+ o->odb_by_path = NULL;
}
void raw_object_store_clear(struct raw_object_store *o)
@@ -532,7 +591,7 @@ void raw_object_store_clear(struct raw_object_store *o)
close_object_store(o);
o->packed_git = NULL;
- hashmap_free(&o->pack_map);
+ hashmap_clear(&o->pack_map);
}
void parsed_object_pool_clear(struct parsed_object_pool *o)