merge-like operations: lock index while working
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
diff --git a/src/cherrypick.c b/src/cherrypick.c
index e58d0ab..3754b1f 100644
--- a/src/cherrypick.c
+++ b/src/cherrypick.c
@@ -10,6 +10,7 @@
#include "filebuf.h"
#include "merge.h"
#include "vector.h"
+#include "index.h"
#include "git2/types.h"
#include "git2/merge.h"
@@ -171,7 +172,9 @@ int git_cherrypick(
char commit_oidstr[GIT_OID_HEXSZ + 1];
const char *commit_msg, *commit_summary;
git_buf their_label = GIT_BUF_INIT;
- git_index *index_new = NULL;
+ git_index *index = NULL, *index_new = NULL;
+ git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
+ bool write_index = false;
int error = 0;
assert(repo && commit);
@@ -191,21 +194,38 @@ int git_cherrypick(
if ((error = write_merge_msg(repo, commit_msg)) < 0 ||
(error = git_buf_printf(&their_label, "%.7s... %s", commit_oidstr, commit_summary)) < 0 ||
- (error = cherrypick_normalize_opts(repo, &opts, given_opts, git_buf_cstr(&their_label))) < 0 ||
- (error = write_cherrypick_head(repo, commit_oidstr)) < 0 ||
+ (error = cherrypick_normalize_opts(repo, &opts, given_opts, git_buf_cstr(&their_label))) < 0)
+ goto on_error;
+
+ write_index = (opts.checkout_opts.checkout_strategy & GIT_CHECKOUT_DONT_WRITE_INDEX) == 0;
+
+ if (write_index) {
+ /* Never let checkout update the index, we'll update it ourselves. */
+ opts.checkout_opts.checkout_strategy |= GIT_CHECKOUT_DONT_WRITE_INDEX;
+
+ if ((error = git_repository_index(&index, repo)) < 0 ||
+ (error = git_indexwriter_init(&indexwriter, index)) < 0)
+ goto on_error;
+ }
+
+ if ((error = write_cherrypick_head(repo, commit_oidstr)) < 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_cherrypick_commit(&index_new, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 ||
(error = git_merge__check_result(repo, index_new)) < 0 ||
(error = git_merge__append_conflicts_to_merge_msg(repo, index_new)) < 0 ||
- (error = git_checkout_index(repo, index_new, &opts.checkout_opts)) < 0)
+ (error = git_checkout_index(repo, index_new, &opts.checkout_opts)) < 0 ||
+ (write_index && (error = git_indexwriter_commit(&indexwriter)) < 0))
goto on_error;
+
goto done;
on_error:
cherrypick_state_cleanup(repo);
done:
+ git_indexwriter_cleanup(&indexwriter);
+ git_index_free(index);
git_index_free(index_new);
git_commit_free(our_commit);
git_reference_free(our_ref);
diff --git a/src/rebase.c b/src/rebase.c
index 2e80592..9e6c4c1 100644
--- a/src/rebase.c
+++ b/src/rebase.c
@@ -14,6 +14,7 @@
#include "array.h"
#include "config.h"
#include "annotated_commit.h"
+#include "index.h"
#include <git2/types.h>
#include <git2/annotated_commit.h>
@@ -725,7 +726,9 @@ static int rebase_next_merge(
git_checkout_options checkout_opts = {0};
git_commit *current_commit = NULL, *parent_commit = NULL;
git_tree *current_tree = NULL, *head_tree = NULL, *parent_tree = NULL;
- git_index *index = NULL;
+ git_index *index = NULL, *index_new = NULL;
+ git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
+ bool write_index = false;
git_rebase_operation *operation;
char current_idstr[GIT_OID_HEXSZ];
unsigned int parent_count;
@@ -755,6 +758,17 @@ static int rebase_next_merge(
git_oid_fmt(current_idstr, &operation->id);
+ write_index = (checkout_opts.checkout_strategy & GIT_CHECKOUT_DONT_WRITE_INDEX) == 0;
+
+ if (write_index) {
+ /* Never let checkout update the index, we'll update it ourselves. */
+ checkout_opts.checkout_strategy |= GIT_CHECKOUT_DONT_WRITE_INDEX;
+
+ if ((error = git_repository_index(&index, rebase->repo)) < 0 ||
+ (error = git_indexwriter_init(&indexwriter, index)) < 0)
+ goto done;
+ }
+
if ((error = rebase_setupfile(rebase, MSGNUM_FILE, -1, "%d\n", rebase->current+1)) < 0 ||
(error = rebase_setupfile(rebase, CURRENT_FILE, -1, "%.*s\n", GIT_OID_HEXSZ, current_idstr)) < 0)
goto done;
@@ -762,14 +776,17 @@ static int rebase_next_merge(
normalize_checkout_opts(rebase, current_commit, &checkout_opts, given_checkout_opts);
if ((error = git_merge_trees(&index, rebase->repo, parent_tree, head_tree, current_tree, NULL)) < 0 ||
- (error = git_merge__check_result(rebase->repo, index)) < 0 ||
- (error = git_checkout_index(rebase->repo, index, &checkout_opts)) < 0)
+ (error = git_merge__check_result(rebase->repo, index_new)) < 0 ||
+ (error = git_checkout_index(rebase->repo, index_new, &checkout_opts)) < 0 ||
+ (write_index && (error = git_indexwriter_commit(&indexwriter)) < 0))
goto done;
*out = operation;
done:
+ git_indexwriter_cleanup(&indexwriter);
git_index_free(index);
+ git_index_free(index_new);
git_tree_free(current_tree);
git_tree_free(head_tree);
git_tree_free(parent_tree);
diff --git a/src/revert.c b/src/revert.c
index 36560a7..f5f1a5d 100644
--- a/src/revert.c
+++ b/src/revert.c
@@ -9,6 +9,7 @@
#include "repository.h"
#include "filebuf.h"
#include "merge.h"
+#include "index.h"
#include "git2/types.h"
#include "git2/merge.h"
@@ -174,7 +175,9 @@ 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;
+ git_index *index = NULL, *index_new = NULL;
+ git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
+ bool write_index = false;
int error;
assert(repo && commit);
@@ -193,15 +196,29 @@ 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 = write_revert_head(repo, commit_oidstr)) < 0 ||
+ (error = revert_normalize_opts(repo, &opts, given_opts, git_buf_cstr(&their_label))) < 0)
+ goto on_error;
+
+ write_index = (opts.checkout_opts.checkout_strategy & GIT_CHECKOUT_DONT_WRITE_INDEX) == 0;
+
+ if (write_index) {
+ /* Never let checkout update the index, we'll update it ourselves. */
+ opts.checkout_opts.checkout_strategy |= GIT_CHECKOUT_DONT_WRITE_INDEX;
+
+ if ((error = git_repository_index(&index, repo)) < 0 ||
+ (error = git_indexwriter_init(&indexwriter, index)) < 0)
+ goto on_error;
+ }
+
+ if ((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__check_result(repo, index_new)) < 0 ||
(error = git_merge__append_conflicts_to_merge_msg(repo, index_new)) < 0 ||
- (error = git_checkout_index(repo, index_new, &opts.checkout_opts)) < 0)
+ (error = git_checkout_index(repo, index_new, &opts.checkout_opts)) < 0 ||
+ (write_index && (error = git_indexwriter_commit(&indexwriter)) < 0))
goto on_error;
goto done;
@@ -210,6 +227,8 @@ on_error:
revert_state_cleanup(repo);
done:
+ git_indexwriter_cleanup(&indexwriter);
+ git_index_free(index);
git_index_free(index_new);
git_commit_free(our_commit);
git_reference_free(our_ref);