Commit 6cb64ce34fdd4b82d1a465875f0f779cb05750ab

Vicent Martí 2012-08-19T15:12:43

Merge pull request #879 from nulltoken/deprecated-mode Handling of 100664 deprecated mode in tree entries

diff --git a/include/git2/tree.h b/include/git2/tree.h
index 85407d7..29aedac 100644
--- a/include/git2/tree.h
+++ b/include/git2/tree.h
@@ -263,11 +263,17 @@ GIT_EXTERN(const git_tree_entry *) git_treebuilder_get(git_treebuilder *bld, con
  * The optional pointer `entry_out` can be used to retrieve a
  * pointer to the newly created/updated entry.
  *
+ * No attempt is being made to ensure that the provided oid points
+ * to an existing git object in the object database, nor that the
+ * attributes make sense regarding the type of the pointed at object.
+ *
  * @param entry_out Pointer to store the entry (optional)
  * @param bld Tree builder
  * @param filename Filename of the entry
  * @param id SHA1 oid of the entry
- * @param attributes Folder attributes of the entry
+ * @param attributes Folder attributes of the entry. This parameter must
+ *			be valued with one of the following entries: 0040000, 0100644,
+ *			0100755, 0120000 or 0160000.
  * @return 0 or an error code
  */
 GIT_EXTERN(int) git_treebuilder_insert(
diff --git a/src/tree.c b/src/tree.c
index 19250fe..0eee947 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -12,12 +12,16 @@
 #include "git2/object.h"
 
 #define DEFAULT_TREE_SIZE 16
-#define MAX_FILEMODE 0777777
 #define MAX_FILEMODE_BYTES 6
 
-static int valid_attributes(const int attributes)
+static bool valid_attributes(const int attributes)
 {
-	return attributes >= 0 && attributes <= MAX_FILEMODE;
+	return (attributes == 0040000	/* Directory */
+		|| attributes == 0100644 /* Non executable file */
+		|| attributes == 0100664 /* Non executable group writable file */
+		|| attributes == 0100755 /* Executable file */
+		|| attributes == 0120000 /* Symbolic link */
+		|| attributes == 0160000); /* Git link */
 }
 
 static int valid_entry_name(const char *filename)
@@ -513,6 +517,19 @@ static void sort_entries(git_treebuilder *bld)
 	git_vector_sort(&bld->entries);
 }
 
+GIT_INLINE(int) normalize_attributes(const int attributes)
+{
+	/* 100664 mode is an early design mistake. Tree entries may bear
+	 * this mode in some old git repositories, but it's now deprecated.
+	 * We silently normalize while inserting new entries in a tree 
+	 * being built.
+	 */
+	if (attributes == 0100664)
+		return 0100644;
+
+	return attributes;
+}
+
 int git_treebuilder_create(git_treebuilder **builder_p, const git_tree *source)
 {
 	git_treebuilder *bld;
@@ -533,7 +550,10 @@ int git_treebuilder_create(git_treebuilder **builder_p, const git_tree *source)
 		for (i = 0; i < source->entries.length; ++i) {
 			git_tree_entry *entry_src = source->entries.contents[i];
 
-			if (append_entry(bld, entry_src->filename, &entry_src->oid, entry_src->attr) < 0)
+			if (append_entry(
+				bld, entry_src->filename,
+				&entry_src->oid,
+				normalize_attributes(entry_src->attr)) < 0)
 				goto on_error;
 		}
 	}
@@ -561,6 +581,8 @@ int git_treebuilder_insert(
 	if (!valid_attributes(attributes))
 		return tree_error("Failed to insert entry. Invalid attributes");
 
+	attributes = normalize_attributes(attributes);
+
 	if (!valid_entry_name(filename))
 		return tree_error("Failed to insert entry. Invalid name for a tree entry");
 
diff --git a/tests-clar/object/tree/attributes.c b/tests-clar/object/tree/attributes.c
new file mode 100644
index 0000000..ed88e74
--- /dev/null
+++ b/tests-clar/object/tree/attributes.c
@@ -0,0 +1,118 @@
+#include "clar_libgit2.h"
+#include "tree.h"
+
+static const char *blob_oid = "3d0970ec547fc41ef8a5882dde99c6adce65b021";
+static const char *tree_oid  = "1b05fdaa881ee45b48cbaa5e9b037d667a47745e";
+
+#define GROUP_WRITABLE_FILE 0100664
+#define REGULAR_FILE 0100644
+
+void test_object_tree_attributes__ensure_correctness_of_attributes_on_insertion(void)
+{
+	git_treebuilder *builder;
+	git_oid oid;
+
+	cl_git_pass(git_oid_fromstr(&oid, blob_oid));
+
+	cl_git_pass(git_treebuilder_create(&builder, NULL));
+
+	cl_git_fail(git_treebuilder_insert(NULL, builder, "one.txt", &oid, 0777777));
+	cl_git_fail(git_treebuilder_insert(NULL, builder, "one.txt", &oid, 0100666));
+	cl_git_fail(git_treebuilder_insert(NULL, builder, "one.txt", &oid, 0000001));
+
+	git_treebuilder_free(builder);
+}
+
+void test_object_tree_attributes__group_writable_tree_entries_created_with_an_antique_git_version_can_still_be_accessed(void)
+{
+	git_repository *repo;
+	git_oid tid;
+	git_tree *tree;
+	const git_tree_entry *entry;
+
+	cl_git_pass(git_repository_open(&repo, cl_fixture("deprecated-mode.git")));
+
+	cl_git_pass(git_oid_fromstr(&tid, tree_oid));
+	cl_git_pass(git_tree_lookup(&tree, repo, &tid));
+
+	entry = git_tree_entry_byname(tree, "old_mode.txt");
+	cl_assert_equal_i(
+		GROUP_WRITABLE_FILE,
+		git_tree_entry_attributes(entry));
+
+	git_tree_free(tree);
+	git_repository_free(repo);
+}
+
+void test_object_tree_attributes__normalize_attributes_when_inserting_in_a_new_tree(void)
+{
+	git_repository *repo;
+	git_treebuilder *builder;
+	git_oid bid, tid;
+	git_tree *tree;
+	const git_tree_entry *entry;
+
+	repo = cl_git_sandbox_init("deprecated-mode.git");
+
+	cl_git_pass(git_oid_fromstr(&bid, blob_oid));
+
+	cl_git_pass(git_treebuilder_create(&builder, NULL));
+
+	cl_git_pass(git_treebuilder_insert(
+		&entry,
+		builder,
+		"normalized.txt",
+		&bid,
+		GROUP_WRITABLE_FILE));
+
+	cl_assert_equal_i(
+		REGULAR_FILE,
+		git_tree_entry_attributes(entry));
+	
+	cl_git_pass(git_treebuilder_write(&tid, repo, builder));
+	git_treebuilder_free(builder);
+
+	cl_git_pass(git_tree_lookup(&tree, repo, &tid));
+
+	entry = git_tree_entry_byname(tree, "normalized.txt");
+	cl_assert_equal_i(
+		REGULAR_FILE,
+		git_tree_entry_attributes(entry));
+
+	git_tree_free(tree);
+	cl_git_sandbox_cleanup();
+}
+
+void test_object_tree_attributes__normalize_attributes_when_creating_a_tree_from_an_existing_one(void)
+{
+	git_repository *repo;
+	git_treebuilder *builder;
+	git_oid tid, tid2;
+	git_tree *tree;
+	const git_tree_entry *entry;
+
+	repo = cl_git_sandbox_init("deprecated-mode.git");
+
+	cl_git_pass(git_oid_fromstr(&tid, tree_oid));
+	cl_git_pass(git_tree_lookup(&tree, repo, &tid));
+
+	cl_git_pass(git_treebuilder_create(&builder, tree));
+	
+	entry = git_treebuilder_get(builder, "old_mode.txt");
+	cl_assert_equal_i(
+		REGULAR_FILE,
+		git_tree_entry_attributes(entry));
+
+	cl_git_pass(git_treebuilder_write(&tid2, repo, builder));
+	git_treebuilder_free(builder);
+	git_tree_free(tree);
+
+	cl_git_pass(git_tree_lookup(&tree, repo, &tid2));
+	entry = git_tree_entry_byname(tree, "old_mode.txt");
+	cl_assert_equal_i(
+		REGULAR_FILE,
+		git_tree_entry_attributes(entry));
+
+	git_tree_free(tree);
+	cl_git_sandbox_cleanup();
+}
diff --git a/tests-clar/resources/deprecated-mode.git/HEAD b/tests-clar/resources/deprecated-mode.git/HEAD
new file mode 100644
index 0000000..cb089cd
--- /dev/null
+++ b/tests-clar/resources/deprecated-mode.git/HEAD
@@ -0,0 +1 @@
+ref: refs/heads/master
diff --git a/tests-clar/resources/deprecated-mode.git/config b/tests-clar/resources/deprecated-mode.git/config
new file mode 100644
index 0000000..f57351f
--- /dev/null
+++ b/tests-clar/resources/deprecated-mode.git/config
@@ -0,0 +1,6 @@
+[core]
+	bare = true
+	repositoryformatversion = 0
+	filemode = false
+	logallrefupdates = true
+	ignorecase = true
diff --git a/tests-clar/resources/deprecated-mode.git/description b/tests-clar/resources/deprecated-mode.git/description
new file mode 100644
index 0000000..498b267
--- /dev/null
+++ b/tests-clar/resources/deprecated-mode.git/description
@@ -0,0 +1 @@
+Unnamed repository; edit this file 'description' to name the repository.
diff --git a/tests-clar/resources/deprecated-mode.git/hooks/README.sample b/tests-clar/resources/deprecated-mode.git/hooks/README.sample
new file mode 100644
index 0000000..d125ec8
--- /dev/null
+++ b/tests-clar/resources/deprecated-mode.git/hooks/README.sample
@@ -0,0 +1,5 @@
+#!/bin/sh
+#
+# Place appropriately named executable hook scripts into this directory
+# to intercept various actions that git takes.  See `git help hooks` for
+# more information.
diff --git a/tests-clar/resources/deprecated-mode.git/index b/tests-clar/resources/deprecated-mode.git/index
new file mode 100644
index 0000000..6827406
Binary files /dev/null and b/tests-clar/resources/deprecated-mode.git/index differ
diff --git a/tests-clar/resources/deprecated-mode.git/info/exclude b/tests-clar/resources/deprecated-mode.git/info/exclude
new file mode 100644
index 0000000..6d05881
--- /dev/null
+++ b/tests-clar/resources/deprecated-mode.git/info/exclude
@@ -0,0 +1,2 @@
+# File patterns to ignore; see `git help ignore` for more information.
+# Lines that start with '#' are comments.
diff --git a/tests-clar/resources/deprecated-mode.git/objects/06/262edc257418e9987caf999f9a7a3e1547adff b/tests-clar/resources/deprecated-mode.git/objects/06/262edc257418e9987caf999f9a7a3e1547adff
new file mode 100644
index 0000000..a030cf7
Binary files /dev/null and b/tests-clar/resources/deprecated-mode.git/objects/06/262edc257418e9987caf999f9a7a3e1547adff differ
diff --git a/tests-clar/resources/deprecated-mode.git/objects/1b/05fdaa881ee45b48cbaa5e9b037d667a47745e b/tests-clar/resources/deprecated-mode.git/objects/1b/05fdaa881ee45b48cbaa5e9b037d667a47745e
new file mode 100644
index 0000000..ae7765a
Binary files /dev/null and b/tests-clar/resources/deprecated-mode.git/objects/1b/05fdaa881ee45b48cbaa5e9b037d667a47745e differ
diff --git a/tests-clar/resources/deprecated-mode.git/objects/3d/0970ec547fc41ef8a5882dde99c6adce65b021 b/tests-clar/resources/deprecated-mode.git/objects/3d/0970ec547fc41ef8a5882dde99c6adce65b021
new file mode 100644
index 0000000..595283e
Binary files /dev/null and b/tests-clar/resources/deprecated-mode.git/objects/3d/0970ec547fc41ef8a5882dde99c6adce65b021 differ
diff --git a/tests-clar/resources/deprecated-mode.git/refs/heads/master b/tests-clar/resources/deprecated-mode.git/refs/heads/master
new file mode 100644
index 0000000..1e106df
--- /dev/null
+++ b/tests-clar/resources/deprecated-mode.git/refs/heads/master
@@ -0,0 +1 @@
+06262edc257418e9987caf999f9a7a3e1547adff