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
* @param len the length of the short identifier
* @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);
}
......@@ -62,12 +66,33 @@ GIT_INLINE(int) git_tree_lookup_prefix(git_tree **tree, git_repository *repo, co
*
* @param tree the tree to close
*/
GIT_INLINE(void) git_tree_free(git_tree *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.
......@@ -143,7 +168,10 @@ GIT_EXTERN(git_otype) git_tree_entry_type(const git_tree_entry *entry);
* @param entry a tree entry
* @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
......@@ -231,7 +259,12 @@ GIT_EXTERN(const git_tree_entry *) git_treebuilder_get(git_treebuilder *bld, con
* @param attributes Folder attributes of the entry
* @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
......@@ -252,7 +285,10 @@ GIT_EXTERN(int) git_treebuilder_remove(git_treebuilder *bld, const char *filenam
* @param bld Tree builder
* @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
......@@ -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);
/**
* Retrieve a subtree contained in a tree, given its
* relative path.
* Retrieve a tree entry contained in a tree or in any
* of its subtrees, given its relative path.
*
* The returned tree is owned by the repository and
* should be closed with the `git_object_free` method.
* The returned tree entry is owned by the user and must
* 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 subtree_path Path to the contained subtree
* @return 0 on success; GIT_ENOTFOUND if the path does not lead to a subtree
* @param subtree_path Path to the contained entry
* @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 */
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 */
enum git_treewalk_mode {
......
......@@ -985,7 +985,7 @@ int git_index_entry_stage(const git_index_entry *entry)
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_entry *entry = NULL;
......
......@@ -97,7 +97,7 @@ static int tree_write(
{
int error;
git_treebuilder *tb = NULL;
git_tree_entry *entry;
const git_tree_entry *entry;
git_oid tree_oid;
if ((error = git_treebuilder_create(&tb, source_tree)) < 0)
......
......@@ -13,11 +13,11 @@
#include "vector.h"
struct git_tree_entry {
unsigned int attr;
char *filename;
uint16_t removed;
uint16_t attr;
git_oid oid;
size_t filename_len;
int removed;
char filename[1];
};
struct git_tree {
......
#include "clar_libgit2.h"
static git_repository *repo;
const char *tree_with_subtrees_oid = "ae90f12eea699729ed24555e40b9fd669da12a12";
static git_tree *tree;
void test_object_tree_frompath__initialize(void)
{
git_oid id;
const char *tree_with_subtrees_oid = "ae90f12eea699729ed24555e40b9fd669da12a12";
cl_fixture_sandbox("testrepo.git");
cl_git_pass(git_repository_open(&repo, "testrepo.git"));
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
cl_assert(repo != NULL);
cl_git_pass(git_oid_fromstr(&id, tree_with_subtrees_oid));
......@@ -24,58 +23,44 @@ void test_object_tree_frompath__cleanup(void)
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);
if (containing_tree == NULL && expected_result != 0)
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);
cl_git_pass(git_tree_entry_bypath(&entry, root, path));
cl_assert_equal_s(git_tree_entry_name(entry), expected_entry_name);
git_tree_entry_free(entry);
}
void test_object_tree_frompath__retrieve_tree_from_path_to_treeentry(void)
{
/* Will return self if given a one path segment... */
assert_tree_from_path(tree, "README", 0, tree_with_subtrees_oid);
/* ...even one that lead to a non existent tree entry. */
assert_tree_from_path(tree, "i-do-not-exist.txt", 0, tree_with_subtrees_oid);
/* Will return fgh tree oid given this following path... */
assert_tree_from_path(tree, "ab/de/fgh/1.txt", 0, "3259a6bd5b57fb9c1281bb7ed3167b50f224cb54");
/* ... and ab tree oid given this one. */
assert_tree_from_path(tree, "ab/de", 0, "f1425cef211cc08caa31e7b545ffb232acb098c3");
git_tree_entry *e;
/* Will succeed if given a valid path which leads to a tree entry which doesn't exist */
assert_tree_from_path(tree, "ab/de/fgh/i-do-not-exist.txt", 0, "3259a6bd5b57fb9c1281bb7ed3167b50f224cb54");
}
assert_tree_from_path(tree, "README", "README");
assert_tree_from_path(tree, "ab/de/fgh/1.txt", "1.txt");
assert_tree_from_path(tree, "ab/de/fgh", "fgh");
assert_tree_from_path(tree, "ab/de/fgh/", "fgh");
assert_tree_from_path(tree, "ab/de", "de");
assert_tree_from_path(tree, "ab/", "ab");
assert_tree_from_path(tree, "ab/de/", "de");
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);
cl_must_fail(git_tree_entry_bypath(&e, tree, "i-do-not-exist.txt"));
cl_must_fail(git_tree_entry_bypath(&e, tree, "README/"));
cl_must_fail(git_tree_entry_bypath(&e, tree, "ab/de/fgh/i-do-not-exist.txt"));
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_invalid_path(void)
{
assert_tree_from_path_klass(tree, "/", GITERR_INVALID, NULL);
assert_tree_from_path_klass(tree, "/ab", GITERR_INVALID, NULL);
assert_tree_from_path_klass(tree, "/ab/de", GITERR_INVALID, NULL);
assert_tree_from_path_klass(tree, "ab/", GITERR_INVALID, NULL);
assert_tree_from_path_klass(tree, "ab//de", GITERR_INVALID, NULL);
assert_tree_from_path_klass(tree, "ab/de/", GITERR_INVALID, NULL);
git_tree_entry *e;
cl_must_fail(git_tree_entry_bypath(&e, tree, "/"));
cl_must_fail(git_tree_entry_bypath(&e, tree, "/ab"));
cl_must_fail(git_tree_entry_bypath(&e, tree, "/ab/de"));
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