diff options
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; } |