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 
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 
106 
107 
108 import nimgit2
import types, free

proc initDiffOptions* (): GitDiffOptions =
    result = cast[GitDiffOptions](sizeof(git_diff_options).alloc)
    discard git_diff_init_options(result, GIT_DIFF_OPTIONS_VERSION)

proc GitDiffFindOptions* (): GitDiffFindOptions =
    result = cast[GitDiffFindOptions](sizeof(git_diff_find_options).alloc)
    discard git_diff_find_init_options(result, GIT_DIFF_FIND_OPTIONS_VERSION)


proc diffTrees* (repo: GitRepository, newTree: GitTree, oldTree: GitTree, options: GitDiffOptions): GitDiff =
    let error = git_diff_tree_to_tree(addr result, repo, newTree, oldTree, options)

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

proc findSimilar* (diff: GitDiff, options: GitDiffFindOptions) =
    let error = git_diff_find_similar(diff, options)

    if error != 0:
        raise newException(CatchableError, "Cannot diff find: " & $error.getResultCode)

proc stats* (diff: GitDiff): GitDiffStats =
    var diffStats : ptr git_diff_stats
    let error     = git_diff_get_stats(addr diffStats, diff)

    if error != 0:
        raise newException(CatchableError, "Cannot get diff-stats: " & $error.getResultCode)

    result.filesChanged = cast[int](git_diff_stats_files_changed(diffStats))
    result.deletions    = cast[int](git_diff_stats_deletions(diffStats))
    result.insertions   = cast[int](git_diff_stats_insertions(diffStats))

    git_diff_stats_free(diffStats)

proc len* (delta: GitDiff): int = cast[int](git_diff_num_deltas(delta))

proc delta* (delta: GitDiff, id: int): GitDiffDelta = git_diff_get_delta(delta, uint(id))

iterator deltas* (diff: GitDiff): (int, GitDiffDelta) =
    var counter : int
    let diffLen = diff.len

    while counter < diffLen:
        yield (counter, diff.delta(counter))
        inc(counter)

proc statusChar* (delta: GitDiffDelta): string =
    case delta.status:
        of GIT_DELTA_ADDED:
            result = "A"
        of GIT_DELTA_COPIED:
            result = "C"
        of GIT_DELTA_DELETED:
            result = "D"
        of GIT_DELTA_MODIFIED:
            result = "M"
        of GIT_DELTA_RENAMED:
            result = "R"
        of GIT_DELTA_TYPECHANGE:
            result = "T"
        else:
            result = " "


proc patch* (diff: GitDiff, id: int): GitPatch =
    let error = git_patch_from_diff(addr result, diff, uint(id))

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


proc hunksLen* (patch: GitPatch): int = cast[int](git_patch_num_hunks(patch))

proc hunk* (patch: GitPatch, id: int): GitDiffHunk =
    var linesCount : uint
    let error      = git_patch_get_hunk(addr result, addr linesCount, patch, uint(id))

    if error != 0:
        raise newException(CatchableError, "Cannot get diff-hunk: " & $error.getResultCode)

iterator hunks* (patch: GitPatch): (int, GitDiffHunk) =
    var counter : int
    let hunkLen = patch.hunksLen

    while counter < hunkLen:
        yield (counter, patch.hunk(counter))
        inc(counter)


proc linesLen* (patch: GitPatch, id: int): int = cast[int](git_patch_num_lines_in_hunk(patch, uint(id)))

proc line* (patch: GitPatch, hunk: int, line: int): GitDiffLine =
    let error = git_patch_get_line_in_hunk(addr result, patch, uint(hunk), uint(line))

    if error != 0:
        raise newException(CatchableError, "Cannot get diff-line: " & $error.getResultCode)

iterator lines* (patch: GitPatch, hunkId: int): (int, GitDiffLine) =
    var counter  : int
    let lineLen = patch.linesLen(hunkId)

    while counter < lineLen:
        yield (counter, patch.line(hunkId, counter))
        inc(counter)