Commit 6b2a1941 by Vicent Marti

Fix the retarded object interdependency system

It's no longer retarded. All object interdependencies are stored as OIDs
instead of actual objects. This should be hundreds of times faster,
specially on big repositories. Heck, who knows, maye it doesn't even
segfault -- wouldn't that be awesome?

What has changed on the API?

	`git_commit_parent`, `git_commit_tree`, `git_tag_target` now return
	their values through a pointer-to-pointer, and have an error code.

	`git_commit_set_tree` and `git_tag_set_target` now return an error
	code and may fail.

	`git_repository_free__no_gc` has been deprecated because it's
	stupid. Since there are no longer any interdependencies between
	objects, we don't need internal reference counting, and GC
	never fails or double-free's pointers.

	`git_object_close` now does a very sane thing: marks an object
	as unused. Closed objects will be eventually free'd from the
	object cache based on LRU. Please use `git_object_close` from
	the garbage collector `destroy` method on your bindings. It's
	100% safe.

	`git_repository_gc` is a new method that forces a garbage collector
	pass through the repo, to free as many LRU objects as possible.
	This is useful if we are running out of memory.
parent 00571828
...@@ -122,10 +122,11 @@ GIT_EXTERN(const git_signature *) git_commit_author(git_commit *commit); ...@@ -122,10 +122,11 @@ GIT_EXTERN(const git_signature *) git_commit_author(git_commit *commit);
/** /**
* Get the tree pointed to by a commit. * Get the tree pointed to by a commit.
* @param tree_out pointer where to store the tree object
* @param commit a previously loaded commit. * @param commit a previously loaded commit.
* @return the tree of a commit * @return 0 on success; error code otherwise
*/ */
GIT_EXTERN(const git_tree *) git_commit_tree(git_commit *commit); GIT_EXTERN(int) git_commit_tree(git_tree **tree_out, git_commit *commit);
/** /**
* Get the number of parents of this commit * Get the number of parents of this commit
...@@ -137,11 +138,13 @@ GIT_EXTERN(unsigned int) git_commit_parentcount(git_commit *commit); ...@@ -137,11 +138,13 @@ GIT_EXTERN(unsigned int) git_commit_parentcount(git_commit *commit);
/** /**
* Get the specified parent of the commit. * Get the specified parent of the commit.
*
* @param parent Pointer where to store the parent commit
* @param commit a previously loaded commit. * @param commit a previously loaded commit.
* @param n the position of the entry * @param n the position of the parent (from 0 to `parentcount`)
* @return a pointer to the commit; NULL if out of bounds * @return 0 on success; error code otherwise
*/ */
GIT_EXTERN(git_commit *) git_commit_parent(git_commit *commit, unsigned int n); GIT_EXTERN(int) git_commit_parent(git_commit **parent, git_commit *commit, unsigned int n);
/** /**
* Add a new parent commit to an existing commit * Add a new parent commit to an existing commit
...@@ -176,8 +179,9 @@ GIT_EXTERN(void) git_commit_set_author(git_commit *commit, const git_signature * ...@@ -176,8 +179,9 @@ GIT_EXTERN(void) git_commit_set_author(git_commit *commit, const git_signature *
* Set the tree which is pointed to by a commit * Set the tree which is pointed to by a commit
* @param commit the commit object * @param commit the commit object
* @param tree the new tree * @param tree the new tree
* @param 0 on success; error code otherwise
*/ */
GIT_EXTERN(void) git_commit_set_tree(git_commit *commit, git_tree *tree); GIT_EXTERN(int) git_commit_set_tree(git_commit *commit, git_tree *tree);
/** @} */ /** @} */
GIT_END_DECL GIT_END_DECL
......
...@@ -131,10 +131,10 @@ GIT_EXTERN(git_repository *) git_object_owner(const git_object *obj); ...@@ -131,10 +131,10 @@ GIT_EXTERN(git_repository *) git_object_owner(const git_object *obj);
* Close an open object * Close an open object
* *
* This method instructs the library to close an existing * This method instructs the library to close an existing
* object; note that git_objects are owned by the repository * object; note that git_objects are owned and cached by the repository
* and are reference counted, so the object may or may not be * so the object may or may not be freed after this library call,
* freed after this library call, depending on whether any other * depending on how agressive is the caching mechanism used
* objects still depend on it. * by the repository.
* *
* IMPORTANT: * IMPORTANT:
* It is *not* necessary to call this method when you stop using * It is *not* necessary to call this method when you stop using
......
...@@ -154,12 +154,20 @@ GIT_EXTERN(int) git_repository_index(git_index **index, git_repository *repo); ...@@ -154,12 +154,20 @@ GIT_EXTERN(int) git_repository_index(git_index **index, git_repository *repo);
/** /**
* Free a previously allocated repository * Free a previously allocated repository
*
* @param repo repository handle to close. If NULL nothing occurs. * @param repo repository handle to close. If NULL nothing occurs.
*/ */
GIT_EXTERN(void) git_repository_free(git_repository *repo); GIT_EXTERN(void) git_repository_free(git_repository *repo);
/**
GIT_EXTERN(void) git_repository_free__no_gc(git_repository *repo); * Force a garbage collector pass on the repository
*
* This will force-free any cached objects that have been
* previously marked by the user as closed (`git_object_close`).
*
* @param repo repository handle to collect. If NULL nothing occurs.
*/
GIT_EXTERN(void) git_repository_close(git_repository *repo);
/** /**
* Creates a new Git repository in the given folder. * Creates a new Git repository in the given folder.
......
...@@ -79,10 +79,18 @@ GIT_EXTERN(const git_oid *) git_tag_id(git_tag *tag); ...@@ -79,10 +79,18 @@ GIT_EXTERN(const git_oid *) git_tag_id(git_tag *tag);
/** /**
* Get the tagged object of a tag * Get the tagged object of a tag
* @param target pointer where to store the target
* @param tag a previously loaded tag. * @param tag a previously loaded tag.
* @return reference to a repository object * @return 0 on success; error code otherwise
*/
GIT_EXTERN(int) git_tag_target(git_object **target, git_tag *t);
/**
* Get the OID of the tagged object of a tag
* @param tag a previously loaded tag.
* @return pointer to the OID
*/ */
GIT_EXTERN(const git_object *) git_tag_target(git_tag *t); GIT_EXTERN(const git_oid *) git_tag_target_oid(git_tag *t);
/** /**
* Get the type of a tag's tagged object * Get the type of a tag's tagged object
...@@ -117,7 +125,7 @@ GIT_EXTERN(const char *) git_tag_message(git_tag *t); ...@@ -117,7 +125,7 @@ GIT_EXTERN(const char *) git_tag_message(git_tag *t);
* @param tag The tag to modify * @param tag The tag to modify
* @param target the new tagged target * @param target the new tagged target
*/ */
GIT_EXTERN(void) git_tag_set_target(git_tag *tag, git_object *target); GIT_EXTERN(int) git_tag_set_target(git_tag *tag, git_object *target);
/** /**
* Set the name of a tag * Set the name of a tag
...@@ -130,6 +138,7 @@ GIT_EXTERN(void) git_tag_set_name(git_tag *tag, const char *name); ...@@ -130,6 +138,7 @@ GIT_EXTERN(void) git_tag_set_name(git_tag *tag, const char *name);
* Set the tagger of a tag * Set the tagger of a tag
* @param tag The tag to modify * @param tag The tag to modify
* @param tagger_sig signature of the tagging action * @param tagger_sig signature of the tagging action
* @return 0 on success; error code otherwise
*/ */
GIT_EXTERN(void) git_tag_set_tagger(git_tag *tag, const git_signature *tagger_sig); GIT_EXTERN(void) git_tag_set_tagger(git_tag *tag, const git_signature *tagger_sig);
......
...@@ -130,9 +130,7 @@ int git_blob_writefile(git_oid *written_id, git_repository *repo, const char *pa ...@@ -130,9 +130,7 @@ int git_blob_writefile(git_oid *written_id, git_repository *repo, const char *pa
git_oid_cpy(written_id, git_object_id((git_object *)blob)); git_oid_cpy(written_id, git_object_id((git_object *)blob));
/* FIXME: maybe we don't want to free this already? git_object_close((git_object*)blob);
* the user may want to access it again */
GIT_OBJECT_DECREF(repo, blob);
return GIT_SUCCESS; return GIT_SUCCESS;
} }
...@@ -46,24 +46,22 @@ static void clear_parents(git_commit *commit) ...@@ -46,24 +46,22 @@ static void clear_parents(git_commit *commit)
{ {
unsigned int i; unsigned int i;
for (i = 0; i < commit->parents.length; ++i) { for (i = 0; i < commit->parent_oids.length; ++i) {
git_commit *parent = git_vector_get(&commit->parents, i); git_oid *parent = git_vector_get(&commit->parent_oids, i);
GIT_OBJECT_DECREF(commit->object.repo, parent); free(parent);
} }
git_vector_clear(&commit->parents); git_vector_clear(&commit->parent_oids);
} }
void git_commit__free(git_commit *commit) void git_commit__free(git_commit *commit)
{ {
clear_parents(commit); clear_parents(commit);
git_vector_free(&commit->parents); git_vector_free(&commit->parent_oids);
git_signature_free(commit->author); git_signature_free(commit->author);
git_signature_free(commit->committer); git_signature_free(commit->committer);
GIT_OBJECT_DECREF(commit->object.repo, commit->tree);
free(commit->message); free(commit->message);
free(commit->message_short); free(commit->message_short);
free(commit); free(commit);
...@@ -78,16 +76,13 @@ int git_commit__writeback(git_commit *commit, git_odb_source *src) ...@@ -78,16 +76,13 @@ int git_commit__writeback(git_commit *commit, git_odb_source *src)
{ {
unsigned int i; unsigned int i;
if (commit->tree == NULL) git__write_oid(src, "tree", &commit->tree_oid);
return GIT_EMISSINGOBJDATA;
git__write_oid(src, "tree", git_tree_id(commit->tree));
for (i = 0; i < commit->parents.length; ++i) { for (i = 0; i < commit->parent_oids.length; ++i) {
git_commit *parent; git_oid *parent_oid;
parent = git_vector_get(&commit->parents, i); parent_oid = git_vector_get(&commit->parent_oids, i);
git__write_oid(src, "parent", git_commit_id(parent)); git__write_oid(src, "parent", parent_oid);
} }
if (commit->author == NULL) if (commit->author == NULL)
...@@ -103,66 +98,49 @@ int git_commit__writeback(git_commit *commit, git_odb_source *src) ...@@ -103,66 +98,49 @@ int git_commit__writeback(git_commit *commit, git_odb_source *src)
if (commit->message != NULL) { if (commit->message != NULL) {
git__source_write(src, "\n", 1); git__source_write(src, "\n", 1);
git__source_write(src, commit->message, strlen(commit->message)); git__source_write(src, commit->message, strlen(commit->message));
} }
/* Mark the commit as having all attributes */
commit->full_parse = 1;
return GIT_SUCCESS; return GIT_SUCCESS;
} }
int commit_parse_buffer(git_commit *commit, void *data, size_t len, unsigned int parse_flags) int commit_parse_buffer(git_commit *commit, void *data, size_t len)
{ {
char *buffer = (char *)data; char *buffer = (char *)data;
const char *buffer_end = (char *)data + len; const char *buffer_end = (char *)data + len;
git_oid oid; git_oid parent_oid;
int error; int error;
/* first parse; the vector hasn't been initialized yet */ /* first parse; the vector hasn't been initialized yet */
if (commit->parents.contents == NULL) { if (commit->parent_oids.contents == NULL) {
git_vector_init(&commit->parents, 4, NULL); git_vector_init(&commit->parent_oids, 4, NULL);
} }
clear_parents(commit); clear_parents(commit);
if ((error = git__parse_oid(&commit->tree_oid, &buffer, buffer_end, "tree ")) < GIT_SUCCESS)
if ((error = git__parse_oid(&oid, &buffer, buffer_end, "tree ")) < GIT_SUCCESS)
return error;
GIT_OBJECT_DECREF(commit->object.repo, commit->tree);
if ((error = git_object_lookup((git_object **)&commit->tree, commit->object.repo, &oid, GIT_OBJ_TREE)) < GIT_SUCCESS)
return error; return error;
/* /*
* TODO: commit grafts! * TODO: commit grafts!
*/ */
while (git__parse_oid(&oid, &buffer, buffer_end, "parent ") == GIT_SUCCESS) { while (git__parse_oid(&parent_oid, &buffer, buffer_end, "parent ") == GIT_SUCCESS) {
git_commit *parent; git_oid *new_oid;
if ((error = git_object_lookup((git_object **)&parent, commit->object.repo, &oid, GIT_OBJ_COMMIT)) < GIT_SUCCESS) new_oid = git__malloc(sizeof(git_oid));
return error; git_oid_cpy(new_oid, &parent_oid);
if (git_vector_insert(&commit->parents, parent) < GIT_SUCCESS) if (git_vector_insert(&commit->parent_oids, new_oid) < GIT_SUCCESS)
return GIT_ENOMEM; return GIT_ENOMEM;
} }
if (commit->author)
git_signature_free(commit->author);
if (parse_flags & COMMIT_FULL_PARSE) { commit->author = git__malloc(sizeof(git_signature));
if (commit->author) if ((error = git_signature__parse(commit->author, &buffer, buffer_end, "author ")) < GIT_SUCCESS)
git_signature_free(commit->author); return error;
commit->author = git__malloc(sizeof(git_signature));
if ((error = git_signature__parse(commit->author, &buffer, buffer_end, "author ")) < GIT_SUCCESS)
return error;
} else {
if ((buffer = memchr(buffer, '\n', buffer_end - buffer)) == NULL)
return GIT_EOBJCORRUPTED;
buffer++;
}
/* Always parse the committer; we need the commit time */ /* Always parse the committer; we need the commit time */
if (commit->committer) if (commit->committer)
...@@ -176,7 +154,7 @@ int commit_parse_buffer(git_commit *commit, void *data, size_t len, unsigned int ...@@ -176,7 +154,7 @@ int commit_parse_buffer(git_commit *commit, void *data, size_t len, unsigned int
while (buffer <= buffer_end && *buffer == '\n') while (buffer <= buffer_end && *buffer == '\n')
buffer++; buffer++;
if (parse_flags & COMMIT_FULL_PARSE && buffer < buffer_end) { if (buffer < buffer_end) {
const char *line_end; const char *line_end;
size_t message_len = buffer_end - buffer; size_t message_len = buffer_end - buffer;
...@@ -203,106 +181,81 @@ int git_commit__parse(git_commit *commit) ...@@ -203,106 +181,81 @@ int git_commit__parse(git_commit *commit)
{ {
assert(commit && commit->object.source.open); assert(commit && commit->object.source.open);
return commit_parse_buffer(commit, return commit_parse_buffer(commit,
commit->object.source.raw.data, commit->object.source.raw.len, COMMIT_BASIC_PARSE); commit->object.source.raw.data, commit->object.source.raw.len);
} }
int git_commit__parse_full(git_commit *commit) #define GIT_COMMIT_GETTER(_rvalue, _name, _return) \
{ _rvalue git_commit_##_name(git_commit *commit) \
int error;
if (commit->full_parse)
return GIT_SUCCESS;
if ((error = git_object__source_open((git_object *)commit)) < GIT_SUCCESS)
return error;
error = commit_parse_buffer(commit,
commit->object.source.raw.data, commit->object.source.raw.len, COMMIT_FULL_PARSE);
git_object__source_close((git_object *)commit);
commit->full_parse = 1;
return error;
}
#define GIT_COMMIT_GETTER(_rvalue, _name) \
const _rvalue git_commit_##_name(git_commit *commit) \
{\ {\
assert(commit); \ assert(commit); \
if (commit->_name) \ return _return; \
return commit->_name; \
if (!commit->object.in_memory) \
git_commit__parse_full(commit); \
return commit->_name; \
} }
#define CHECK_FULL_PARSE() \ GIT_COMMIT_GETTER(const git_signature *, author, commit->author)
if (!commit->object.in_memory && !commit->full_parse)\ GIT_COMMIT_GETTER(const git_signature *, committer, commit->committer)
git_commit__parse_full(commit); GIT_COMMIT_GETTER(const char *, message, commit->message)
GIT_COMMIT_GETTER(const char *, message_short, commit->message_short)
GIT_COMMIT_GETTER(time_t, time, commit->committer->when.time)
GIT_COMMIT_GETTER(int, time_offset, commit->committer->when.offset)
GIT_COMMIT_GETTER(unsigned int, parentcount, commit->parent_oids.length)
int git_commit_tree(git_tree **tree_out, git_commit *commit)
{
assert(commit);
return git_tree_lookup(tree_out, commit->object.repo, &commit->tree_oid);
}
const git_tree *git_commit_tree(git_commit *commit) int git_commit_parent(git_commit **parent, git_commit *commit, unsigned int n)
{ {
git_oid *parent_oid;
assert(commit); assert(commit);
if (!commit->object.in_memory && commit->tree == NULL) parent_oid = git_vector_get(&commit->parent_oids, n);
git_commit__parse_full(commit); if (parent_oid == NULL)
return GIT_ENOTFOUND;
GIT_OBJECT_INCREF(commit->object.repo, commit->tree); return git_commit_lookup(parent, commit->object.repo, parent_oid);
return commit->tree;
} }
GIT_COMMIT_GETTER(git_signature *, author)
GIT_COMMIT_GETTER(git_signature *, committer)
GIT_COMMIT_GETTER(char *, message)
GIT_COMMIT_GETTER(char *, message_short)
time_t git_commit_time(git_commit *commit)
{
assert(commit && commit->committer);
return commit->committer->when.time;
}
int git_commit_time_offset(git_commit *commit) int git_commit_set_tree(git_commit *commit, git_tree *tree)
{ {
assert(commit && commit->committer); const git_oid *oid;
return commit->committer->when.offset;
}
unsigned int git_commit_parentcount(git_commit *commit) assert(commit && tree);
{
assert(commit); if ((oid = git_object_id((git_object *)tree)) == NULL)
return commit->parents.length; return GIT_EMISSINGOBJDATA;
commit->object.modified = 1;
git_oid_cpy(&commit->tree_oid, oid);
return GIT_SUCCESS;
} }
git_commit *git_commit_parent(git_commit *commit, unsigned int n) int git_commit_add_parent(git_commit *commit, git_commit *new_parent)
{ {
git_commit *parent; const git_oid *parent_oid;
git_oid *new_oid;
assert(commit && new_parent);
assert(commit); if ((parent_oid = git_object_id((git_object *)new_parent)) == NULL)
return GIT_EMISSINGOBJDATA;
parent = git_vector_get(&commit->parents, n); new_oid = git__malloc(sizeof(git_oid));
GIT_OBJECT_INCREF(commit->object.repo, parent); if (new_oid == NULL)
return parent; return GIT_ENOMEM;
}
void git_commit_set_tree(git_commit *commit, git_tree *tree)
{
assert(commit && tree);
commit->object.modified = 1; commit->object.modified = 1;
CHECK_FULL_PARSE(); git_oid_cpy(new_oid, parent_oid);
return git_vector_insert(&commit->parent_oids, new_oid);
GIT_OBJECT_DECREF(commit->object.repo, commit->tree);
GIT_OBJECT_INCREF(commit->object.repo, tree);
commit->tree = tree;
} }
void git_commit_set_author(git_commit *commit, const git_signature *author_sig) void git_commit_set_author(git_commit *commit, const git_signature *author_sig)
{ {
assert(commit && author_sig); assert(commit && author_sig);
commit->object.modified = 1; commit->object.modified = 1;
CHECK_FULL_PARSE();
git_signature_free(commit->author); git_signature_free(commit->author);
commit->author = git_signature_dup(author_sig); commit->author = git_signature_dup(author_sig);
...@@ -312,7 +265,6 @@ void git_commit_set_committer(git_commit *commit, const git_signature *committer ...@@ -312,7 +265,6 @@ void git_commit_set_committer(git_commit *commit, const git_signature *committer
{ {
assert(commit && committer_sig); assert(commit && committer_sig);
commit->object.modified = 1; commit->object.modified = 1;
CHECK_FULL_PARSE();
git_signature_free(commit->committer); git_signature_free(commit->committer);
commit->committer = git_signature_dup(committer_sig); commit->committer = git_signature_dup(committer_sig);
...@@ -324,7 +276,6 @@ void git_commit_set_message(git_commit *commit, const char *message) ...@@ -324,7 +276,6 @@ void git_commit_set_message(git_commit *commit, const char *message)
size_t message_len; size_t message_len;
commit->object.modified = 1; commit->object.modified = 1;
CHECK_FULL_PARSE();
if (commit->message) if (commit->message)
free(commit->message); free(commit->message);
...@@ -347,12 +298,3 @@ void git_commit_set_message(git_commit *commit, const char *message) ...@@ -347,12 +298,3 @@ void git_commit_set_message(git_commit *commit, const char *message)
commit->message_short[message_len] = 0; commit->message_short[message_len] = 0;
} }
int git_commit_add_parent(git_commit *commit, git_commit *new_parent)
{
assert(commit && new_parent);
CHECK_FULL_PARSE();
commit->object.modified = 1;
GIT_OBJECT_INCREF(commit->object.repo, new_parent);
return git_vector_insert(&commit->parents, new_parent);
}
...@@ -11,21 +11,18 @@ ...@@ -11,21 +11,18 @@
struct git_commit { struct git_commit {
git_object object; git_object object;
git_vector parents; git_vector parent_oids;
git_oid tree_oid;
git_tree *tree;
git_signature *author; git_signature *author;
git_signature *committer; git_signature *committer;
char *message; char *message;
char *message_short; char *message_short;
unsigned full_parse:1;
}; };
void git_commit__free(git_commit *c); void git_commit__free(git_commit *c);
int git_commit__parse(git_commit *commit); int git_commit__parse(git_commit *commit);
int git_commit__parse_full(git_commit *commit);
int git_commit__writeback(git_commit *commit, git_odb_source *src); int git_commit__writeback(git_commit *commit, git_odb_source *src);
......
...@@ -261,7 +261,7 @@ int git_object_new(git_object **object_out, git_repository *repo, git_otype type ...@@ -261,7 +261,7 @@ int git_object_new(git_object **object_out, git_repository *repo, git_otype type
object->source.raw.type = type; object->source.raw.type = type;
object->refcount++; object->lru = ++repo->lru_counter;
*object_out = object; *object_out = object;
return GIT_SUCCESS; return GIT_SUCCESS;
} }
...@@ -277,7 +277,8 @@ int git_object_lookup(git_object **object_out, git_repository *repo, const git_o ...@@ -277,7 +277,8 @@ int git_object_lookup(git_object **object_out, git_repository *repo, const git_o
object = git_hashtable_lookup(repo->objects, id); object = git_hashtable_lookup(repo->objects, id);
if (object != NULL) { if (object != NULL) {
*object_out = object; *object_out = object;
GIT_OBJECT_INCREF(repo, object); object->lru = ++repo->lru_counter;
object->can_free = 0;
return GIT_SUCCESS; return GIT_SUCCESS;
} }
...@@ -330,7 +331,7 @@ int git_object_lookup(git_object **object_out, git_repository *repo, const git_o ...@@ -330,7 +331,7 @@ int git_object_lookup(git_object **object_out, git_repository *repo, const git_o
git_object__source_close(object); git_object__source_close(object);
git_hashtable_insert(repo->objects, &object->id, object); git_hashtable_insert(repo->objects, &object->id, object);
GIT_OBJECT_INCREF(repo, object); object->lru = ++repo->lru_counter;
*object_out = object; *object_out = object;
return GIT_SUCCESS; return GIT_SUCCESS;
} }
...@@ -412,17 +413,12 @@ void git_object_close(git_object *object) ...@@ -412,17 +413,12 @@ void git_object_close(git_object *object)
if (object == NULL) if (object == NULL)
return; return;
if (--object->refcount <= 0) { if (object->in_memory) {
if (object->repo != NULL) { int idx = git_vector_search(&object->repo->memory_objects, object);
if (object->in_memory) { git_vector_remove(&object->repo->memory_objects, idx);
int idx = git_vector_search(&object->repo->memory_objects, object);
git_vector_remove(&object->repo->memory_objects, idx);
} else {
git_hashtable_remove(object->repo->objects, &object->id);
}
}
git_object__free(object); git_object__free(object);
} else {
object->can_free = 1;
} }
} }
......
...@@ -681,7 +681,6 @@ static int packed_write_ref(reference_oid *ref, git_filebuf *file) ...@@ -681,7 +681,6 @@ static int packed_write_ref(reference_oid *ref, git_filebuf *file)
static int packed_find_peel(reference_oid *ref) static int packed_find_peel(reference_oid *ref)
{ {
git_tag *tag; git_tag *tag;
const git_object *peeled_target;
int error; int error;
if (ref->ref.type & GIT_REF_HAS_PEEL) if (ref->ref.type & GIT_REF_HAS_PEEL)
...@@ -706,11 +705,7 @@ static int packed_find_peel(reference_oid *ref) ...@@ -706,11 +705,7 @@ static int packed_find_peel(reference_oid *ref)
/* /*
* Find the object pointed at by this tag * Find the object pointed at by this tag
*/ */
peeled_target = git_tag_target(tag); git_oid_cpy(&ref->peel_target, git_tag_target_oid(tag));
if (peeled_target == NULL)
return GIT_EOBJCORRUPTED;
git_oid_cpy(&ref->peel_target, git_object_id(peeled_target));
ref->ref.type |= GIT_REF_HAS_PEEL; ref->ref.type |= GIT_REF_HAS_PEEL;
/* /*
......
...@@ -209,7 +209,6 @@ static git_repository *repository_alloc() ...@@ -209,7 +209,6 @@ static git_repository *repository_alloc()
return NULL; return NULL;
} }
repo->gc_enabled = 1;
return repo; return repo;
} }
...@@ -331,50 +330,20 @@ cleanup: ...@@ -331,50 +330,20 @@ cleanup:
return error; return error;
} }
static void repository_free(git_repository *repo) int git_repository_gc(git_repository *repo)
{
assert(repo);
free(repo->path_workdir);
free(repo->path_index);
free(repo->path_repository);
free(repo->path_odb);
git_hashtable_free(repo->objects);
git_vector_free(&repo->memory_objects);
git_repository__refcache_free(&repo->references);
if (repo->db != NULL)
git_odb_close(repo->db);
if (repo->index != NULL)
git_index_free(repo->index);
free(repo);
}
void git_repository_free__no_gc(git_repository *repo)
{ {
int collected = 0;
git_object *object; git_object *object;
const void *_unused; const void *_unused;
unsigned int i;
if (repo == NULL)
return;
GIT_HASHTABLE_FOREACH(repo->objects, _unused, object, GIT_HASHTABLE_FOREACH(repo->objects, _unused, object,
object->repo = NULL; if (object->can_free) {
object->refcount = 0; git_object__free(object);
collected++;
}
); );
for (i = 0; i < repo->memory_objects.length; ++i) { return collected;
object = git_vector_get(&repo->memory_objects, i);
object->repo = NULL;
object->refcount = 0;
}
repository_free(repo);
} }
void git_repository_free(git_repository *repo) void git_repository_free(git_repository *repo)
...@@ -386,8 +355,6 @@ void git_repository_free(git_repository *repo) ...@@ -386,8 +355,6 @@ void git_repository_free(git_repository *repo)
if (repo == NULL) if (repo == NULL)
return; return;
repo->gc_enabled = 0;
/* force free all the objects */ /* force free all the objects */
GIT_HASHTABLE_FOREACH(repo->objects, _unused, object, GIT_HASHTABLE_FOREACH(repo->objects, _unused, object,
git_object__free(object); git_object__free(object);
...@@ -398,7 +365,23 @@ void git_repository_free(git_repository *repo) ...@@ -398,7 +365,23 @@ void git_repository_free(git_repository *repo)
git_object__free(object); git_object__free(object);
} }
repository_free(repo); free(repo->path_workdir);
free(repo->path_index);
free(repo->path_repository);
free(repo->path_odb);
git_hashtable_free(repo->objects);
git_vector_free(&repo->memory_objects);
git_repository__refcache_free(&repo->references);
if (repo->db != NULL)
git_odb_close(repo->db);
if (repo->index != NULL)
git_index_free(repo->index);
free(repo);
} }
int git_repository_index(git_index **index_out, git_repository *repo) int git_repository_index(git_index **index_out, git_repository *repo)
......
...@@ -27,8 +27,8 @@ struct git_object { ...@@ -27,8 +27,8 @@ struct git_object {
git_oid id; git_oid id;
git_repository *repo; git_repository *repo;
git_odb_source source; git_odb_source source;
unsigned short refcount; unsigned int lru;
unsigned char in_memory, modified; unsigned char in_memory, modified, can_free, _pad;
}; };
struct git_repository { struct git_repository {
...@@ -45,7 +45,8 @@ struct git_repository { ...@@ -45,7 +45,8 @@ struct git_repository {
char *path_odb; char *path_odb;
char *path_workdir; char *path_workdir;
unsigned is_bare:1, gc_enabled:1; unsigned is_bare:1;
unsigned int lru_counter;
}; };
int git_object__source_open(git_object *object); int git_object__source_open(git_object *object);
...@@ -61,19 +62,4 @@ int git__source_write(git_odb_source *source, const void *bytes, size_t len); ...@@ -61,19 +62,4 @@ int git__source_write(git_odb_source *source, const void *bytes, size_t len);
int git__parse_oid(git_oid *oid, char **buffer_out, const char *buffer_end, const char *header); int git__parse_oid(git_oid *oid, char **buffer_out, const char *buffer_end, const char *header);
int git__write_oid(git_odb_source *src, const char *header, const git_oid *oid); int git__write_oid(git_odb_source *src, const char *header, const git_oid *oid);
#define GIT_OBJECT_INCREF(repo, ob) git_object__incref((repo), (git_object *)(ob))
#define GIT_OBJECT_DECREF(repo, ob) git_object__decref((repo), (git_object *)(ob))
GIT_INLINE(void) git_object__incref(git_repository *repo, struct git_object *object)
{
if (repo && repo->gc_enabled && object)
object->refcount++;
}
GIT_INLINE(void) git_object__decref(git_repository *repo, struct git_object *object)
{
if (repo && repo->gc_enabled && object)
git_object_close(object);
}
#endif #endif
...@@ -35,7 +35,6 @@ ...@@ -35,7 +35,6 @@
void git_tag__free(git_tag *tag) void git_tag__free(git_tag *tag)
{ {
git_signature_free(tag->tagger); git_signature_free(tag->tagger);
GIT_OBJECT_DECREF(tag->object.repo, tag->target);
free(tag->message); free(tag->message);
free(tag->tag_name); free(tag->tag_name);
free(tag); free(tag);
...@@ -46,23 +45,31 @@ const git_oid *git_tag_id(git_tag *c) ...@@ -46,23 +45,31 @@ const git_oid *git_tag_id(git_tag *c)
return git_object_id((git_object *)c); return git_object_id((git_object *)c);
} }
const git_object *git_tag_target(git_tag *t) int git_tag_target(git_object **target, git_tag *t)
{ {
assert(t); assert(t);
GIT_OBJECT_INCREF(t->object.repo, t->target); return git_object_lookup(target, t->object.repo, &t->target, t->type);
return t->target;
} }
void git_tag_set_target(git_tag *tag, git_object *target) const git_oid *git_tag_target_oid(git_tag *t)
{ {
assert(t);
return &t->target;
}
int git_tag_set_target(git_tag *tag, git_object *target)
{
const git_oid *oid;
assert(tag && target); assert(tag && target);
GIT_OBJECT_DECREF(tag->object.repo, tag->target); if ((oid = git_object_id(target)) == NULL)
GIT_OBJECT_INCREF(tag->object.repo, target); return GIT_EMISSINGOBJDATA;
tag->object.modified = 1; tag->object.modified = 1;
tag->target = target; git_oid_cpy(&tag->target, oid);
tag->type = git_object_type(target); tag->type = git_object_type(target);
return GIT_SUCCESS;
} }
git_otype git_tag_type(git_tag *t) git_otype git_tag_type(git_tag *t)
...@@ -81,7 +88,6 @@ void git_tag_set_name(git_tag *tag, const char *name) ...@@ -81,7 +88,6 @@ void git_tag_set_name(git_tag *tag, const char *name)
{ {
assert(tag && name); assert(tag && name);
/* TODO: sanity check? no newlines in message */
tag->object.modified = 1; tag->object.modified = 1;
if (tag->tag_name) if (tag->tag_name)
...@@ -128,12 +134,11 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end) ...@@ -128,12 +134,11 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
NULL, "commit\n", "tree\n", "blob\n", "tag\n" NULL, "commit\n", "tree\n", "blob\n", "tag\n"
}; };
git_oid target_oid;
unsigned int i, text_len; unsigned int i, text_len;
char *search; char *search;
int error; int error;
if ((error = git__parse_oid(&target_oid, &buffer, buffer_end, "object ")) < 0) if ((error = git__parse_oid(&tag->target, &buffer, buffer_end, "object ")) < 0)
return error; return error;
if (buffer + 5 >= buffer_end) if (buffer + 5 >= buffer_end)
...@@ -161,11 +166,6 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end) ...@@ -161,11 +166,6 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
if (tag->type == GIT_OBJ_BAD) if (tag->type == GIT_OBJ_BAD)
return GIT_EOBJCORRUPTED; return GIT_EOBJCORRUPTED;
git_object_close(tag->target);
error = git_object_lookup(&tag->target, tag->object.repo, &target_oid, tag->type);
if (error < 0)
return error;
if (buffer + 4 >= buffer_end) if (buffer + 4 >= buffer_end)
return GIT_EOBJCORRUPTED; return GIT_EOBJCORRUPTED;
...@@ -210,10 +210,10 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end) ...@@ -210,10 +210,10 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
int git_tag__writeback(git_tag *tag, git_odb_source *src) int git_tag__writeback(git_tag *tag, git_odb_source *src)
{ {
if (tag->target == NULL || tag->tag_name == NULL || tag->tagger == NULL) if (tag->tag_name == NULL || tag->tagger == NULL)
return GIT_EMISSINGOBJDATA; return GIT_EMISSINGOBJDATA;
git__write_oid(src, "object", git_object_id(tag->target)); git__write_oid(src, "object", &tag->target);
git__source_printf(src, "type %s\n", git_object_type2string(tag->type)); git__source_printf(src, "type %s\n", git_object_type2string(tag->type));
git__source_printf(src, "tag %s\n", tag->tag_name); git__source_printf(src, "tag %s\n", tag->tag_name);
git_signature__write(src, "tagger", tag->tagger); git_signature__write(src, "tagger", tag->tagger);
......
...@@ -7,8 +7,9 @@ ...@@ -7,8 +7,9 @@
struct git_tag { struct git_tag {
git_object object; git_object object;
git_object *target; git_oid target;
git_otype type; git_otype type;
char *tag_name; char *tag_name;
git_signature *tagger; git_signature *tagger;
char *message; char *message;
......
...@@ -390,11 +390,11 @@ BEGIN_TEST(details0, "query the details on a parsed commit") ...@@ -390,11 +390,11 @@ BEGIN_TEST(details0, "query the details on a parsed commit")
must_be_true(commit_time > 0); must_be_true(commit_time > 0);
must_be_true(parents <= 2); must_be_true(parents <= 2);
for (p = 0;p < parents;p++) { for (p = 0;p < parents;p++) {
parent = git_commit_parent(commit, p); must_pass(git_commit_parent(&parent, commit, p));
must_be_true(parent != NULL); must_be_true(parent != NULL);
must_be_true(git_commit_author(parent) != NULL); // is it really a commit? must_be_true(git_commit_author(parent) != NULL); // is it really a commit?
} }
must_be_true(git_commit_parent(commit, parents) == NULL); must_fail(git_commit_parent(&parent, commit, parents));
} }
git_repository_free(repo); git_repository_free(repo);
......
...@@ -48,12 +48,12 @@ BEGIN_TEST(read0, "read and parse a tag from the repository") ...@@ -48,12 +48,12 @@ BEGIN_TEST(read0, "read and parse a tag from the repository")
must_be_true(strcmp(git_tag_name(tag1), "test") == 0); must_be_true(strcmp(git_tag_name(tag1), "test") == 0);
must_be_true(git_tag_type(tag1) == GIT_OBJ_TAG); must_be_true(git_tag_type(tag1) == GIT_OBJ_TAG);
tag2 = (git_tag *)git_tag_target(tag1); must_pass(git_tag_target((git_object **)&tag2, tag1));
must_be_true(tag2 != NULL); must_be_true(tag2 != NULL);
must_be_true(git_oid_cmp(&id2, git_tag_id(tag2)) == 0); must_be_true(git_oid_cmp(&id2, git_tag_id(tag2)) == 0);
commit = (git_commit *)git_tag_target(tag2); must_pass(git_tag_target((git_object **)&commit, tag2));
must_be_true(commit != NULL); must_be_true(commit != NULL);
must_be_true(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0); must_be_true(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment