summaryrefslogtreecommitdiffstats
path: root/showLastCommit.nim
blob: 5f4947dc348aae77c0e6b6fc5017da2c6116a0ca (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
import os, times
import nimgit

if paramCount() == 0:
    echo "No git-repo given."
    quit(QuitFailure)

discard git_libgit2_init()

try:
    let
      gitRepository = openGitRepository(paramStr(1))
      config        = gitRepository.config
      objId         = gitRepository.lookupObjectIdByName("HEAD")
      commit        = gitRepository.lookupCommit(objId)
      author        = commit.author
      committer     = commit.committer
      parentCount   = commit.parentCount
      signature     = commit.gpgSignature
      tree          = commit.tree

    echo "Last commit on HEAD in repo: " & $gitRepository
    echo "==================="
    echo "config value of core.bare: " & $config.getBool("core.bare")
    echo "hash: " & $objId & " (" & commit.shortId & ")"
    echo "committer: " & committer.name & " <" & committer.email & ">"
    echo "author: " & author.name & " <" & author.email & ">"
    echo "when: " & $commit.time
    echo "message: " & commit.summary
    echo "tree id: " & $commit.treeId
    echo "parentCount: " & $parentCount

    for parentId in commit.parentIds:
        echo "parent: " & $parentId

    if signature[0] != "":
        echo "signature: " & $signature

    echo ""

    if commit.hasParents:
        let parent     = gitRepository.lookupCommit(commit.parentIds[0])
        let parentTree = gitRepository.lookupTree(parent.treeId)

        let diffopts   = initDiffOptions()
        diffopts.flags = cast[uint32](GIT_DIFF_DISABLE_PATHSPEC_MATCH) or cast[uint32](GIT_DIFF_IGNORE_SUBMODULES) or cast[uint32](GIT_DIFF_INCLUDE_TYPECHANGE)

        let findopts   = GitDiffFindOptions()
        findopts.flags = cast[uint32](GIT_DIFF_FIND_RENAMES) or cast[uint32](GIT_DIFF_FIND_COPIES) or cast[uint32](GIT_DIFF_FIND_EXACT_MATCH_ONLY)

        let diff       = gitRepository.diffTrees(tree, parentTree, diffopts)
        diff.findSimilar(findopts)

        free(parentTree)
        free(parent)

        echo diff.len
        echo diff.stats

        for deltaIndex, delta in diff.deltas:
            echo delta.statusChar & " " & $delta.old_file.path & " " & $delta.new_file.path

            let patch = diff.patch(deltaIndex)

            for hunkIndex, hunk in patch.hunks():

                echo $hunk.header

                for lineIndex, line in patch.lines(hunkIndex):
                    var status: string

                    if line.old_lineno == -1:
                        status = "+"
                    elif line.new_lineno == -1:
                        status = "-"
                    else:
                        status = " "

                    echo status & $line.content

            free(patch)

    free(tree)
    free(commit)
    free(config)
    free(gitRepository)

except:
    echo "Error:\n", getCurrentExceptionMsg()