static int repo_init_structure(repo_init *results) { const int mode = 0755; /* or 0777 ? */ char temp_path[GIT_PATH_MAX]; char *git_dir = results->path_repository; if (gitfo_mkdir_recurs(git_dir, mode)) 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) return GIT_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; /* 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; /* Creates the '/refs/tags/' directory */ git__joinpath(temp_path, git_dir, GIT_REFS_TAGS_DIR); if (gitfo_mkdir(temp_path, mode)) return GIT_ERROR; /* TODO: what's left? templates? */ return GIT_SUCCESS; }
static int repo_init_check_head_existence(char * repository_path) { char temp_path[GIT_PATH_MAX]; git__joinpath(temp_path, repository_path, GIT_HEAD_FILE); return gitfo_exists(temp_path); }
static int load_alternates(git_odb *odb, const char *objects_dir) { char alternates_path[GIT_PATH_MAX]; char alternate[GIT_PATH_MAX]; char *buffer; gitfo_buf alternates_buf = GITFO_BUF_INIT; int error; git__joinpath(alternates_path, objects_dir, GIT_ALTERNATES_FILE); if (gitfo_exists(alternates_path) < GIT_SUCCESS) return GIT_SUCCESS; if (gitfo_read_file(&alternates_buf, alternates_path) < GIT_SUCCESS) return GIT_EOSERR; buffer = (char *)alternates_buf.data; error = GIT_SUCCESS; /* add each alternate as a new backend; one alternate per line */ while ((error == GIT_SUCCESS) && (buffer = git__strtok(alternate, buffer, "\r\n")) != NULL) error = add_default_backends(odb, alternate, 1); gitfo_free_buf(&alternates_buf); return error; }
END_TEST #define STANDARD_REPOSITORY 0 #define BARE_REPOSITORY 1 static int ensure_repository_init( const char *working_directory, int repository_kind, const char *expected_path_index, const char *expected_path_repository, const char *expected_working_directory) { char path_odb[GIT_PATH_MAX]; git_repository *repo; if (gitfo_isdir(working_directory) == GIT_SUCCESS) return GIT_ERROR; git__joinpath(path_odb, expected_path_repository, GIT_OBJECTS_DIR); if (git_repository_init(&repo, working_directory, repository_kind) < GIT_SUCCESS) return GIT_ERROR; if (repo->path_workdir != NULL || expected_working_directory != NULL) { if (git__suffixcmp(repo->path_workdir, expected_working_directory) != 0) goto cleanup; } if (git__suffixcmp(repo->path_odb, path_odb) != 0) goto cleanup; if (git__suffixcmp(repo->path_repository, expected_path_repository) != 0) goto cleanup; if (repo->path_index != NULL || expected_path_index != NULL) { if (git__suffixcmp(repo->path_index, expected_path_index) != 0) goto cleanup; } git_repository_free(repo); rmdir_recurs(working_directory); return GIT_SUCCESS; cleanup: git_repository_free(repo); rmdir_recurs(working_directory); return GIT_ERROR; }
int git_blob_create_fromfile(git_oid *oid, git_repository *repo, const char *path) { int error, fd; char full_path[GIT_PATH_MAX]; char buffer[2048]; git_off_t size; git_odb_stream *stream; if (repo->path_workdir == NULL) return GIT_ENOTFOUND; git__joinpath(full_path, repo->path_workdir, path); if ((fd = gitfo_open(full_path, O_RDONLY)) < 0) return GIT_ENOTFOUND; if ((size = gitfo_size(fd)) < 0 || !git__is_sizet(size)) { gitfo_close(fd); return GIT_EOSERR; } if ((error = git_odb_open_wstream(&stream, repo->db, (size_t)size, GIT_OBJ_BLOB)) < GIT_SUCCESS) { gitfo_close(fd); return error; } while (size > 0) { ssize_t read_len; read_len = read(fd, buffer, sizeof(buffer)); if (read_len < 0) { gitfo_close(fd); stream->free(stream); return GIT_EOSERR; } stream->write(stream, buffer, read_len); size -= read_len; } error = stream->finalize_write(oid, stream); stream->free(stream); return error; }
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; /* 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 repo_init_find_dir(repo_init *results, const char* path) { char temp_path[GIT_PATH_MAX]; int error = GIT_SUCCESS; error = gitfo_prettify_dir_path(temp_path, sizeof(temp_path), path); if (error < GIT_SUCCESS) return error; if (!results->is_bare) { git__joinpath(temp_path, temp_path, GIT_DIR); } results->path_repository = git__strdup(temp_path); if (results->path_repository == NULL) return GIT_ENOMEM; return GIT_SUCCESS; }
int gitfo_mv_force(const char *from, const char *to) { const int mode = 0755; /* or 0777 ? */ int error = GIT_SUCCESS; char target_folder_path[GIT_PATH_MAX]; error = git__dirname_r(target_folder_path, sizeof(target_folder_path), to); if (error < GIT_SUCCESS) return error; /* Does the containing folder exist? */ if (gitfo_isdir(target_folder_path)) { git__joinpath(target_folder_path, target_folder_path, ""); /* Ensure there's a trailing slash */ /* Let's create the tree structure */ error = gitfo_mkdir_recurs(target_folder_path, mode); if (error < GIT_SUCCESS) return error; } return gitfo_mv(from, to); }
/* * Git repository open methods * * Open a repository object from its path */ 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) { char path_aux[GIT_PATH_MAX]; int error = GIT_SUCCESS; assert(repo); if (git_dir == NULL) return GIT_ENOTFOUND; error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_dir); if (error < GIT_SUCCESS) return error; /* store GIT_DIR */ repo->path_repository = git__strdup(path_aux); if (repo->path_repository == NULL) return GIT_ENOMEM; /* path to GIT_OBJECT_DIRECTORY */ if (git_object_directory == NULL) git__joinpath(path_aux, repo->path_repository, GIT_OBJECTS_DIR); else { error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_object_directory); if (error < GIT_SUCCESS) return error; } /* Store GIT_OBJECT_DIRECTORY */ repo->path_odb = git__strdup(path_aux); if (repo->path_odb == NULL) return GIT_ENOMEM; /* path to GIT_WORK_TREE */ if (git_work_tree == NULL) repo->is_bare = 1; else { error = gitfo_prettify_dir_path(path_aux, sizeof(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, sizeof(path_aux), git_index_file); if (error < GIT_SUCCESS) return error; } /* store GIT_INDEX_FILE */ repo->path_index = git__strdup(path_aux); if (repo->path_index == NULL) return GIT_ENOMEM; } return GIT_SUCCESS; }