ctucx.git: nimgit

[nimlang] nim-wrapper for libgit2

commit 6df94530647028cd67cd4fd80f84f56a4dc6a7e6
parent efec5fe11242e75104f5b0e6e9de3fb920d52732
Author: Leah (ctucx) <leah@ctu.cx>
Date: Wed, 17 Mar 2021 00:00:56 +0100

reference.nim: split revWalker and branch-stuff, refactor revWalker
3 files changed, 140 insertions(+), 95 deletions(-)
A
nimgit/branch.nim
|
50
++++++++++++++++++++++++++++++++++++++++++++++++++
M
nimgit/reference.nim
|
98
+++----------------------------------------------------------------------------
A
nimgit/revisionWalker.nim
|
87
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/nimgit/branch.nim b/nimgit/branch.nim
@@ -0,0 +1,50 @@
+import nimgit2
+import types, free
+
+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 isHead* (reference: GitReference): bool = cast[bool](git_branch_is_head(reference))
+
+proc isCheckedOut* (reference: GitReference): bool = cast[bool](git_branch_is_checked_out(reference))
+
+
+proc getBranchName* (reference: GitReference): string =
+    var name: cstring
+    let error = git_branch_name(addr name, reference).getResultCode
+
+    if error != grcOk:
+        if error == grcInvalid:
+            raise newException(ValueError, "Not a symbolic reference!")
+        else:
+            raise newException(CatchableError, "Cannot get branch name: " & $error)
+
+    result = $name
+
+
+iterator branches* (repo: GitRepository, branchType: GitBranchType = branchLocal): GitReference =
+    var
+      cbranchType = cast[git_branch_t](branchType.ord)
+      branchIterator: GitBranchIterator
+
+    let error = git_branch_iterator_new(addr branchIterator, repo, cbranchType)
+
+    if error != 0:
+        free(branchIterator)
+        raise newException(CatchableError, "Cannot create branch iterator: " & $error.getResultCode)
+
+    while true:
+        var branch: GitReference
+        let error = git_branch_next(addr branch, addr cbranchType, branchIterator).getResultCode
+        case error:
+            of grcOk:
+                yield branch
+
+            of grcIterOver:
+                free(branch)
+                free(branchIterator)
+                break
+
+            else:
+                raise newException(CatchableError, "iteration error: " & $error)
diff --git a/nimgit/reference.nim b/nimgit/reference.nim
@@ -1,5 +1,5 @@
 import nimgit2
-import types, free, objects
+import types, free
 
 proc getName* (reference: GitReference): string = $git_reference_name(reference)
 

@@ -9,22 +9,6 @@ proc getShorthand* (reference: GitReference): string = reference.getShortName()
 
 proc `$`* (reference: GitReference): string = reference.getName()
 
-proc getBranchName* (reference: GitReference): string =
-    var name: cstring
-    let error = git_branch_name(addr name, reference).getResultCode
-
-    if error != grcOk:
-        if error == grcInvalid:
-            raise newException(ValueError, "Not a symbolic reference!")
-        else:
-            raise newException(CatchableError, "Cannot get branch name: " & $error)
-
-    result = $name
-
-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))

@@ -81,81 +65,4 @@ proc lookupGitReference* (repo: GitRepository, refName: string): GitReference =
 
     if error != 0:
         free(result)
