Commit 3c79bafc by Miguel Arroz

Add reverting reflog move if an error happens.

parent 2546030d
......@@ -1779,17 +1779,20 @@ static int refdb_fs_backend__rename(
}
if ((error = refdb_fs_backend__delete(_backend, old_name, NULL, NULL)) < 0) {
refdb_reflog_fs__rename(_backend, new_name, old_name);
git_reference_free(old);
return error;
}
new = git_reference__realloc(&old, new_name);
if (!new) {
refdb_reflog_fs__rename(_backend, new_name, old_name);
git_reference_free(old);
return -1;
}
if ((error = loose_lock(&file, backend, new->name)) < 0) {
refdb_reflog_fs__rename(_backend, new_name, old_name);
git_reference_free(new);
return error;
}
......
......@@ -232,3 +232,19 @@ void test_refs_branches_move__moves_reflog_correctly(void)
git_reflog_free(original_reflog);
git_reflog_free(new_reflog);
}
void test_refs_branches_move__failed_move_restores_reflog(void)
{
git_reference *original_ref, *new_ref;
git_reflog *recovered_reflog;
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
cl_assert_equal_i(GIT_EINVALIDSPEC, git_branch_move(&new_ref, original_ref, "Inv@{id", 0));
cl_git_pass(git_reflog_read(&recovered_reflog, repo, "refs/heads/br2"));
cl_assert_equal_i(2, git_reflog_entrycount(recovered_reflog));
git_reference_free(original_ref);
git_reflog_free(recovered_reflog);
}
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