Commit be8404a7680fa1951e20abdaea704156b3345876

Edward Thomson 2015-02-03T21:51:48

merge-like operations: lock index while working

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);