]> git.proxmox.com Git - libgit2.git/blobdiff - src/revert.c
treebuilder: fix memory leaks in `write_with_buffer`
[libgit2.git] / src / revert.c
index 29e124f6c866bfeb9f2c2ae1bffd51ff1ce8edff..747938fb33473a17be4fd6dfdb89053a2b5df76a 100644 (file)
@@ -9,6 +9,7 @@
 #include "repository.h"
 #include "filebuf.h"
 #include "merge.h"
+#include "index.h"
 
 #include "git2/types.h"
 #include "git2/merge.h"
@@ -26,7 +27,7 @@ static int write_revert_head(
        git_buf file_path = GIT_BUF_INIT;
        int error = 0;
 
-       if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_REVERT_HEAD_FILE)) >= 0 &&
+       if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_REVERT_HEAD_FILE)) >= 0 &&
                (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_REVERT_FILE_MODE)) >= 0 &&
                (error = git_filebuf_printf(&file, "%s\n", commit_oidstr)) >= 0)
                error = git_filebuf_commit(&file);
@@ -48,7 +49,7 @@ static int write_merge_msg(
        git_buf file_path = GIT_BUF_INIT;
        int error = 0;
 
-       if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_MSG_FILE)) < 0 ||
+       if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 ||
                (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_REVERT_FILE_MODE)) < 0 ||
                (error = git_filebuf_printf(&file, "Revert \"%s\"\n\nThis reverts commit %s.\n",
                commit_msgline, commit_oidstr)) < 0)
@@ -72,7 +73,7 @@ static int revert_normalize_opts(
        const char *their_label)
 {
        int error = 0;
-       unsigned int default_checkout_strategy = GIT_CHECKOUT_SAFE_CREATE |
+       unsigned int default_checkout_strategy = GIT_CHECKOUT_SAFE |
                GIT_CHECKOUT_ALLOW_CONFLICTS;
 
        GIT_UNUSED(repo);
@@ -132,13 +133,13 @@ int git_revert_commit(
        if (git_commit_parentcount(revert_commit) > 1) {
                if (!mainline)
                        return revert_seterr(revert_commit,
-                               "Mainline branch is not specified but %s is a merge commit");
+                               "mainline branch is not specified but %s is a merge commit");
 
                parent = mainline;
        } else {
                if (mainline)
                        return revert_seterr(revert_commit,
-                               "Mainline branch specified but %s is not a merge commit");
+                               "mainline branch specified but %s is not a merge commit");
 
                parent = git_commit_parentcount(revert_commit);
        }
@@ -174,7 +175,8 @@ int git_revert(
        char commit_oidstr[GIT_OID_HEXSZ + 1];
        const char *commit_msg;
        git_buf their_label = GIT_BUF_INIT;
-       git_index *index_new = NULL, *index_repo = NULL;
+       git_index *index = NULL;
+       git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
        int error;
 
        assert(repo && commit);
@@ -194,15 +196,16 @@ int git_revert(
 
        if ((error = git_buf_printf(&their_label, "parent of %.7s... %s", commit_oidstr, commit_msg)) < 0 ||
                (error = revert_normalize_opts(repo, &opts, given_opts, git_buf_cstr(&their_label))) < 0 ||
+               (error = git_indexwriter_init_for_operation(&indexwriter, repo, &opts.checkout_opts.checkout_strategy)) < 0 ||
                (error = write_revert_head(repo, commit_oidstr)) < 0 ||
                (error = write_merge_msg(repo, commit_oidstr, commit_msg)) < 0 ||
                (error = git_repository_head(&our_ref, repo)) < 0 ||
                (error = git_reference_peel((git_object **)&our_commit, our_ref, GIT_OBJ_COMMIT)) < 0 ||
-               (error = git_revert_commit(&index_new, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 ||
-               (error = git_merge__indexes(repo, index_new)) < 0 ||
-               (error = git_repository_index(&index_repo, repo)) < 0 ||
-               (error = git_merge__append_conflicts_to_merge_msg(repo, index_repo)) < 0 ||
-               (error = git_checkout_index(repo, index_repo, &opts.checkout_opts)) < 0)
+               (error = git_revert_commit(&index, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 ||
+               (error = git_merge__check_result(repo, index)) < 0 ||
+               (error = git_merge__append_conflicts_to_merge_msg(repo, index)) < 0 ||
+               (error = git_checkout_index(repo, index, &opts.checkout_opts)) < 0 ||
+               (error = git_indexwriter_commit(&indexwriter)) < 0)
                goto on_error;
 
        goto done;
@@ -211,8 +214,8 @@ on_error:
        revert_state_cleanup(repo);
 
 done:
-       git_index_free(index_new);
-       git_index_free(index_repo);
+       git_indexwriter_cleanup(&indexwriter);
+       git_index_free(index);
        git_commit_free(our_commit);
        git_reference_free(our_ref);
        git_buf_free(&their_label);
@@ -220,14 +223,9 @@ done:
        return error;
 }
 
-int git_revert_init_opts(git_revert_options* opts, int version)
+int git_revert_init_options(git_revert_options *opts, unsigned int version)
 {
-       if (version != GIT_REVERT_OPTIONS_VERSION) {
-               giterr_set(GITERR_INVALID, "Invalid version %d for git_revert_options", version);
-               return -1;
-       } else {
-               git_revert_options o = GIT_REVERT_OPTIONS_INIT;
-               memcpy(opts, &o, sizeof(o));
-               return 0;
-       }
+       GIT_INIT_STRUCTURE_FROM_TEMPLATE(
+               opts, version, git_revert_options, GIT_REVERT_OPTIONS_INIT);
+       return 0;
 }