From 8ff0f3250a62a128fb68ce3fae1719549a3fe47a Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Wed, 31 Oct 2012 22:26:57 +0100 Subject: [PATCH 1/4] index: Switch to git_futils_filestamp --- src/index.c | 36 +++++++++++++++++------------------- src/index.h | 2 +- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/index.c b/src/index.c index 35cf5dd8f..8f684a34b 100644 --- a/src/index.c +++ b/src/index.c @@ -334,7 +334,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,9 +390,9 @@ 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); @@ -402,23 +402,21 @@ 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); + git_index_clear(index); + error = parse_index(index, buffer.ptr, buffer.size); - if (!error) - index->last_modified = mtime; - - git_buf_free(&buffer); - } + if (!error) + git_futils_filestamp_set(&index->stamp, &stamp); + git_buf_free(&buffer); return error; } @@ -443,11 +441,11 @@ 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; } 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; From 276ea401b3a45c85e49182f39db00ca5447aa340 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Thu, 1 Nov 2012 20:15:53 +0100 Subject: [PATCH 2/4] index: Add git_index_write_tree --- include/git2/index.h | 32 +++++++++++++++++++++++++++ include/git2/tree.h | 18 --------------- src/index.c | 52 +++++++++++++++++++++++++++++++++++++------- src/tree.c | 8 ++----- src/tree.h | 6 +++++ 5 files changed, 84 insertions(+), 32 deletions(-) diff --git a/include/git2/index.h b/include/git2/index.h index 1d91663d8..2a0b001ff 100644 --- a/include/git2/index.h +++ b/include/git2/index.h @@ -190,6 +190,38 @@ GIT_EXTERN(int) git_index_write(git_index *index); */ GIT_EXTERN(int) git_index_read_tree(git_index *index, git_tree *tree); +/** + * Write the index as a tree + * + * This method will scan the index and write a representation + * of its current state back to disk; it recursively creates + * tree objects for each of the subtrees stored in the index, + * but only returns the OID of the root tree. This is the OID + * that can be used e.g. to create a commit. + * + * The index instance cannot be bare, and needs to be associated + * to an existing repository. + * + * @param oid Pointer where to store the OID of the written tree + * @param index Index to write + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_write_tree(git_oid *oid, git_index *index); + +/** + * Write the index as a tree to the given repository + * + * This method will do the same as `git_index_write_tree`, but + * letting the user choose the repository where the tree will + * be written. + * + * @param oid Pointer where to store OID of the the written tree + * @param index Index to write + * @param repo Repository where to write the tree + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_write_tree_to(git_oid *oid, git_index *index, git_repository *repo); + /**@}*/ /** @name Raw Index Entry Functions diff --git a/include/git2/tree.h b/include/git2/tree.h index 2ee1f4afa..527f81819 100644 --- a/include/git2/tree.h +++ b/include/git2/tree.h @@ -184,24 +184,6 @@ GIT_EXTERN(int) git_tree_entry_to_object( git_repository *repo, const git_tree_entry *entry); -/** - * Write a tree to the ODB from the index file - * - * This method will scan the index and write a representation - * of its current state back to disk; it recursively creates - * tree objects for each of the subtrees stored in the index, - * but only returns the OID of the root tree. This is the OID - * that can be used e.g. to create a commit. - * - * The index instance cannot be bare, and needs to be associated - * to an existing repository. - * - * @param oid Pointer where to store the written tree - * @param index Index to write - * @return 0 or an error code - */ -GIT_EXTERN(int) git_tree_create_fromindex(git_oid *oid, git_index *index); - /** * Create a new tree builder. * diff --git a/src/index.c b/src/index.c index 8f684a34b..73fd40acf 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,10 @@ 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; } static void index_free(git_index *index) @@ -394,7 +397,11 @@ int git_index_read(git_index *index) git_buf buffer = GIT_BUF_INIT; 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); @@ -426,6 +433,12 @@ int git_index_write(git_index *index) 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); @@ -449,6 +462,29 @@ int git_index_write(git_index *index) 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/tree.c b/src/tree.c index 9ecefbb61..dd9f94869 100644 --- a/src/tree.c +++ b/src/tree.c @@ -491,16 +491,12 @@ 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 */ From 43eeca04a7fe00332fe8c4723e29fa82a5304b13 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Thu, 1 Nov 2012 20:24:43 +0100 Subject: [PATCH 3/4] index: Fix tests --- src/index.c | 3 +-- src/stash.c | 2 +- src/tree.c | 1 - tests-clar/index/read_tree.c | 4 ++-- tests-clar/object/commit/commitstagedfile.c | 2 +- tests-clar/stash/stash_helpers.c | 2 +- 6 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/index.c b/src/index.c index 73fd40acf..ffa63f6a7 100644 --- a/src/index.c +++ b/src/index.c @@ -430,7 +430,6 @@ int git_index_read(git_index *index) int git_index_write(git_index *index) { git_filebuf file = GIT_FILEBUF_INIT; - struct stat indexst; int error; if (!index->index_file_path) { @@ -473,7 +472,7 @@ int git_index_write_tree(git_oid *oid, git_index *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 -1; } return git_tree__write_index(oid, index, repo); 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 dd9f94869..46b4a6dd1 100644 --- a/src/tree.c +++ b/src/tree.c @@ -494,7 +494,6 @@ on_error: int git_tree__write_index(git_oid *oid, git_index *index, git_repository *repo) { int ret; - git_repository *repo; assert(oid && index && repo); diff --git a/tests-clar/index/read_tree.c b/tests-clar/index/read_tree.c index f63a54bf2..3ae883d18 100644 --- a/tests-clar/index/read_tree.c +++ b/tests-clar/index/read_tree.c @@ -29,14 +29,14 @@ void test_index_read_tree__read_write_involution(void) cl_git_pass(git_index_add_from_workdir(index, "abc/d")); /* write-tree */ - cl_git_pass(git_tree_create_fromindex(&expected, index)); + cl_git_pass(git_index_write_tree(&expected, index)); /* read-tree */ git_tree_lookup(&tree, repo, &expected); cl_git_pass(git_index_read_tree(index, tree)); git_tree_free(tree); - cl_git_pass(git_tree_create_fromindex(&tree_oid, index)); + cl_git_pass(git_index_write_tree(&tree_oid, index)); cl_assert(git_oid_cmp(&expected, &tree_oid) == 0); git_index_free(index); diff --git a/tests-clar/object/commit/commitstagedfile.c b/tests-clar/object/commit/commitstagedfile.c index ac38acf5b..eb78cedaa 100644 --- a/tests-clar/object/commit/commitstagedfile.c +++ b/tests-clar/object/commit/commitstagedfile.c @@ -99,7 +99,7 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void) /* * Build the tree from the index */ - cl_git_pass(git_tree_create_fromindex(&tree_oid, index)); + cl_git_pass(git_index_write_tree(&tree_oid, index)); cl_assert(git_oid_cmp(&expected_tree_oid, &tree_oid) == 0); diff --git a/tests-clar/stash/stash_helpers.c b/tests-clar/stash/stash_helpers.c index 0e93ecff0..000a0f1a9 100644 --- a/tests-clar/stash/stash_helpers.c +++ b/tests-clar/stash/stash_helpers.c @@ -13,7 +13,7 @@ void commit_staged_files( repo = git_index_owner(index); - cl_git_pass(git_tree_create_fromindex(&tree_oid, index)); + cl_git_pass(git_index_write_tree(&tree_oid, index)); cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid)); cl_git_pass(git_commit_create_v( From 1e808f9cda598fef83ff93deb007212ebf61be6d Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Thu, 1 Nov 2012 20:28:28 +0100 Subject: [PATCH 4/4] index: Add `git_index_new` --- include/git2/index.h | 13 +++++++++++++ src/index.c | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/include/git2/index.h b/include/git2/index.h index 2a0b001ff..bca9791c5 100644 --- a/include/git2/index.h +++ b/include/git2/index.h @@ -125,6 +125,19 @@ enum { */ GIT_EXTERN(int) git_index_open(git_index **index, const char *index_path); +/** + * Create an in-memory index object. + * + * This index object cannot be read/written to the filesystem, + * but may be used to perform in-memory index operations. + * + * The index must be freed once it's no longer in use. + * + * @param index the pointer for the new index + * @return 0 or an error code + */ +GIT_EXTERN(int) git_index_new(git_index **index); + /** * Free an existing index object. * diff --git a/src/index.c b/src/index.c index ffa63f6a7..cb83015a6 100644 --- a/src/index.c +++ b/src/index.c @@ -287,6 +287,11 @@ int git_index_open(git_index **index_out, const char *index_path) 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) { git_index_entry *e;