Commit a291790a by Carlos Martín Nieto

Merge pull request #2831 from ethomson/merge_lock

merge: lock index during the merge (not just checkout)
parents a7fa970f 41fae48d
......@@ -135,7 +135,10 @@ typedef enum {
/** Only update existing files, don't create new ones */
GIT_CHECKOUT_UPDATE_ONLY = (1u << 7),
/** Normally checkout updates index entries as it goes; this stops that */
/**
* Normally checkout updates index entries as it goes; this stops that.
* Implies `GIT_CHECKOUT_DONT_WRITE_INDEX`.
*/
GIT_CHECKOUT_DONT_UPDATE_INDEX = (1u << 8),
/** Don't refresh index/config/etc before doing checkout */
......@@ -166,6 +169,9 @@ typedef enum {
/** Don't overwrite existing files or folders */
GIT_CHECKOUT_DONT_REMOVE_EXISTING = (1u << 22),
/** Normally checkout writes the index upon completion; this prevents that. */
GIT_CHECKOUT_DONT_WRITE_INDEX = (1u << 23),
/**
* THE FOLLOWING OPTIONS ARE NOT YET IMPLEMENTED
*/
......
......@@ -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);
......
......@@ -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);
......
......@@ -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;
}
......@@ -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
......@@ -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);
......
......@@ -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);
......
......@@ -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);
......
......@@ -1184,3 +1184,83 @@ void test_checkout_tree__caches_attributes_during_checkout(void)
git_buf_free(&ident2);
git_object_free(obj);
}
void test_checkout_tree__can_not_update_index(void)
{
git_oid oid;
git_object *head;
unsigned int status;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
opts.checkout_strategy |=
GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_UPDATE_INDEX;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
cl_git_pass(git_object_lookup(&head, g_repo, &oid, GIT_OBJ_ANY));
cl_git_pass(git_reset(g_repo, head, GIT_RESET_HARD, &g_opts, NULL, NULL));
cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));
cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_write(index));
cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
git_object_free(head);
git_index_free(index);
}
void test_checkout_tree__can_update_but_not_write_index(void)
{
git_oid oid;
git_object *head;
unsigned int status;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
git_repository *other;
opts.checkout_strategy |=
GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_WRITE_INDEX;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
cl_git_pass(git_object_lookup(&head, g_repo, &oid, GIT_OBJ_ANY));
cl_git_pass(git_reset(g_repo, head, GIT_RESET_HARD, &g_opts, NULL, NULL));
cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));
cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
cl_assert_equal_i(GIT_STATUS_INDEX_NEW, status);
cl_git_pass(git_repository_open(&other, "testrepo"));
cl_git_pass(git_status_file(&status, other, "ab/de/2.txt"));
cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
git_repository_free(other);
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_write(index));
cl_git_pass(git_repository_open(&other, "testrepo"));
cl_git_pass(git_status_file(&status, other, "ab/de/2.txt"));
cl_assert_equal_i(GIT_STATUS_INDEX_NEW, status);
git_repository_free(other);
git_object_free(head);
git_index_free(index);
}
......@@ -677,3 +677,24 @@ void test_index_tests__reload_while_ignoring_case(void)
git_index_free(index);
}
void test_index_tests__can_lock_index(void)
{
git_index *index;
git_indexwriter one = GIT_INDEXWRITER_INIT,
two = GIT_INDEXWRITER_INIT;
cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
cl_git_pass(git_indexwriter_init(&one, index));
cl_git_fail_with(GIT_ELOCKED, git_indexwriter_init(&two, index));
cl_git_fail_with(GIT_ELOCKED, git_index_write(index));
cl_git_pass(git_indexwriter_commit(&one));
cl_git_pass(git_index_write(index));
git_indexwriter_cleanup(&one);
git_indexwriter_cleanup(&two);
git_index_free(index);
}
......@@ -1018,6 +1018,7 @@ void test_merge_workdir_setup__retained_after_success(void)
git_annotated_commit_free(their_heads[0]);
}
void test_merge_workdir_setup__removed_after_failure(void)
{
git_oid our_oid;
......@@ -1030,16 +1031,63 @@ void test_merge_workdir_setup__removed_after_failure(void)
cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
cl_git_write2file("merge-resolve/.git/index.lock", "foo\n", 4, O_RDWR|O_CREAT, 0666);
cl_git_fail(git_merge(
repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL));
cl_assert(!git_path_exists("merge-resolve/.git/" GIT_MERGE_HEAD_FILE));
cl_assert(!git_path_exists("merge-resolve/.git/" GIT_MERGE_MODE_FILE));
cl_assert(!git_path_exists("merge-resolve/.git/" GIT_MERGE_MSG_FILE));
git_reference_free(octo1_ref);
git_annotated_commit_free(our_head);
git_annotated_commit_free(their_heads[0]);
}
void test_merge_workdir_setup__unlocked_after_success(void)
{
git_oid our_oid;
git_reference *octo1_ref;
git_annotated_commit *our_head, *their_heads[1];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
cl_git_pass(git_merge(
repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL));
cl_assert(!git_path_exists("merge-resolve/.git/index.lock"));
git_reference_free(octo1_ref);
git_annotated_commit_free(our_head);
git_annotated_commit_free(their_heads[0]);
}
void test_merge_workdir_setup__unlocked_after_conflict(void)
{
git_oid our_oid;
git_reference *octo1_ref;
git_annotated_commit *our_head, *their_heads[1];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
cl_git_rewritefile("merge-resolve/new-in-octo1.txt",
"Conflicting file!\n\nMerge will fail!\n");
cl_git_fail(git_merge(
repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL));
cl_assert(!git_path_exists("merge-resolve/" GIT_MERGE_HEAD_FILE));
cl_assert(!git_path_exists("merge-resolve/" GIT_ORIG_HEAD_FILE));
cl_assert(!git_path_exists("merge-resolve/" GIT_MERGE_MODE_FILE));
cl_assert(!git_path_exists("merge-resolve/" GIT_MERGE_MSG_FILE));
cl_assert(!git_path_exists("merge-resolve/.git/index.lock"));
git_reference_free(octo1_ref);
......
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