ctucx.git: nimgit

nim-wrapper for libgit2

commit c4dae0726f643688e1c1b05f5b58a1706ed930a4
parent db4fea498da41c9ffb799eb960acabe701717188
Author: Leah (ctucx) <leah@ctu.cx>
Date: Thu, 18 Mar 2021 00:26:38 +0100

tree.nim: add new procs lookupTree, type, owner, id, shortId, entry; add new iterator items
1 file changed, 45 insertions(+), 0 deletions(-)
A
nimgit/tree.nim
|
45
+++++++++++++++++++++++++++++++++++++++++++++
diff --git a/nimgit/tree.nim 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