Commit 5de079b8 by Vicent Marti

Change the object creation/lookup API

The methods previously known as

	git_repository_lookup
	git_repository_newobject
	git_repository_lookup_ref

are now part of their respective namespaces:

	git_object_lookup
	git_object_new
	git_reference_lookup

This makes the API more consistent with the new references API.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
parent 3dccfed1
...@@ -121,7 +121,7 @@ int commit_parse_buffer(git_commit *commit, void *data, size_t len, unsigned int ...@@ -121,7 +121,7 @@ int commit_parse_buffer(git_commit *commit, void *data, size_t len, unsigned int
if ((error = git__parse_oid(&oid, &buffer, buffer_end, "tree ")) < GIT_SUCCESS) if ((error = git__parse_oid(&oid, &buffer, buffer_end, "tree ")) < GIT_SUCCESS)
return error; return error;
if ((error = git_repository_lookup((git_object **)&commit->tree, commit->object.repo, &oid, GIT_OBJ_TREE)) < GIT_SUCCESS) if ((error = git_object_lookup((git_object **)&commit->tree, commit->object.repo, &oid, GIT_OBJ_TREE)) < GIT_SUCCESS)
return error; return error;
/* /*
...@@ -131,7 +131,7 @@ int commit_parse_buffer(git_commit *commit, void *data, size_t len, unsigned int ...@@ -131,7 +131,7 @@ int commit_parse_buffer(git_commit *commit, void *data, size_t len, unsigned int
while (git__parse_oid(&oid, &buffer, buffer_end, "parent ") == GIT_SUCCESS) { while (git__parse_oid(&oid, &buffer, buffer_end, "parent ") == GIT_SUCCESS) {
git_commit *parent; git_commit *parent;
if ((error = git_repository_lookup((git_object **)&parent, commit->object.repo, &oid, GIT_OBJ_COMMIT)) < GIT_SUCCESS) if ((error = git_object_lookup((git_object **)&parent, commit->object.repo, &oid, GIT_OBJ_COMMIT)) < GIT_SUCCESS)
return error; return error;
if (git_vector_insert(&commit->parents, parent) < GIT_SUCCESS) if (git_vector_insert(&commit->parents, parent) < GIT_SUCCESS)
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
#include "common.h" #include "common.h"
#include "types.h" #include "types.h"
#include "oid.h" #include "oid.h"
#include "repository.h" #include "object.h"
/** /**
* @file git2/blob.h * @file git2/blob.h
...@@ -51,7 +51,7 @@ GIT_BEGIN_DECL ...@@ -51,7 +51,7 @@ GIT_BEGIN_DECL
*/ */
GIT_INLINE(int) git_blob_lookup(git_blob **blob, git_repository *repo, const git_oid *id) GIT_INLINE(int) git_blob_lookup(git_blob **blob, git_repository *repo, const git_oid *id)
{ {
return git_repository_lookup((git_object **)blob, repo, id, GIT_OBJ_BLOB); return git_object_lookup((git_object **)blob, repo, id, GIT_OBJ_BLOB);
} }
/** /**
...@@ -67,7 +67,7 @@ GIT_INLINE(int) git_blob_lookup(git_blob **blob, git_repository *repo, const git ...@@ -67,7 +67,7 @@ GIT_INLINE(int) git_blob_lookup(git_blob **blob, git_repository *repo, const git
*/ */
GIT_INLINE(int) git_blob_new(git_blob **blob, git_repository *repo) GIT_INLINE(int) git_blob_new(git_blob **blob, git_repository *repo)
{ {
return git_repository_newobject((git_object **)blob, repo, GIT_OBJ_BLOB); return git_object_new((git_object **)blob, repo, GIT_OBJ_BLOB);
} }
/** /**
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
#include "common.h" #include "common.h"
#include "types.h" #include "types.h"
#include "oid.h" #include "oid.h"
#include "repository.h" #include "object.h"
/** /**
* @file git2/commit.h * @file git2/commit.h
...@@ -52,7 +52,7 @@ GIT_BEGIN_DECL ...@@ -52,7 +52,7 @@ GIT_BEGIN_DECL
*/ */
GIT_INLINE(int) git_commit_lookup(git_commit **commit, git_repository *repo, const git_oid *id) GIT_INLINE(int) git_commit_lookup(git_commit **commit, git_repository *repo, const git_oid *id)
{ {
return git_repository_lookup((git_object **)commit, repo, id, GIT_OBJ_COMMIT); return git_object_lookup((git_object **)commit, repo, id, GIT_OBJ_COMMIT);
} }
/** /**
...@@ -68,7 +68,7 @@ GIT_INLINE(int) git_commit_lookup(git_commit **commit, git_repository *repo, con ...@@ -68,7 +68,7 @@ GIT_INLINE(int) git_commit_lookup(git_commit **commit, git_repository *repo, con
*/ */
GIT_INLINE(int) git_commit_new(git_commit **commit, git_repository *repo) GIT_INLINE(int) git_commit_new(git_commit **commit, git_repository *repo)
{ {
return git_repository_newobject((git_object **)commit, repo, GIT_OBJ_COMMIT); return git_object_new((git_object **)commit, repo, GIT_OBJ_COMMIT);
} }
/** /**
......
...@@ -39,6 +39,49 @@ ...@@ -39,6 +39,49 @@
GIT_BEGIN_DECL GIT_BEGIN_DECL
/** /**
* Lookup a reference to one of the objects in a repostory.
*
* The generated reference is owned by the repository and
* should not be freed by the user.
*
* The 'type' parameter must match the type of the object
* in the odb; the method will fail otherwise.
* The special value 'GIT_OBJ_ANY' may be passed to let
* the method guess the object's type.
*
* @param object pointer to the looked-up object
* @param repo the repository to look up the object
* @param id the unique identifier for the object
* @param type the type of the object
* @return a reference to the object
*/
GIT_EXTERN(int) git_object_lookup(git_object **object, git_repository *repo, const git_oid *id, git_otype type);
/**
* Create a new in-memory repository object with
* the given type.
*
* The object's attributes can be filled in using the
* corresponding setter methods.
*
* The object will be written back to given git_repository
* when the git_object_write() function is called; objects
* cannot be written to disk until all their main
* attributes have been properly filled.
*
* Objects are instantiated with no SHA1 id; their id
* will be automatically generated when writing to the
* repository.
*
* @param object pointer to the new object
* @parem repo Repository where the object belongs
* @param type Type of the object to be created
* @return the new object
*/
GIT_EXTERN(int) git_object_new(git_object **object, git_repository *repo, git_otype type);
/**
* Write back an object to disk. * Write back an object to disk.
* *
* The object will be written to its corresponding * The object will be written to its corresponding
......
...@@ -39,6 +39,19 @@ ...@@ -39,6 +39,19 @@
GIT_BEGIN_DECL GIT_BEGIN_DECL
/** /**
* Lookup a reference by its name in a repository.
*
* The generated reference is owned by the repository and
* should not be freed by the user.
*
* @param reference_out pointer to the looked-up reference
* @param repo the repository to look up the reference
* @param name the long name for the reference (e.g. HEAD, ref/heads/master, refs/tags/v0.1.0, ...)
* @return 0 on success; error code otherwise
*/
GIT_EXTERN(int) git_reference_lookup(git_reference **reference_out, git_repository *repo, const char *name);
/**
* Create a new symbolic reference. * Create a new symbolic reference.
* *
* The reference will be created in the repository and written * The reference will be created in the repository and written
......
...@@ -132,26 +132,6 @@ GIT_EXTERN(int) git_repository_open3(git_repository **repository, ...@@ -132,26 +132,6 @@ GIT_EXTERN(int) git_repository_open3(git_repository **repository,
const char *git_index_file, const char *git_index_file,
const char *git_work_tree); const char *git_work_tree);
/**
* Lookup a reference to one of the objects in the repostory.
*
* The generated reference is owned by the repository and
* should not be freed by the user.
*
* The 'type' parameter must match the type of the object
* in the odb; the method will fail otherwise.
* The special value 'GIT_OBJ_ANY' may be passed to let
* the method guess the object's type.
*
* @param object pointer to the looked-up object
* @param repo the repository to look up the object
* @param id the unique identifier for the object
* @param type the type of the object
* @return a reference to the object
*/
GIT_EXTERN(int) git_repository_lookup(git_object **object, git_repository *repo, const git_oid *id, git_otype type);
/** /**
* Get the object database behind a Git repository * Get the object database behind a Git repository
* *
...@@ -173,29 +153,6 @@ GIT_EXTERN(git_odb *) git_repository_database(git_repository *repo); ...@@ -173,29 +153,6 @@ GIT_EXTERN(git_odb *) git_repository_database(git_repository *repo);
GIT_EXTERN(int) git_repository_index(git_index **index, git_repository *repo); GIT_EXTERN(int) git_repository_index(git_index **index, git_repository *repo);
/** /**
* Create a new in-memory repository object with
* the given type.
*
* The object's attributes can be filled in using the
* corresponding setter methods.
*
* The object will be written back to given git_repository
* when the git_object_write() function is called; objects
* cannot be written to disk until all their main
* attributes have been properly filled.
*
* Objects are instantiated with no SHA1 id; their id
* will be automatically generated when writing to the
* repository.
*
* @param object pointer to the new object
* @parem repo Repository where the object belongs
* @param type Type of the object to be created
* @return the new object
*/
GIT_EXTERN(int) git_repository_newobject(git_object **object, git_repository *repo, git_otype type);
/**
* 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.
*/ */
...@@ -218,22 +175,6 @@ GIT_EXTERN(void) git_repository_free(git_repository *repo); ...@@ -218,22 +175,6 @@ GIT_EXTERN(void) git_repository_free(git_repository *repo);
*/ */
GIT_EXTERN(int) git_repository_init(git_repository **repo_out, const char *path, unsigned is_bare); GIT_EXTERN(int) git_repository_init(git_repository **repo_out, const char *path, unsigned is_bare);
/**
* Lookup a reference by its name in the repository.
*
* The generated reference is owned by the repository and
* should not be freed by the user.
*
* TODO:
* - Ensure the reference name is valid
*
* @param reference_out pointer to the looked-up reference
* @param repo the repository to look up the reference
* @param name the long name for the reference (e.g. HEAD, ref/heads/master, refs/tags/v0.1.0, ...)
* @return a reference to the reference
*/
GIT_EXTERN(int) git_repository_lookup_ref(git_reference **reference_out, git_repository *repo, const char *name);
/** @} */ /** @} */
GIT_END_DECL GIT_END_DECL
#endif #endif
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
#include "common.h" #include "common.h"
#include "types.h" #include "types.h"
#include "oid.h" #include "oid.h"
#include "repository.h" #include "object.h"
/** /**
* @file git2/tag.h * @file git2/tag.h
...@@ -51,7 +51,7 @@ GIT_BEGIN_DECL ...@@ -51,7 +51,7 @@ GIT_BEGIN_DECL
*/ */
GIT_INLINE(int) git_tag_lookup(git_tag **tag, git_repository *repo, const git_oid *id) GIT_INLINE(int) git_tag_lookup(git_tag **tag, git_repository *repo, const git_oid *id)
{ {
return git_repository_lookup((git_object **)tag, repo, id, (git_otype)GIT_OBJ_TAG); return git_object_lookup((git_object **)tag, repo, id, (git_otype)GIT_OBJ_TAG);
} }
/** /**
...@@ -67,7 +67,7 @@ GIT_INLINE(int) git_tag_lookup(git_tag **tag, git_repository *repo, const git_oi ...@@ -67,7 +67,7 @@ GIT_INLINE(int) git_tag_lookup(git_tag **tag, git_repository *repo, const git_oi
*/ */
GIT_INLINE(int) git_tag_new(git_tag **tag, git_repository *repo) GIT_INLINE(int) git_tag_new(git_tag **tag, git_repository *repo)
{ {
return git_repository_newobject((git_object **)tag, repo, (git_otype)GIT_OBJ_TAG); return git_object_new((git_object **)tag, repo, (git_otype)GIT_OBJ_TAG);
} }
/** /**
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
#include "common.h" #include "common.h"
#include "types.h" #include "types.h"
#include "oid.h" #include "oid.h"
#include "repository.h" #include "object.h"
/** /**
* @file git2/tree.h * @file git2/tree.h
...@@ -51,7 +51,7 @@ GIT_BEGIN_DECL ...@@ -51,7 +51,7 @@ GIT_BEGIN_DECL
*/ */
GIT_INLINE(int) git_tree_lookup(git_tree **tree, git_repository *repo, const git_oid *id) GIT_INLINE(int) git_tree_lookup(git_tree **tree, git_repository *repo, const git_oid *id)
{ {
return git_repository_lookup((git_object **)tree, repo, id, GIT_OBJ_TREE); return git_object_lookup((git_object **)tree, repo, id, GIT_OBJ_TREE);
} }
/** /**
...@@ -67,7 +67,7 @@ GIT_INLINE(int) git_tree_lookup(git_tree **tree, git_repository *repo, const git ...@@ -67,7 +67,7 @@ GIT_INLINE(int) git_tree_lookup(git_tree **tree, git_repository *repo, const git
*/ */
GIT_INLINE(int) git_tree_new(git_tree **tree, git_repository *repo) GIT_INLINE(int) git_tree_new(git_tree **tree, git_repository *repo)
{ {
return git_repository_newobject((git_object **)tree, repo, GIT_OBJ_TREE); return git_object_new((git_object **)tree, repo, GIT_OBJ_TREE);
} }
/** /**
......
...@@ -209,6 +209,125 @@ void git_object__source_close(git_object *object) ...@@ -209,6 +209,125 @@ void git_object__source_close(git_object *object)
} }
} }
static int create_object(git_object **object_out, git_otype type)
{
git_object *object = NULL;
assert(object_out);
*object_out = NULL;
switch (type) {
case GIT_OBJ_COMMIT:
case GIT_OBJ_TAG:
case GIT_OBJ_BLOB:
object = git__malloc(git_object__size(type));
if (object == NULL)
return GIT_ENOMEM;
memset(object, 0x0, git_object__size(type));
break;
case GIT_OBJ_TREE:
object = (git_object *)git_tree__new();
if (object == NULL)
return GIT_ENOMEM;
break;
default:
return GIT_EINVALIDTYPE;
}
*object_out = object;
return GIT_SUCCESS;
}
int git_object_new(git_object **object_out, git_repository *repo, git_otype type)
{
git_object *object = NULL;
int error;
assert(object_out && repo);
if ((error = create_object(&object, type)) < GIT_SUCCESS)
return error;
object->repo = repo;
object->in_memory = 1;
object->modified = 1;
object->source.raw.type = type;
*object_out = object;
return GIT_SUCCESS;
}
int git_object_lookup(git_object **object_out, git_repository *repo, const git_oid *id, git_otype type)
{
git_object *object = NULL;
git_rawobj obj_file;
int error = GIT_SUCCESS;
assert(repo && object_out && id);
object = git_hashtable_lookup(repo->objects, id);
if (object != NULL) {
*object_out = object;
return GIT_SUCCESS;
}
error = git_odb_read(&obj_file, repo->db, id);
if (error < GIT_SUCCESS)
return error;
if (type != GIT_OBJ_ANY && type != obj_file.type) {
git_rawobj_close(&obj_file);
return GIT_EINVALIDTYPE;
}
type = obj_file.type;
if ((error = create_object(&object, type)) < GIT_SUCCESS)
return error;
/* Initialize parent object */
git_oid_cpy(&object->id, id);
object->repo = repo;
memcpy(&object->source.raw, &obj_file, sizeof(git_rawobj));
object->source.open = 1;
switch (type) {
case GIT_OBJ_COMMIT:
error = git_commit__parse((git_commit *)object);
break;
case GIT_OBJ_TREE:
error = git_tree__parse((git_tree *)object);
break;
case GIT_OBJ_TAG:
error = git_tag__parse((git_tag *)object);
break;
case GIT_OBJ_BLOB:
error = git_blob__parse((git_blob *)object);
break;
default:
break;
}
if (error < GIT_SUCCESS) {
git_object_free(object);
return error;
}
git_object__source_close(object);
git_hashtable_insert(repo->objects, &object->id, object);
*object_out = object;
return GIT_SUCCESS;
}
int git_object_write(git_object *object) int git_object_write(git_object *object)
{ {
int error; int error;
......
...@@ -818,7 +818,7 @@ cleanup: ...@@ -818,7 +818,7 @@ cleanup:
/** /**
* Constructors * Constructors
*/ */
int git_repository_lookup_ref(git_reference **ref_out, git_repository *repo, const char *name) int git_reference_lookup(git_reference **ref_out, git_repository *repo, const char *name)
{ {
int error; int error;
char normalized_name[GIT_PATH_MAX]; char normalized_name[GIT_PATH_MAX];
...@@ -1118,7 +1118,7 @@ int git_reference_delete(git_reference *ref) ...@@ -1118,7 +1118,7 @@ int git_reference_delete(git_reference *ref)
/* When deleting a loose reference, we have to ensure that an older /* When deleting a loose reference, we have to ensure that an older
* packed version of it doesn't exist * packed version of it doesn't exist
*/ */
if (!git_repository_lookup_ref(&reference, ref->owner, ref->name)) { if (!git_reference_lookup(&reference, ref->owner, ref->name)) {
assert((reference->type & GIT_REF_PACKED) != 0); assert((reference->type & GIT_REF_PACKED) != 0);
error = git_reference_delete(reference); error = git_reference_delete(reference);
} }
...@@ -1157,7 +1157,7 @@ int git_reference_rename(git_reference *ref, const char *new_name) ...@@ -1157,7 +1157,7 @@ int git_reference_rename(git_reference *ref, const char *new_name)
return error; return error;
/* Ensure we're not going to overwrite an existing reference */ /* Ensure we're not going to overwrite an existing reference */
error = git_repository_lookup_ref(&looked_up_ref, ref->owner, new_name); error = git_reference_lookup(&looked_up_ref, ref->owner, new_name);
if (error == GIT_SUCCESS) if (error == GIT_SUCCESS)
return GIT_EINVALIDREFNAME; return GIT_EINVALIDREFNAME;
...@@ -1269,7 +1269,7 @@ int git_reference_resolve(git_reference **resolved_ref, git_reference *ref) ...@@ -1269,7 +1269,7 @@ int git_reference_resolve(git_reference **resolved_ref, git_reference *ref)
} }
ref_sym = (reference_symbolic *)ref; ref_sym = (reference_symbolic *)ref;
if ((error = git_repository_lookup_ref(&ref, repo, ref_sym->target)) < GIT_SUCCESS) if ((error = git_reference_lookup(&ref, repo, ref_sym->target)) < GIT_SUCCESS)
return error; return error;
} }
......
...@@ -379,125 +379,6 @@ git_odb *git_repository_database(git_repository *repo) ...@@ -379,125 +379,6 @@ git_odb *git_repository_database(git_repository *repo)
return repo->db; return repo->db;
} }
static int create_object(git_object **object_out, git_otype type)
{
git_object *object = NULL;
assert(object_out);
*object_out = NULL;
switch (type) {
case GIT_OBJ_COMMIT:
case GIT_OBJ_TAG:
case GIT_OBJ_BLOB:
object = git__malloc(git_object__size(type));
if (object == NULL)
return GIT_ENOMEM;
memset(object, 0x0, git_object__size(type));
break;
case GIT_OBJ_TREE:
object = (git_object *)git_tree__new();
if (object == NULL)
return GIT_ENOMEM;
break;
default:
return GIT_EINVALIDTYPE;
}
*object_out = object;
return GIT_SUCCESS;
}
int git_repository_newobject(git_object **object_out, git_repository *repo, git_otype type)
{
git_object *object = NULL;
int error;
assert(object_out && repo);
if ((error = create_object(&object, type)) < GIT_SUCCESS)
return error;
object->repo = repo;
object->in_memory = 1;
object->modified = 1;
object->source.raw.type = type;
*object_out = object;
return GIT_SUCCESS;
}
int git_repository_lookup(git_object **object_out, git_repository *repo, const git_oid *id, git_otype type)
{
git_object *object = NULL;
git_rawobj obj_file;
int error = GIT_SUCCESS;
assert(repo && object_out && id);
object = git_hashtable_lookup(repo->objects, id);
if (object != NULL) {
*object_out = object;
return GIT_SUCCESS;
}
error = git_odb_read(&obj_file, repo->db, id);
if (error < GIT_SUCCESS)
return error;
if (type != GIT_OBJ_ANY && type != obj_file.type) {
git_rawobj_close(&obj_file);
return GIT_EINVALIDTYPE;
}
type = obj_file.type;
if ((error = create_object(&object, type)) < GIT_SUCCESS)
return error;
/* Initialize parent object */
git_oid_cpy(&object->id, id);
object->repo = repo;
memcpy(&object->source.raw, &obj_file, sizeof(git_rawobj));
object->source.open = 1;
switch (type) {
case GIT_OBJ_COMMIT:
error = git_commit__parse((git_commit *)object);
break;
case GIT_OBJ_TREE:
error = git_tree__parse((git_tree *)object);
break;
case GIT_OBJ_TAG:
error = git_tag__parse((git_tag *)object);
break;
case GIT_OBJ_BLOB:
error = git_blob__parse((git_blob *)object);
break;
default:
break;
}
if (error < GIT_SUCCESS) {
git_object_free(object);
return error;
}
git_object__source_close(object);
git_hashtable_insert(repo->objects, &object->id, object);
*object_out = object;
return GIT_SUCCESS;
}
static int repo_init_reinit(repo_init *results) static int repo_init_reinit(repo_init *results)
{ {
/* TODO: reinit the repository */ /* TODO: reinit the repository */
......
...@@ -164,7 +164,7 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end) ...@@ -164,7 +164,7 @@ 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;
error = git_repository_lookup(&tag->target, tag->object.repo, &target_oid, tag->type); error = git_object_lookup(&tag->target, tag->object.repo, &target_oid, tag->type);
if (error < 0) if (error < 0)
return error; return error;
......
...@@ -153,7 +153,7 @@ const git_oid *git_tree_entry_id(git_tree_entry *entry) ...@@ -153,7 +153,7 @@ const git_oid *git_tree_entry_id(git_tree_entry *entry)
int git_tree_entry_2object(git_object **object_out, git_tree_entry *entry) int git_tree_entry_2object(git_object **object_out, git_tree_entry *entry)
{ {
assert(entry && object_out); assert(entry && object_out);
return git_repository_lookup(object_out, entry->owner->object.repo, &entry->oid, GIT_OBJ_ANY); return git_object_lookup(object_out, entry->owner->object.repo, &entry->oid, GIT_OBJ_ANY);
} }
static void sort_entries(git_tree *tree) static void sort_entries(git_tree *tree)
......
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