ctucx.git: nimgit

[nimlang] nim-wrapper for libgit2

commit 74b4e93e6e52dfd54c58f09b4b39971d826ccd3b
parent e3cce2406706cbbcc2fa2defe16a50d220b3f101
Author: Leah (ctucx) <leah@ctu.cx>
Date: Wed, 17 Mar 2021 02:22:07 +0100

split objects.nim
2 files changed, 39 insertions(+), 23 deletions(-)
M
nimgit/objects.nim
|
34
+++++++++++-----------------------
A
nimgit/oid.nim
|
28
++++++++++++++++++++++++++++
diff --git a/nimgit/objects.nim b/nimgit/objects.nim
@@ -1,24 +1,5 @@
 import nimgit2
-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))
-
-    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.toString()
-
-
-proc `==`* (a: GitObjectId, b: GitObjectId): bool = cast[bool](git_oid_equal(a, b))
-
+import types, free, oid
 
 proc lookupObjectIdByName* (repo: GitRepository, name: string): GitObjectId =
     result = initGitObjectId()

@@ -30,8 +11,14 @@ proc lookupObjectIdByName* (repo: GitRepository, name: string): GitObjectId =
 
 
 proc lookupObject* (repo: GitRepository, name: string): GitObject = 
-    let error = git_revparse_single(addr result, repo, cstring(name))
+    let error = git_revparse_single(addr result, repo, cstring(name)).getResultCode
 
-    if error != 0:
+    if error != grcOk:
         free(result)
-        raise newException(CatchableError, "Object lookup failed: " & $error.getResultCode)
+        case error:
+            of grcNotFound: raise newException(CatchableError, "Object not found.")
+            of grcAmbiguous: raise newException(CatchableError, "Object lookup was ambiguous")
+            of grcInvalidSpec: raise newException(CatchableError, "Object lookup was ambiguous")
+            else: raise newException(CatchableError, "Object lookup failed: " & $error)
+
+proc kind* (obj: GitObject): GitObjectKind = GitObjectKind(git_object_type(obj) - GIT_OBJECT_ANY)+
\ No newline at end of file
diff --git a/nimgit/oid.nim b/nimgit/oid.nim
@@ -0,0 +1,28 @@
+import nimgit2
+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))
+
+    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 isZero (obj: GitObjectId): bool = cast[bool](git_oid_is_zero(obj))
+
+proc `$`* (obj: GitObjectId): string = obj.toString()
+
+proc `==`* (a: GitObjectId, b: GitObjectId): bool =
+    if a.isNil or b.isNil:
+        result = false
+    elif a.isZero or b.isZero:
+        result = false
+    else:   
+        result = cast[bool](git_oid_equal(a, b))
+