Commit 3abe3bba5ab234a8fcbf4cc7a50adc86323d7287

nulltoken 2011-05-14T16:05:33

Move repository.c to the new error handling

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;
 }