diff options
| author | Russell Belfer <rb@github.com> | 2012-11-01 14:16:19 -0700 |
|---|---|---|
| committer | Russell Belfer <rb@github.com> | 2012-11-01 14:16:19 -0700 |
| commit | 60ad7d52b8828a7c1fd0720e166d00a6ea3fe6bc (patch) | |
| tree | ed40a431c2884cb87ea489640e4217463df9e325 /src | |
| parent | dbd6850d06111eb0761499d7c876ff7cd4ad57fa (diff) | |
| parent | 1e808f9cda598fef83ff93deb007212ebf61be6d (diff) | |
| download | libgit2-60ad7d52b8828a7c1fd0720e166d00a6ea3fe6bc.tar.gz | |
Merge pull request #1037 from libgit2/index-open-cleanup
Some more changes to the Index API
Diffstat (limited to 'src')
| -rw-r--r-- | src/index.c | 94 | ||||
| -rw-r--r-- | src/index.h | 2 | ||||
| -rw-r--r-- | src/stash.c | 2 | ||||
| -rw-r--r-- | src/tree.c | 9 | ||||
| -rw-r--r-- | src/tree.h | 6 |
5 files changed, 76 insertions, 37 deletions
diff --git a/src/index.c b/src/index.c index 35cf5dd8f..cb83015a6 100644 --- a/src/index.c +++ b/src/index.c @@ -264,8 +264,14 @@ int git_index_open(git_index **index_out, const char *index_path) index = git__calloc(1, sizeof(git_index)); GITERR_CHECK_ALLOC(index); - index->index_file_path = git__strdup(index_path); - GITERR_CHECK_ALLOC(index->index_file_path); + if (index_path != NULL) { + index->index_file_path = git__strdup(index_path); + GITERR_CHECK_ALLOC(index->index_file_path); + + /* Check if index file is stored on disk already */ + if (git_path_exists(index->index_file_path) == true) + index->on_disk = 1; + } if (git_vector_init(&index->entries, 32, index_cmp) < 0) return -1; @@ -275,13 +281,15 @@ int git_index_open(git_index **index_out, const char *index_path) index->entries_search_path = index_srch_path; index->reuc_search = reuc_srch; - /* Check if index file is stored on disk already */ - if (git_path_exists(index->index_file_path) == true) - index->on_disk = 1; - *index_out = index; GIT_REFCOUNT_INC(index); - return git_index_read(index); + + return (index_path != NULL) ? git_index_read(index) : 0; +} + +int git_index_new(git_index **out) +{ + return git_index_open(out, NULL); } static void index_free(git_index *index) @@ -334,7 +342,7 @@ void git_index_clear(git_index *index) git_vector_clear(&index->entries); git_vector_clear(&index->reuc); - index->last_modified = 0; + git_futils_filestamp_set(&index->stamp, NULL); git_tree_cache_free(index->tree); index->tree = NULL; @@ -390,11 +398,15 @@ unsigned int git_index_caps(const git_index *index) int git_index_read(git_index *index) { - int error, updated; + int error = 0, updated; git_buf buffer = GIT_BUF_INIT; - time_t mtime; + git_futils_filestamp stamp; - assert(index->index_file_path); + if (!index->index_file_path) { + giterr_set(GITERR_INDEX, + "Failed to read index: The index is in-memory only"); + return -1; + } if (!index->on_disk || git_path_exists(index->index_file_path) == false) { git_index_clear(index); @@ -402,32 +414,35 @@ int git_index_read(git_index *index) return 0; } - /* We don't want to update the mtime if we fail to parse the index */ - mtime = index->last_modified; - error = git_futils_readbuffer_updated( - &buffer, index->index_file_path, &mtime, NULL, &updated); + updated = git_futils_filestamp_check(&stamp, index->index_file_path); + if (updated <= 0) + return updated; + + error = git_futils_readbuffer(&buffer, index->index_file_path); if (error < 0) return error; - if (updated) { - git_index_clear(index); - error = parse_index(index, buffer.ptr, buffer.size); - - if (!error) - index->last_modified = mtime; + git_index_clear(index); + error = parse_index(index, buffer.ptr, buffer.size); - git_buf_free(&buffer); - } + if (!error) + git_futils_filestamp_set(&index->stamp, &stamp); + git_buf_free(&buffer); return error; } int git_index_write(git_index *index) { git_filebuf file = GIT_FILEBUF_INIT; - struct stat indexst; int error; + if (!index->index_file_path) { + giterr_set(GITERR_INDEX, + "Failed to write index: The index is in-memory only"); + return -1; + } + git_vector_sort(&index->entries); git_vector_sort(&index->reuc); @@ -443,14 +458,37 @@ int git_index_write(git_index *index) if ((error = git_filebuf_commit(&file, GIT_INDEX_FILE_MODE)) < 0) return error; - if (p_stat(index->index_file_path, &indexst) == 0) { - index->last_modified = indexst.st_mtime; - index->on_disk = 1; - } + error = git_futils_filestamp_check(&index->stamp, index->index_file_path); + if (error < 0) + return error; + index->on_disk = 1; return 0; } +int git_index_write_tree(git_oid *oid, git_index *index) +{ + git_repository *repo; + + assert(oid && index); + + repo = (git_repository *)GIT_REFCOUNT_OWNER(index); + + if (repo == NULL) { + giterr_set(GITERR_INDEX, "Failed to write tree. " + "The index file is not backed up by an existing repository"); + return -1; + } + + return git_tree__write_index(oid, index, repo); +} + +int git_index_write_tree_to(git_oid *oid, git_index *index, git_repository *repo) +{ + assert(oid && index && repo); + return git_tree__write_index(oid, index, repo); +} + unsigned int git_index_entrycount(git_index *index) { assert(index); diff --git a/src/index.h b/src/index.h index 0fd59dd45..86158eb84 100644 --- a/src/index.h +++ b/src/index.h @@ -22,7 +22,7 @@ struct git_index { char *index_file_path; - time_t last_modified; + git_futils_filestamp stamp; git_vector entries; unsigned int on_disk:1; diff --git a/src/stash.c b/src/stash.c index 9c9c5dce7..1d6940e3c 100644 --- a/src/stash.c +++ b/src/stash.c @@ -115,7 +115,7 @@ static int build_tree_from_index(git_tree **out, git_index *index) { git_oid i_tree_oid; - if (git_tree_create_fromindex(&i_tree_oid, index) < 0) + if (git_index_write_tree(&i_tree_oid, index) < 0) return -1; return git_tree_lookup(out, git_index_owner(index), &i_tree_oid); diff --git a/src/tree.c b/src/tree.c index 9ecefbb61..46b4a6dd1 100644 --- a/src/tree.c +++ b/src/tree.c @@ -491,16 +491,11 @@ on_error: return -1; } -int git_tree_create_fromindex(git_oid *oid, git_index *index) +int git_tree__write_index(git_oid *oid, git_index *index, git_repository *repo) { int ret; - git_repository *repo; - repo = (git_repository *)GIT_REFCOUNT_OWNER(index); - - if (repo == NULL) - return tree_error("Failed to create tree. " - "The index file is not backed up by an existing repository"); + assert(oid && index && repo); if (index->tree != NULL && index->tree->entries >= 0) { git_oid_cpy(oid, &index->tree->oid); diff --git a/src/tree.h b/src/tree.h index 24b517ce3..b67c55202 100644 --- a/src/tree.h +++ b/src/tree.h @@ -47,6 +47,12 @@ int git_tree__parse(git_tree *tree, git_odb_object *obj); */ int git_tree__prefix_position(git_tree *tree, const char *prefix); + +/** + * Write a tree to the given repository + */ +int git_tree__write_index(git_oid *oid, git_index *index, git_repository *repo); + /** * Obsolete mode kept for compatibility reasons */ |