-        raise newException(CatchableError, "Lookup failed: " & $error.getResultCode)
-
-
-proc createRevisionWalker* (repo: GitRepository, sort: git_sort_t = GIT_SORT_TOPOLOGICAL): GitRevisionWalker =
-    var error: cint
-
-    error = git_revwalk_new(addr result, repo)
-
-    if error != 0:
-        free(result)
-        raise newException(CatchableError, "Cannot create RevWalker: " & $error.getResultCode)
-
-    error = git_revwalk_sorting(result, cast[cuint](sort))
-
-    if error != 0:
-        free(result)
-        raise newException(CatchableError, "Cannot set sorting: " & $error.getResultCode)
-
-
-proc createRevisionWalker* (repo: GitRepository, reference: string, sort: git_sort_t = GIT_SORT_TOPOLOGICAL): GitRevisionWalker =
-    var error: cint
-    var gitWalker = repo.createRevisionWalker(sort)
-
-    error = git_revwalk_push_ref(gitWalker, "HEAD")
-
-    if error != 0:
-        free(gitWalker)
-        raise newException(CatchableError, "Cannot push: " & $error.getResultCode)
-
-    result = gitWalker
-
-
-proc next* (walker: GitRevisionWalker): GitObjectId =
-    var error: cint
-    var objectId = initGitObjectId()
-
-    error = git_revwalk_next(objectId, walker)
-
-    if error != 0:
-        free(objectId)
-        raise newException(CatchableError, "Cannot get next: " & $error.getResultCode)
-
-    result = objectId
-
-
-iterator walk* (walker: GitRevisionWalker): GitObjectId =
-    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 =
-    var
-      cbranchType = cast[git_branch_t](branchType.ord)
-      branchIterator: GitBranchIterator
-
-    let error = git_branch_iterator_new(addr branchIterator, repo, cbranchType).getResultCode
-
-    if error != grcOk:
-        free(branchIterator)
-        raise newException(CatchableError, "Cannot create branch iterator: " & $error)
-
-    while true:
-        var branch: GitReference
-        let code = git_branch_next(addr branch, addr cbranchType, branchIterator).getResultCode
-        case code:
-            of grcOk:
-                yield branch
-
-            of grcIterOver:
-                free(branchIterator)
-                break
-
-            else:
-                raise newException(CatchableError, "iteration error: " & $error)
+        raise newException(CatchableError, "Lookup failed: " & $error.getResultCode)+
\ No newline at end of file
diff --git a/nimgit/revisionWalker.nim b/nimgit/revisionWalker.nim
@@ -0,0 +1,87 @@
+import nimgit2
+import types, free, objects
+
+proc walk* (repo: GitRepository): GitRevisionWalker =
+    let error = git_revwalk_new(addr result, repo)
+
+    if error != 0:
+        free(result)
+        raise newException(CatchableError, "Cannot create RevWalker: " & $error.getResultCode)
+
+
+proc sort* (walker: GitRevisionWalker, sort: git_sort_t = GIT_SORT_TOPOLOGICAL) = 
+    let error = git_revwalk_sorting(walker, cast[cuint](sort))
+
+    if error != 0:
+        free(walker)
+        raise newException(CatchableError, "Cannot set sorting: " & $error.getResultCode)
+
+
+proc push* (walker: GitRevisionWalker, reference: string) =
+    let error = git_revwalk_push_ref(walker, cstring(reference))
+
+    if error != 0:
+        free(walker)
+        raise newException(CatchableError, "Cannot push reference: " & $error.getResultCode)
+
+proc push* (walker: GitRevisionWalker, objectId: GitObjectId) =
+    let error = git_revwalk_push(walker, objectId)
+
+    if error != 0:
+        free(walker)
+        raise newException(CatchableError, "Cannot push objectId: " & $error.getResultCode)
+
+proc pushHead* (walker: GitRevisionWalker) =
+    let error = git_revwalk_push_head(walker)
+
+    if error != 0:
+        free(walker)
+        raise newException(CatchableError, "Cannot push head: " & $error.getResultCode)
+
+
+proc hide* (walker: GitRevisionWalker, objectId: GitObjectId) =
+    let error = git_revwalk_hide(walker, objectId)
+
+    if error != 0:
+        free(walker)
+        raise newException(CatchableError, "Cannot push objectId: " & $error.getResultCode)
+
+proc hideHead* (walker: GitRevisionWalker) =
+    let error = git_revwalk_hide_head(walker)
+
+    if error != 0:
+        free(walker)
+        raise newException(CatchableError, "Cannot push head: " & $error.getResultCode)
+
+
+proc simplifyFirstParent* (walker: GitRevisionWalker) =
+    let error = git_revwalk_simplify_first_parent(walker)
+
+    if error != 0:
+        free(walker)
+        raise newException(CatchableError, "Cannot simplify walker: " & $error.getResultCode)
+
+proc reset* (walker: GitRevisionWalker) = 
+    let error = git_revwalk_reset(walker)
+
+    if error != 0:
+        free(walker)
+        raise newException(CatchableError, "Cannot simplify walker: " & $error.getResultCode)
+
+
+iterator items* (walker: GitRevisionWalker): GitObjectId =
+    var
+      objectId = initGitObjectId()
+
+    while true:
+        let code = git_revwalk_next(objectId, walker).getResultCode
+        case code:
+            of grcOk:
+                yield objectId
+
+            of grcIterOver:
+                free(walker)
+                break
+
+            else:
+                raise newException(CatchableError, "iteration error: " & $code)