clean up state metadata more consistently
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
diff --git a/include/git2/repository.h b/include/git2/repository.h
index b4d5619..c6bd4dc 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 c31a935..05e656d 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 dcc02e4..278c038 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 832df3b..4e79714 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 a9780bf..261a365 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 34ba343..5660c99 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 ed39121..0000000
--- 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