summaryrefslogtreecommitdiffstats
path: root/nimgit/branch.nim
diff options
context:
space:
mode:
Diffstat (limited to 'nimgit/branch.nim')
-rw-r--r--nimgit/branch.nim50
1 files changed, 50 insertions, 0 deletions
diff --git a/nimgit/branch.nim b/nimgit/branch.nim
new file mode 100644
index 0000000..41bea32
--- /dev/null
+++ 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)