diff options
| author | Edward Thomson <ethomson@microsoft.com> | 2015-02-03 22:31:10 -0500 |
|---|---|---|
| committer | Edward Thomson <ethomson@edwardthomson.com> | 2015-02-14 09:25:36 -0500 |
| commit | 41fae48df21c7d1140da539cb61fa3fd0598a3e7 (patch) | |
| tree | 0a022196f9c01f9036f24a4b6abb2f2dfb299828 /src/index.c | |
| parent | be8404a7680fa1951e20abdaea704156b3345876 (diff) | |
| download | libgit2-41fae48df21c7d1140da539cb61fa3fd0598a3e7.tar.gz | |
indexwriter: an indexwriter for repo operations
Provide git_indexwriter_init_for_operation for the common locking
pattern in merge, rebase, revert and cherry-pick.
Diffstat (limited to 'src/index.c')
| -rw-r--r-- | src/index.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/index.c b/src/index.c index 58575fec3..6c01b40f2 100644 --- a/src/index.c +++ b/src/index.c @@ -2669,6 +2669,8 @@ int git_indexwriter_init( { int error; + GIT_REFCOUNT_INC(index); + writer->index = index; if (!index->index_file_path) @@ -2677,12 +2679,33 @@ int git_indexwriter_init( 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; } @@ -2690,6 +2713,9 @@ 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; @@ -2711,10 +2737,16 @@ int git_indexwriter_commit(git_indexwriter *writer) 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; } |
