Fix the opening of empty repositories We were checking for the index file, which is not assured to exist on clean git repositories. Signed-off-by: Vicent Marti <tanoku@gmail.com>
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
diff --git a/src/repository.c b/src/repository.c
index d6909ae..f2cb985 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -68,12 +68,12 @@ uint32_t object_table_hash(const void *key, int hash_id)
*
* 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_object_directory,
const char *git_index_file,
- const char *git_work_tree,
- int is_repo_being_created)
+ const char *git_work_tree)
{
char path_aux[GIT_PATH_MAX];
size_t git_dir_path_len;
@@ -104,22 +104,11 @@ static int assign_repository_DIRs(git_repository *repo,
return error;
}
- /* Ensure GIT_OBJECT_DIRECTORY exists */
- if (gitfo_isdir(path_aux) < GIT_SUCCESS)
- return GIT_ENOTFOUND;
-
/* Store GIT_OBJECT_DIRECTORY */
repo->path_odb = git__strdup(path_aux);
if (repo->path_odb == NULL)
return GIT_ENOMEM;
- if (!is_repo_being_created) {
- /* Ensure HEAD file exists */
- git__joinpath(path_aux, repo->path_repository, GIT_HEAD_FILE);
- if (gitfo_exists(path_aux) < 0)
- return GIT_ENOTAREPO;
- }
-
/* path to GIT_WORK_TREE */
if (git_work_tree == NULL)
repo->is_bare = 1;
@@ -142,12 +131,6 @@ static int assign_repository_DIRs(git_repository *repo,
return error;
}
- if (!is_repo_being_created) {
- /* Ensure GIT_INDEX_FILE exists */
- if (gitfo_exists(path_aux) < 0)
- return GIT_ENOTAREPO;
- }
-
/* store GIT_INDEX_FILE */
repo->path_index = git__strdup(path_aux);
if (repo->path_index == NULL)
@@ -157,32 +140,42 @@ static int assign_repository_DIRs(git_repository *repo,
return GIT_SUCCESS;
}
-static int guess_repository_DIRs(git_repository *repo, const char *repository_path, int is_repo_being_created)
+static int check_repository_dirs(git_repository *repo)
{
- char path_odb[GIT_PATH_MAX] = "\0", path_index[GIT_PATH_MAX] = "\0", path_work_tree[GIT_PATH_MAX] = "\0";
- char dir_name[MAX_GITDIR_TREE_STRUCTURE_PATH_LENGTH];
+ char path_aux[GIT_PATH_MAX];
- int error = GIT_SUCCESS;
+ if (gitfo_isdir(repo->path_repository) < GIT_SUCCESS)
+ return GIT_ENOTAREPO;
- /* Path to objects database */
- git__joinpath(path_odb, repository_path, GIT_OBJECTS_DIR);
+ /* Ensure GIT_OBJECT_DIRECTORY exists */
+ if (gitfo_isdir(repo->path_odb) < GIT_SUCCESS)
+ return GIT_ENOTAREPO;
+
+ /* 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_SUCCESS;
+}
+
+static int guess_repository_dirs(git_repository *repo, const char *repository_path)
+{
+ char buffer[GIT_PATH_MAX];
+ const char *path_work_tree = NULL;
/* Git directory name */
- if (git__basename_r(dir_name, sizeof(dir_name), repository_path) < 0)
+ if (git__basename_r(buffer, sizeof(buffer), repository_path) < 0)
return GIT_EINVALIDPATH;
- if (strcmp(dir_name, DOT_GIT) == 0) {
-
- /* Path to index file */
- git__joinpath(path_index, repository_path, GIT_INDEX_FILE);
-
+ if (strcmp(buffer, DOT_GIT) == 0) {
/* Path to working dir */
- if (git__dirname_r(path_work_tree, sizeof(path_work_tree), repository_path) < 0)
+ if (git__dirname_r(buffer, sizeof(buffer), repository_path) < 0)
return GIT_EINVALIDPATH;
+ path_work_tree = buffer;
}
- error = assign_repository_DIRs(repo, repository_path, path_odb, !*path_index ? NULL : path_index, !*path_work_tree ? NULL : path_work_tree, is_repo_being_created);
- return error;
+ return assign_repository_dirs(repo, repository_path, NULL, NULL, path_work_tree);
}
static git_repository *repository_alloc()
@@ -243,16 +236,19 @@ int git_repository_open3(git_repository **repo_out,
if (repo == NULL)
return GIT_ENOMEM;
- error = assign_repository_DIRs(repo,
+ error = assign_repository_dirs(repo,
git_dir,
NULL,
git_index_file,
- git_work_tree,
- 0);
+ 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;
@@ -279,13 +275,16 @@ int git_repository_open2(git_repository **repo_out,
if (repo == NULL)
return GIT_ENOMEM;
- error = assign_repository_DIRs(repo,
+ error = assign_repository_dirs(repo,
git_dir,
git_object_directory,
git_index_file,
- git_work_tree,
- 0);
+ git_work_tree);
+
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+ error = check_repository_dirs(repo);
if (error < GIT_SUCCESS)
goto cleanup;
@@ -301,7 +300,7 @@ cleanup:
return error;
}
-static int repository_open_internal(git_repository **repo_out, const char *path, int is_repo_being_created)
+int git_repository_open(git_repository **repo_out, const char *path)
{
git_repository *repo;
int error = GIT_SUCCESS;
@@ -312,7 +311,11 @@ static int repository_open_internal(git_repository **repo_out, const char *path,
if (repo == NULL)
return GIT_ENOMEM;
- error = guess_repository_DIRs(repo, path, is_repo_being_created);
+ error = guess_repository_dirs(repo, path);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ error = check_repository_dirs(repo);
if (error < GIT_SUCCESS)
goto cleanup;
@@ -328,11 +331,6 @@ cleanup:
return error;
}
-int git_repository_open(git_repository **repo_out, const char *path)
-{
- return repository_open_internal(repo_out, path, 0);
-}
-
static void repository_free(git_repository *repo)
{
assert(repo);
@@ -506,6 +504,7 @@ static int repo_init_find_dir(repo_init *results, const char* path)
int git_repository_init(git_repository **repo_out, const char *path, unsigned is_bare)
{
int error = GIT_SUCCESS;
+ git_repository *repo = NULL;
repo_init results;
assert(repo_out && path);
@@ -524,15 +523,36 @@ int git_repository_init(git_repository **repo_out, const char *path, unsigned is
if (error < GIT_SUCCESS)
goto cleanup;
- error = repository_open_internal(repo_out, results.path_repository, 1);
+ repo = repository_alloc();
+ if (repo == NULL) {
+ error = GIT_ENOMEM;
+ goto cleanup;
+ }
+
+ error = guess_repository_dirs(repo, results.path_repository);
if (error < GIT_SUCCESS)
goto cleanup;
- assert((*repo_out)->is_bare == is_bare);
+ assert(repo->is_bare == is_bare);
- error = repo_init_createhead(*repo_out);
+ error = init_odb(repo);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ error = repo_init_createhead(repo);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ /* should never fail */
+ assert(check_repository_dirs(repo) == GIT_SUCCESS);
+
+ free(results.path_repository);
+ *repo_out = repo;
+ return GIT_SUCCESS;
cleanup:
free(results.path_repository);
+ git_repository_free(repo);
return error;
}
+