mirror of
https://git.proxmox.com/git/libgit2
synced 2025-08-13 23:05:27 +00:00
Split object methods from repository.c
All the relevant git_object methods have been moved to object.c Signed-off-by: Vicent Marti <tanoku@gmail.com>
This commit is contained in:
parent
fb3cd6bca4
commit
e52ed7a559
@ -28,6 +28,7 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "oid.h"
|
#include "oid.h"
|
||||||
|
#include "repository.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file git2/blob.h
|
* @file git2/blob.h
|
||||||
@ -48,7 +49,10 @@ GIT_BEGIN_DECL
|
|||||||
* @param id identity of the blob to locate.
|
* @param id identity of the blob to locate.
|
||||||
* @return 0 on success; error code otherwise
|
* @return 0 on success; error code otherwise
|
||||||
*/
|
*/
|
||||||
GIT_EXTERN(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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new in-memory git_blob.
|
* Create a new in-memory git_blob.
|
||||||
@ -61,7 +65,10 @@ GIT_EXTERN(int) git_blob_lookup(git_blob **blob, git_repository *repo, const git
|
|||||||
* @param repo The repository where the object will reside
|
* @param repo The repository where the object will reside
|
||||||
* @return 0 on success; error code otherwise
|
* @return 0 on success; error code otherwise
|
||||||
*/
|
*/
|
||||||
GIT_EXTERN(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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fill a blob with the contents inside
|
* Fill a blob with the contents inside
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "oid.h"
|
#include "oid.h"
|
||||||
|
#include "repository.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file git2/commit.h
|
* @file git2/commit.h
|
||||||
@ -49,7 +50,10 @@ GIT_BEGIN_DECL
|
|||||||
* an annotated tag it will be peeled back to the commit.
|
* an annotated tag it will be peeled back to the commit.
|
||||||
* @return 0 on success; error code otherwise
|
* @return 0 on success; error code otherwise
|
||||||
*/
|
*/
|
||||||
GIT_EXTERN(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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new in-memory git_commit.
|
* Create a new in-memory git_commit.
|
||||||
@ -62,7 +66,10 @@ GIT_EXTERN(int) git_commit_lookup(git_commit **commit, git_repository *repo, con
|
|||||||
* @param repo The repository where the object will reside
|
* @param repo The repository where the object will reside
|
||||||
* @return 0 on success; error code otherwise
|
* @return 0 on success; error code otherwise
|
||||||
*/
|
*/
|
||||||
GIT_EXTERN(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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the id of a commit.
|
* Get the id of a commit.
|
||||||
|
@ -126,6 +126,20 @@ GIT_EXTERN(git_otype) git_object_string2type(const char *str);
|
|||||||
*/
|
*/
|
||||||
GIT_EXTERN(int) git_object_typeisloose(git_otype type);
|
GIT_EXTERN(int) git_object_typeisloose(git_otype type);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the size in bytes for the structure which
|
||||||
|
* acts as an in-memory representation of any given
|
||||||
|
* object type.
|
||||||
|
*
|
||||||
|
* For all the core types, this would the equivalent
|
||||||
|
* of calling `sizeof(git_commit)` if the core types
|
||||||
|
* were not opaque on the external API.
|
||||||
|
*
|
||||||
|
* @param type object type to get its size
|
||||||
|
* @return size in bytes of the object
|
||||||
|
*/
|
||||||
|
GIT_EXTERN(size_t) git_object__size(git_otype type);
|
||||||
|
|
||||||
/** @} */
|
/** @} */
|
||||||
GIT_END_DECL
|
GIT_END_DECL
|
||||||
|
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "oid.h"
|
#include "oid.h"
|
||||||
|
#include "repository.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file git2/tag.h
|
* @file git2/tag.h
|
||||||
@ -48,7 +49,10 @@ GIT_BEGIN_DECL
|
|||||||
* @param id identity of the tag to locate.
|
* @param id identity of the tag to locate.
|
||||||
* @return 0 on success; error code otherwise
|
* @return 0 on success; error code otherwise
|
||||||
*/
|
*/
|
||||||
GIT_EXTERN(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_OBJ_TAG);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new in-memory git_tag.
|
* Create a new in-memory git_tag.
|
||||||
@ -61,7 +65,10 @@ GIT_EXTERN(int) git_tag_lookup(git_tag **tag, git_repository *repo, const git_oi
|
|||||||
* @param repo The repository where the object will reside
|
* @param repo The repository where the object will reside
|
||||||
* @return 0 on success; error code otherwise
|
* @return 0 on success; error code otherwise
|
||||||
*/
|
*/
|
||||||
GIT_EXTERN(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_OBJ_TAG);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the id of a tag.
|
* Get the id of a tag.
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "oid.h"
|
#include "oid.h"
|
||||||
|
#include "repository.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file git2/tree.h
|
* @file git2/tree.h
|
||||||
@ -48,7 +49,10 @@ GIT_BEGIN_DECL
|
|||||||
* @param id identity of the tree to locate.
|
* @param id identity of the tree to locate.
|
||||||
* @return 0 on success; error code otherwise
|
* @return 0 on success; error code otherwise
|
||||||
*/
|
*/
|
||||||
GIT_EXTERN(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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new in-memory git_tree.
|
* Create a new in-memory git_tree.
|
||||||
@ -61,7 +65,10 @@ GIT_EXTERN(int) git_tree_lookup(git_tree **tree, git_repository *repo, const git
|
|||||||
* @param repo The repository where the object will reside
|
* @param repo The repository where the object will reside
|
||||||
* @return 0 on success; error code otherwise
|
* @return 0 on success; error code otherwise
|
||||||
*/
|
*/
|
||||||
GIT_EXTERN(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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the id of a tree.
|
* Get the id of a tree.
|
||||||
|
@ -27,7 +27,6 @@
|
|||||||
#include "repository.h"
|
#include "repository.h"
|
||||||
#include "commit.h"
|
#include "commit.h"
|
||||||
|
|
||||||
static const int default_table_size = 32;
|
|
||||||
static const double max_load_factor = 0.65;
|
static const double max_load_factor = 0.65;
|
||||||
|
|
||||||
static void hashtable_resize(git_hashtable *table)
|
static void hashtable_resize(git_hashtable *table)
|
||||||
|
348
src/object.c
Normal file
348
src/object.c
Normal file
@ -0,0 +1,348 @@
|
|||||||
|
/*
|
||||||
|
* This file is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License, version 2,
|
||||||
|
* as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* In addition to the permissions in the GNU General Public License,
|
||||||
|
* the authors give you unlimited permission to link the compiled
|
||||||
|
* version of this file into combinations with other programs,
|
||||||
|
* and to distribute those combinations without any restriction
|
||||||
|
* coming from the use of this file. (The General Public License
|
||||||
|
* restrictions do apply in other respects; for example, they cover
|
||||||
|
* modification of the file, and distribution when not linked into
|
||||||
|
* a combined executable.)
|
||||||
|
*
|
||||||
|
* This file is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; see the file COPYING. If not, write to
|
||||||
|
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
|
||||||
|
* Boston, MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
#include "git2/object.h"
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
#include "repository.h"
|
||||||
|
|
||||||
|
#include "commit.h"
|
||||||
|
#include "tree.h"
|
||||||
|
#include "blob.h"
|
||||||
|
#include "tag.h"
|
||||||
|
|
||||||
|
static const int OBJECT_BASE_SIZE = 4096;
|
||||||
|
|
||||||
|
static struct {
|
||||||
|
const char *str; /* type name string */
|
||||||
|
int loose; /* valid loose object type flag */
|
||||||
|
size_t size; /* size in bytes of the object structure */
|
||||||
|
} git_objects_table[] = {
|
||||||
|
/* 0 = GIT_OBJ__EXT1 */
|
||||||
|
{ "", 0, 0},
|
||||||
|
|
||||||
|
/* 1 = GIT_OBJ_COMMIT */
|
||||||
|
{ "commit", 1, sizeof(struct git_commit)},
|
||||||
|
|
||||||
|
/* 2 = GIT_OBJ_TREE */
|
||||||
|
{ "tree", 1, sizeof(struct git_tree) },
|
||||||
|
|
||||||
|
/* 3 = GIT_OBJ_BLOB */
|
||||||
|
{ "blob", 1, sizeof(struct git_blob) },
|
||||||
|
|
||||||
|
/* 4 = GIT_OBJ_TAG */
|
||||||
|
{ "tag", 1, sizeof(struct git_tag) },
|
||||||
|
|
||||||
|
/* 5 = GIT_OBJ__EXT2 */
|
||||||
|
{ "", 0, 0 },
|
||||||
|
|
||||||
|
/* 6 = GIT_OBJ_OFS_DELTA */
|
||||||
|
{ "OFS_DELTA", 0, 0 },
|
||||||
|
|
||||||
|
/* 7 = GIT_OBJ_REF_DELTA */
|
||||||
|
{ "REF_DELTA", 0, 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Object source methods
|
||||||
|
*
|
||||||
|
* Abstract buffer methods that allow the writeback system
|
||||||
|
* to prepare the contents of any git file in-memory before
|
||||||
|
* writing them to disk.
|
||||||
|
*/
|
||||||
|
static int source_resize(git_odb_source *src)
|
||||||
|
{
|
||||||
|
size_t write_offset, new_size;
|
||||||
|
void *new_data;
|
||||||
|
|
||||||
|
write_offset = (size_t)((char *)src->write_ptr - (char *)src->raw.data);
|
||||||
|
|
||||||
|
new_size = src->raw.len * 2;
|
||||||
|
if ((new_data = git__malloc(new_size)) == NULL)
|
||||||
|
return GIT_ENOMEM;
|
||||||
|
|
||||||
|
memcpy(new_data, src->raw.data, src->written_bytes);
|
||||||
|
free(src->raw.data);
|
||||||
|
|
||||||
|
src->raw.data = new_data;
|
||||||
|
src->raw.len = new_size;
|
||||||
|
src->write_ptr = (char *)new_data + write_offset;
|
||||||
|
|
||||||
|
return GIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int git__source_printf(git_odb_source *source, const char *format, ...)
|
||||||
|
{
|
||||||
|
va_list arglist;
|
||||||
|
int len, did_resize = 0;
|
||||||
|
|
||||||
|
assert(source->open && source->write_ptr);
|
||||||
|
|
||||||
|
va_start(arglist, format);
|
||||||
|
|
||||||
|
len = vsnprintf(source->write_ptr, source->raw.len - source->written_bytes, format, arglist);
|
||||||
|
|
||||||
|
while (source->written_bytes + len >= source->raw.len) {
|
||||||
|
if (source_resize(source) < GIT_SUCCESS)
|
||||||
|
return GIT_ENOMEM;
|
||||||
|
|
||||||
|
did_resize = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (did_resize)
|
||||||
|
vsnprintf(source->write_ptr, source->raw.len - source->written_bytes, format, arglist);
|
||||||
|
|
||||||
|
source->write_ptr = (char *)source->write_ptr + len;
|
||||||
|
source->written_bytes += len;
|
||||||
|
|
||||||
|
return GIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int git__source_write(git_odb_source *source, const void *bytes, size_t len)
|
||||||
|
{
|
||||||
|
assert(source);
|
||||||
|
|
||||||
|
assert(source->open && source->write_ptr);
|
||||||
|
|
||||||
|
while (source->written_bytes + len >= source->raw.len) {
|
||||||
|
if (source_resize(source) < GIT_SUCCESS)
|
||||||
|
return GIT_ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(source->write_ptr, bytes, len);
|
||||||
|
source->write_ptr = (char *)source->write_ptr + len;
|
||||||
|
source->written_bytes += len;
|
||||||
|
|
||||||
|
return GIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void prepare_write(git_object *object)
|
||||||
|
{
|
||||||
|
if (object->source.write_ptr != NULL || object->source.open)
|
||||||
|
git_object__source_close(object);
|
||||||
|
|
||||||
|
/* TODO: proper size calculation */
|
||||||
|
object->source.raw.data = git__malloc(OBJECT_BASE_SIZE);
|
||||||
|
object->source.raw.len = OBJECT_BASE_SIZE;
|
||||||
|
|
||||||
|
object->source.write_ptr = object->source.raw.data;
|
||||||
|
object->source.written_bytes = 0;
|
||||||
|
|
||||||
|
object->source.open = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int write_back(git_object *object)
|
||||||
|
{
|
||||||
|
int error;
|
||||||
|
git_oid new_id;
|
||||||
|
|
||||||
|
assert(object);
|
||||||
|
|
||||||
|
assert(object->source.open);
|
||||||
|
assert(object->modified);
|
||||||
|
|
||||||
|
object->source.raw.len = object->source.written_bytes;
|
||||||
|
|
||||||
|
if ((error = git_odb_write(&new_id, object->repo->db, &object->source.raw)) < GIT_SUCCESS)
|
||||||
|
return error;
|
||||||
|
|
||||||
|
if (!object->in_memory)
|
||||||
|
git_hashtable_remove(object->repo->objects, &object->id);
|
||||||
|
|
||||||
|
git_oid_cpy(&object->id, &new_id);
|
||||||
|
git_hashtable_insert(object->repo->objects, &object->id, object);
|
||||||
|
|
||||||
|
object->source.write_ptr = NULL;
|
||||||
|
object->source.written_bytes = 0;
|
||||||
|
|
||||||
|
object->modified = 0;
|
||||||
|
object->in_memory = 0;
|
||||||
|
|
||||||
|
git_object__source_close(object);
|
||||||
|
return GIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int git_object__source_open(git_object *object)
|
||||||
|
{
|
||||||
|
int error;
|
||||||
|
|
||||||
|
assert(object && !object->in_memory);
|
||||||
|
|
||||||
|
if (object->source.open)
|
||||||
|
git_object__source_close(object);
|
||||||
|
|
||||||
|
error = git_odb_read(&object->source.raw, object->repo->db, &object->id);
|
||||||
|
if (error < GIT_SUCCESS)
|
||||||
|
return error;
|
||||||
|
|
||||||
|
object->source.open = 1;
|
||||||
|
return GIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void git_object__source_close(git_object *object)
|
||||||
|
{
|
||||||
|
assert(object);
|
||||||
|
|
||||||
|
if (object->source.open) {
|
||||||
|
git_rawobj_close(&object->source.raw);
|
||||||
|
object->source.open = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int git_object_write(git_object *object)
|
||||||
|
{
|
||||||
|
int error;
|
||||||
|
git_odb_source *source;
|
||||||
|
|
||||||
|
assert(object);
|
||||||
|
|
||||||
|
if (object->modified == 0)
|
||||||
|
return GIT_SUCCESS;
|
||||||
|
|
||||||
|
prepare_write(object);
|
||||||
|
source = &object->source;
|
||||||
|
|
||||||
|
switch (source->raw.type) {
|
||||||
|
case GIT_OBJ_COMMIT:
|
||||||
|
error = git_commit__writeback((git_commit *)object, source);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GIT_OBJ_TREE:
|
||||||
|
error = git_tree__writeback((git_tree *)object, source);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GIT_OBJ_TAG:
|
||||||
|
error = git_tag__writeback((git_tag *)object, source);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GIT_OBJ_BLOB:
|
||||||
|
error = git_blob__writeback((git_blob *)object, source);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
error = GIT_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error < GIT_SUCCESS) {
|
||||||
|
git_object__source_close(object);
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return write_back(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
void git_object_free(git_object *object)
|
||||||
|
{
|
||||||
|
if (object == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
git_object__source_close(object);
|
||||||
|
git_hashtable_remove(object->repo->objects, &object->id);
|
||||||
|
|
||||||
|
switch (object->source.raw.type) {
|
||||||
|
case GIT_OBJ_COMMIT:
|
||||||
|
git_commit__free((git_commit *)object);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GIT_OBJ_TREE:
|
||||||
|
git_tree__free((git_tree *)object);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GIT_OBJ_TAG:
|
||||||
|
git_tag__free((git_tag *)object);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GIT_OBJ_BLOB:
|
||||||
|
git_blob__free((git_blob *)object);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
free(object);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const git_oid *git_object_id(git_object *obj)
|
||||||
|
{
|
||||||
|
assert(obj);
|
||||||
|
|
||||||
|
if (obj->in_memory)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return &obj->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
git_otype git_object_type(git_object *obj)
|
||||||
|
{
|
||||||
|
assert(obj);
|
||||||
|
return obj->source.raw.type;
|
||||||
|
}
|
||||||
|
|
||||||
|
git_repository *git_object_owner(git_object *obj)
|
||||||
|
{
|
||||||
|
assert(obj);
|
||||||
|
return obj->repo;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *git_object_type2string(git_otype type)
|
||||||
|
{
|
||||||
|
if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
|
||||||
|
return "";
|
||||||
|
|
||||||
|
return git_objects_table[type].str;
|
||||||
|
}
|
||||||
|
|
||||||
|
git_otype git_object_string2type(const char *str)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
if (!str || !*str)
|
||||||
|
return GIT_OBJ_BAD;
|
||||||
|
|
||||||
|
for (i = 0; i < ARRAY_SIZE(git_objects_table); i++)
|
||||||
|
if (!strcmp(str, git_objects_table[i].str))
|
||||||
|
return (git_otype)i;
|
||||||
|
|
||||||
|
return GIT_OBJ_BAD;
|
||||||
|
}
|
||||||
|
|
||||||
|
int git_object_typeisloose(git_otype type)
|
||||||
|
{
|
||||||
|
if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return git_objects_table[type].loose;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t git_object__size(git_otype type)
|
||||||
|
{
|
||||||
|
if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return git_objects_table[type].size;
|
||||||
|
}
|
||||||
|
|
344
src/repository.c
344
src/repository.c
@ -47,41 +47,20 @@
|
|||||||
#define GIT_SYMREF "ref: "
|
#define GIT_SYMREF "ref: "
|
||||||
#define GIT_BRANCH_MASTER "master"
|
#define GIT_BRANCH_MASTER "master"
|
||||||
|
|
||||||
static const int default_table_size = 32;
|
static const int OBJECT_TABLE_SIZE = 32;
|
||||||
static const double max_load_factor = 0.65;
|
|
||||||
|
|
||||||
static const int OBJECT_BASE_SIZE = 4096;
|
|
||||||
|
|
||||||
static struct {
|
|
||||||
const char *str; /* type name string */
|
|
||||||
int loose; /* valid loose object type flag */
|
|
||||||
size_t size; /* size in bytes of the object structure */
|
|
||||||
} git_objects_table[] = {
|
|
||||||
{ "", 0, 0 }, /* 0 = GIT_OBJ__EXT1 */
|
|
||||||
{ "commit", 1, sizeof(git_commit)}, /* 1 = GIT_OBJ_COMMIT */
|
|
||||||
{ "tree", 1, sizeof(git_tree) }, /* 2 = GIT_OBJ_TREE */
|
|
||||||
{ "blob", 1, sizeof(git_blob) }, /* 3 = GIT_OBJ_BLOB */
|
|
||||||
{ "tag", 1, sizeof(git_tag) }, /* 4 = GIT_OBJ_TAG */
|
|
||||||
{ "", 0, 0 }, /* 5 = GIT_OBJ__EXT2 */
|
|
||||||
{ "OFS_DELTA", 0, 0 }, /* 6 = GIT_OBJ_OFS_DELTA */
|
|
||||||
{ "REF_DELTA", 0, 0 } /* 7 = GIT_OBJ_REF_DELTA */
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *path_repository;
|
char *path_repository;
|
||||||
unsigned is_bare:1,
|
unsigned is_bare:1, has_been_reinit:1;
|
||||||
has_been_reinit:1;
|
|
||||||
} repo_init;
|
} repo_init;
|
||||||
|
|
||||||
/***********************************************************
|
/*
|
||||||
|
* Hash table methods
|
||||||
*
|
*
|
||||||
* MISCELANEOUS HELPER FUNCTIONS
|
* Callbacks for the ODB cache, implemented
|
||||||
*
|
* as a hash table
|
||||||
***********************************************************/
|
*/
|
||||||
|
uint32_t object_table_hash(const void *key)
|
||||||
|
|
||||||
|
|
||||||
uint32_t git__objtable_hash(const void *key)
|
|
||||||
{
|
{
|
||||||
uint32_t r;
|
uint32_t r;
|
||||||
git_oid *id;
|
git_oid *id;
|
||||||
@ -91,7 +70,7 @@ uint32_t git__objtable_hash(const void *key)
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
int git__objtable_haskey(void *object, const void *key)
|
int object_table_hashkey(void *object, const void *key)
|
||||||
{
|
{
|
||||||
git_object *obj;
|
git_object *obj;
|
||||||
git_oid *oid;
|
git_oid *oid;
|
||||||
@ -103,7 +82,11 @@ int git__objtable_haskey(void *object, const void *key)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Git repository open methods
|
||||||
|
*
|
||||||
|
* Open a repository object from its path
|
||||||
|
*/
|
||||||
static int assign_repository_DIRs(git_repository *repo,
|
static int assign_repository_DIRs(git_repository *repo,
|
||||||
const char *git_dir,
|
const char *git_dir,
|
||||||
const char *git_object_directory,
|
const char *git_object_directory,
|
||||||
@ -221,7 +204,7 @@ static int guess_repository_DIRs(git_repository *repo, const char *repository_pa
|
|||||||
return GIT_SUCCESS;
|
return GIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
git_repository *git_repository__alloc()
|
static git_repository *repository_alloc()
|
||||||
{
|
{
|
||||||
git_repository *repo = git__malloc(sizeof(git_repository));
|
git_repository *repo = git__malloc(sizeof(git_repository));
|
||||||
if (!repo)
|
if (!repo)
|
||||||
@ -230,9 +213,9 @@ git_repository *git_repository__alloc()
|
|||||||
memset(repo, 0x0, sizeof(git_repository));
|
memset(repo, 0x0, sizeof(git_repository));
|
||||||
|
|
||||||
repo->objects = git_hashtable_alloc(
|
repo->objects = git_hashtable_alloc(
|
||||||
default_table_size,
|
OBJECT_TABLE_SIZE,
|
||||||
git__objtable_hash,
|
object_table_hash,
|
||||||
git__objtable_haskey);
|
object_table_hashkey);
|
||||||
|
|
||||||
if (repo->objects == NULL) {
|
if (repo->objects == NULL) {
|
||||||
free(repo);
|
free(repo);
|
||||||
@ -253,7 +236,7 @@ int git_repository_open2(git_repository **repo_out,
|
|||||||
|
|
||||||
assert(repo_out);
|
assert(repo_out);
|
||||||
|
|
||||||
repo = git_repository__alloc();
|
repo = repository_alloc();
|
||||||
if (repo == NULL)
|
if (repo == NULL)
|
||||||
return GIT_ENOMEM;
|
return GIT_ENOMEM;
|
||||||
|
|
||||||
@ -285,7 +268,7 @@ int git_repository_open(git_repository **repo_out, const char *path)
|
|||||||
|
|
||||||
assert(repo_out && path);
|
assert(repo_out && path);
|
||||||
|
|
||||||
repo = git_repository__alloc();
|
repo = repository_alloc();
|
||||||
if (repo == NULL)
|
if (repo == NULL)
|
||||||
return GIT_ENOMEM;
|
return GIT_ENOMEM;
|
||||||
|
|
||||||
@ -348,247 +331,12 @@ git_index *git_repository_index(git_repository *repo)
|
|||||||
return repo->index;
|
return repo->index;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int source_resize(git_odb_source *src)
|
|
||||||
{
|
|
||||||
size_t write_offset, new_size;
|
|
||||||
void *new_data;
|
|
||||||
|
|
||||||
write_offset = (size_t)((char *)src->write_ptr - (char *)src->raw.data);
|
|
||||||
|
|
||||||
new_size = src->raw.len * 2;
|
|
||||||
if ((new_data = git__malloc(new_size)) == NULL)
|
|
||||||
return GIT_ENOMEM;
|
|
||||||
|
|
||||||
memcpy(new_data, src->raw.data, src->written_bytes);
|
|
||||||
free(src->raw.data);
|
|
||||||
|
|
||||||
src->raw.data = new_data;
|
|
||||||
src->raw.len = new_size;
|
|
||||||
src->write_ptr = (char *)new_data + write_offset;
|
|
||||||
|
|
||||||
return GIT_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
int git__source_printf(git_odb_source *source, const char *format, ...)
|
|
||||||
{
|
|
||||||
va_list arglist;
|
|
||||||
int len, did_resize = 0;
|
|
||||||
|
|
||||||
assert(source->open && source->write_ptr);
|
|
||||||
|
|
||||||
va_start(arglist, format);
|
|
||||||
|
|
||||||
len = vsnprintf(source->write_ptr, source->raw.len - source->written_bytes, format, arglist);
|
|
||||||
|
|
||||||
while (source->written_bytes + len >= source->raw.len) {
|
|
||||||
if (source_resize(source) < GIT_SUCCESS)
|
|
||||||
return GIT_ENOMEM;
|
|
||||||
|
|
||||||
did_resize = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (did_resize)
|
|
||||||
vsnprintf(source->write_ptr, source->raw.len - source->written_bytes, format, arglist);
|
|
||||||
|
|
||||||
source->write_ptr = (char *)source->write_ptr + len;
|
|
||||||
source->written_bytes += len;
|
|
||||||
|
|
||||||
return GIT_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
int git__source_write(git_odb_source *source, const void *bytes, size_t len)
|
|
||||||
{
|
|
||||||
assert(source);
|
|
||||||
|
|
||||||
assert(source->open && source->write_ptr);
|
|
||||||
|
|
||||||
while (source->written_bytes + len >= source->raw.len) {
|
|
||||||
if (source_resize(source) < GIT_SUCCESS)
|
|
||||||
return GIT_ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(source->write_ptr, bytes, len);
|
|
||||||
source->write_ptr = (char *)source->write_ptr + len;
|
|
||||||
source->written_bytes += len;
|
|
||||||
|
|
||||||
return GIT_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void prepare_write(git_object *object)
|
|
||||||
{
|
|
||||||
if (object->source.write_ptr != NULL || object->source.open)
|
|
||||||
git_object__source_close(object);
|
|
||||||
|
|
||||||
/* TODO: proper size calculation */
|
|
||||||
object->source.raw.data = git__malloc(OBJECT_BASE_SIZE);
|
|
||||||
object->source.raw.len = OBJECT_BASE_SIZE;
|
|
||||||
|
|
||||||
object->source.write_ptr = object->source.raw.data;
|
|
||||||
object->source.written_bytes = 0;
|
|
||||||
|
|
||||||
object->source.open = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int write_back(git_object *object)
|
|
||||||
{
|
|
||||||
int error;
|
|
||||||
git_oid new_id;
|
|
||||||
|
|
||||||
assert(object);
|
|
||||||
|
|
||||||
assert(object->source.open);
|
|
||||||
assert(object->modified);
|
|
||||||
|
|
||||||
object->source.raw.len = object->source.written_bytes;
|
|
||||||
|
|
||||||
if ((error = git_odb_write(&new_id, object->repo->db, &object->source.raw)) < GIT_SUCCESS)
|
|
||||||
return error;
|
|
||||||
|
|
||||||
if (!object->in_memory)
|
|
||||||
git_hashtable_remove(object->repo->objects, &object->id);
|
|
||||||
|
|
||||||
git_oid_cpy(&object->id, &new_id);
|
|
||||||
git_hashtable_insert(object->repo->objects, &object->id, object);
|
|
||||||
|
|
||||||
object->source.write_ptr = NULL;
|
|
||||||
object->source.written_bytes = 0;
|
|
||||||
|
|
||||||
object->modified = 0;
|
|
||||||
object->in_memory = 0;
|
|
||||||
|
|
||||||
git_object__source_close(object);
|
|
||||||
return GIT_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
int git_object__source_open(git_object *object)
|
|
||||||
{
|
|
||||||
int error;
|
|
||||||
|
|
||||||
assert(object && !object->in_memory);
|
|
||||||
|
|
||||||
if (object->source.open)
|
|
||||||
git_object__source_close(object);
|
|
||||||
|
|
||||||
error = git_odb_read(&object->source.raw, object->repo->db, &object->id);
|
|
||||||
if (error < GIT_SUCCESS)
|
|
||||||
return error;
|
|
||||||
|
|
||||||
object->source.open = 1;
|
|
||||||
return GIT_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
void git_object__source_close(git_object *object)
|
|
||||||
{
|
|
||||||
assert(object);
|
|
||||||
|
|
||||||
if (object->source.open) {
|
|
||||||
git_rawobj_close(&object->source.raw);
|
|
||||||
object->source.open = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int git_object_write(git_object *object)
|
|
||||||
{
|
|
||||||
int error;
|
|
||||||
git_odb_source *source;
|
|
||||||
|
|
||||||
assert(object);
|
|
||||||
|
|
||||||
if (object->modified == 0)
|
|
||||||
return GIT_SUCCESS;
|
|
||||||
|
|
||||||
prepare_write(object);
|
|
||||||
source = &object->source;
|
|
||||||
|
|
||||||
switch (source->raw.type) {
|
|
||||||
case GIT_OBJ_COMMIT:
|
|
||||||
error = git_commit__writeback((git_commit *)object, source);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case GIT_OBJ_TREE:
|
|
||||||
error = git_tree__writeback((git_tree *)object, source);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case GIT_OBJ_TAG:
|
|
||||||
error = git_tag__writeback((git_tag *)object, source);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case GIT_OBJ_BLOB:
|
|
||||||
error = git_blob__writeback((git_blob *)object, source);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
error = GIT_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (error < GIT_SUCCESS) {
|
|
||||||
git_object__source_close(object);
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
|
|
||||||
return write_back(object);
|
|
||||||
}
|
|
||||||
|
|
||||||
void git_object_free(git_object *object)
|
|
||||||
{
|
|
||||||
if (object == NULL)
|
|
||||||
return;
|
|
||||||
|
|
||||||
git_object__source_close(object);
|
|
||||||
git_hashtable_remove(object->repo->objects, &object->id);
|
|
||||||
|
|
||||||
switch (object->source.raw.type) {
|
|
||||||
case GIT_OBJ_COMMIT:
|
|
||||||
git_commit__free((git_commit *)object);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case GIT_OBJ_TREE:
|
|
||||||
git_tree__free((git_tree *)object);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case GIT_OBJ_TAG:
|
|
||||||
git_tag__free((git_tag *)object);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case GIT_OBJ_BLOB:
|
|
||||||
git_blob__free((git_blob *)object);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
free(object);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
git_odb *git_repository_database(git_repository *repo)
|
git_odb *git_repository_database(git_repository *repo)
|
||||||
{
|
{
|
||||||
assert(repo);
|
assert(repo);
|
||||||
return repo->db;
|
return repo->db;
|
||||||
}
|
}
|
||||||
|
|
||||||
const git_oid *git_object_id(git_object *obj)
|
|
||||||
{
|
|
||||||
assert(obj);
|
|
||||||
|
|
||||||
if (obj->in_memory)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
return &obj->id;
|
|
||||||
}
|
|
||||||
|
|
||||||
git_otype git_object_type(git_object *obj)
|
|
||||||
{
|
|
||||||
assert(obj);
|
|
||||||
return obj->source.raw.type;
|
|
||||||
}
|
|
||||||
|
|
||||||
git_repository *git_object_owner(git_object *obj)
|
|
||||||
{
|
|
||||||
assert(obj);
|
|
||||||
return obj->repo;
|
|
||||||
}
|
|
||||||
|
|
||||||
int git_repository_newobject(git_object **object_out, git_repository *repo, git_otype type)
|
int git_repository_newobject(git_object **object_out, git_repository *repo, git_otype type)
|
||||||
{
|
{
|
||||||
git_object *object = NULL;
|
git_object *object = NULL;
|
||||||
@ -608,12 +356,12 @@ int git_repository_newobject(git_object **object_out, git_repository *repo, git_
|
|||||||
return GIT_EINVALIDTYPE;
|
return GIT_EINVALIDTYPE;
|
||||||
}
|
}
|
||||||
|
|
||||||
object = git__malloc(git_objects_table[type].size);
|
object = git__malloc(git_object__size(type));
|
||||||
|
|
||||||
if (object == NULL)
|
if (object == NULL)
|
||||||
return GIT_ENOMEM;
|
return GIT_ENOMEM;
|
||||||
|
|
||||||
memset(object, 0x0, git_objects_table[type].size);
|
memset(object, 0x0, git_object__size(type));
|
||||||
object->repo = repo;
|
object->repo = repo;
|
||||||
object->in_memory = 1;
|
object->in_memory = 1;
|
||||||
object->modified = 1;
|
object->modified = 1;
|
||||||
@ -649,12 +397,12 @@ int git_repository_lookup(git_object **object_out, git_repository *repo, const g
|
|||||||
|
|
||||||
type = obj_file.type;
|
type = obj_file.type;
|
||||||
|
|
||||||
object = git__malloc(git_objects_table[type].size);
|
object = git__malloc(git_object__size(type));
|
||||||
|
|
||||||
if (object == NULL)
|
if (object == NULL)
|
||||||
return GIT_ENOMEM;
|
return GIT_ENOMEM;
|
||||||
|
|
||||||
memset(object, 0x0, git_objects_table[type].size);
|
memset(object, 0x0, git_object__size(type));
|
||||||
|
|
||||||
/* Initialize parent object */
|
/* Initialize parent object */
|
||||||
git_oid_cpy(&object->id, id);
|
git_oid_cpy(&object->id, id);
|
||||||
@ -696,50 +444,6 @@ int git_repository_lookup(git_object **object_out, git_repository *repo, const g
|
|||||||
return GIT_SUCCESS;
|
return GIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define GIT_NEWOBJECT_TEMPLATE(obj, tp) \
|
|
||||||
int git_##obj##_new(git_##obj **o, git_repository *repo) {\
|
|
||||||
return git_repository_newobject((git_object **)o, repo, GIT_OBJ_##tp); } \
|
|
||||||
int git_##obj##_lookup(git_##obj **o, git_repository *repo, const git_oid *id) { \
|
|
||||||
return git_repository_lookup((git_object **)o, repo, id, GIT_OBJ_##tp); }
|
|
||||||
|
|
||||||
GIT_NEWOBJECT_TEMPLATE(commit, COMMIT)
|
|
||||||
GIT_NEWOBJECT_TEMPLATE(tag, TAG)
|
|
||||||
GIT_NEWOBJECT_TEMPLATE(tree, TREE)
|
|
||||||
GIT_NEWOBJECT_TEMPLATE(blob, BLOB)
|
|
||||||
|
|
||||||
|
|
||||||
const char *git_object_type2string(git_otype type)
|
|
||||||
{
|
|
||||||
if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
|
|
||||||
return "";
|
|
||||||
|
|
||||||
return git_objects_table[type].str;
|
|
||||||
}
|
|
||||||
|
|
||||||
git_otype git_object_string2type(const char *str)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
if (!str || !*str)
|
|
||||||
return GIT_OBJ_BAD;
|
|
||||||
|
|
||||||
for (i = 0; i < ARRAY_SIZE(git_objects_table); i++)
|
|
||||||
if (!strcmp(str, git_objects_table[i].str))
|
|
||||||
return (git_otype)i;
|
|
||||||
|
|
||||||
return GIT_OBJ_BAD;
|
|
||||||
}
|
|
||||||
|
|
||||||
int git_object_typeisloose(git_otype type)
|
|
||||||
{
|
|
||||||
if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
return git_objects_table[type].loose;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int repo_init_reinit(repo_init *results)
|
static int repo_init_reinit(repo_init *results)
|
||||||
{
|
{
|
||||||
/* TODO: reinit the repository */
|
/* TODO: reinit the repository */
|
||||||
|
@ -36,7 +36,6 @@ struct git_repository {
|
|||||||
unsigned is_bare:1;
|
unsigned is_bare:1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
int git_object__source_open(git_object *object);
|
int git_object__source_open(git_object *object);
|
||||||
void git_object__source_close(git_object *object);
|
void git_object__source_close(git_object *object);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user