Commit 8cf2de07 by Vicent Marti

tree: Fix lookups by entry name

parent 5fa1bed0
...@@ -88,6 +88,9 @@ GIT_EXTERN(unsigned int) git_tree_entrycount(git_tree *tree); ...@@ -88,6 +88,9 @@ GIT_EXTERN(unsigned int) git_tree_entrycount(git_tree *tree);
/** /**
* Lookup a tree entry by its filename * Lookup a tree entry by its filename
* *
* Note that if the entry in the tree is a folder instead of
* a standard file, the given name must be ended with a slash.
*
* @param tree a previously loaded tree. * @param tree a previously loaded tree.
* @param filename the filename of the desired entry * @param filename the filename of the desired entry
* @return the tree entry; NULL if not found * @return the tree entry; NULL if not found
...@@ -203,6 +206,9 @@ GIT_EXTERN(void) git_treebuilder_free(git_treebuilder *bld); ...@@ -203,6 +206,9 @@ GIT_EXTERN(void) git_treebuilder_free(git_treebuilder *bld);
/** /**
* Get an entry from the builder from its filename * Get an entry from the builder from its filename
* *
* Note that if the entry in the tree is a folder instead of
* a standard file, the given name must be ended with a slash.
*
* The returned entry is owned by the builder and should * The returned entry is owned by the builder and should
* not be freed manually. * not be freed manually.
* *
......
...@@ -23,6 +23,7 @@ static int valid_attributes(const int attributes) ...@@ -23,6 +23,7 @@ static int valid_attributes(const int attributes)
struct tree_key_search { struct tree_key_search {
const char *filename; const char *filename;
size_t filename_len; size_t filename_len;
int is_folder;
}; };
static int entry_search_cmp(const void *key, const void *array_member) static int entry_search_cmp(const void *key, const void *array_member)
...@@ -32,7 +33,7 @@ static int entry_search_cmp(const void *key, const void *array_member) ...@@ -32,7 +33,7 @@ static int entry_search_cmp(const void *key, const void *array_member)
int result = int result =
git_futils_cmp_path( git_futils_cmp_path(
ksearch->filename, ksearch->filename_len, entry->attr & 040000, ksearch->filename, ksearch->filename_len, ksearch->is_folder,
entry->filename, entry->filename_len, entry->attr & 040000); entry->filename, entry->filename_len, entry->attr & 040000);
return result ? result : ((int)ksearch->filename_len - (int)entry->filename_len); return result ? result : ((int)ksearch->filename_len - (int)entry->filename_len);
...@@ -51,15 +52,19 @@ static int entry_sort_cmp(const void *a, const void *b) ...@@ -51,15 +52,19 @@ static int entry_sort_cmp(const void *a, const void *b)
static int build_ksearch(struct tree_key_search *ksearch, const char *path) static int build_ksearch(struct tree_key_search *ksearch, const char *path)
{ {
size_t len = strlen(path); size_t len = strlen(path);
int is_folder = 0;
if (len && path[len - 1] == '/') if (len && path[len - 1] == '/') {
is_folder = 1;
len--; len--;
}
if (len == 0 || memchr(path, '/', len) != NULL) if (len == 0 || memchr(path, '/', len) != NULL)
return GIT_ERROR; return GIT_ERROR;
ksearch->filename = path; ksearch->filename = path;
ksearch->filename_len = len; ksearch->filename_len = len;
ksearch->is_folder = is_folder;
return GIT_SUCCESS; return GIT_SUCCESS;
} }
...@@ -420,6 +425,10 @@ int git_treebuilder_insert(git_tree_entry **entry_out, git_treebuilder *bld, con ...@@ -420,6 +425,10 @@ int git_treebuilder_insert(git_tree_entry **entry_out, git_treebuilder *bld, con
if (build_ksearch(&ksearch, filename) < GIT_SUCCESS) if (build_ksearch(&ksearch, filename) < GIT_SUCCESS)
return git__throw(GIT_ERROR, "Failed to insert entry. Invalid filename '%s'", filename); return git__throw(GIT_ERROR, "Failed to insert entry. Invalid filename '%s'", filename);
if ((attributes & 040000) != 0) {
ksearch.is_folder = 1;
}
if ((pos = git_vector_bsearch2(&bld->entries, entry_search_cmp, &ksearch)) != GIT_ENOTFOUND) { if ((pos = git_vector_bsearch2(&bld->entries, entry_search_cmp, &ksearch)) != GIT_ENOTFOUND) {
entry = git_vector_get(&bld->entries, pos); entry = git_vector_get(&bld->entries, pos);
if (entry->removed) { if (entry->removed) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment