ctucx.git: nimgit

[nimlang] nim-wrapper for libgit2

commit b84069a736b112dc50f1a371d890e9c1003f1ac7
parent c563d6b781509730e3f1a34e9135d79043555b77
Author: Leah (ctucx) <leah@ctu.cx>
Date: Wed, 17 Mar 2021 18:27:33 +0100

commit.nim: new procs rawHeader, rawMessage, gpgSignature, time, parentIds, parents
1 file changed, 50 insertions(+), 0 deletions(-)
M
nimgit/commit.nim
|
50
++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/nimgit/commit.nim b/nimgit/commit.nim
@@ -1,3 +1,4 @@
+import times
 import nimgit2
 import types, free, utils, oid
 

@@ -12,6 +13,8 @@ proc owner* (commit: GitCommit): GitRepository = git_commit_owner(commit)
 
 proc repo* (commit: GitCommit): GitRepository = commit.owner()
 
+proc rawHeader* (commit: GitCommit): seq[byte] = cast[seq[byte]](git_commit_raw_header(commit))
+
 proc id* (commit: GitCommit): GitObjectId = git_commit_id(commit)
 
 proc `$`* (commit: GitCommit): string = $commit.id()

@@ -24,6 +27,36 @@ proc message* (commit: GitCommit): string = $git_commit_message(commit)
 
 proc messageEncoding* (commit: GitCommit): string = $git_commit_message_encoding(commit)
 
+proc rawMessage* (commit: GitCommit): seq[byte] = cast[seq[byte]](git_commit_message_raw(commit))
+
+proc gpgSignature* (commit: GitCommit): (string, string) =
+    var signature:  git_buf
+    var signedData: git_buf
+
+    let grc = git_commit_extract_signature(addr signature, addr signedData, commit.owner, commit.id, nil).getResultCode
+
+    if grc != grcOk:
+        free(addr signature);
+        free(addr signedData);
+
+        let error = getLastError()
+
+        if grc == grcNotFound:
+            return ("", "")
+        else:
+            raise newException(CatchableError, "Unkown Error while extracting signature: " & error.message)
+
+    result = ($signature.ptr, $signed_data.ptr)
+
+    free(addr signature);
+    free(addr signedData);
+
+
+proc time* (commit: GitCommit): GitTime =
+    result.time     = fromUnix(git_commit_time(commit))
+    result.offset   = git_commit_time_offset(commit)
+
+
 proc author* (commit: GitCommit): GitSignature =
     let author = git_commit_author(commit)
 

@@ -45,6 +78,14 @@ proc parentCount* (commit: GitCommit): int = cast[int](git_commit_parentcount(co
 
 proc parentId* (commit: GitCommit, id: int = 0): GitObjectId = git_commit_parent_id(commit, cuint(id))
 
+proc parentIds* (commit: GitCommit): seq[GitObjectId] =
+    var counter: int
+    let parentCount = commit.parentCount
+
+    while counter < parentCount:
+        result.add(commit.parentId(counter))
+        inc(counter)
+
 proc parent* (commit: GitCommit, id: int = 0): GitCommit =
     let error = git_commit_parent(addr result, commit, cuint(id))
 

@@ -52,6 +93,15 @@ proc parent* (commit: GitCommit, id: int = 0): GitCommit =
         free(result)
         raise newException(CatchableError, "parrent lookup failed: " & $error.getResultCode)
 
+proc parents* (commit: GitCommit): seq[GitCommit] =
+    var counter: int
+    let parentCount = commit.parentCount
+
+    while counter < parentCount:
+        result.add(commit.parent(counter))
+        inc(counter)
+
+
 proc treeId* (commit: GitCommit): GitObjectId = git_commit_tree_id(commit)
 
 proc tree* (commit: GitCommit): GitTree =