Keep the tree entries always internally sorted Don't allow access to any tree entries whilst the entries array is unsorted. We keep track on when the array is unsorted, and any methods that access the array while it is unsorted now sort the array before accessing it. Signed-off-by: Vicent Marti <tanoku@gmail.com>
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
diff --git a/src/tree.c b/src/tree.c
index ab3bec8..3b5cf05 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -87,6 +87,7 @@ void git_tree_clear_entries(git_tree *tree)
git_vector_clear(&tree->entries);
tree->object.modified = 1;
+ tree->sorted = 1;
}
@@ -108,6 +109,7 @@ git_tree *git_tree__new(void)
return NULL;
}
+ tree->sorted = 1;
return tree;
}
@@ -178,6 +180,9 @@ git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
assert(tree && filename);
+ if (!tree->sorted)
+ git_tree_sort_entries(tree);
+
idx = git_vector_search(&tree->entries, filename);
if (idx == GIT_ENOTFOUND)
return NULL;
@@ -188,6 +193,10 @@ git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
git_tree_entry *git_tree_entry_byindex(git_tree *tree, int idx)
{
assert(tree);
+
+ if (!tree->sorted)
+ git_tree_sort_entries(tree);
+
return git_vector_get(&tree->entries, (unsigned int)idx);
}
@@ -219,22 +228,23 @@ int git_tree_add_entry_unsorted(git_tree_entry **entry_out, git_tree *tree, cons
*entry_out = entry;
tree->object.modified = 1;
+ tree->sorted = 0;
return GIT_SUCCESS;
}
int git_tree_add_entry(git_tree_entry **entry_out, git_tree *tree, const git_oid *id, const char *filename, int attributes)
{
int result = git_tree_add_entry_unsorted(entry_out, tree, id, filename, attributes);
- if (result == GIT_SUCCESS) {
- git_vector_sort(&tree->entries);
- return GIT_SUCCESS;
- }
+ if (result == GIT_SUCCESS)
+ git_tree_sort_entries(tree);
+
return result;
}
int git_tree_sort_entries(git_tree *tree)
{
git_vector_sort(&tree->entries);
+ tree->sorted = 1;
return GIT_SUCCESS;
}
@@ -244,6 +254,9 @@ int git_tree_remove_entry_byindex(git_tree *tree, int idx)
assert(tree);
+ if (!tree->sorted)
+ git_tree_sort_entries(tree);
+
remove_ptr = git_vector_get(&tree->entries, (unsigned int)idx);
if (remove_ptr == NULL)
return GIT_ENOTFOUND;
@@ -262,6 +275,9 @@ int git_tree_remove_entry_byname(git_tree *tree, const char *filename)
assert(tree && filename);
+ if (!tree->sorted)
+ git_tree_sort_entries(tree);
+
idx = git_vector_search(&tree->entries, filename);
if (idx == GIT_ENOTFOUND)
return GIT_ENOTFOUND;
@@ -279,7 +295,8 @@ int git_tree__writeback(git_tree *tree, git_odb_source *src)
if (tree->entries.length == 0)
return GIT_EMISSINGOBJDATA;
- git_vector_sort(&tree->entries);
+ if (!tree->sorted)
+ git_tree_sort_entries(tree);
for (i = 0; i < tree->entries.length; ++i) {
git_tree_entry *entry;
diff --git a/src/tree.h b/src/tree.h
index 78500c4..796c5b9 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -16,6 +16,7 @@ struct git_tree_entry {
struct git_tree {
git_object object;
git_vector entries;
+ unsigned sorted:1;
};
void git_tree__free(git_tree *tree);