Rename `git_tree_frompath` to `git_tree_get_subtree` That makes more sense to me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
diff --git a/include/git2/tree.h b/include/git2/tree.h
index 68d8235..2ff167f 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 6efedcf..4586891 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 33a76e8..1effcb1 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;