summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/checkout.c5
-rw-r--r--src/cherrypick.c18
-rw-r--r--src/index.c122
-rw-r--r--src/index.h29
-rw-r--r--src/merge.c25
-rw-r--r--src/rebase.c15
-rw-r--r--src/revert.c17
7 files changed, 172 insertions, 59 deletions
diff --git a/src/checkout.c b/src/checkout.c
index 3f65a9ed7..395384030 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -2375,6 +2375,9 @@ cleanup:
return error;
}
+#define CHECKOUT_INDEX_DONT_WRITE_MASK \
+ (GIT_CHECKOUT_DONT_UPDATE_INDEX | GIT_CHECKOUT_DONT_WRITE_INDEX)
+
int git_checkout_iterator(
git_iterator *target,
git_index *index,
@@ -2481,7 +2484,7 @@ int git_checkout_iterator(
cleanup:
if (!error && data.index != NULL &&
- (data.strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0)
+ (data.strategy & CHECKOUT_INDEX_DONT_WRITE_MASK) == 0)
error = git_index_write(data.index);
git_diff_free(data.diff);
diff --git a/src/cherrypick.c b/src/cherrypick.c
index e58d0ab4c..ebc9fcdd8 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,8 @@ 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;
+ git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
int error = 0;
assert(repo && commit);
@@ -192,21 +194,25 @@ 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 = git_indexwriter_init_for_operation(&indexwriter, repo, &opts.checkout_opts.checkout_strategy)) < 0 ||
(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_cherrypick_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;
on_error:
cherrypick_state_cleanup(repo);
done:
- git_index_free(index_new);
+ git_indexwriter_cleanup(&indexwriter);
+ git_index_free(index);
git_commit_free(our_commit);
git_reference_free(our_ref);
git_buf_free(&their_label);
diff --git a/src/index.c b/src/index.c
index 75700a719..c122cdf6d 100644
--- a/src/index.c
+++ b/src/index.c
@@ -656,39 +656,15 @@ int git_index__changed_relative_to(
int git_index_write(git_index *index)
{
- git_filebuf file = GIT_FILEBUF_INIT;
+ git_indexwriter writer = GIT_INDEXWRITER_INIT;
int error;
- if (!index->index_file_path)
- return create_index_error(-1,
- "Failed to read index: The index is in-memory only");
-
- if (index_sort_if_needed(index, true) < 0)
- return -1;
- git_vector_sort(&index->reuc);
-
- if ((error = git_filebuf_open(
- &file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS, GIT_INDEX_FILE_MODE)) < 0) {
- if (error == GIT_ELOCKED)
- giterr_set(GITERR_INDEX, "The index is locked. This might be due to a concurrent or crashed process");
-
- return error;
- }
-
- if ((error = write_index(index, &file)) < 0) {
- git_filebuf_cleanup(&file);
- return error;
- }
+ if ((error = git_indexwriter_init(&writer, index)) == 0)
+ error = git_indexwriter_commit(&writer);
- if ((error = git_filebuf_commit(&file)) < 0)
- return error;
-
- if (git_futils_filestamp_check(&index->stamp, index->index_file_path) < 0)
- /* index could not be read from disk! */;
- else
- index->on_disk = 1;
+ git_indexwriter_cleanup(&writer);
- return 0;
+ return error;
}
const char * git_index_path(const git_index *index)
@@ -2695,3 +2671,91 @@ int git_index_snapshot_find(
{
return index_find_in_entries(out, entries, entry_srch, path, path_len, stage);
}
+
+int git_indexwriter_init(
+ git_indexwriter *writer,
+ git_index *index)
+{
+ int error;
+
+ GIT_REFCOUNT_INC(index);
+
+ writer->index = index;
+
+ if (!index->index_file_path)
+ return create_index_error(-1,
+ "Failed to write index: The index is in-memory only");
+
+ if ((error = git_filebuf_open(
+ &writer->file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS, GIT_INDEX_FILE_MODE)) < 0) {
+
+ if (error == GIT_ELOCKED)
+ giterr_set(GITERR_INDEX, "The index is locked. This might be due to a concurrent or crashed process");
+
+ return error;
+ }
+
+ writer->should_write = 1;
+
+ return 0;
+}
+
+int git_indexwriter_init_for_operation(
+ git_indexwriter *writer,
+ git_repository *repo,
+ unsigned int *checkout_strategy)
+{
+ git_index *index;
+ int error;
+
+ if ((error = git_repository_index__weakptr(&index, repo)) < 0 ||
+ (error = git_indexwriter_init(writer, index)) < 0)
+ return error;
+
+ writer->should_write = (*checkout_strategy & GIT_CHECKOUT_DONT_WRITE_INDEX) == 0;
+ *checkout_strategy |= GIT_CHECKOUT_DONT_WRITE_INDEX;
+
+ return 0;
+}
+
+int git_indexwriter_commit(git_indexwriter *writer)
+{
+ int error;
+
+ if (!writer->should_write)
+ return 0;
+
+ if (index_sort_if_needed(writer->index, true) < 0)
+ return -1;
+
+ git_vector_sort(&writer->index->reuc);
+
+ if ((error = write_index(writer->index, &writer->file)) < 0) {
+ git_indexwriter_cleanup(writer);
+ return error;
+ }
+
+ if ((error = git_filebuf_commit(&writer->file)) < 0)
+ return error;
+
+ if ((error = git_futils_filestamp_check(
+ &writer->index->stamp, writer->index->index_file_path)) < 0) {
+ giterr_set(GITERR_OS, "Could not read index timestamp");
+ return -1;
+ }
+
+ writer->index->on_disk = 1;
+
+ git_index_free(writer->index);
+ writer->index = NULL;
+
+ return 0;
+}
+
+void git_indexwriter_cleanup(git_indexwriter *writer)
+{
+ git_filebuf_cleanup(&writer->file);
+
+ git_index_free(writer->index);
+ writer->index = NULL;
+}
diff --git a/src/index.h b/src/index.h
index 2eb93fb17..6d2904fdc 100644
--- a/src/index.h
+++ b/src/index.h
@@ -94,4 +94,33 @@ extern int git_index_snapshot_find(
const char *path, size_t path_len, int stage);
+typedef struct {
+ git_index *index;
+ git_filebuf file;
+ unsigned int should_write:1;
+} git_indexwriter;
+
+#define GIT_INDEXWRITER_INIT { NULL, GIT_FILEBUF_INIT }
+
+/* Lock the index for eventual writing. */
+extern int git_indexwriter_init(git_indexwriter *writer, git_index *index);
+
+/* Lock the index for eventual writing by a repository operation: a merge,
+ * revert, cherry-pick or a rebase. Note that the given checkout strategy
+ * will be updated for the operation's use so that checkout will not write
+ * the index.
+ */
+extern int git_indexwriter_init_for_operation(
+ git_indexwriter *writer,
+ git_repository *repo,
+ unsigned int *checkout_strategy);
+
+/* Write the index and unlock it. */
+extern int git_indexwriter_commit(git_indexwriter *writer);
+
+/* Cleanup an index writing session, unlocking the file (if it is still
+ * locked and freeing any data structures.
+ */
+extern void git_indexwriter_cleanup(git_indexwriter *writer);
+
#endif
diff --git a/src/merge.c b/src/merge.c
index e4b60c847..75ad0915e 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -2652,7 +2652,8 @@ int git_merge(
git_checkout_options checkout_opts;
git_annotated_commit *ancestor_head = NULL, *our_head = NULL;
git_tree *ancestor_tree = NULL, *our_tree = NULL, **their_trees = NULL;
- git_index *index_new = NULL;
+ git_index *index = NULL;
+ git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
size_t i;
int error = 0;
@@ -2666,11 +2667,10 @@ int git_merge(
their_trees = git__calloc(their_heads_len, sizeof(git_tree *));
GITERR_CHECK_ALLOC(their_trees);
- if ((error = merge_heads(&ancestor_head, &our_head, repo, their_heads, their_heads_len)) < 0)
- goto on_error;
-
- if ((error = merge_normalize_checkout_opts(repo, &checkout_opts, given_checkout_opts,
- ancestor_head, our_head, their_heads_len, their_heads)) < 0)
+ if ((error = merge_heads(&ancestor_head, &our_head, repo, their_heads, their_heads_len)) < 0 ||
+ (error = merge_normalize_checkout_opts(repo, &checkout_opts, given_checkout_opts,
+ ancestor_head, our_head, their_heads_len, their_heads)) < 0 ||
+ (error = git_indexwriter_init_for_operation(&indexwriter, repo, &checkout_opts.checkout_strategy)) < 0)
goto on_error;
/* Write the merge files to the repository. */
@@ -2691,10 +2691,11 @@ int git_merge(
/* TODO: recursive, octopus, etc... */
- if ((error = git_merge_trees(&index_new, repo, ancestor_tree, our_tree, their_trees[0], 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, &checkout_opts)) < 0)
+ if ((error = git_merge_trees(&index, repo, ancestor_tree, our_tree, their_trees[0], 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, &checkout_opts)) < 0 ||
+ (error = git_indexwriter_commit(&indexwriter)) < 0)
goto on_error;
goto done;
@@ -2703,7 +2704,9 @@ on_error:
merge_state_cleanup(repo);
done:
- git_index_free(index_new);
+ git_indexwriter_cleanup(&indexwriter);
+
+ git_index_free(index);
git_tree_free(ancestor_tree);
git_tree_free(our_tree);
diff --git a/src/rebase.c b/src/rebase.c
index b0a93a63b..8078eedc3 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>
@@ -763,6 +764,7 @@ static int rebase_next_merge(
git_commit *current_commit = NULL, *parent_commit = NULL;
git_tree *current_tree = NULL, *head_tree = NULL, *parent_tree = NULL;
git_index *index = NULL;
+ git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
git_rebase_operation *operation;
char current_idstr[GIT_OID_HEXSZ];
unsigned int parent_count;
@@ -792,20 +794,21 @@ static int rebase_next_merge(
git_oid_fmt(current_idstr, &operation->id);
- 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;
-
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 ||
+ if ((error = git_indexwriter_init_for_operation(&indexwriter, rebase->repo, &checkout_opts.checkout_strategy)) < 0 ||
+ (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 ||
+ (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_checkout_index(rebase->repo, index, &checkout_opts)) < 0 ||
+ (error = git_indexwriter_commit(&indexwriter)) < 0)
goto done;
*out = operation;
done:
+ git_indexwriter_cleanup(&indexwriter);
git_index_free(index);
git_tree_free(current_tree);
git_tree_free(head_tree);
diff --git a/src/revert.c b/src/revert.c
index 36560a77c..f8a7f4333 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,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;
+ git_index *index = NULL;
+ git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
int error;
assert(repo && commit);
@@ -194,14 +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__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_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;
@@ -210,7 +214,8 @@ on_error:
revert_state_cleanup(repo);
done:
- git_index_free(index_new);
+ git_indexwriter_cleanup(&indexwriter);
+ git_index_free(index);
git_commit_free(our_commit);
git_reference_free(our_ref);
git_buf_free(&their_label);