Commit bab0b9f2 by Edward Thomson Committed by Edward Thomson

clean up state metadata more consistently

parent 300d192f
......@@ -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,
......
......@@ -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)
......
......@@ -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;
......
......@@ -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
......@@ -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;
}
......
......@@ -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;
}
/*
* 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment