Refactored the opening and the initialization of a repository.
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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
diff --git a/src/refs.h b/src/refs.h
index 6efc61b..a542ac0 100644
--- a/src/refs.h
+++ b/src/refs.h
@@ -16,6 +16,9 @@
#define GIT_PACKEDREFS_HEADER "# pack-refs with: peeled "
#define MAX_GITDIR_TREE_STRUCTURE_PATH_LENGTH 100
+#define GIT_HEAD_FILE "HEAD"
+#define GIT_REFS_HEADS_MASTER_FILE GIT_REFS_HEADS_DIR "master"
+
struct git_reference {
git_repository *owner;
char *name;
diff --git a/src/repository.c b/src/repository.c
index a1d2798..9d457b7 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -35,14 +35,9 @@
#include "refs.h"
-#define GIT_DIR ".git/"
-#define GIT_OBJECTS_DIR "objects/"
#define GIT_OBJECTS_INFO_DIR GIT_OBJECTS_DIR "info/"
#define GIT_OBJECTS_PACK_DIR GIT_OBJECTS_DIR "pack/"
-#define GIT_INDEX_FILE "index"
-#define GIT_HEAD_FILE "HEAD"
-
#define GIT_BRANCH_MASTER "master"
static const int OBJECT_TABLE_SIZE = 32;
@@ -77,7 +72,8 @@ 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)
+ const char *git_work_tree,
+ int is_repo_being_created)
{
char path_aux[GIT_PATH_MAX];
size_t git_dir_path_len;
@@ -92,9 +88,6 @@ static int assign_repository_DIRs(git_repository *repo,
if (error < GIT_SUCCESS)
return error;
- if (gitfo_isdir(path_aux) < GIT_SUCCESS)
- return GIT_ENOTFOUND;
-
git_dir_path_len = strlen(path_aux);
/* store GIT_DIR */
@@ -102,124 +95,94 @@ static int assign_repository_DIRs(git_repository *repo,
if (repo->path_repository == NULL)
return GIT_ENOMEM;
- /* store GIT_OBJECT_DIRECTORY */
+ /* path to GIT_OBJECT_DIRECTORY */
if (git_object_directory == NULL)
- strcpy(repo->path_repository + git_dir_path_len, GIT_OBJECTS_DIR);
+ git__joinpath(path_aux, repo->path_repository, GIT_OBJECTS_DIR);
else {
error = gitfo_prettify_dir_path(path_aux, git_object_directory);
if (error < GIT_SUCCESS)
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;
- /* store GIT_INDEX_FILE */
- if (git_index_file == NULL)
- strcpy(repo->path_repository + git_dir_path_len, GIT_INDEX_FILE);
- else {
- error = gitfo_prettify_file_path(path_aux, git_index_file);
- if (error < GIT_SUCCESS)
- return error;
+ 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;
}
- if (gitfo_exists(path_aux) < 0)
- return GIT_ENOTFOUND;
-
- repo->path_index = git__strdup(path_aux);
- if (repo->path_index == NULL)
- return GIT_ENOMEM;
-
- /* store GIT_WORK_TREE */
+ /* path to GIT_WORK_TREE */
if (git_work_tree == NULL)
repo->is_bare = 1;
else {
error = gitfo_prettify_dir_path(path_aux, git_work_tree);
if (error < GIT_SUCCESS)
return error;
+
+ /* 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, git_index_file);
+ if (error < GIT_SUCCESS)
+ 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)
+ return GIT_ENOMEM;
}
return GIT_SUCCESS;
}
-static int guess_repository_DIRs(git_repository *repo, const char *repository_path)
+static int guess_repository_DIRs(git_repository *repo, const char *repository_path, int is_repo_being_created)
{
- char path_aux[GIT_PATH_MAX];
- const char *topdir;
+ 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];
- int path_len;
int error = GIT_SUCCESS;
- error = gitfo_prettify_dir_path(path_aux, repository_path);
- if (error < GIT_SUCCESS)
- return error;
-
- if (gitfo_isdir(path_aux) < GIT_SUCCESS)
- return GIT_ENOTAREPO;
-
- path_len = strlen(path_aux);
-
- repo->path_repository = git__strdup(path_aux);
- if (repo->path_repository == NULL)
- return GIT_ENOMEM;
-
- /* objects database */
- strcpy(path_aux + path_len, GIT_OBJECTS_DIR);
- if (gitfo_isdir(path_aux) < GIT_SUCCESS)
- return GIT_ENOTAREPO;
- repo->path_odb = git__strdup(path_aux);
- if (repo->path_odb == NULL)
- return GIT_ENOMEM;
-
- /* HEAD file */
- strcpy(path_aux + path_len, GIT_HEAD_FILE);
- if (gitfo_exists(path_aux) < 0)
- return GIT_ENOTAREPO;
-
- path_aux[path_len] = 0;
+ /* Path to objects database */
+ git__joinpath(path_odb, repository_path, GIT_OBJECTS_DIR);
- if ((topdir = git__topdir(path_aux)) == NULL)
+ /* Git directory name */
+ if (git__basename_r(dir_name, sizeof(dir_name), repository_path) < 0)
return GIT_EINVALIDPATH;
- if (strcmp(topdir, GIT_DIR) == 0) {
- int workdir_len;
-
- repo->is_bare = 0;
-
- /* index file */
- strcpy(path_aux + path_len, GIT_INDEX_FILE);
- repo->path_index = git__strdup(path_aux);
- if (repo->path_index == NULL)
- return GIT_ENOMEM;
+ if (strcmp(dir_name, DOT_GIT) == 0) {
+
+ /* Path to index file */
+ git__joinpath(path_index, repository_path, GIT_INDEX_FILE);
- /* working dir */
- path_aux[path_len] = 0;
- workdir_len = git__dirname_r(path_aux, GIT_PATH_MAX, path_aux);
- if (workdir_len < 0)
+ /* Path to working dir */
+ if (git__dirname_r(path_work_tree, sizeof(path_work_tree), repository_path) < 0)
return GIT_EINVALIDPATH;
-
- if (path_aux[workdir_len - 1] != '/') {
- path_aux[workdir_len] = '/';
- path_aux[workdir_len + 1] = '\0';
- }
-
- repo->path_workdir = git__strdup(path_aux);
- if (repo->path_workdir == NULL)
- return GIT_ENOMEM;
-
- } else {
- repo->is_bare = 1;
- repo->path_workdir = NULL;
}
- return GIT_SUCCESS;
+ 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;
}
static git_repository *repository_alloc()
@@ -276,7 +239,8 @@ int git_repository_open3(git_repository **repo_out,
git_dir,
NULL,
git_index_file,
- git_work_tree);
+ git_work_tree,
+ 0);
if (error < GIT_SUCCESS)
goto cleanup;
@@ -311,7 +275,8 @@ int git_repository_open2(git_repository **repo_out,
git_dir,
git_object_directory,
git_index_file,
- git_work_tree);
+ git_work_tree,
+ 0);
if (error < GIT_SUCCESS)
goto cleanup;
@@ -328,7 +293,7 @@ cleanup:
return error;
}
-int git_repository_open(git_repository **repo_out, const char *path)
+static int repository_open_internal(git_repository **repo_out, const char *path, int is_repo_being_created)
{
git_repository *repo;
int error = GIT_SUCCESS;
@@ -339,7 +304,7 @@ int git_repository_open(git_repository **repo_out, const char *path)
if (repo == NULL)
return GIT_ENOMEM;
- error = guess_repository_DIRs(repo, path);
+ error = guess_repository_DIRs(repo, path, is_repo_being_created);
if (error < GIT_SUCCESS)
goto cleanup;
@@ -355,6 +320,11 @@ cleanup:
return error;
}
+int git_repository_open(git_repository **repo_out, const char *path)
+{
+ return repository_open_internal(repo_out, path, 0);
+}
+
void git_repository_free(git_repository *repo)
{
git_object *object;
@@ -535,21 +505,18 @@ static int repo_init_reinit(repo_init *results)
return GIT_SUCCESS;
}
-static int repo_init_createhead(const char *head_path)
+static int repo_init_createhead(git_repository *repo)
{
- git_file fd;
- int error = GIT_SUCCESS;
- char head_symlink[50];
-
- sprintf(head_symlink, "%s %s%s\n", GIT_SYMREF, GIT_REFS_HEADS_DIR, GIT_BRANCH_MASTER);
-
- if ((fd = gitfo_creat(head_path, S_IREAD | S_IWRITE)) < GIT_SUCCESS)
- return GIT_ERROR;
+ git_reference *head_reference;
+ return git_reference_create_symbolic(&head_reference, repo, GIT_HEAD_FILE, GIT_REFS_HEADS_MASTER_FILE);
+}
- error = gitfo_write(fd, (void*)head_symlink, strlen(head_symlink));
+static int repo_init_check_head_existence(char * repository_path)
+{
+ char temp_path[GIT_PATH_MAX];
- gitfo_close(fd);
- return error;
+ git__joinpath(temp_path, repository_path, GIT_HEAD_FILE);
+ return gitfo_exists(temp_path);
}
static int repo_init_structure(repo_init *results)
@@ -557,24 +524,11 @@ static int repo_init_structure(repo_init *results)
const int mode = 0755; /* or 0777 ? */
char temp_path[GIT_PATH_MAX];
- int path_len;
char *git_dir = results->path_repository;
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 ? */
- git__joinpath(temp_path, git_dir, GIT_HEAD_FILE);
-
- if (gitfo_exists(temp_path) == GIT_SUCCESS)
- return repo_init_reinit(results);
-
- if (repo_init_createhead(temp_path) < GIT_SUCCESS)
- return GIT_ERROR;
-
/* Creates the '/objects/info/' directory */
git__joinpath(temp_path, git_dir, GIT_OBJECTS_INFO_DIR);
if (gitfo_mkdir_recurs(temp_path, mode) < GIT_SUCCESS)
@@ -609,11 +563,6 @@ static int repo_init_find_dir(repo_init *results, const char* path)
if (error < GIT_SUCCESS)
return error;
-/*
- if (gitfo_isdir(temp_path) < GIT_SUCCESS)
- return GIT_ENOTAREPO;
-*/
-
if (!results->is_bare) {
git__joinpath(temp_path, temp_path, GIT_DIR);
}
@@ -639,11 +588,20 @@ int git_repository_init(git_repository **repo_out, const char *path, unsigned is
if (error < GIT_SUCCESS)
goto cleanup;
+ if (!repo_init_check_head_existence(results.path_repository))
+ return repo_init_reinit(&results);
+
error = repo_init_structure(&results);
if (error < GIT_SUCCESS)
goto cleanup;
- error = git_repository_open(repo_out, results.path_repository);
+ error = repository_open_internal(repo_out, results.path_repository, 1);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ assert((*repo_out)->is_bare == is_bare);
+
+ error = repo_init_createhead(*repo_out);
cleanup:
free(results.path_repository);
diff --git a/src/repository.h b/src/repository.h
index 5bf0411..c8e4186 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -10,6 +10,11 @@
#include "index.h"
#include "refs.h"
+#define DOT_GIT ".git"
+#define GIT_DIR DOT_GIT "/"
+#define GIT_OBJECTS_DIR "objects/"
+#define GIT_INDEX_FILE "index"
+
typedef struct {
git_rawobj raw;
void *write_ptr;