Move repository.c to the new error handling
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
diff --git a/src/cache.c b/src/cache.c
index fd42e2c..a372df1 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -51,7 +51,7 @@ void git_cache_init(git_cache *cache, size_t size, git_cached_obj_freeptr free_p
cache->lru_count = 0;
cache->free_obj = free_ptr;
- cache->nodes = git__malloc((size + 1) * sizeof(cache_node));
+ cache->nodes = git__malloc((size + 1) * sizeof(cache_node)); //TODO: How should we deal with GIT_ENOMEM?
for (i = 0; i < (size + 1); ++i) {
git_mutex_init(&cache->nodes[i].lock);
diff --git a/src/repository.c b/src/repository.c
index 8cc2644..8a5934f 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -124,16 +124,16 @@ static int check_repository_dirs(git_repository *repo)
char path_aux[GIT_PATH_MAX];
if (gitfo_isdir(repo->path_repository) < GIT_SUCCESS)
- return GIT_ENOTAREPO;
+ return git__throw(GIT_ENOTAREPO, "`%s` is not a folder", repo->path_repository);
/* Ensure GIT_OBJECT_DIRECTORY exists */
if (gitfo_isdir(repo->path_odb) < GIT_SUCCESS)
- return GIT_ENOTAREPO;
+ return git__throw(GIT_ENOTAREPO, "`%s` does not exist", repo->path_odb);
/* Ensure HEAD file exists */
git__joinpath(path_aux, repo->path_repository, GIT_HEAD_FILE);
if (gitfo_exists(path_aux) < 0)
- return GIT_ENOTAREPO;
+ return git__throw(GIT_ENOTAREPO, "HEAD file is missing");
return GIT_SUCCESS;
}
@@ -145,12 +145,12 @@ static int guess_repository_dirs(git_repository *repo, const char *repository_pa
/* Git directory name */
if (git__basename_r(buffer, sizeof(buffer), repository_path) < 0)
- return GIT_EINVALIDPATH;
+ return git__throw(GIT_EINVALIDPATH, "Unable to parse folder name from `%s`", repository_path);
if (strcmp(buffer, DOT_GIT) == 0) {
/* Path to working dir */
if (git__dirname_r(buffer, sizeof(buffer), repository_path) < 0)
- return GIT_EINVALIDPATH;
+ return git__throw(GIT_EINVALIDPATH, "Unable to parse parent folder name from `%s`", repository_path);
path_work_tree = buffer;
}
@@ -177,7 +177,7 @@ static git_repository *repository_alloc()
static int init_odb(git_repository *repo)
{
- return git_odb_open(&repo->db, repo->path_odb);
+ 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,
@@ -192,7 +192,7 @@ int git_repository_open3(git_repository **repo_out,
assert(repo_out);
if (object_database == NULL)
- return GIT_ERROR;
+ return git__throw(GIT_EINVALIDARGS, "Failed to open repository. `object_database` can't be null");
repo = repository_alloc();
if (repo == NULL)
@@ -218,7 +218,7 @@ int git_repository_open3(git_repository **repo_out,
cleanup:
git_repository_free(repo);
- return error;
+ return git__rethrow(error, "Failed to open repository");
}
@@ -259,7 +259,7 @@ int git_repository_open2(git_repository **repo_out,
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)
@@ -290,7 +290,7 @@ int git_repository_open(git_repository **repo_out, const char *path)
cleanup:
git_repository_free(repo);
- return error;
+ return git__rethrow(error, "Failed to open repository");
}
void git_repository_free(git_repository *repo)
@@ -322,7 +322,7 @@ int git_repository_index(git_index **index_out, git_repository *repo)
assert(index_out && repo);
if (repo->index == NULL) {
- error = git_index_open_inrepo(&repo->index, repo);
+ error = git_index_open_inrepo(&repo->index, repo); /* TODO: move index.c to new error handling */
if (error < GIT_SUCCESS)
return error;
@@ -349,7 +349,7 @@ static int repo_init_reinit(repo_init *results)
static int repo_init_createhead(git_repository *repo)
{
git_reference *head_reference;
- return git_reference_create_symbolic(&head_reference, repo, GIT_HEAD_FILE, GIT_REFS_HEADS_MASTER_FILE);
+ 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 int repo_init_check_head_existence(char * repository_path)
@@ -363,6 +363,7 @@ static int repo_init_check_head_existence(char * repository_path)
static int repo_init_structure(repo_init *results)
{
const int mode = 0755; /* or 0777 ? */
+ int error;
char temp_path[GIT_PATH_MAX];
char *git_dir = results->path_repository;
@@ -372,23 +373,27 @@ static int repo_init_structure(repo_init *results)
/* Creates the '/objects/info/' directory */
git__joinpath(temp_path, git_dir, GIT_OBJECTS_INFO_DIR);
- if (gitfo_mkdir_recurs(temp_path, mode) < GIT_SUCCESS)
- return GIT_ERROR;
+ error = gitfo_mkdir_recurs(temp_path, mode);
+ if (error < GIT_SUCCESS)
+ return error;
/* Creates the '/objects/pack/' directory */
git__joinpath(temp_path, git_dir, GIT_OBJECTS_PACK_DIR);
- if (gitfo_mkdir(temp_path, mode))
- return GIT_ERROR;
+ error = gitfo_mkdir(temp_path, mode);
+ if (error < GIT_SUCCESS)
+ return git__throw(error, "Unable to create `%s` folder", temp_path);
/* Creates the '/refs/heads/' directory */
git__joinpath(temp_path, git_dir, GIT_REFS_HEADS_DIR);
- if (gitfo_mkdir_recurs(temp_path, mode))
- return GIT_ERROR;
+ error = gitfo_mkdir_recurs(temp_path, mode);
+ if (error < GIT_SUCCESS)
+ return error;
/* Creates the '/refs/tags/' directory */
git__joinpath(temp_path, git_dir, GIT_REFS_TAGS_DIR);
- if (gitfo_mkdir(temp_path, mode))
- return GIT_ERROR;
+ error = gitfo_mkdir(temp_path, mode);
+ if (error < GIT_SUCCESS)
+ return git__throw(error, "Unable to create `%s` folder", temp_path);
/* TODO: what's left? templates? */
@@ -467,7 +472,7 @@ int git_repository_init(git_repository **repo_out, const char *path, unsigned is
cleanup:
free(results.path_repository);
git_repository_free(repo);
- return error;
+ return git__rethrow(error, "Failed to (re)init the repository `%s`", path);
}
int git_repository_is_empty(git_repository *repo)
@@ -477,10 +482,10 @@ int git_repository_is_empty(git_repository *repo)
error = git_reference_lookup(&head, repo, "HEAD");
if (error < GIT_SUCCESS)
- return error;
+ return git__throw(error, "Failed to determine the emptiness of the repository. An error occured while retrieving the HEAD reference");
if (git_reference_type(head) != GIT_REF_SYMBOLIC)
- return GIT_EOBJCORRUPTED;
+ return git__throw(GIT_EOBJCORRUPTED, "Failed to determine the emptiness of the repository. HEAD is probably in detached state");
return git_reference_resolve(&branch, head) == GIT_SUCCESS ? 0 : 1;
}