ctucx.git: nimgit

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 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 import nimgit2
import types, free

proc openGitRepository* (path: string): GitRepository = 
    var repository: GitRepository
    let error = git_repository_open(addr repository, path)

    if error != 0:
        free(repository)
        raise newException(CatchableError, "Cannot open repository: " & $error.getResultCode)

    result = repository


proc getHeadForWorktree* (repo: GitRepository, name: string): GitReference = 
    let error = git_repository_head_for_worktree(addr result, repo, cstring(name))

    if error != 0:
        free(result)
        raise newException(CatchableError, "Cannot get HEAD for worktree: " & $error.getResultCode)

proc head* (repo: GitRepository): GitReference = 
    let error = git_repository_head(addr result, repo)

    if error != 0:
        free(result)
        raise newException(CatchableError, "Cannot get HEAD: " & $error.getResultCode)

proc isHeadDetached* (repo: GitRepository): bool = cast[bool](git_repository_head_detached(repo))

proc isHeadUnborn* (repo: GitRepository): bool = cast[bool](git_repository_head_unborn(repo))

proc tagList* (repo: GitRepository): seq[string] =
    var list: git_strarray
    
    let error = git_tag_list(addr list, repo)

    if error != 0:
        free(addr list)
        raise newException(CatchableError, "Cannot get tagList: " & $error.getResultCode)
    
    if list.count == 0'u:
        result = newSeq[string]()
    else:
        result = cstringArrayToSeq(cast[cstringArray](list.strings), list.count)

    free(addr list)


proc isEmpty* (repo: GitRepository): bool = cast[bool](git_repository_is_empty(repo))

proc isWorktree* (repo: GitRepository): bool = cast[bool](git_repository_is_worktree(repo))

proc isBare* (repo: GitRepository): bool = cast[bool](git_repository_is_bare(repo))

proc path* (repo: GitRepository): string = $git_repository_path(repo)

proc workdir* (repo: GitRepository): string = $git_repository_workdir(repo)

proc `$`* (repo: GitRepository): string = repo.path()