ctucx.git: nimgit

[nimlang] nim-wrapper for libgit2

commit efec5fe11242e75104f5b0e6e9de3fb920d52732
parent c423de3b9188c4a1fe5d09329275bcf5e39d2377
Author: Leah (ctucx) <leah@ctu.cx>
Date: Tue, 16 Mar 2021 23:59:21 +0100

objects.nim: new procs ==, fromString; rename getSha to toString
1 file changed, 16 insertions(+), 7 deletions(-)
M
nimgit/objects.nim
|
23
++++++++++++++++-------
diff --git a/nimgit/objects.nim b/nimgit/objects.nim
@@ -3,22 +3,31 @@ import types, free
 
 proc initGitObjectId* (): GitObjectId = cast[GitObjectId](sizeof(git_oid).alloc)
 
+proc fromString* (str: string): GitObjectId =
+    result = initGitObjectId()
+    let error = git_oid_fromstr(result, cstring(str))
 
-proc getSha* (obj: GitObjectId): string = $git_oid_tostr_s(obj)
+    if error != 0:
+        free(result)
+        raise newException(CatchableError, "Cannot parse string to GitObjectId: " & $error.getResultCode)
+
+
+proc toString* (obj: GitObjectId): string = $git_oid_tostr_s(obj)
 
-proc `$`* (obj: GitObjectId): string = obj.getSha()
+proc `$`* (obj: GitObjectId): string = obj.toString()
+
+
+proc `==`* (a: GitObjectId, b: GitObjectId): bool = cast[bool](git_oid_equal(a, b))
 
 
 proc lookupObjectIdByName* (repo: GitRepository, name: string): GitObjectId =
-    var objId = initGitObjectId()
-    let error = git_reference_name_to_id(objId, repo, cstring(name))
+    result = initGitObjectId()
+    let error = git_reference_name_to_id(result, repo, cstring(name))
 
     if error != 0:
-        free(objId)
+        free(result)
         raise newException(CatchableError, "Lookup failed: " & $error.getResultCode)
 
-    result = objId
-
 
 proc lookupObject* (repo: GitRepository, name: string): GitObject = 
     let error = git_revparse_single(addr result, repo, cstring(name))