diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/fileops.c | 30 | ||||
-rw-r--r-- | src/fileops.h | 10 | ||||
-rw-r--r-- | src/refs.c | 29 |
3 files changed, 33 insertions, 36 deletions
diff --git a/src/fileops.c b/src/fileops.c index 9f3a65d27..2d1915a96 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -205,6 +205,14 @@ void git_futils_mmap_free(git_map *out) p_munmap(out); } +/* Taken from git.git */ +GIT_INLINE(int) is_dot_or_dotdot(const char *name) +{ + return (name[0] == '.' && + (name[1] == '\0' || + (name[1] == '.' && name[2] == '\0'))); +} + int git_futils_direach( char *path, size_t path_sz, @@ -301,34 +309,32 @@ int git_futils_mkdir_r(const char *path, int mode) return GIT_SUCCESS; } -static int _rmdir_recurs_foreach(void *force_removal_of_non_empty_directory, char *path) +static int _rmdir_recurs_foreach(void *opaque, char *path) { int error = GIT_SUCCESS; + int force = *(int *)opaque; - GIT_UNUSED_ARG(nil) - - error = git_futils_isdir(path); - if (error == GIT_SUCCESS) { + if (git_futils_isdir(path) == GIT_SUCCESS) { size_t root_size = strlen(path); - if ((error = git_futils_direach(path, GIT_PATH_MAX, _rmdir_recurs_foreach, force_removal_of_non_empty_directory)) < GIT_SUCCESS) + if ((error = git_futils_direach(path, GIT_PATH_MAX, _rmdir_recurs_foreach, opaque)) < GIT_SUCCESS) return git__rethrow(error, "Failed to remove directory `%s`", path); path[root_size] = '\0'; return p_rmdir(path); - } - if (*(int *)(force_removal_of_non_empty_directory)) + } else if (force) { return p_unlink(path); - else - return git__rethrow(error, "Failed to remove directory. `%s` is not a directory", path); + } + + return git__rethrow(error, "Failed to remove directory. `%s` is not empty", path); } -int git_futils_rmdir_recurs(const char *path, int force_removal_of_non_empty_directory) +int git_futils_rmdir_r(const char *path, int force) { char p[GIT_PATH_MAX]; strncpy(p, path, GIT_PATH_MAX); - return _rmdir_recurs_foreach(&force_removal_of_non_empty_directory, p); + return _rmdir_recurs_foreach(&force, p); } int git_futils_cmp_path(const char *name1, int len1, int isdir1, diff --git a/src/fileops.h b/src/fileops.h index 48071d6d1..f1c169c3a 100644 --- a/src/fileops.h +++ b/src/fileops.h @@ -80,7 +80,7 @@ extern int git_futils_mkdir_r(const char *path, int mode); */ extern int git_futils_mkpath2file(const char *path); -extern int git_futils_rmdir_recurs(const char *path, int force_removal_of_non_empty_directory); +extern int git_futils_rmdir_r(const char *path, int force); /** * Create and open a temporary file with a `_git2_` suffix @@ -104,14 +104,6 @@ extern int git_futils_mv_withpath(const char *from, const char *to); */ extern git_off_t git_futils_filesize(git_file fd); -/* Taken from git.git */ -GIT_INLINE(int) is_dot_or_dotdot(const char *name) -{ - return (name[0] == '.' && - (name[1] == '\0' || - (name[1] == '.' && name[2] == '\0'))); -} - /** * Read-only map all or part of a file into memory. * When possible this function should favor a virtual memory diff --git a/src/refs.c b/src/refs.c index 94a7a15c1..84224e88e 100644 --- a/src/refs.c +++ b/src/refs.c @@ -1291,10 +1291,10 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force) { int error; char *old_name = git__strdup(ref->name); - char new_path[GIT_PATH_MAX]; - char old_path[GIT_PATH_MAX]; - char old_logs[GIT_PATH_MAX]; + + char aux_path[GIT_PATH_MAX]; char normalized[GIT_REFNAME_MAX]; + const char *target_ref = NULL; const char *head_target = NULL; const git_oid *target_oid = NULL; @@ -1344,18 +1344,19 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force) if ((error = packed_write(ref->owner)) < GIT_SUCCESS) goto rollback; } else { - git_path_join(old_path, ref->owner->path_repository, old_name); - if ((error = p_unlink(old_path)) < GIT_SUCCESS) + git_path_join(aux_path, ref->owner->path_repository, old_name); + if ((error = p_unlink(aux_path)) < GIT_SUCCESS) goto cleanup; git_hashtable_remove(ref->owner->references.loose_cache, old_name); } - git_path_join(new_path, ref->owner->path_repository, new_name); + /* build new path */ + git_path_join(aux_path, ref->owner->path_repository, new_name); - if (git_futils_exists(new_path) == GIT_SUCCESS) { - if (git_futils_isdir(new_path) == GIT_SUCCESS) { - if ((error = git_futils_rmdir_recurs(new_path, 0)) < GIT_SUCCESS) + if (git_futils_exists(aux_path) == GIT_SUCCESS) { + if (git_futils_isdir(aux_path) == GIT_SUCCESS) { + if ((error = git_futils_rmdir_r(aux_path, 0)) < GIT_SUCCESS) goto rollback; } else goto rollback; } @@ -1377,17 +1378,15 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force) * */ - git_path_join_n(old_logs, 3, ref->owner->path_repository, "logs", old_name); - if (git_futils_exists(old_logs) == GIT_SUCCESS) { - if (git_futils_isfile(old_logs) == GIT_SUCCESS) - if ((error = p_unlink(old_logs)) < GIT_SUCCESS) - goto rollback; + git_path_join_n(aux_path, 3, ref->owner->path_repository, "logs", old_name); + if (git_futils_isfile(aux_path) == GIT_SUCCESS) { + if ((error = p_unlink(aux_path)) < GIT_SUCCESS) + goto rollback; } /* * Finally we can create the new reference. */ - if (ref->type & GIT_REF_SYMBOLIC) { if ((error = git_reference_create_symbolic(&new_ref, ref->owner, new_name, target_ref, 0)) < GIT_SUCCESS) goto rollback; |