#include "blob.h"
#include "fileops.h"
-#define GIT_FOLDER "/.git/"
-#define GIT_OBJECTS_FOLDER "objects/"
-#define GIT_REFS_FOLDER "refs/"
-#define GIT_INDEX_FILE "index"
-#define GIT_HEAD_FILE "HEAD"
-
-#define GIT_SYMREF "ref: "
-#define GIT_REFS_HEADS "refs/heads/"
-#define GIT_REFS_TAGS "refs/tags/"
+#include "refs.h"
+
+#define GIT_OBJECTS_INFO_DIR GIT_OBJECTS_DIR "info/"
+#define GIT_OBJECTS_PACK_DIR GIT_OBJECTS_DIR "pack/"
+
#define GIT_BRANCH_MASTER "master"
-static const int default_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 git_repository_init_results {
+typedef struct {
char *path_repository;
+ unsigned is_bare:1, has_been_reinit:1;
+} repo_init;
- unsigned is_bare:1;
- unsigned has_been_reinit:1;
-} git_repository_init_results;
-
-/***********************************************************
- *
- * MISCELANEOUS HELPER FUNCTIONS
+/*
+ * Git repository open methods
*
- ***********************************************************/
-
-
-
-uint32_t git__objtable_hash(const void *key)
-{
- uint32_t r;
- git_oid *id;
-
- id = (git_oid *)key;
- memcpy(&r, id->id, sizeof(r));
- return r;
-}
-
-int git__objtable_haskey(void *object, const void *key)
-{
- git_object *obj;
- git_oid *oid;
-
- obj = (git_object *)object;
- oid = (git_oid *)key;
-
- return (git_oid_cmp(oid, &obj->id) == 0);
-}
-
-
-
-static int assign_repository_folders(git_repository *repo,
+ * Open a repository object from its path
+ */
+static int assign_repository_dirs(
+ git_repository *repo,
const char *git_dir,
const char *git_object_directory,
const char *git_index_file,
const char *git_work_tree)
{
char path_aux[GIT_PATH_MAX];
- size_t path_len;
+ int error = GIT_SUCCESS;
assert(repo);
- if (git_dir == NULL || gitfo_isdir(git_dir) < GIT_SUCCESS)
- return GIT_ENOTFOUND;
-
- /* store GIT_DIR */
- path_len = strlen(git_dir);
- strcpy(path_aux, git_dir);
-
- if (path_aux[path_len - 1] != '/') {
- path_aux[path_len] = '/';
- path_aux[path_len + 1] = 0;
+ if (git_dir == NULL)
+ return git__throw(GIT_ENOTFOUND, "Failed to open repository. Git dir not found");
- path_len = path_len + 1;
- }
+ error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_dir);
+ if (error < GIT_SUCCESS)
+ return git__rethrow(error, "Failed to open repository");
+ /* store GIT_DIR */
repo->path_repository = git__strdup(path_aux);
+ if (repo->path_repository == NULL)
+ return GIT_ENOMEM;
- /* store GIT_OBJECT_DIRECTORY */
+ /* path to GIT_OBJECT_DIRECTORY */
if (git_object_directory == NULL)
- strcpy(path_aux + path_len, GIT_OBJECTS_FOLDER);
- else
- strcpy(path_aux, git_object_directory);
-
- if (gitfo_isdir(path_aux) < GIT_SUCCESS)
- return GIT_ENOTFOUND;
+ git__joinpath(path_aux, repo->path_repository, GIT_OBJECTS_DIR);
+ else {
+ error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_object_directory);
+ if (error < GIT_SUCCESS)
+ return git__rethrow(error, "Failed to open repository");
+ }
+ /* Store GIT_OBJECT_DIRECTORY */
repo->path_odb = git__strdup(path_aux);
+ if (repo->path_odb == NULL)
+ return GIT_ENOMEM;
-
- /* store GIT_INDEX_FILE */
- if (git_index_file == NULL)
- strcpy(path_aux + path_len, GIT_INDEX_FILE);
- else
- strcpy(path_aux, git_index_file);
-
- if (gitfo_exists(path_aux) < 0)
- return GIT_ENOTFOUND;
-
- repo->path_index = git__strdup(path_aux);
-
-
- /* store GIT_WORK_TREE */
+ /* path to GIT_WORK_TREE */
if (git_work_tree == NULL)
repo->is_bare = 1;
- else
- repo->path_workdir = git__strdup(git_work_tree);
+ else {
+ error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_work_tree);
+ if (error < GIT_SUCCESS)
+ return git__rethrow(error, "Failed to open repository");
+ /* Store GIT_WORK_TREE */
+ repo->path_workdir = git__strdup(path_aux);
+ if (repo->path_workdir == NULL)
+ return GIT_ENOMEM;
+
+ /* Path to GIT_INDEX_FILE */
+ if (git_index_file == NULL)
+ git__joinpath(path_aux, repo->path_repository, GIT_INDEX_FILE);
+ else {
+ error = gitfo_prettify_file_path(path_aux, sizeof(path_aux), git_index_file);
+ if (error < GIT_SUCCESS)
+ return git__rethrow(error, "Failed to open repository");
+ }
+
+ /* store GIT_INDEX_FILE */
+ repo->path_index = git__strdup(path_aux);
+ if (repo->path_index == NULL)
+ return GIT_ENOMEM;
+ }
+
return GIT_SUCCESS;
}
-static int guess_repository_folders(git_repository *repo, const char *repository_path)
+static int check_repository_dirs(git_repository *repo)
{
- char path_aux[GIT_PATH_MAX], *last_folder;
- int path_len;
-
- if (gitfo_isdir(repository_path) < GIT_SUCCESS)
- return GIT_ENOTAREPO;
-
- path_len = strlen(repository_path);
- strcpy(path_aux, repository_path);
-
- if (path_aux[path_len - 1] != '/') {
- path_aux[path_len] = '/';
- path_aux[path_len + 1] = 0;
-
- path_len = path_len + 1;
- }
+ char path_aux[GIT_PATH_MAX];
- repo->path_repository = git__strdup(path_aux);
+ if (gitfo_isdir(repo->path_repository) < GIT_SUCCESS)
+ return git__throw(GIT_ENOTAREPO, "`%s` is not a folder", repo->path_repository);
- /* objects database */
- strcpy(path_aux + path_len, GIT_OBJECTS_FOLDER);
- if (gitfo_isdir(path_aux) < GIT_SUCCESS)
- return GIT_ENOTAREPO;
- repo->path_odb = git__strdup(path_aux);
+ /* Ensure GIT_OBJECT_DIRECTORY exists */
+ if (gitfo_isdir(repo->path_odb) < GIT_SUCCESS)
+ return git__throw(GIT_ENOTAREPO, "`%s` does not exist", repo->path_odb);
- /* HEAD file */
- strcpy(path_aux + path_len, GIT_HEAD_FILE);
+ /* Ensure HEAD file exists */
+ git__joinpath(path_aux, repo->path_repository, GIT_HEAD_FILE);
if (gitfo_exists(path_aux) < 0)
- return GIT_ENOTAREPO;
-
- path_aux[path_len] = 0;
-
- last_folder = (path_aux + path_len - 2);
+ return git__throw(GIT_ENOTAREPO, "HEAD file is missing");
- while (*last_folder != '/')
- last_folder--;
-
- if (strcmp(last_folder, GIT_FOLDER) == 0) {
- repo->is_bare = 0;
+ return GIT_SUCCESS;
+}
- /* index file */
- strcpy(path_aux + path_len, GIT_INDEX_FILE);
- repo->path_index = git__strdup(path_aux);
+static int guess_repository_dirs(git_repository *repo, const char *repository_path)
+{
+ char buffer[GIT_PATH_MAX];
+ const char *path_work_tree = NULL;
- /* working dir */
- *(last_folder + 1) = 0;
- repo->path_workdir = git__strdup(path_aux);
+ /* Git directory name */
+ if (git__basename_r(buffer, sizeof(buffer), repository_path) < 0)
+ return git__throw(GIT_EINVALIDPATH, "Unable to parse folder name from `%s`", repository_path);
- } else {
- repo->is_bare = 1;
- repo->path_workdir = NULL;
+ if (strcmp(buffer, DOT_GIT) == 0) {
+ /* Path to working dir */
+ if (git__dirname_r(buffer, sizeof(buffer), repository_path) < 0)
+ return git__throw(GIT_EINVALIDPATH, "Unable to parse parent folder name from `%s`", repository_path);
+ path_work_tree = buffer;
}
- return GIT_SUCCESS;
+ return assign_repository_dirs(repo, repository_path, NULL, NULL, path_work_tree);
}
-git_repository *git_repository__alloc()
+static git_repository *repository_alloc()
{
+ int error;
+
git_repository *repo = git__malloc(sizeof(git_repository));
if (!repo)
return NULL;
memset(repo, 0x0, sizeof(git_repository));
- repo->objects = git_hashtable_alloc(
- default_table_size,
- git__objtable_hash,
- git__objtable_haskey);
+ error = git_cache_init(&repo->objects, GIT_DEFAULT_CACHE_SIZE, &git_object__free);
+ if (error < GIT_SUCCESS) {
+ free(repo);
+ return NULL;
+ }
- if (repo->objects == NULL) {
+ if (git_repository__refcache_init(&repo->references) < GIT_SUCCESS) {
free(repo);
return NULL;
}
return repo;
}
+static int init_odb(git_repository *repo)
+{
+ return git_odb_open(&repo->db, repo->path_odb); /* TODO: Move odb.c to new error handling */
+}
+
+int git_repository_open3(git_repository **repo_out,
+ const char *git_dir,
+ git_odb *object_database,
+ const char *git_index_file,
+ const char *git_work_tree)
+{
+ git_repository *repo;
+ int error = GIT_SUCCESS;
+
+ assert(repo_out);
+
+ if (object_database == NULL)
+ return git__throw(GIT_EINVALIDARGS, "Failed to open repository. `object_database` can't be null");
+
+ repo = repository_alloc();
+ if (repo == NULL)
+ return GIT_ENOMEM;
+
+ error = assign_repository_dirs(repo,
+ git_dir,
+ NULL,
+ git_index_file,
+ git_work_tree);
+
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ error = check_repository_dirs(repo);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ repo->db = object_database;
+
+ *repo_out = repo;
+ return GIT_SUCCESS;
+
+cleanup:
+ git_repository_free(repo);
+ return git__rethrow(error, "Failed to open repository");
+}
+
+
int git_repository_open2(git_repository **repo_out,
const char *git_dir,
const char *git_object_directory,
assert(repo_out);
- repo = git_repository__alloc();
+ repo = repository_alloc();
if (repo == NULL)
return GIT_ENOMEM;
- error = assign_repository_folders(repo,
+ error = assign_repository_dirs(repo,
git_dir,
git_object_directory,
git_index_file,
if (error < GIT_SUCCESS)
goto cleanup;
- error = git_odb_open(&repo->db, repo->path_odb);
+ error = check_repository_dirs(repo);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ error = init_odb(repo);
if (error < GIT_SUCCESS)
goto cleanup;
cleanup:
git_repository_free(repo);
- return error;
+ return git__rethrow(error, "Failed to open repository");
}
int git_repository_open(git_repository **repo_out, const char *path)
assert(repo_out && path);
- repo = git_repository__alloc();
+ repo = repository_alloc();
if (repo == NULL)
return GIT_ENOMEM;
- error = guess_repository_folders(repo, path);
+ error = guess_repository_dirs(repo, path);
if (error < GIT_SUCCESS)
goto cleanup;
+ error = check_repository_dirs(repo);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
- error = git_odb_open(&repo->db, repo->path_odb);
+ error = init_odb(repo);
if (error < GIT_SUCCESS)
goto cleanup;
cleanup:
git_repository_free(repo);
- return error;
+ return git__rethrow(error, "Failed to open repository");
}
void git_repository_free(git_repository *repo)
{
- git_hashtable_iterator it;
- git_object *object;
-
if (repo == NULL)
return;
+ git_cache_free(&repo->objects);
+ git_repository__refcache_free(&repo->references);
+
free(repo->path_workdir);
free(repo->path_index);
free(repo->path_repository);
free(repo->path_odb);
- git_hashtable_iterator_init(repo->objects, &it);
-
- while ((object = (git_object *)
- git_hashtable_iterator_next(&it)) != NULL)
- git_object_free(object);
-
- git_hashtable_free(repo->objects);
-
if (repo->db != NULL)
git_odb_close(repo->db);
- if (repo->index != NULL)
- git_index_free(repo->index);
-
free(repo);
}
-git_index *git_repository_index(git_repository *repo)
-{
- if (repo->index == NULL) {
- if (git_index_open_inrepo(&repo->index, repo) < GIT_SUCCESS)
- return NULL;
-
- assert(repo->index);
- }
-
- return repo->index;
-}
-
-static int source_resize(git_odb_source *src)
+git_odb *git_repository_database(git_repository *repo)
{
- 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;
+ assert(repo);
+ return repo->db;
}
-int git__source_printf(git_odb_source *source, const char *format, ...)
+static int repo_init_reinit(repo_init *results)
{
- 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;
+ /* TODO: reinit the repository */
+ results->has_been_reinit = 1;
+ return git__throw(GIT_ENOTIMPLEMENTED, "Failed to reinitialize the repository. This feature is not yet implemented");
}
-int git__source_write(git_odb_source *source, const void *bytes, size_t len)
+static int repo_init_createhead(git_repository *repo)
{
- 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;
+ git_reference *head_reference;
+ return git_reference_create_symbolic(&head_reference, repo, GIT_HEAD_FILE, GIT_REFS_HEADS_MASTER_FILE); /* TODO: finalize moving refs.c to new error handling */
}
-static void prepare_write(git_object *object)
+static int repo_init_check_head_existence(char * repository_path)
{
- 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;
+ char temp_path[GIT_PATH_MAX];
- object->source.open = 1;
+ git__joinpath(temp_path, repository_path, GIT_HEAD_FILE);
+ return gitfo_exists(temp_path);
}
-static int write_back(git_object *object)
+static int repo_init_structure(repo_init *results)
{
+ const int mode = 0755; /* or 0777 ? */
int error;
- git_oid new_id;
-
- assert(object);
- assert(object->source.open);
- assert(object->modified);
+ char temp_path[GIT_PATH_MAX];
+ char *git_dir = results->path_repository;
- object->source.raw.len = object->source.written_bytes;
+ if (gitfo_mkdir_recurs(git_dir, mode))
+ return git__throw(GIT_ERROR, "Failed to initialize repository structure. Could not mkdir");
- if ((error = git_odb_write(&new_id, object->repo->db, &object->source.raw)) < GIT_SUCCESS)
- return error;
+ /* Creates the '/objects/info/' directory */
+ git__joinpath(temp_path, git_dir, GIT_OBJECTS_INFO_DIR);
+ error = gitfo_mkdir_recurs(temp_path, mode);
+ if (error < GIT_SUCCESS)
+ return git__rethrow(error, "Failed to initialize repository structure");
- if (!object->in_memory)
- git_hashtable_remove(object->repo->objects, &object->id);
+ /* Creates the '/objects/pack/' directory */
+ git__joinpath(temp_path, git_dir, GIT_OBJECTS_PACK_DIR);
+ error = gitfo_mkdir(temp_path, mode);
+ if (error < GIT_SUCCESS)
+ return git__throw(error, "Unable to create `%s` folder", temp_path);
- git_oid_cpy(&object->id, &new_id);
- git_hashtable_insert(object->repo->objects, &object->id, object);
+ /* Creates the '/refs/heads/' directory */
+ git__joinpath(temp_path, git_dir, GIT_REFS_HEADS_DIR);
+ error = gitfo_mkdir_recurs(temp_path, mode);
+ if (error < GIT_SUCCESS)
+ return git__rethrow(error, "Failed to initialize repository structure");
- object->source.write_ptr = NULL;
- object->source.written_bytes = 0;
+ /* Creates the '/refs/tags/' directory */
+ git__joinpath(temp_path, git_dir, GIT_REFS_TAGS_DIR);
+ error = gitfo_mkdir(temp_path, mode);
+ if (error < GIT_SUCCESS)
+ return git__throw(error, "Unable to create `%s` folder", temp_path);
- object->modified = 0;
- object->in_memory = 0;
+ /* TODO: what's left? templates? */
- git_object__source_close(object);
return GIT_SUCCESS;
}
-int git_object__source_open(git_object *object)
+static int repo_init_find_dir(repo_init *results, const char* path)
{
- int error;
-
- assert(object && !object->in_memory);
-
- if (object->source.open)
- git_object__source_close(object);
+ char temp_path[GIT_PATH_MAX];
+ int error = GIT_SUCCESS;
- error = git_odb_read(&object->source.raw, object->repo->db, &object->id);
+ error = gitfo_prettify_dir_path(temp_path, sizeof(temp_path), path);
if (error < GIT_SUCCESS)
- return error;
-
- object->source.open = 1;
- return GIT_SUCCESS;
-}
-
-void git_object__source_close(git_object *object)
-{
- assert(object);
+ return git__rethrow(error, "Failed to find directory to initialize repository");
- 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)
-{
- assert(repo);
- 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)
-{
- git_object *object = NULL;
-
- assert(object_out && repo);
-
- *object_out = NULL;
-
- switch (type) {
- case GIT_OBJ_COMMIT:
- case GIT_OBJ_TAG:
- case GIT_OBJ_TREE:
- case GIT_OBJ_BLOB:
- break;
-
- default:
- return GIT_EINVALIDTYPE;
+ if (!results->is_bare) {
+ git__joinpath(temp_path, temp_path, GIT_DIR);
}
- object = git__malloc(git_objects_table[type].size);
-
- if (object == NULL)
+ results->path_repository = git__strdup(temp_path);
+ if (results->path_repository == NULL)
return GIT_ENOMEM;
- memset(object, 0x0, git_objects_table[type].size);
- 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)
+int git_repository_init(git_repository **repo_out, const char *path, unsigned is_bare)
{
- git_object *object = NULL;
- git_rawobj obj_file;
int error = GIT_SUCCESS;
+ git_repository *repo = NULL;
+ repo_init results;
+
+ assert(repo_out && path);
- assert(repo && object_out && id);
-
- object = git_hashtable_lookup(repo->objects, id);
- if (object != NULL) {
- *object_out = object;
- return GIT_SUCCESS;
- }
+ results.path_repository = NULL;
+ results.is_bare = is_bare;
- error = git_odb_read(&obj_file, repo->db, id);
+ error = repo_init_find_dir(&results, path);
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;
-
- object = git__malloc(git_objects_table[type].size);
-
- if (object == NULL)
- return GIT_ENOMEM;
-
- memset(object, 0x0, git_objects_table[type].size);
-
- /* 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;
+ goto cleanup;
- case GIT_OBJ_BLOB:
- error = git_blob__parse((git_blob *)object);
- break;
+ if (!repo_init_check_head_existence(results.path_repository))
+ return repo_init_reinit(&results);
- default:
- break;
- }
+ error = repo_init_structure(&results);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
- if (error < GIT_SUCCESS) {
- git_object_free(object);
- return error;
+ repo = repository_alloc();
+ if (repo == NULL) {
+ error = GIT_ENOMEM;
+ goto cleanup;
}
- git_object__source_close(object);
- git_hashtable_insert(repo->objects, &object->id, object);
-
- *object_out = object;
- 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)
-
+ error = guess_repository_dirs(repo, results.path_repository);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
-const char *git_object_type2string(git_otype type)
-{
- if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
- return "";
+ assert(repo->is_bare == is_bare);
- return git_objects_table[type].str;
-}
+ error = init_odb(repo);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
-git_otype git_object_string2type(const char *str)
-{
- size_t i;
+ error = repo_init_createhead(repo);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
- if (!str || !*str)
- return GIT_OBJ_BAD;
+ /* should never fail */
+ assert(check_repository_dirs(repo) == GIT_SUCCESS);
- for (i = 0; i < ARRAY_SIZE(git_objects_table); i++)
- if (!strcmp(str, git_objects_table[i].str))
- return (git_otype)i;
+ free(results.path_repository);
+ *repo_out = repo;
+ return GIT_SUCCESS;
- return GIT_OBJ_BAD;
+cleanup:
+ free(results.path_repository);
+ git_repository_free(repo);
+ return git__rethrow(error, "Failed to (re)init the repository `%s`", path);
}
-int git_object_typeisloose(git_otype type)
+int git_repository_is_empty(git_repository *repo)
{
- if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
- return 0;
+ git_reference *head, *branch;
+ int error;
- return git_objects_table[type].loose;
-}
+ error = git_reference_lookup(&head, repo, "HEAD");
+ if (error < GIT_SUCCESS)
+ return git__throw(error, "Failed to determine the emptiness of the repository. An error occured while retrieving the HEAD reference");
-int git_repository_init__reinit(git_repository_init_results* results)
-{
- /* To be implemented */
+ if (git_reference_type(head) != GIT_REF_SYMBOLIC)
+ return git__throw(GIT_EOBJCORRUPTED, "Failed to determine the emptiness of the repository. HEAD is probably in detached state");
- results->has_been_reinit = 1;
- return GIT_SUCCESS;
+ return git_reference_resolve(&branch, head) == GIT_SUCCESS ? 0 : 1;
}
-int git_repository_init__create_head(const char* head_path)
+const char *git_repository_path(git_repository *repo)
{
- git_file fd;
- int error = GIT_SUCCESS;
- char head_symlink[50];
- int len;
-
- len = sprintf(head_symlink, "%s %s%s\n", GIT_SYMREF, GIT_REFS_HEADS, GIT_BRANCH_MASTER);
-
- if ((fd = gitfo_creat(head_path, S_IREAD | S_IWRITE)) < 0)
- return GIT_ERROR;
-
- error = gitfo_write(fd, (void*)head_symlink, strlen(head_symlink));
- if (error < GIT_SUCCESS)
- goto cleanup;
-
-cleanup:
- gitfo_close(fd);
- return error;
+ assert(repo);
+ return repo->path_repository;
}
-int git_repository_init__create_structure_or_reinit(git_repository_init_results* results)
+const char *git_repository_workdir(git_repository *repo)
{
- char temp_path[GIT_PATH_MAX];
- int path_len;
- const int mode = 0755; /* or 0777 ? */
-
- char* git_dir = results->path_repository;
-
- //TODO : Ensure the parent tree structure already exists by recursively building it through gitfo_mkdir_recurs().
- if (gitfo_mkdir_recurs(git_dir, mode))
- return GIT_ERROR;
-
- path_len = strlen(git_dir);
- strcpy(temp_path, git_dir);
-
- /* Does HEAD file already exist ? */
- strcpy(temp_path + path_len, GIT_HEAD_FILE);
-
- if (!gitfo_exists(temp_path) && gitfo_isdir(temp_path) < GIT_SUCCESS)
- {
- /* Something very wrong has happened to this repository :-) */
- return GIT_ERROR;
- }
-
- if (!gitfo_exists(temp_path))
- {
- return git_repository_init__reinit(results);
- }
-
- if (git_repository_init__create_head(temp_path) < GIT_SUCCESS)
- return GIT_ERROR;
-
- /* Creates the '/objects/' directory */
- strcpy(temp_path + path_len, GIT_OBJECTS_FOLDER);
- if (gitfo_mkdir(temp_path, mode))
- return GIT_ERROR;
-
- /* Creates the '/refs/' directory */
- strcpy(temp_path + path_len, GIT_REFS_FOLDER);
- if (gitfo_mkdir(temp_path, mode))
- return GIT_ERROR;
-
- /* Creates the '/refs/heads/' directory */
- strcpy(temp_path + path_len, GIT_REFS_HEADS);
- if (gitfo_mkdir(temp_path, mode))
- return GIT_ERROR;
-
- /* Creates the '/refs/tags/' directory */
- strcpy(temp_path + path_len, GIT_REFS_TAGS);
- if (gitfo_mkdir(temp_path, mode))
- return GIT_ERROR;
-
-
- /* To be implemented */
-
- return GIT_SUCCESS;
+ assert(repo);
+ return repo->path_workdir;
}
-int git_repository_init__assign_git_directory(git_repository_init_results* results, const char* path)
+int git_repository_is_bare(git_repository *repo)
{
- const int MAX_GITDIR_TREE_STRUCTURE_PATH_LENGTH = 66; // TODO: How many ?
- char temp_path[GIT_PATH_MAX];
- int path_len;
-
- path_len = strlen(path);
- strcpy(temp_path, path);
-
- /* Ensure path has a trailing slash */
- if (temp_path[path_len - 1] != '/') {
- temp_path[path_len] = '/';
- temp_path[path_len + 1] = 0;
-
- path_len = path_len + 1;
- }
-
- assert(path_len < GIT_PATH_MAX - MAX_GITDIR_TREE_STRUCTURE_PATH_LENGTH);
-
- if (!results->is_bare)
- {
- strcpy(temp_path + path_len - 1, GIT_FOLDER);
- path_len = path_len + strlen(GIT_FOLDER) - 1; /* Skip the leading slash from the constant */
- }
-
- results->path_repository = git__strdup(temp_path);
-
- return GIT_SUCCESS;
+ assert(repo);
+ return repo->is_bare;
}
-
-int git_repository_init(git_repository** repo_out, const char* path, unsigned is_bare)
-{
- git_repository_init_results results;
- int error = GIT_SUCCESS;
-
- assert(repo_out && path);
-
- results.is_bare = is_bare;
-
- error = git_repository_init__assign_git_directory(&results, path);
- if (error < GIT_SUCCESS)
- goto cleanup;
-
- error = git_repository_init__create_structure_or_reinit(&results);
- if (error < GIT_SUCCESS)
- goto cleanup;
-
- error = git_repository_open(repo_out, results.path_repository);
- if (error < GIT_SUCCESS)
- goto cleanup;
-
-cleanup:
- free(results.path_repository);
- return error;
-}
\ No newline at end of file