Skip to content

Commit 9432af3

Browse files
author
Vicent Marti
committed
Rename git_tree_frompath to git_tree_get_subtree
That makes more sense to me.
1 parent 010879d commit 9432af3

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

include/git2/tree.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,19 +269,19 @@ GIT_EXTERN(void) git_treebuilder_filter(git_treebuilder *bld, int (*filter)(cons
269269
GIT_EXTERN(int) git_treebuilder_write(git_oid *oid, git_repository *repo, git_treebuilder *bld);
270270

271271
/**
272-
* Retrieve the tree object containing a tree entry, given
273-
* a relative path to this tree entry
272+
* Retrieve a subtree contained in a tree, given its
273+
* relative path.
274274
*
275275
* The returned tree is owned by the repository and
276276
* should be closed with the `git_object_close` method.
277277
*
278-
* @param parent_out Pointer where to store the parent tree
278+
* @param subtree Pointer where to store the subtree
279279
* @param root A previously loaded tree which will be the root of the relative path
280-
* @param treeentry_path Path to the tree entry from which to extract the last tree object
281-
* @return GIT_SUCCESS on success; GIT_ENOTFOUND if the path does not lead to an
282-
* entry, GIT_EINVALIDPATH or an error code
280+
* @param subtree_path Path to the contained subtree
281+
* @return GIT_SUCCESS on success; GIT_ENOTFOUND if the path does not lead to a
282+
* subtree, GIT_EINVALIDPATH or an error code
283283
*/
284-
GIT_EXTERN(int) git_tree_frompath(git_tree **parent_out, git_tree *root, const char *treeentry_path);
284+
GIT_EXTERN(int) git_tree_get_subtree(git_tree **subtree, git_tree *root, const char *subtree_path);
285285

286286
/** Callback for the tree traversal method */
287287
typedef int (*git_treewalk_cb)(const char *root, git_tree_entry *entry);

src/tree.c

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -610,23 +610,33 @@ void git_treebuilder_free(git_treebuilder *bld)
610610
git__free(bld);
611611
}
612612

613-
static int tree_frompath(git_tree **parent_out, git_tree *root, const char *treeentry_path, int offset)
613+
static int tree_frompath(
614+
git_tree **parent_out,
615+
git_tree *root,
616+
const char *treeentry_path,
617+
int offset)
614618
{
615619
char *slash_pos = NULL;
616620
const git_tree_entry* entry;
617621
int error = GIT_SUCCESS;
618622
git_tree *subtree;
619623

620624
if (!*(treeentry_path + offset))
621-
return git__rethrow(GIT_EINVALIDPATH, "Invalid relative path to a tree entry '%s'.", treeentry_path);
625+
return git__rethrow(GIT_EINVALIDPATH,
626+
"Invalid relative path to a tree entry '%s'.", treeentry_path);
622627

623628
slash_pos = (char *)strchr(treeentry_path + offset, '/');
624629

625630
if (slash_pos == NULL)
626-
return git_tree_lookup(parent_out, root->object.repo, git_object_id((const git_object *)root));
631+
return git_tree_lookup(
632+
parent_out,
633+
root->object.repo,
634+
git_object_id((const git_object *)root)
635+
);
627636

628637
if (slash_pos == treeentry_path + offset)
629-
return git__rethrow(GIT_EINVALIDPATH, "Invalid relative path to a tree entry '%s'.", treeentry_path);
638+
return git__rethrow(GIT_EINVALIDPATH,
639+
"Invalid relative path to a tree entry '%s'.", treeentry_path);
630640

631641
*slash_pos = '\0';
632642

@@ -636,28 +646,44 @@ static int tree_frompath(git_tree **parent_out, git_tree *root, const char *tree
636646
*slash_pos = '/';
637647

638648
if (entry == NULL)
639-
return git__rethrow(GIT_ENOTFOUND, "No tree entry can be found from the given tree and relative path '%s'.", treeentry_path);
649+
return git__rethrow(GIT_ENOTFOUND,
650+
"No tree entry can be found from "
651+
"the given tree and relative path '%s'.", treeentry_path);
640652

641-
if ((error = git_tree_lookup(&subtree, root->object.repo, &entry->oid)) < GIT_SUCCESS)
653+
654+
error = git_tree_lookup(&subtree, root->object.repo, &entry->oid);
655+
if (error < GIT_SUCCESS)
642656
return error;
643657

644-
error = tree_frompath(parent_out, subtree, treeentry_path, slash_pos - treeentry_path + 1);
658+
error = tree_frompath(
659+
parent_out,
660+
subtree,
661+
treeentry_path,
662+
slash_pos - treeentry_path + 1
663+
);
645664

646665
git_tree_close(subtree);
647666
return error;
648667
}
649668

650-
int git_tree_frompath(git_tree **parent_out, git_tree *root, const char *treeentry_path)
669+
int git_tree_get_subtree(
670+
git_tree **subtree,
671+
git_tree *root,
672+
const char *subtree_path)
651673
{
652674
char buffer[GIT_PATH_MAX];
653675

654-
assert(root && treeentry_path);
676+
assert(subtree && root && subtree_path);
655677

656-
strcpy(buffer, treeentry_path);
657-
return tree_frompath(parent_out, root, buffer, 0);
678+
strncpy(buffer, subtree_path, GIT_PATH_MAX);
679+
return tree_frompath(subtree, root, buffer, 0);
658680
}
659681

660-
static int tree_walk_post(git_tree *tree, git_treewalk_cb callback, char *root, size_t root_len)
682+
static int tree_walk_post(
683+
git_tree *tree,
684+
git_treewalk_cb callback,
685+
char *root,
686+
size_t root_len)
661687
{
662688
int error;
663689
unsigned int i;
@@ -673,13 +699,15 @@ static int tree_walk_post(git_tree *tree, git_treewalk_cb callback, char *root,
673699
if (ENTRY_IS_TREE(entry)) {
674700
git_tree *subtree;
675701

676-
if ((error = git_tree_lookup(&subtree, tree->object.repo, &entry->oid)) < 0)
702+
if ((error = git_tree_lookup(
703+
&subtree, tree->object.repo, &entry->oid)) < 0)
677704
return error;
678705

679706
strcpy(root + root_len, entry->filename);
680707
root[root_len + entry->filename_len] = '/';
681708

682-
tree_walk_post(subtree, callback, root, root_len + entry->filename_len + 1);
709+
tree_walk_post(subtree,
710+
callback, root, root_len + entry->filename_len + 1);
683711

684712
git_tree_close(subtree);
685713
}

tests-clay/object/tree/frompath.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static void assert_tree_from_path(git_tree *root, const char *path, git_error ex
2929
{
3030
git_tree *containing_tree = NULL;
3131

32-
cl_assert(git_tree_frompath(&containing_tree, root, path) == expected_result);
32+
cl_assert(git_tree_get_subtree(&containing_tree, root, path) == expected_result);
3333

3434
if (containing_tree == NULL && expected_result != GIT_SUCCESS)
3535
return;

0 commit comments

Comments
 (0)