/* * 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]; size_t path_len; assert(repo); if (git_dir == NULL || gitfo_isdir(git_dir) < GIT_SUCCESS) return GIT_ENOTFOUND; /* store GIT_DIR */ path_len = strlen(git_dir); strcpy(path_aux, git_dir); if (path_aux[path_len - 1] != '/') { path_aux[path_len] = '/'; path_aux[path_len + 1] = 0; path_len = path_len + 1; } repo->path_repository = git__strdup(path_aux); /* store GIT_OBJECT_DIRECTORY */ if (git_object_directory == NULL) strcpy(path_aux + path_len, GIT_OBJECTS_DIR); else strcpy(path_aux, git_object_directory); if (gitfo_isdir(path_aux) < GIT_SUCCESS) return GIT_ENOTFOUND; repo->path_odb = git__strdup(path_aux); /* store GIT_INDEX_FILE */ if (git_index_file == NULL) strcpy(path_aux + path_len, GIT_INDEX_FILE); else strcpy(path_aux, git_index_file); if (gitfo_exists(path_aux) < 0) return GIT_ENOTFOUND; repo->path_index = git__strdup(path_aux); /* store GIT_WORK_TREE */ if (git_work_tree == NULL) repo->is_bare = 1; else repo->path_workdir = git__strdup(git_work_tree); return GIT_SUCCESS; }
static int guess_repository_DIRs(git_repository *repo, const char *repository_path) { char path_aux[GIT_PATH_MAX], *last_DIR; int path_len; if (gitfo_isdir(repository_path) < GIT_SUCCESS) return GIT_ENOTAREPO; path_len = strlen(repository_path); strcpy(path_aux, repository_path); if (path_aux[path_len - 1] != '/') { path_aux[path_len] = '/'; path_aux[path_len + 1] = 0; path_len = path_len + 1; } repo->path_repository = git__strdup(path_aux); /* 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); /* HEAD file */ strcpy(path_aux + path_len, GIT_HEAD_FILE); if (gitfo_exists(path_aux) < 0) return GIT_ENOTAREPO; path_aux[path_len] = 0; last_DIR = (path_aux + path_len - 2); while (*last_DIR != '/') last_DIR--; if (strcmp(last_DIR, GIT_DIR) == 0) { repo->is_bare = 0; /* index file */ strcpy(path_aux + path_len, GIT_INDEX_FILE); repo->path_index = git__strdup(path_aux); /* working dir */ *(last_DIR + 1) = 0; repo->path_workdir = git__strdup(path_aux); } else { repo->is_bare = 1; repo->path_workdir = NULL; } return GIT_SUCCESS; }
static int parse_repository_folders(git_repository *repo, const char *repository_path) { char path_aux[GIT_PATH_MAX], *last_folder; int path_len; if (gitfo_isdir(repository_path) < 0) return GIT_ENOTAREPO; path_len = strlen(repository_path); strcpy(path_aux, repository_path); if (path_aux[path_len - 1] != '/') { path_aux[path_len] = '/'; path_aux[path_len + 1] = 0; path_len = path_len + 1; } repo->path_repository = git__strdup(path_aux); /* objects database */ strcpy(path_aux + path_len, "objects/"); if (gitfo_isdir(path_aux) < 0) return GIT_ENOTAREPO; repo->path_odb = git__strdup(path_aux); /* HEAD file */ strcpy(path_aux + path_len, "HEAD"); if (gitfo_exists(path_aux) < 0) return GIT_ENOTAREPO; path_aux[path_len] = 0; last_folder = (path_aux + path_len - 2); while (*last_folder != '/') last_folder--; if (strcmp(last_folder, "/.git/") == 0) { repo->is_bare = 0; /* index file */ strcpy(path_aux + path_len, "index"); repo->path_index = git__strdup(path_aux); /* working dir */ *(last_folder + 1) = 0; repo->path_workdir = git__strdup(path_aux); } else { repo->is_bare = 1; repo->path_workdir = NULL; } return GIT_SUCCESS; }
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; }
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 gitfo_mkdir_recurs(const char *path, int mode) { int error; char *pp, *sp; char *path_copy = git__strdup(path); if (path_copy == NULL) return GIT_ENOMEM; error = GIT_SUCCESS; pp = path_copy; #ifdef GIT_WIN32 if (!is_windows_rooted_path(pp)) pp += 2; /* Skip the drive name (eg. C: or D:) */ #endif while (error == GIT_SUCCESS && (sp = strchr(pp, '/')) != 0) { if (sp != pp && gitfo_isdir(path_copy) < GIT_SUCCESS) { *sp = 0; error = gitfo_mkdir(path_copy, mode); /* Do not choke while trying to recreate an existing directory */ if (errno == EEXIST) error = GIT_SUCCESS; *sp = '/'; } pp = sp + 1; } if (*(pp - 1) != '/' && error == GIT_SUCCESS) error = gitfo_mkdir(path, mode); free(path_copy); return error; }
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]; size_t git_dir_path_len; int error = GIT_SUCCESS; assert(repo); if (git_dir == NULL) return GIT_ENOTFOUND; error = gitfo_prettify_dir_path(path_aux, git_dir); 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 */ repo->path_repository = git__strdup(path_aux); /* store GIT_OBJECT_DIRECTORY */ if (git_object_directory == NULL) strcpy(repo->path_repository + git_dir_path_len, GIT_OBJECTS_DIR); else { error = gitfo_prettify_dir_path(path_aux, git_object_directory); if (error < GIT_SUCCESS) return error; } if (gitfo_isdir(path_aux) < GIT_SUCCESS) return GIT_ENOTFOUND; repo->path_odb = git__strdup(path_aux); /* 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 (gitfo_exists(path_aux) < 0) return GIT_ENOTFOUND; repo->path_index = git__strdup(path_aux); /* store 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; repo->path_workdir = git__strdup(path_aux); } return GIT_SUCCESS; }