From 29cc374d2e8a73c458805f8e76a286b8b688b80e Mon Sep 17 00:00:00 2001 From: nulltoken Date: Thu, 8 Nov 2012 17:37:01 +0100 Subject: [PATCH 1/3] tree: enforce coverage of silent entry replacement --- tests-clar/object/tree/duplicateentries.c | 117 ++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 tests-clar/object/tree/duplicateentries.c diff --git a/tests-clar/object/tree/duplicateentries.c b/tests-clar/object/tree/duplicateentries.c new file mode 100644 index 000000000..330690d7a --- /dev/null +++ b/tests-clar/object/tree/duplicateentries.c @@ -0,0 +1,117 @@ +#include "clar_libgit2.h" +#include "tree.h" + +static git_repository *_repo; + +void test_object_tree_duplicateentries__initialize(void) { + _repo = cl_git_sandbox_init("testrepo"); +} + +void test_object_tree_duplicateentries__cleanup(void) { + cl_git_sandbox_cleanup(); +} + +/* + * $ git show --format=raw refs/heads/dir + * commit 144344043ba4d4a405da03de3844aa829ae8be0e + * tree d52a8fe84ceedf260afe4f0287bbfca04a117e83 + * parent cf80f8de9f1185bf3a05f993f6121880dd0cfbc9 + * author Ben Straub 1343755506 -0700 + * committer Ben Straub 1343755506 -0700 + * + * Change a file mode + * + * diff --git a/a/b.txt b/a/b.txt + * old mode 100644 + * new mode 100755 + * + * $ git ls-tree d52a8fe84ceedf260afe4f0287bbfca04a117e83 + * 100644 blob a8233120f6ad708f843d861ce2b7228ec4e3dec6 README + * 040000 tree 4e0883eeeeebc1fb1735161cea82f7cb5fab7e63 a + * 100644 blob 45b983be36b73c0788dc9cbcb76cbb80fc7bb057 branch_file.txt + * 100644 blob a71586c1dfe8a71c6cbf6c129f404c5642ff31bd new.txt + */ + +static void tree_checker( + git_oid *tid, + const char *expected_sha, + git_filemode_t expected_filemode) +{ + git_tree *tree; + const git_tree_entry *entry; + git_oid oid; + + cl_git_pass(git_tree_lookup(&tree, _repo, tid)); + cl_assert_equal_i(1, git_tree_entrycount(tree)); + entry = git_tree_entry_byindex(tree, 0); + + cl_git_pass(git_oid_fromstr(&oid, expected_sha)); + + cl_assert_equal_i(0, git_oid_cmp(&oid, git_tree_entry_id(entry))); + cl_assert_equal_i(expected_filemode, git_tree_entry_filemode(entry)); + + git_tree_free(tree); +} + +static void tree_creator(git_oid *out, void (*fn)(git_treebuilder *)) +{ + git_treebuilder *builder; + + cl_git_pass(git_treebuilder_create(&builder, NULL)); + + fn(builder); + + cl_git_pass(git_treebuilder_write(out, _repo, builder)); + git_treebuilder_free(builder); +} + +static void two_blobs(git_treebuilder *bld) +{ + git_oid oid; + const git_tree_entry *entry; + + cl_git_pass(git_oid_fromstr(&oid, + "a8233120f6ad708f843d861ce2b7228ec4e3dec6")); /* blob oid (README) */ + + cl_git_pass(git_treebuilder_insert( + &entry, bld, "duplicate", &oid, + GIT_FILEMODE_BLOB)); + + cl_git_pass(git_oid_fromstr(&oid, + "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd")); /* blob oid (new.txt) */ + + cl_git_pass(git_treebuilder_insert( + &entry, bld, "duplicate", &oid, + GIT_FILEMODE_BLOB)); +} + +static void one_blob_and_one_tree(git_treebuilder *bld) +{ + git_oid oid; + const git_tree_entry *entry; + + cl_git_pass(git_oid_fromstr(&oid, + "a8233120f6ad708f843d861ce2b7228ec4e3dec6")); /* blob oid (README) */ + + cl_git_pass(git_treebuilder_insert( + &entry, bld, "duplicate", &oid, + GIT_FILEMODE_BLOB)); + + cl_git_pass(git_oid_fromstr(&oid, + "4e0883eeeeebc1fb1735161cea82f7cb5fab7e63")); /* tree oid (a) */ + + cl_git_pass(git_treebuilder_insert( + &entry, bld, "duplicate", &oid, + GIT_FILEMODE_TREE)); +} + +void test_object_tree_duplicateentries__cannot_create_a_duplicate_entry_through_the_treebuilder(void) +{ + git_oid tid; + + tree_creator(&tid, two_blobs); + tree_checker(&tid, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd", GIT_FILEMODE_BLOB); + + tree_creator(&tid, one_blob_and_one_tree); + tree_checker(&tid, "4e0883eeeeebc1fb1735161cea82f7cb5fab7e63", GIT_FILEMODE_TREE); +} From 7cc1bf0fcb6de212dc50e02ff187880af9103bf4 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Thu, 8 Nov 2012 21:08:59 +0100 Subject: [PATCH 2/3] index: Introduce git_index_has_conflicts() --- include/git2/index.h | 7 +++++++ src/index.c | 15 +++++++++++++++ tests-clar/index/conflicts.c | 4 ++++ 3 files changed, 26 insertions(+) diff --git a/include/git2/index.h b/include/git2/index.h index bca9791c5..1efca72b5 100644 --- a/include/git2/index.h +++ b/include/git2/index.h @@ -431,6 +431,13 @@ GIT_EXTERN(int) git_index_conflict_remove(git_index *index, const char *path); */ GIT_EXTERN(void) git_index_conflict_cleanup(git_index *index); +/** + * Determine if the index contains entries representing file conflicts. + * + * @return 1 if at least one conflict is found, 0 otherwise. + */ +GIT_EXTERN(int) git_index_has_conflicts(git_index *index); + /**@}*/ /** @name Resolve Undo (REUC) index entry manipulation. diff --git a/src/index.c b/src/index.c index 214d29def..0a1cd03f0 100644 --- a/src/index.c +++ b/src/index.c @@ -959,6 +959,21 @@ void git_index_conflict_cleanup(git_index *index) git_vector_remove_matching(&index->entries, index_conflicts_match); } +int git_index_has_conflicts(git_index *index) +{ + unsigned int i; + git_index_entry *entry; + + assert(index); + + git_vector_foreach(&index->entries, i, entry) { + if (index_entry_stage(entry) > 0) + return 1; + } + + return 0; +} + unsigned int git_index_reuc_entrycount(git_index *index) { assert(index); diff --git a/tests-clar/index/conflicts.c b/tests-clar/index/conflicts.c index 59df257c5..e101b1659 100644 --- a/tests-clar/index/conflicts.c +++ b/tests-clar/index/conflicts.c @@ -180,8 +180,12 @@ void test_index_conflicts__remove_all_conflicts(void) cl_assert(git_index_entrycount(repo_index) == 8); + cl_assert_equal_i(true, git_index_has_conflicts(repo_index)); + git_index_conflict_cleanup(repo_index); + cl_assert_equal_i(false, git_index_has_conflicts(repo_index)); + cl_assert(git_index_entrycount(repo_index) == 2); for (i = 0; i < git_index_entrycount(repo_index); i++) { From f92bcaea499611bf0e6a71e845a2d74c790ea659 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Thu, 8 Nov 2012 17:39:23 +0100 Subject: [PATCH 3/3] index: prevent tree creation from a non merged state Fix libgit2/libgit2sharp#243 --- include/git2/index.h | 10 ++++-- src/tree.c | 6 ++++ tests-clar/object/tree/duplicateentries.c | 40 +++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/include/git2/index.h b/include/git2/index.h index 1efca72b5..8e1a7e521 100644 --- a/include/git2/index.h +++ b/include/git2/index.h @@ -215,9 +215,12 @@ GIT_EXTERN(int) git_index_read_tree(git_index *index, git_tree *tree); * The index instance cannot be bare, and needs to be associated * to an existing repository. * + * The index must not contain any file in conflict. + * * @param oid Pointer where to store the OID of the written tree * @param index Index to write - * @return 0 or an error code + * @return 0 on success, GIT_EUNMERGED when the index is not clean + * or an error code */ GIT_EXTERN(int) git_index_write_tree(git_oid *oid, git_index *index); @@ -228,10 +231,13 @@ GIT_EXTERN(int) git_index_write_tree(git_oid *oid, git_index *index); * letting the user choose the repository where the tree will * be written. * + * The index must not contain any file in conflict. + * * @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 + * @return 0 on success, GIT_EUNMERGED when the index is not clean + * or an error code */ GIT_EXTERN(int) git_index_write_tree_to(git_oid *oid, git_index *index, git_repository *repo); diff --git a/src/tree.c b/src/tree.c index 46b4a6dd1..7b47af347 100644 --- a/src/tree.c +++ b/src/tree.c @@ -497,6 +497,12 @@ int git_tree__write_index(git_oid *oid, git_index *index, git_repository *repo) assert(oid && index && repo); + if (git_index_has_conflicts(index)) { + giterr_set(GITERR_INDEX, + "Cannot create a tree from a not fully merged index."); + return GIT_EUNMERGED; + } + if (index->tree != NULL && index->tree->entries >= 0) { git_oid_cpy(oid, &index->tree->oid); return 0; diff --git a/tests-clar/object/tree/duplicateentries.c b/tests-clar/object/tree/duplicateentries.c index 330690d7a..27e1e2b59 100644 --- a/tests-clar/object/tree/duplicateentries.c +++ b/tests-clar/object/tree/duplicateentries.c @@ -115,3 +115,43 @@ void test_object_tree_duplicateentries__cannot_create_a_duplicate_entry_through_ tree_creator(&tid, one_blob_and_one_tree); tree_checker(&tid, "4e0883eeeeebc1fb1735161cea82f7cb5fab7e63", GIT_FILEMODE_TREE); } + +void add_fake_conflicts(git_index *index) +{ + git_index_entry ancestor_entry, our_entry, their_entry; + + memset(&ancestor_entry, 0x0, sizeof(git_index_entry)); + memset(&our_entry, 0x0, sizeof(git_index_entry)); + memset(&their_entry, 0x0, sizeof(git_index_entry)); + + ancestor_entry.path = "duplicate"; + ancestor_entry.mode = GIT_FILEMODE_BLOB; + ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT); + git_oid_fromstr(&ancestor_entry.oid, "a8233120f6ad708f843d861ce2b7228ec4e3dec6"); + + our_entry.path = "duplicate"; + our_entry.mode = GIT_FILEMODE_BLOB; + ancestor_entry.flags |= (2 << GIT_IDXENTRY_STAGESHIFT); + git_oid_fromstr(&our_entry.oid, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057"); + + their_entry.path = "duplicate"; + their_entry.mode = GIT_FILEMODE_BLOB; + ancestor_entry.flags |= (3 << GIT_IDXENTRY_STAGESHIFT); + git_oid_fromstr(&their_entry.oid, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"); + + cl_git_pass(git_index_conflict_add(index, &ancestor_entry, &our_entry, &their_entry)); +} + +void test_object_tree_duplicateentries__cannot_create_a_duplicate_entry_building_a_tree_from_a_index_with_conflicts(void) +{ + git_index *index; + git_oid tid; + + cl_git_pass(git_repository_index(&index, _repo)); + + add_fake_conflicts(index); + + cl_assert_equal_i(GIT_EUNMERGED, git_index_write_tree(&tid, index)); + + git_index_free(index); +}