ctucx.git: nimgit

[nimlang] nim-wrapper for libgit2

commit 4634d09b6989f2c481b3ab8f500f356483a540aa
parent e0ba5b236384f0c3463433effa5b779379ecb358
Author: Leah (ctucx) <leah@ctu.cx>
Date: Tue, 16 Mar 2021 15:27:39 +0100

reference.nim: new functions (isNote, getOwner, getRepo, getTarget, getSymbolicTarget, improve iterator walk
3 files changed, 45 insertions(+), 18 deletions(-)
M
listAllRefs.nim
|
14
+++++---------
M
nimgit/reference.nim
|
46
+++++++++++++++++++++++++++++++++++++---------
M
nimgit/types.nim
|
3
+++
diff --git a/listAllRefs.nim b/listAllRefs.nim
@@ -14,16 +14,12 @@ try:
     for gitRefName in gitRefNames:
         let gitReference = gitRepository.lookupGitReference(gitRefName)
 
-        case git_reference_type(gitReference):
-            of GIT_REFERENCE_DIRECT:
-                echo gitRefName & ": " & $git_oid_tostr_s(git_reference_target(gitReference))
+        case gitReference.getType():
+            of referenceDirect:
+                echo gitRefName & ": " & $gitReference.getTarget()
 
-            of GIT_REFERENCE_SYMBOLIC:
-                echo gitRefName & " => " & $git_reference_symbolic_target(gitReference)
-
-            else:
-                echo "Unexpected reference type"
-                quit(QuitFailure)
+            of referenceSymbolic:
+                echo gitRefName & " => " & gitReference.getSymbolicTarget() & ": " & $gitReference.getTarget()
 
         free(gitReference)
 
diff --git a/nimgit/reference.nim b/nimgit/reference.nim
@@ -6,12 +6,43 @@ proc getShorthand* (reference: GitReference): string =
 
 proc `$`* (reference: GitReference): string = reference.getShorthand()
 
+
 proc isLocalBranch* (reference: GitReference): bool = cast[bool](git_reference_is_branch(reference))
 
 proc isRemoteBranch* (reference: GitReference): bool = cast[bool](git_reference_is_remote(reference))
 
 proc isTag* (reference: GitReference): bool = cast[bool](git_reference_is_tag(reference))
 
+proc isNote* (reference: GitReference): bool = cast[bool](git_reference_is_note(reference))
+
+
+proc getOwner* (reference: GitReference): GitRepository = git_reference_owner(reference)
+
+proc getRepo* (reference: GitReference): GitRepository = reference.getOwner()
+
+proc getType* (reference: GitReference): GitReferenceType = cast[GitReferenceType](git_reference_type(reference))
+
+proc getTarget* (reference: GitReference): GitObjectId =
+    case reference.getType():
+        of referenceDirect:
+            result = git_reference_target(reference)
+        of referenceSymbolic:
+            var targetReference: GitReference
+            let error = git_reference_resolve(addr targetReference, reference)
+
+            if error != 0:
+                raise newException(CatchableError, "Cannot lookup symbolic reference: " & $error.getResultCode)
+
+            result = git_reference_target(targetReference)
+            free(targetReference)
+
+proc getSymbolicTarget* (reference: GitReference): string =
+    if reference.getType() != referenceSymbolic:
+        raise newException(ValueError, "Not a symbolic reference!")
+
+    result = $git_reference_symbolic_target(reference)
+
+
 proc getGitReferenceNames* (repo: GitRepository): seq[string] =
     var gitRefsArr: git_strarray
 

@@ -72,15 +103,12 @@ proc next* (walker: GitRevisionWalker): GitObjectId =
 
 
 iterator walk* (walker: GitRevisionWalker): GitObjectId =
-    block:
-        try:
-            while true:
-                yield walker.next()
-        except:
-            if getCurrentExceptionMsg() == "Cannot get next: end of iteration":
-                break
-            else:
-                raise newException(CatchableError, getCurrentExceptionMsg())
+    try:
+        while true:
+            yield walker.next()
+    except:
+        if getCurrentExceptionMsg() != "Cannot get next: end of iteration":
+            raise newException(CatchableError, getCurrentExceptionMsg())
 
 
 iterator branches* (repo: GitRepository, branchType: GitBranchType = branchLocal): GitReference =
diff --git a/nimgit/types.nim b/nimgit/types.nim
@@ -20,6 +20,9 @@ type
         email*  : string
         `when`* : GitTime
 
+    GitReferenceType* = enum
+        referenceDirect   = (GIT_REFERENCE_DIRECT, "direct")
+        referenceSymbolic = (GIT_REFERENCE_SYMBOLIC, "symbolic")
 
     GitBranchType* = enum
         branchLocal  = (GIT_BRANCH_LOCAL, "local")