Commit f92bcaea499611bf0e6a71e845a2d74c790ea659

nulltoken 2012-11-08T17:39:23

index: prevent tree creation from a non merged state Fix libgit2/libgit2sharp#243

diff --git a/include/git2/index.h b/include/git2/index.h
index 1efca72..8e1a7e5 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 46b4a6d..7b47af3 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 330690d..27e1e2b 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);
+}