summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeah (ctucx) <leah@ctu.cx>2021-03-18 00:26:38 +0100
committerLeah (ctucx) <leah@ctu.cx>2021-03-18 00:26:38 +0100
commitc4dae0726f643688e1c1b05f5b58a1706ed930a4 (patch)
treef87930df88cf555f118b4a26bacd1a6204895a76
parentdb4fea498da41c9ffb799eb960acabe701717188 (diff)
downloadnimgit-c4dae0726f643688e1c1b05f5b58a1706ed930a4.tar.gz
nimgit-c4dae0726f643688e1c1b05f5b58a1706ed930a4.tar.bz2
nimgit-c4dae0726f643688e1c1b05f5b58a1706ed930a4.zip
tree.nim: add new procs lookupTree, type, owner, id, shortId, entry; add new iterator items
-rw-r--r--nimgit/tree.nim44
1 files changed, 44 insertions, 0 deletions
diff --git a/nimgit/tree.nim b/nimgit/tree.nim
new file mode 100644
index 0000000..7de0b0f
--- /dev/null
+++ b/nimgit/tree.nim
@@ -0,0 +1,44 @@
+import nimgit2
+import types, free, utils, objects
+
+proc lookupTree* (repo: GitRepository, id: GitObjectId): GitTree =
+ let error = git_tree_lookup(addr result, repo, id)
+
+ if error != 0:
+ free(result)
+ raise newException(CatchableError, "Tree lookup failed: " & $error.getResultCode)
+
+proc type* (obj: GitTree): GitObjectKind = cast[GitObject](obj).type
+
+proc owner* (tree: GitTree): GitRepository = git_tree_owner(tree)
+
+proc id* (tree: GitTree): GitObjectId = git_tree_id(tree)
+
+proc shortId* (tree: GitTree): string = cast[GitObject](tree).shortId()
+
+proc len* (tree: GitTree): int = cast[int](git_tree_entrycount(tree))
+
+proc entry* (tree: GitTree, id: int): GitTreeEntry = git_tree_entry_byindex(tree, cuint(id))
+
+proc entry* (tree: GitTree, id: GitObjectId): GitTreeEntry = git_tree_entry_byid(tree, id)
+
+proc entry* (tree: GitTree, name: string): GitTreeEntry = git_tree_entry_byname(tree, cstring(name))
+
+proc entry* (tree: GitTree, path: string): GitTreeEntry =
+ let error = git_tree_entry_bypath(addr result, tree, cstring(path)).getResultCode
+
+ if error != grcOk:
+ free(result)
+ if error != grcNotFound:
+ raise newException(CatchableError, "TreeEntry lookup failed: " & $error)
+ else:
+ raise newException(CatchableError, "TreeEntry not found")
+
+
+iterator items* (tree: GitTree): GitTreeEntry =
+ var counter: int
+ let treeLen = tree.len
+
+ while counter < treeLen:
+ yield tree.entry(counter)
+ inc(counter) \ No newline at end of file