Merge pull request #879 from nulltoken/deprecated-mode Handling of 100664 deprecated mode in tree entries
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
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