ctucx.git: nimgit

[nimlang] nim-wrapper for libgit2

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
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)