add got_object_open_as_tree() and got_object_open_by_path()
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
diff --git a/include/got_object.h b/include/got_object.h
index ec5c5b1..6687d7a 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -182,6 +182,13 @@ const struct got_error *got_object_blob_read_block(size_t *,
const struct got_error *
got_object_open_as_commit(struct got_commit_object **,
struct got_repository *, struct got_object_id *);
+const struct got_error *
+got_object_open_as_tree(struct got_tree_object **,
+ struct got_repository *, struct got_object_id *);
+
+const struct got_error *
+got_object_open_by_path(struct got_object **, struct got_repository *,
+ struct got_object_id *, const char *);
const struct got_error *got_object_commit_add_parent(struct got_commit_object *,
const char *);
diff --git a/lib/object.c b/lib/object.c
index 1765ec7..7e037bb 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -1125,6 +1125,27 @@ got_object_tree_open(struct got_tree_object **tree,
return err;
}
+const struct got_error *
+got_object_open_as_tree(struct got_tree_object **tree,
+ struct got_repository *repo, struct got_object_id *id)
+{
+ const struct got_error *err;
+ struct got_object *obj;
+
+ err = got_object_open(&obj, repo, id);
+ if (err)
+ return err;
+ if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
+ err = got_error(GOT_ERR_OBJ_TYPE);
+ goto done;
+ }
+
+ err = got_object_tree_open(tree, repo, obj);
+done:
+ got_object_close(obj);
+ return err;
+}
+
void
got_object_tree_close(struct got_tree_object *tree)
{
@@ -1336,3 +1357,102 @@ got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
*outlenp = n;
return NULL;
}
+
+static struct got_tree_entry *
+find_entry_by_name(struct got_tree_object *tree, const char *name)
+{
+ struct got_tree_entry *te;
+
+ SIMPLEQ_FOREACH(te, &tree->entries, entry) {
+ if (strcmp(te->name, name) == 0)
+ return te;
+ }
+ return NULL;
+}
+
+const struct got_error *
+got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
+ struct got_object_id *commit_id, const char *path)
+{
+ const struct got_error *err = NULL;
+ struct got_commit_object *commit = NULL;
+ struct got_tree_object *tree = NULL;
+ struct got_tree_entry *entry = NULL;
+ char *seg, *s = NULL;
+
+ *obj = NULL;
+
+ /* We are expecting an absolute in-repository path. */
+ if (path[0] != '/')
+ return got_error(GOT_ERR_NOT_ABSPATH);
+
+ err = got_object_open_as_commit(&commit, repo, commit_id);
+ if (err)
+ goto done;
+
+ /* Handle opening of root of commit's tree. */
+ if (path[1] == '\0') {
+ err = got_object_open(obj, repo, commit->tree_id);
+ if (err)
+ goto done;
+ return NULL;
+ }
+
+ err = got_object_open_as_tree(&tree, repo, commit->tree_id);
+ if (err)
+ goto done;
+
+ s = strdup(path);
+ if (s == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ err = got_canonpath(path, s, strlen(s) + 1);
+ if (err)
+ goto done;
+
+ s++; /* skip leading '/' */
+ seg = s;
+ while (*s) {
+ struct got_tree_object *next_tree;
+
+ if (*s != '/') {
+ s++;
+ continue;
+ }
+
+ /* end of path segment */
+ *s = '\0';
+
+ entry = find_entry_by_name(tree, seg);
+ if (entry == NULL) {
+ err = got_error(GOT_ERR_NO_OBJ);
+ goto done;
+ }
+
+ seg = s + 1;
+ s++;
+
+ if (*s) {
+ err = got_object_open_as_tree(&next_tree, repo,
+ entry->id);
+ entry = NULL;
+ if (err)
+ goto done;
+ got_object_tree_close(tree);
+ tree = next_tree;
+ }
+ }
+
+ if (entry)
+ err = got_object_open(obj, repo, entry->id);
+ else
+ err = got_error(GOT_ERR_NO_OBJ);
+done:
+ free(s);
+ if (commit)
+ got_object_commit_close(commit);
+ if (tree)
+ got_object_tree_close(tree);
+ return err;
+}