summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/git2/tree.h14
-rw-r--r--src/tree.c56
-rw-r--r--tests-clay/object/tree/frompath.c2
3 files changed, 50 insertions, 22 deletions
diff --git a/include/git2/tree.h b/include/git2/tree.h
index 68d82351a..2ff167f44 100644
--- a/include/git2/tree.h
+++ b/include/git2/tree.h
@@ -269,19 +269,19 @@ 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 the tree object containing a tree entry, given
- * a relative path to this tree entry
+ * Retrieve a subtree contained in a tree, given its
+ * relative path.
*
* The returned tree is owned by the repository and
* should be closed with the `git_object_close` method.
*
- * @param parent_out Pointer where to store the parent tree
+ * @param subtree Pointer where to store the subtree
* @param root A previously loaded tree which will be the root of the relative path
- * @param treeentry_path Path to the tree entry from which to extract the last tree object
- * @return GIT_SUCCESS on success; GIT_ENOTFOUND if the path does not lead to an
- * entry, GIT_EINVALIDPATH or an error code
+ * @param subtree_path Path to the contained subtree
+ * @return GIT_SUCCESS on success; GIT_ENOTFOUND if the path does not lead to a
+ * subtree, GIT_EINVALIDPATH or an error code
*/
-GIT_EXTERN(int) git_tree_frompath(git_tree **parent_out, git_tree *root, const char *treeentry_path);
+GIT_EXTERN(int) git_tree_get_subtree(git_tree **subtree, git_tree *root, const char *subtree_path);
/** Callback for the tree traversal method */
typedef int (*git_treewalk_cb)(const char *root, git_tree_entry *entry);
diff --git a/src/tree.c b/src/tree.c
index 6efedcff5..458689196 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -610,7 +610,11 @@ void git_treebuilder_free(git_treebuilder *bld)
git__free(bld);
}
-static int tree_frompath(git_tree **parent_out, git_tree *root, const char *treeentry_path, int offset)
+static int tree_frompath(
+ git_tree **parent_out,
+ git_tree *root,
+ const char *treeentry_path,
+ int offset)
{
char *slash_pos = NULL;
const git_tree_entry* entry;
@@ -618,15 +622,21 @@ static int tree_frompath(git_tree **parent_out, git_tree *root, const char *tree
git_tree *subtree;
if (!*(treeentry_path + offset))
- return git__rethrow(GIT_EINVALIDPATH, "Invalid relative path to a tree entry '%s'.", treeentry_path);
+ return git__rethrow(GIT_EINVALIDPATH,
+ "Invalid relative path to a tree entry '%s'.", treeentry_path);
slash_pos = (char *)strchr(treeentry_path + offset, '/');
if (slash_pos == NULL)
- return git_tree_lookup(parent_out, root->object.repo, git_object_id((const git_object *)root));
+ return git_tree_lookup(
+ parent_out,
+ root->object.repo,
+ git_object_id((const git_object *)root)
+ );
if (slash_pos == treeentry_path + offset)
- return git__rethrow(GIT_EINVALIDPATH, "Invalid relative path to a tree entry '%s'.", treeentry_path);
+ return git__rethrow(GIT_EINVALIDPATH,
+ "Invalid relative path to a tree entry '%s'.", treeentry_path);
*slash_pos = '\0';
@@ -636,28 +646,44 @@ static int tree_frompath(git_tree **parent_out, git_tree *root, const char *tree
*slash_pos = '/';
if (entry == NULL)
- return git__rethrow(GIT_ENOTFOUND, "No tree entry can be found from the given tree and relative path '%s'.", treeentry_path);
+ return git__rethrow(GIT_ENOTFOUND,
+ "No tree entry can be found from "
+ "the given tree and relative path '%s'.", treeentry_path);
- if ((error = git_tree_lookup(&subtree, root->object.repo, &entry->oid)) < GIT_SUCCESS)
+
+ error = git_tree_lookup(&subtree, root->object.repo, &entry->oid);
+ if (error < GIT_SUCCESS)
return error;
- error = tree_frompath(parent_out, subtree, treeentry_path, slash_pos - treeentry_path + 1);
+ error = tree_frompath(
+ parent_out,
+ subtree,
+ treeentry_path,
+ slash_pos - treeentry_path + 1
+ );
git_tree_close(subtree);
return error;
}
-int git_tree_frompath(git_tree **parent_out, git_tree *root, const char *treeentry_path)
+int git_tree_get_subtree(
+ git_tree **subtree,
+ git_tree *root,
+ const char *subtree_path)
{
char buffer[GIT_PATH_MAX];
- assert(root && treeentry_path);
+ assert(subtree && root && subtree_path);
- strcpy(buffer, treeentry_path);
- return tree_frompath(parent_out, root, buffer, 0);
+ strncpy(buffer, subtree_path, GIT_PATH_MAX);
+ return tree_frompath(subtree, root, buffer, 0);
}
-static int tree_walk_post(git_tree *tree, git_treewalk_cb callback, char *root, size_t root_len)
+static int tree_walk_post(
+ git_tree *tree,
+ git_treewalk_cb callback,
+ char *root,
+ size_t root_len)
{
int error;
unsigned int i;
@@ -673,13 +699,15 @@ static int tree_walk_post(git_tree *tree, git_treewalk_cb callback, char *root,
if (ENTRY_IS_TREE(entry)) {
git_tree *subtree;
- if ((error = git_tree_lookup(&subtree, tree->object.repo, &entry->oid)) < 0)
+ if ((error = git_tree_lookup(
+ &subtree, tree->object.repo, &entry->oid)) < 0)
return error;
strcpy(root + root_len, entry->filename);
root[root_len + entry->filename_len] = '/';
- tree_walk_post(subtree, callback, root, root_len + entry->filename_len + 1);
+ tree_walk_post(subtree,
+ callback, root, root_len + entry->filename_len + 1);
git_tree_close(subtree);
}
diff --git a/tests-clay/object/tree/frompath.c b/tests-clay/object/tree/frompath.c
index 33a76e8aa..1effcb1db 100644
--- a/tests-clay/object/tree/frompath.c
+++ b/tests-clay/object/tree/frompath.c
@@ -29,7 +29,7 @@ static void assert_tree_from_path(git_tree *root, const char *path, git_error ex
{
git_tree *containing_tree = NULL;
- cl_assert(git_tree_frompath(&containing_tree, root, path) == expected_result);
+ cl_assert(git_tree_get_subtree(&containing_tree, root, path) == expected_result);
if (containing_tree == NULL && expected_result != GIT_SUCCESS)
return;