int git_repository_init(git_repository **repo_out, const char *path, unsigned is_bare) { git_buf repository_path = GIT_BUF_INIT; assert(repo_out && path); if (git_buf_joinpath(&repository_path, path, is_bare ? "" : GIT_DIR) < 0) return -1; if (git_path_isdir(repository_path.ptr) == true) { if (valid_repository_path(&repository_path) == true) { int res = repo_init_reinit(repo_out, repository_path.ptr, is_bare); git_buf_free(&repository_path); return res; } } if (repo_init_structure(repository_path.ptr, is_bare) < 0 || repo_init_config(repository_path.ptr, is_bare) < 0 || repo_init_createhead(repository_path.ptr) < 0 || git_repository_open(repo_out, repository_path.ptr) < 0) { git_buf_free(&repository_path); return -1; } git_buf_free(&repository_path); return 0; }
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); results.path_repository = NULL; results.is_bare = is_bare; error = repo_init_find_dir(&results, path); 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; 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->is_bare == is_bare); 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; }
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 ? */ strcpy(temp_path + path_len, 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 */ strcpy(temp_path + path_len, GIT_OBJECTS_INFO_DIR); if (gitfo_mkdir_recurs(temp_path, mode)) return GIT_ERROR; /* Creates the '/objects/pack/' directory */ strcpy(temp_path + path_len, GIT_OBJECTS_PACK_DIR); if (gitfo_mkdir(temp_path, mode)) return GIT_ERROR; /* Creates the '/refs/heads/' directory */ strcpy(temp_path + path_len, GIT_REFS_HEADS_DIR); if (gitfo_mkdir_recurs(temp_path, mode)) return GIT_ERROR; /* Creates the '/refs/tags/' directory */ strcpy(temp_path + path_len, GIT_REFS_TAGS_DIR); if (gitfo_mkdir(temp_path, mode)) return GIT_ERROR; /* TODO: what's left? templates? */ return GIT_SUCCESS; }