From bab0b9f2d21d993c3f25ee00ce2d243a4dc0de93 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Fri, 22 Nov 2013 18:02:12 -0500 Subject: [PATCH] clean up state metadata more consistently --- include/git2/repository.h | 6 ++--- src/merge.c | 46 ++++++++++----------------------------- src/repository.c | 36 ++++++++++++++++++++++++++++++ src/repository.h | 2 ++ src/reset.c | 2 +- src/revert.c | 37 +++++++------------------------ src/revert.h | 14 ------------ 7 files changed, 62 insertions(+), 81 deletions(-) delete mode 100644 src/revert.h diff --git a/include/git2/repository.h b/include/git2/repository.h index b4d561992..c6bd4dca4 100644 --- a/include/git2/repository.h +++ b/include/git2/repository.h @@ -488,13 +488,13 @@ GIT_EXTERN(int) git_repository_message(char *out, size_t len, git_repository *re GIT_EXTERN(int) git_repository_message_remove(git_repository *repo); /** - * Remove all the metadata associated with an ongoing git merge, including - * MERGE_HEAD, MERGE_MSG, etc. + * Remove all the metadata associated with an ongoing command like merge, + * revert, cherry-pick, etc. For example: MERGE_HEAD, MERGE_MSG, etc. * * @param repo A repository object * @return 0 on success, or error */ -GIT_EXTERN(int) git_repository_merge_cleanup(git_repository *repo); +GIT_EXTERN(int) git_repository_state_cleanup(git_repository *repo); typedef int (*git_repository_fetchhead_foreach_cb)(const char *ref_name, const char *remote_url, diff --git a/src/merge.c b/src/merge.c index c31a935b0..05e656d1e 100644 --- a/src/merge.c +++ b/src/merge.c @@ -2362,6 +2362,17 @@ done: return error; } +static int merge_state_cleanup(git_repository *repo) +{ + const char *state_files[] = { + GIT_MERGE_HEAD_FILE, + GIT_MERGE_MODE_FILE, + GIT_MERGE_MSG_FILE, + }; + + return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files)); +} + int git_merge( git_merge_result **out, git_repository *repo, @@ -2453,7 +2464,7 @@ int git_merge( goto done; on_error: - git_repository_merge_cleanup(repo); + merge_state_cleanup(repo); git_index_free(index_new); git__free(result); @@ -2497,39 +2508,6 @@ int git_merge__setup( return error; } -int git_repository_merge_cleanup(git_repository *repo) -{ - int error = 0; - git_buf merge_head_path = GIT_BUF_INIT, - merge_mode_path = GIT_BUF_INIT, - merge_msg_path = GIT_BUF_INIT; - - assert(repo); - - if (git_buf_joinpath(&merge_head_path, repo->path_repository, GIT_MERGE_HEAD_FILE) < 0 || - git_buf_joinpath(&merge_mode_path, repo->path_repository, GIT_MERGE_MODE_FILE) < 0 || - git_buf_joinpath(&merge_msg_path, repo->path_repository, GIT_MERGE_MSG_FILE) < 0) - return -1; - - if (git_path_isfile(merge_head_path.ptr)) { - if ((error = p_unlink(merge_head_path.ptr)) < 0) - goto cleanup; - } - - if (git_path_isfile(merge_mode_path.ptr)) - (void)p_unlink(merge_mode_path.ptr); - - if (git_path_isfile(merge_msg_path.ptr)) - (void)p_unlink(merge_msg_path.ptr); - -cleanup: - git_buf_free(&merge_msg_path); - git_buf_free(&merge_mode_path); - git_buf_free(&merge_head_path); - - return error; -} - /* Merge result data */ int git_merge_result_is_uptodate(git_merge_result *merge_result) diff --git a/src/repository.c b/src/repository.c index dcc02e4fb..278c0384e 100644 --- a/src/repository.c +++ b/src/repository.c @@ -1965,6 +1965,42 @@ int git_repository_state(git_repository *repo) return state; } +int git_repository__cleanup_files(git_repository *repo, const char *files[], size_t files_len) +{ + git_buf path = GIT_BUF_INIT; + size_t i; + int error = 0; + + for (i = 0; i < files_len; ++i) { + git_buf_clear(&path); + + if ((error = git_buf_joinpath(&path, repo->path_repository, files[i])) < 0 || + (git_path_isfile(git_buf_cstr(&path)) && + (error = p_unlink(git_buf_cstr(&path))) < 0)) + goto done; + } + +done: + git_buf_free(&path); + + return error; +} + +static const char *state_files[] = { + GIT_MERGE_HEAD_FILE, + GIT_MERGE_MODE_FILE, + GIT_MERGE_MSG_FILE, + GIT_REVERT_HEAD_FILE, + GIT_CHERRY_PICK_HEAD_FILE, +}; + +int git_repository_state_cleanup(git_repository *repo) +{ + assert(repo); + + return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files)); +} + int git_repository_is_shallow(git_repository *repo) { git_buf path = GIT_BUF_INIT; diff --git a/src/repository.h b/src/repository.h index 832df3bd2..4e79714f1 100644 --- a/src/repository.h +++ b/src/repository.h @@ -169,4 +169,6 @@ GIT_INLINE(int) git_repository__ensure_not_bare( return GIT_EBAREREPO; } +int git_repository__cleanup_files(git_repository *repo, const char *files[], size_t files_len); + #endif diff --git a/src/reset.c b/src/reset.c index a9780bfbc..261a36576 100644 --- a/src/reset.c +++ b/src/reset.c @@ -149,7 +149,7 @@ int git_reset( (error = git_index_write(index)) < 0) goto cleanup; - if ((error = git_repository_merge_cleanup(repo)) < 0) { + if ((error = git_repository_state_cleanup(repo)) < 0) { giterr_set(GITERR_INDEX, "%s - failed to clean up merge data", ERROR_MSG); goto cleanup; } diff --git a/src/revert.c b/src/revert.c index 34ba343b6..5660c9919 100644 --- a/src/revert.c +++ b/src/revert.c @@ -9,7 +9,6 @@ #include "repository.h" #include "filebuf.h" #include "merge.h" -#include "revert.h" #include "git2/types.h" #include "git2/merge.h" @@ -103,6 +102,13 @@ static int revert_normalize_opts( return error; } +static int revert_state_cleanup(git_repository *repo) +{ + const char *state_files[] = { GIT_REVERT_HEAD_FILE, GIT_MERGE_MSG_FILE }; + + return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files)); +} + int git_revert( git_repository *repo, git_commit *commit, @@ -175,7 +181,7 @@ int git_revert( goto done; on_error: - git_revert__cleanup(repo); + revert_state_cleanup(repo); done: git_index_free(index_new); @@ -188,30 +194,3 @@ done: return error; } - -int git_revert__cleanup(git_repository *repo) -{ - int error = 0; - git_buf revert_head_path = GIT_BUF_INIT, - merge_msg_path = GIT_BUF_INIT; - - assert(repo); - - if (git_buf_joinpath(&revert_head_path, repo->path_repository, GIT_REVERT_HEAD_FILE) < 0 || - git_buf_joinpath(&merge_msg_path, repo->path_repository, GIT_MERGE_MSG_FILE) < 0) - return -1; - - if (git_path_isfile(revert_head_path.ptr)) { - if ((error = p_unlink(revert_head_path.ptr)) < 0) - goto cleanup; - } - - if (git_path_isfile(merge_msg_path.ptr)) - (void)p_unlink(merge_msg_path.ptr); - -cleanup: - git_buf_free(&merge_msg_path); - git_buf_free(&revert_head_path); - - return error; -} diff --git a/src/revert.h b/src/revert.h deleted file mode 100644 index ed3912198..000000000 --- a/src/revert.h +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ -#ifndef INCLUDE_revert_h__ -#define INCLUDE_revert_h__ - -#include "git2/repository.h" - -int git_revert__cleanup(git_repository *repo); - -#endif