summaryrefslogtreecommitdiffstats
path: root/nimgit/commit.nim
blob: 0cac63bc0aaab5e9f1f5588a2747a911a4144e42 (plain) (blame)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import times
import nimgit2
import types, free, utils, oid, objects

proc lookupCommit* (repo: GitRepository, oid: GitObjectId): GitCommit =
    let error = git_commit_lookup(addr result, repo, oid)

    if error != 0:
        free(result)
        raise newException(CatchableError, "Commit lookup failed: " & $error.getResultCode)

proc type* (obj: GitCommit): GitObjectKind = cast[GitObject](obj).type

proc owner* (commit: GitCommit): GitRepository = git_commit_owner(commit)

proc repo* (commit: GitCommit): GitRepository = commit.owner()

proc rawHeader* (commit: GitCommit): seq[byte] = cast[seq[byte]](git_commit_raw_header(commit))

proc id* (commit: GitCommit): GitObjectId = git_commit_id(commit)

proc `$`* (commit: GitCommit): string = $commit.id()

proc shortId* (commit: GitCommit): string = cast[GitObject](commit).shortId()

proc summary* (commit: GitCommit): string = $git_commit_summary(commit)

proc body* (commit: GitCommit): string = $git_commit_body(commit)

proc message* (commit: GitCommit): string = $git_commit_message(commit)

proc messageEncoding* (commit: GitCommit): string = $git_commit_message_encoding(commit)

proc rawMessage* (commit: GitCommit): seq[byte] = cast[seq[byte]](git_commit_message_raw(commit))

proc gpgSignature* (commit: GitCommit): (string, string) =
    var signature:  git_buf
    var signedData: git_buf

    let grc = git_commit_extract_signature(addr signature, addr signedData, commit.owner, commit.id, nil).getResultCode

    if grc != grcOk:
        free(addr signature);
        free(addr signedData);

        let error = getLastError()

        if grc == grcNotFound:
            return ("", "")
        else:
            raise newException(CatchableError, "Unkown Error while extracting signature: " & error.message)

    result = ($signature.ptr, $signed_data.ptr)

    free(addr signature);
    free(addr signedData);


proc time* (commit: GitCommit): GitTime =
    result.time   = fromUnix(git_commit_time(commit))
    result.offset = git_commit_time_offset(commit)


proc author* (commit: GitCommit): GitSignature = git_commit_author(commit).parseGitSignature

proc committer* (commit: GitCommit): GitSignature = git_commit_committer(commit).parseGitSignature

proc hasParents* (commit: GitCommit): bool = cast[bool](git_commit_parentcount(commit))

proc parentCount* (commit: GitCommit): int = cast[int](git_commit_parentcount(commit))

proc parentId* (commit: GitCommit, id: int = 0): GitObjectId = git_commit_parent_id(commit, cuint(id))

proc parentIds* (commit: GitCommit): seq[GitObjectId] =
    var counter: int
    let parentCount = commit.parentCount

    while counter < parentCount:
        result.add(commit.parentId(counter))
        inc(counter)

proc parent* (commit: GitCommit, id: int = 0): GitCommit =
    let error = git_commit_parent(addr result, commit, cuint(id))

    if error != 0:
        free(result)
        raise newException(CatchableError, "parrent lookup failed: " & $error.getResultCode)

proc parents* (commit: GitCommit): seq[GitCommit] =
    var counter: int
    let parentCount = commit.parentCount

    while counter < parentCount:
        result.add(commit.parent(counter))
        inc(counter)


proc treeId* (commit: GitCommit): GitObjectId = git_commit_tree_id(commit)

proc tree* (commit: GitCommit): GitTree =
    let error = git_commit_tree(addr result, commit)

    if error != 0:
        free(result)
        raise newException(CatchableError, "tree lookup failed: " & $error.getResultCode)