summaryrefslogtreecommitdiff
path: root/src/tree.c
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2011-02-05 09:11:17 +0200
committerVicent Marti <tanoku@gmail.com>2011-02-05 09:11:17 +0200
commit4569bfa55a44be3a03cf925df44bf1af0f21ddaa (patch)
treea3eb7ca3f6b2ebc201c2bb453b4d24d99f0d2221 /src/tree.c
parent5d773a6ee8542a5eaf94c34efcd27124c78e0bc7 (diff)
downloadlibgit2-4569bfa55a44be3a03cf925df44bf1af0f21ddaa.tar.gz
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>
Diffstat (limited to 'src/tree.c')
-rw-r--r--src/tree.c27
1 files changed, 22 insertions, 5 deletions
diff --git a/src/tree.c b/src/tree.c
index ab3bec8cd..3b5cf05a1 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;