Commit 0e2fcca8 by Vicent Marti

tree: Bring back `entry_bypath`

Smaller, simpler, faster.
parent cbc02c10
...@@ -46,7 +46,11 @@ GIT_INLINE(int) git_tree_lookup(git_tree **tree, git_repository *repo, const git ...@@ -46,7 +46,11 @@ GIT_INLINE(int) git_tree_lookup(git_tree **tree, git_repository *repo, const git
* @param len the length of the short identifier * @param len the length of the short identifier
* @return 0 or an error code * @return 0 or an error code
*/ */
GIT_INLINE(int) git_tree_lookup_prefix(git_tree **tree, git_repository *repo, const git_oid *id, unsigned int len) GIT_INLINE(int) git_tree_lookup_prefix(
git_tree **tree,
git_repository *repo,
const git_oid *id,
unsigned int len)
{ {
return git_object_lookup_prefix((git_object **)tree, repo, id, len, GIT_OBJ_TREE); return git_object_lookup_prefix((git_object **)tree, repo, id, len, GIT_OBJ_TREE);
} }
...@@ -62,12 +66,33 @@ GIT_INLINE(int) git_tree_lookup_prefix(git_tree **tree, git_repository *repo, co ...@@ -62,12 +66,33 @@ GIT_INLINE(int) git_tree_lookup_prefix(git_tree **tree, git_repository *repo, co
* *
* @param tree the tree to close * @param tree the tree to close
*/ */
GIT_INLINE(void) git_tree_free(git_tree *tree) GIT_INLINE(void) git_tree_free(git_tree *tree)
{ {
git_object_free((git_object *) tree); git_object_free((git_object *) tree);
} }
/**
* Free a tree entry
*
* IMPORTANT: This function is only needed for tree
* entries owned by the user, such as the ones returned
* by `git_tree_entry_copy`.
*
* @param entry The entry to free
*/
GIT_EXTERN(void) git_tree_entry_free(git_tree_entry *entry);
/**
* Duplicate a tree entry
*
* Create a copy of a tree entry. The returned copy is owned
* by the user, and must be freed manually with
* `git_tree_entry_free`.
*
* @param entry A tree entry to duplicate
* @return a copy of the original entry
*/
GIT_EXTERN(git_tree_entry *) git_tree_entry_copy(const git_tree_entry *entry);
/** /**
* Get the id of a tree. * Get the id of a tree.
...@@ -143,7 +168,10 @@ GIT_EXTERN(git_otype) git_tree_entry_type(const git_tree_entry *entry); ...@@ -143,7 +168,10 @@ GIT_EXTERN(git_otype) git_tree_entry_type(const git_tree_entry *entry);
* @param entry a tree entry * @param entry a tree entry
* @return 0 or an error code * @return 0 or an error code
*/ */
GIT_EXTERN(int) git_tree_entry_to_object(git_object **object_out, git_repository *repo, const git_tree_entry *entry); GIT_EXTERN(int) git_tree_entry_to_object(
git_object **object_out,
git_repository *repo,
const git_tree_entry *entry);
/** /**
* Write a tree to the ODB from the index file * Write a tree to the ODB from the index file
...@@ -231,7 +259,12 @@ GIT_EXTERN(const git_tree_entry *) git_treebuilder_get(git_treebuilder *bld, con ...@@ -231,7 +259,12 @@ GIT_EXTERN(const git_tree_entry *) git_treebuilder_get(git_treebuilder *bld, con
* @param attributes Folder attributes of the entry * @param attributes Folder attributes of the entry
* @return 0 or an error code * @return 0 or an error code
*/ */
GIT_EXTERN(int) git_treebuilder_insert(git_tree_entry **entry_out, git_treebuilder *bld, const char *filename, const git_oid *id, unsigned int attributes); GIT_EXTERN(int) git_treebuilder_insert(
const git_tree_entry **entry_out,
git_treebuilder *bld,
const char *filename,
const git_oid *id,
unsigned int attributes);
/** /**
* Remove an entry from the builder by its filename * Remove an entry from the builder by its filename
...@@ -252,7 +285,10 @@ GIT_EXTERN(int) git_treebuilder_remove(git_treebuilder *bld, const char *filenam ...@@ -252,7 +285,10 @@ GIT_EXTERN(int) git_treebuilder_remove(git_treebuilder *bld, const char *filenam
* @param bld Tree builder * @param bld Tree builder
* @param filter Callback to filter entries * @param filter Callback to filter entries
*/ */
GIT_EXTERN(void) git_treebuilder_filter(git_treebuilder *bld, int (*filter)(const git_tree_entry *, void *), void *payload); GIT_EXTERN(void) git_treebuilder_filter(
git_treebuilder *bld,
int (*filter)(const git_tree_entry *, void *),
void *payload);
/** /**
* Write the contents of the tree builder as a tree object * Write the contents of the tree builder as a tree object
...@@ -269,21 +305,24 @@ GIT_EXTERN(void) git_treebuilder_filter(git_treebuilder *bld, int (*filter)(cons ...@@ -269,21 +305,24 @@ GIT_EXTERN(void) git_treebuilder_filter(git_treebuilder *bld, int (*filter)(cons
GIT_EXTERN(int) git_treebuilder_write(git_oid *oid, git_repository *repo, git_treebuilder *bld); GIT_EXTERN(int) git_treebuilder_write(git_oid *oid, git_repository *repo, git_treebuilder *bld);
/** /**
* Retrieve a subtree contained in a tree, given its * Retrieve a tree entry contained in a tree or in any
* relative path. * of its subtrees, given its relative path.
* *
* The returned tree is owned by the repository and * The returned tree entry is owned by the user and must
* should be closed with the `git_object_free` method. * be freed manually with `git_tree_entry_free`.
* *
* @param subtree Pointer where to store the subtree * @param entry Pointer where to store the tree entry
* @param root A previously loaded tree which will be the root of the relative path * @param root A previously loaded tree which will be the root of the relative path
* @param subtree_path Path to the contained subtree * @param subtree_path Path to the contained entry
* @return 0 on success; GIT_ENOTFOUND if the path does not lead to a subtree * @return 0 on success; GIT_ENOTFOUND if the path does not exist
*/ */
GIT_EXTERN(int) git_tree_get_subtree(git_tree **subtree, git_tree *root, const char *subtree_path); GIT_EXTERN(int) git_tree_entry_bypath(
git_tree_entry **entry,
git_tree *root,
const char *path);
/** Callback for the tree traversal method */ /** Callback for the tree traversal method */
typedef int (*git_treewalk_cb)(const char *root, git_tree_entry *entry, void *payload); typedef int (*git_treewalk_cb)(const char *root, const git_tree_entry *entry, void *payload);
/** Tree traversal modes */ /** Tree traversal modes */
enum git_treewalk_mode { enum git_treewalk_mode {
......
...@@ -985,7 +985,7 @@ int git_index_entry_stage(const git_index_entry *entry) ...@@ -985,7 +985,7 @@ int git_index_entry_stage(const git_index_entry *entry)
return (entry->flags & GIT_IDXENTRY_STAGEMASK) >> GIT_IDXENTRY_STAGESHIFT; return (entry->flags & GIT_IDXENTRY_STAGEMASK) >> GIT_IDXENTRY_STAGESHIFT;
} }
static int read_tree_cb(const char *root, git_tree_entry *tentry, void *data) static int read_tree_cb(const char *root, const git_tree_entry *tentry, void *data)
{ {
git_index *index = data; git_index *index = data;
git_index_entry *entry = NULL; git_index_entry *entry = NULL;
......
...@@ -97,7 +97,7 @@ static int tree_write( ...@@ -97,7 +97,7 @@ static int tree_write(
{ {
int error; int error;
git_treebuilder *tb = NULL; git_treebuilder *tb = NULL;
git_tree_entry *entry; const git_tree_entry *entry;
git_oid tree_oid; git_oid tree_oid;
if ((error = git_treebuilder_create(&tb, source_tree)) < 0) if ((error = git_treebuilder_create(&tb, source_tree)) < 0)
......
...@@ -35,6 +35,22 @@ static int entry_sort_cmp(const void *a, const void *b) ...@@ -35,6 +35,22 @@ static int entry_sort_cmp(const void *a, const void *b)
entry_b->filename, entry_b->filename_len, git_tree_entry__is_tree(entry_b)); entry_b->filename, entry_b->filename_len, git_tree_entry__is_tree(entry_b));
} }
static git_tree_entry *alloc_entry(const char *filename)
{
git_tree_entry *entry = NULL;
size_t filename_len = strlen(filename);
entry = git__malloc(sizeof(git_tree_entry) + filename_len + 1);
if (!entry)
return NULL;
memset(entry, 0x0, sizeof(git_tree_entry));
memcpy(entry->filename, filename, filename_len);
entry->filename[filename_len] = 0;
entry->filename_len = filename_len;
return entry;
}
struct tree_key_search { struct tree_key_search {
const char *filename; const char *filename;
...@@ -76,7 +92,7 @@ static int homing_search_cmp(const void *key, const void *array_member) ...@@ -76,7 +92,7 @@ static int homing_search_cmp(const void *key, const void *array_member)
* ambiguous because of folder vs file sorting, we look linearly * ambiguous because of folder vs file sorting, we look linearly
* around the area for our target file. * around the area for our target file.
*/ */
static int tree_key_search(git_vector *entries, const char *filename) static int tree_key_search(git_vector *entries, const char *filename, size_t filename_len)
{ {
struct tree_key_search ksearch; struct tree_key_search ksearch;
const git_tree_entry *entry; const git_tree_entry *entry;
...@@ -84,7 +100,7 @@ static int tree_key_search(git_vector *entries, const char *filename) ...@@ -84,7 +100,7 @@ static int tree_key_search(git_vector *entries, const char *filename)
int homing, i; int homing, i;
ksearch.filename = filename; ksearch.filename = filename;
ksearch.filename_len = strlen(filename); ksearch.filename_len = filename_len;
/* Initial homing search; find an entry on the tree with /* Initial homing search; find an entry on the tree with
* the same prefix as the filename we're looking for */ * the same prefix as the filename we're looking for */
...@@ -100,7 +116,8 @@ static int tree_key_search(git_vector *entries, const char *filename) ...@@ -100,7 +116,8 @@ static int tree_key_search(git_vector *entries, const char *filename)
if (homing_search_cmp(&ksearch, entry) < 0) if (homing_search_cmp(&ksearch, entry) < 0)
break; break;
if (strcmp(filename, entry->filename) == 0) if (entry->filename_len == filename_len &&
memcmp(filename, entry->filename, filename_len) == 0)
return i; return i;
} }
...@@ -112,7 +129,8 @@ static int tree_key_search(git_vector *entries, const char *filename) ...@@ -112,7 +129,8 @@ static int tree_key_search(git_vector *entries, const char *filename)
if (homing_search_cmp(&ksearch, entry) > 0) if (homing_search_cmp(&ksearch, entry) > 0)
break; break;
if (strcmp(filename, entry->filename) == 0) if (entry->filename_len == filename_len &&
memcmp(filename, entry->filename, filename_len) == 0)
return i; return i;
} }
...@@ -120,16 +138,35 @@ static int tree_key_search(git_vector *entries, const char *filename) ...@@ -120,16 +138,35 @@ static int tree_key_search(git_vector *entries, const char *filename)
return GIT_ENOTFOUND; return GIT_ENOTFOUND;
} }
void git_tree_entry_free(git_tree_entry *entry)
{
git__free(entry);
}
git_tree_entry *git_tree_entry_copy(const git_tree_entry *entry)
{
size_t total_size;
git_tree_entry *copy;
assert(entry);
total_size = sizeof(git_tree_entry) + entry->filename_len + 1;
copy = git__malloc(total_size);
if (!copy)
return NULL;
memcpy(copy, entry, total_size);
return copy;
}
void git_tree__free(git_tree *tree) void git_tree__free(git_tree *tree)
{ {
unsigned int i; unsigned int i;
for (i = 0; i < tree->entries.length; ++i) { for (i = 0; i < tree->entries.length; ++i) {
git_tree_entry *e; git_tree_entry *e = git_vector_get(&tree->entries, i);
e = git_vector_get(&tree->entries, i); git_tree_entry_free(e);
git__free(e->filename);
git__free(e);
} }
git_vector_free(&tree->entries); git_vector_free(&tree->entries);
...@@ -179,19 +216,21 @@ int git_tree_entry_to_object( ...@@ -179,19 +216,21 @@ int git_tree_entry_to_object(
return git_object_lookup(object_out, repo, &entry->oid, GIT_OBJ_ANY); return git_object_lookup(object_out, repo, &entry->oid, GIT_OBJ_ANY);
} }
const git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename) static git_tree_entry *entry_fromname(git_tree *tree, const char *name, size_t name_len)
{ {
int idx; int idx = tree_key_search(&tree->entries, name, name_len);
if (idx < 0)
assert(tree && filename);
idx = tree_key_search(&tree->entries, filename);
if (idx == GIT_ENOTFOUND)
return NULL; return NULL;
return git_vector_get(&tree->entries, idx); return git_vector_get(&tree->entries, idx);
} }
const git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
{
assert(tree && filename);
return entry_fromname(tree, filename, strlen(filename));
}
const git_tree_entry *git_tree_entry_byindex(git_tree *tree, unsigned int idx) const git_tree_entry *git_tree_entry_byindex(git_tree *tree, unsigned int idx)
{ {
assert(tree); assert(tree);
...@@ -244,28 +283,28 @@ static int tree_parse_buffer(git_tree *tree, const char *buffer, const char *buf ...@@ -244,28 +283,28 @@ static int tree_parse_buffer(git_tree *tree, const char *buffer, const char *buf
while (buffer < buffer_end) { while (buffer < buffer_end) {
git_tree_entry *entry; git_tree_entry *entry;
int tmp; int attr;
entry = git__calloc(1, sizeof(git_tree_entry)); if (git__strtol32(&attr, buffer, &buffer, 8) < 0 ||
GITERR_CHECK_ALLOC(entry); !buffer || !valid_attributes(attr))
if (git_vector_insert(&tree->entries, entry) < 0)
return -1;
if (git__strtol32(&tmp, buffer, &buffer, 8) < 0 ||
!buffer || !valid_attributes(tmp))
return tree_error("Failed to parse tree. Can't parse attributes"); return tree_error("Failed to parse tree. Can't parse attributes");
entry->attr = tmp;
if (*buffer++ != ' ') if (*buffer++ != ' ')
return tree_error("Failed to parse tree. Object is corrupted"); return tree_error("Failed to parse tree. Object is corrupted");
if (memchr(buffer, 0, buffer_end - buffer) == NULL) if (memchr(buffer, 0, buffer_end - buffer) == NULL)
return tree_error("Failed to parse tree. Object is corrupted"); return tree_error("Failed to parse tree. Object is corrupted");
entry->filename = git__strdup(buffer); /** Allocate the entry and store it in the entries vector */
entry->filename_len = strlen(buffer); {
entry = alloc_entry(buffer);
GITERR_CHECK_ALLOC(entry);
if (git_vector_insert(&tree->entries, entry) < 0)
return -1;
entry->attr = attr;
}
while (buffer < buffer_end && *buffer != 0) while (buffer < buffer_end && *buffer != 0)
buffer++; buffer++;
...@@ -303,16 +342,17 @@ static unsigned int find_next_dir(const char *dirname, git_index *index, unsigne ...@@ -303,16 +342,17 @@ static unsigned int find_next_dir(const char *dirname, git_index *index, unsigne
return i; return i;
} }
static int append_entry(git_treebuilder *bld, const char *filename, const git_oid *id, unsigned int attributes) static int append_entry(
git_treebuilder *bld,
const char *filename,
const git_oid *id,
unsigned int attributes)
{ {
git_tree_entry *entry; git_tree_entry *entry;
entry = git__calloc(1, sizeof(git_tree_entry)); entry = alloc_entry(filename);
GITERR_CHECK_ALLOC(entry); GITERR_CHECK_ALLOC(entry);
entry->filename = git__strdup(filename);
entry->filename_len = strlen(entry->filename);
git_oid_cpy(&entry->oid, id); git_oid_cpy(&entry->oid, id);
entry->attr = attributes; entry->attr = attributes;
...@@ -488,7 +528,12 @@ on_error: ...@@ -488,7 +528,12 @@ on_error:
return -1; return -1;
} }
int git_treebuilder_insert(git_tree_entry **entry_out, git_treebuilder *bld, const char *filename, const git_oid *id, unsigned int attributes) int git_treebuilder_insert(
const git_tree_entry **entry_out,
git_treebuilder *bld,
const char *filename,
const git_oid *id,
unsigned int attributes)
{ {
git_tree_entry *entry; git_tree_entry *entry;
int pos; int pos;
...@@ -501,30 +546,28 @@ int git_treebuilder_insert(git_tree_entry **entry_out, git_treebuilder *bld, con ...@@ -501,30 +546,28 @@ int git_treebuilder_insert(git_tree_entry **entry_out, git_treebuilder *bld, con
if (!valid_entry_name(filename)) if (!valid_entry_name(filename))
return tree_error("Failed to insert entry. Invalid name for a tree entry"); return tree_error("Failed to insert entry. Invalid name for a tree entry");
pos = tree_key_search(&bld->entries, filename); pos = tree_key_search(&bld->entries, filename, strlen(filename));
if (pos >= 0) { if (pos >= 0) {
entry = git_vector_get(&bld->entries, pos); entry = git_vector_get(&bld->entries, pos);
if (entry->removed) if (entry->removed)
entry->removed = 0; entry->removed = 0;
} else { } else {
entry = git__calloc(1, sizeof(git_tree_entry)); entry = alloc_entry(filename);
GITERR_CHECK_ALLOC(entry); GITERR_CHECK_ALLOC(entry);
entry->filename = git__strdup(filename);
entry->filename_len = strlen(entry->filename);
} }
git_oid_cpy(&entry->oid, id); git_oid_cpy(&entry->oid, id);
entry->attr = attributes; entry->attr = attributes;
if (pos == GIT_ENOTFOUND) { if (pos < 0) {
if (git_vector_insert(&bld->entries, entry) < 0) if (git_vector_insert(&bld->entries, entry) < 0)
return -1; return -1;
} }
if (entry_out != NULL) if (entry_out != NULL) {
*entry_out = entry; *entry_out = entry;
}
return 0; return 0;
} }
...@@ -536,7 +579,7 @@ static git_tree_entry *treebuilder_get(git_treebuilder *bld, const char *filenam ...@@ -536,7 +579,7 @@ static git_tree_entry *treebuilder_get(git_treebuilder *bld, const char *filenam
assert(bld && filename); assert(bld && filename);
idx = tree_key_search(&bld->entries, filename); idx = tree_key_search(&bld->entries, filename, strlen(filename));
if (idx < 0) if (idx < 0)
return NULL; return NULL;
...@@ -625,8 +668,7 @@ void git_treebuilder_clear(git_treebuilder *bld) ...@@ -625,8 +668,7 @@ void git_treebuilder_clear(git_treebuilder *bld)
for (i = 0; i < bld->entries.length; ++i) { for (i = 0; i < bld->entries.length; ++i) {
git_tree_entry *e = bld->entries.contents[i]; git_tree_entry *e = bld->entries.contents[i];
git__free(e->filename); git_tree_entry_free(e);
git__free(e);
} }
git_vector_clear(&bld->entries); git_vector_clear(&bld->entries);
...@@ -642,85 +684,78 @@ void git_treebuilder_free(git_treebuilder *bld) ...@@ -642,85 +684,78 @@ void git_treebuilder_free(git_treebuilder *bld)
git__free(bld); git__free(bld);
} }
static int tree_frompath( static size_t subpath_len(const char *path)
git_tree **parent_out, {
const char *slash_pos = strchr(path, '/');
if (slash_pos == NULL)
return strlen(path);
return slash_pos - path;
}
int git_tree_entry_bypath(
git_tree_entry **entry_out,
git_tree *root, git_tree *root,
git_buf *treeentry_path, const char *path)
size_t offset)
{ {
char *slash_pos = NULL;
const git_tree_entry* entry;
int error = 0; int error = 0;
git_tree *subtree; git_tree *subtree;
const git_tree_entry *entry;
size_t filename_len;
if (!*(treeentry_path->ptr + offset)) { /* Find how long is the current path component (i.e.
giterr_set(GITERR_INVALID, * the filename between two slashes */
"Invalid relative path to a tree entry '%s'.", treeentry_path->ptr); filename_len = subpath_len(path);
return -1;
}
slash_pos = (char *)strchr(treeentry_path->ptr + offset, '/');
if (slash_pos == NULL)
return git_tree_lookup(
parent_out,
root->object.repo,
git_object_id((const git_object *)root)
);
if (slash_pos == treeentry_path->ptr + offset) { if (filename_len == 0) {
giterr_set(GITERR_INVALID, giterr_set(GITERR_TREE, "Invalid tree path given");
"Invalid relative path to a tree entry '%s'.", treeentry_path->ptr); return GIT_ENOTFOUND;
return -1;
} }
*slash_pos = '\0'; entry = entry_fromname(root, path, filename_len);
entry = git_tree_entry_byname(root, treeentry_path->ptr + offset);
if (slash_pos != NULL)
*slash_pos = '/';
if (entry == NULL) { if (entry == NULL) {
giterr_set(GITERR_TREE, giterr_set(GITERR_TREE,
"No tree entry can be found from " "The path '%s' does not exist in the given tree", path);
"the given tree and relative path '%s'.", treeentry_path->ptr);
return GIT_ENOTFOUND; return GIT_ENOTFOUND;
} }
switch (path[filename_len]) {
case '/':
/* If there are more components in the path...
* then this entry *must* be a tree */
if (!git_tree_entry__is_tree(entry)) {
giterr_set(GITERR_TREE,
"The path '%s' does not exist in the given tree", path);
return -1;
}
/* If there's only a slash left in the path, we
* return the current entry; otherwise, we keep
* walking down the path */
if (path[filename_len + 1] != '\0')
break;
case '\0':
/* If there are no more components in the path, return
* this entry */
*entry_out = git_tree_entry_copy(entry);
return 0;
}
if (git_tree_lookup(&subtree, root->object.repo, &entry->oid) < 0) if (git_tree_lookup(&subtree, root->object.repo, &entry->oid) < 0)
return error; return -1;
error = tree_frompath( error = git_tree_entry_bypath(
parent_out, entry_out,
subtree, subtree,
treeentry_path, path + filename_len + 1
(slash_pos - treeentry_path->ptr) + 1
); );
git_tree_free(subtree); git_tree_free(subtree);
return error; return error;
} }
int git_tree_get_subtree(
git_tree **subtree,
git_tree *root,
const char *subtree_path)
{
int error;
git_buf buffer = GIT_BUF_INIT;
assert(subtree && root && subtree_path);
if ((error = git_buf_sets(&buffer, subtree_path)) == 0)
error = tree_frompath(subtree, root, &buffer, 0);
git_buf_free(&buffer);
return error;
}
static int tree_walk_post( static int tree_walk_post(
git_tree *tree, git_tree *tree,
git_treewalk_cb callback, git_treewalk_cb callback,
......
...@@ -13,11 +13,11 @@ ...@@ -13,11 +13,11 @@
#include "vector.h" #include "vector.h"
struct git_tree_entry { struct git_tree_entry {
unsigned int attr; uint16_t removed;
char *filename; uint16_t attr;
git_oid oid; git_oid oid;
size_t filename_len; size_t filename_len;
int removed; char filename[1];
}; };
struct git_tree { struct git_tree {
......
#include "clar_libgit2.h" #include "clar_libgit2.h"
static git_repository *repo; static git_repository *repo;
const char *tree_with_subtrees_oid = "ae90f12eea699729ed24555e40b9fd669da12a12";
static git_tree *tree; static git_tree *tree;
void test_object_tree_frompath__initialize(void) void test_object_tree_frompath__initialize(void)
{ {
git_oid id; git_oid id;
const char *tree_with_subtrees_oid = "ae90f12eea699729ed24555e40b9fd669da12a12";
cl_fixture_sandbox("testrepo.git"); cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
cl_git_pass(git_repository_open(&repo, "testrepo.git"));
cl_assert(repo != NULL); cl_assert(repo != NULL);
cl_git_pass(git_oid_fromstr(&id, tree_with_subtrees_oid)); cl_git_pass(git_oid_fromstr(&id, tree_with_subtrees_oid));
...@@ -24,58 +23,44 @@ void test_object_tree_frompath__cleanup(void) ...@@ -24,58 +23,44 @@ void test_object_tree_frompath__cleanup(void)
cl_fixture_cleanup("testrepo.git"); cl_fixture_cleanup("testrepo.git");
} }
static void assert_tree_from_path(git_tree *root, const char *path, int expected_result, const char *expected_raw_oid) static void assert_tree_from_path(
git_tree *root,
const char *path,
const char *expected_entry_name)
{ {
git_tree *containing_tree = NULL; git_tree_entry *entry;
cl_assert(git_tree_get_subtree(&containing_tree, root, path) == expected_result); cl_git_pass(git_tree_entry_bypath(&entry, root, path));
cl_assert_equal_s(git_tree_entry_name(entry), expected_entry_name);
if (containing_tree == NULL && expected_result != 0) git_tree_entry_free(entry);
return;
cl_assert(containing_tree != NULL && expected_result == 0);
cl_git_pass(git_oid_streq(git_object_id((const git_object *)containing_tree), expected_raw_oid));
git_tree_free(containing_tree);
}
static void assert_tree_from_path_klass(git_tree *root, const char *path, int expected_result, const char *expected_raw_oid)
{
assert_tree_from_path(root, path, GIT_ERROR, expected_raw_oid);
cl_assert(giterr_last()->klass == expected_result);
} }
void test_object_tree_frompath__retrieve_tree_from_path_to_treeentry(void) void test_object_tree_frompath__retrieve_tree_from_path_to_treeentry(void)
{ {
/* Will return self if given a one path segment... */ git_tree_entry *e;
assert_tree_from_path(tree, "README", 0, tree_with_subtrees_oid);
assert_tree_from_path(tree, "README", "README");
/* ...even one that lead to a non existent tree entry. */ assert_tree_from_path(tree, "ab/de/fgh/1.txt", "1.txt");
assert_tree_from_path(tree, "i-do-not-exist.txt", 0, tree_with_subtrees_oid); assert_tree_from_path(tree, "ab/de/fgh", "fgh");
assert_tree_from_path(tree, "ab/de/fgh/", "fgh");
/* Will return fgh tree oid given this following path... */ assert_tree_from_path(tree, "ab/de", "de");
assert_tree_from_path(tree, "ab/de/fgh/1.txt", 0, "3259a6bd5b57fb9c1281bb7ed3167b50f224cb54"); assert_tree_from_path(tree, "ab/", "ab");
assert_tree_from_path(tree, "ab/de/", "de");
/* ... and ab tree oid given this one. */
assert_tree_from_path(tree, "ab/de", 0, "f1425cef211cc08caa31e7b545ffb232acb098c3"); cl_must_fail(git_tree_entry_bypath(&e, tree, "i-do-not-exist.txt"));
cl_must_fail(git_tree_entry_bypath(&e, tree, "README/"));
/* Will succeed if given a valid path which leads to a tree entry which doesn't exist */ cl_must_fail(git_tree_entry_bypath(&e, tree, "ab/de/fgh/i-do-not-exist.txt"));
assert_tree_from_path(tree, "ab/de/fgh/i-do-not-exist.txt", 0, "3259a6bd5b57fb9c1281bb7ed3167b50f224cb54"); cl_must_fail(git_tree_entry_bypath(&e, tree, "nope/de/fgh/1.txt"));
} cl_must_fail(git_tree_entry_bypath(&e, tree, "ab/me-neither/fgh/2.txt"));
cl_must_fail(git_tree_entry_bypath(&e, tree, "ab/me-neither/fgh/2.txt/"));
void test_object_tree_frompath__fail_when_processing_an_unknown_tree_segment(void)
{
assert_tree_from_path(tree, "nope/de/fgh/1.txt", GIT_ENOTFOUND, NULL);
assert_tree_from_path(tree, "ab/me-neither/fgh/2.txt", GIT_ENOTFOUND, NULL);
} }
void test_object_tree_frompath__fail_when_processing_an_invalid_path(void) void test_object_tree_frompath__fail_when_processing_an_invalid_path(void)
{ {
assert_tree_from_path_klass(tree, "/", GITERR_INVALID, NULL); git_tree_entry *e;
assert_tree_from_path_klass(tree, "/ab", GITERR_INVALID, NULL);
assert_tree_from_path_klass(tree, "/ab/de", GITERR_INVALID, NULL); cl_must_fail(git_tree_entry_bypath(&e, tree, "/"));
assert_tree_from_path_klass(tree, "ab/", GITERR_INVALID, NULL); cl_must_fail(git_tree_entry_bypath(&e, tree, "/ab"));
assert_tree_from_path_klass(tree, "ab//de", GITERR_INVALID, NULL); cl_must_fail(git_tree_entry_bypath(&e, tree, "/ab/de"));
assert_tree_from_path_klass(tree, "ab/de/", GITERR_INVALID, NULL); cl_must_fail(git_tree_entry_bypath(&e, tree, "ab//de"));
} }
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