ctucx.git: nimgit

[nimlang] nim-wrapper for libgit2

commit c77cb8b9940f4c1bdc100e6f9eab180583ffce9d
parent 662df89660a3dc094fcaefb9e335f10c31f9d9eb
Author: Leah (ctucx) <leah@ctu.cx>
Date: Tue, 16 Mar 2021 16:30:49 +0100

objects.nim: new proc lookupObjectIdByName
2 files changed, 19 insertions(+), 7 deletions(-)
M
nimgit/objects.nim
|
23
+++++++++++++++++------
M
showLastCommit.nim
|
3
++-
diff --git a/nimgit/objects.nim b/nimgit/objects.nim
@@ -3,14 +3,26 @@ import types, free
 
 proc initGitObjectId* (): GitObjectId = cast[GitObjectId](sizeof(git_oid).alloc)
 
+
+proc getSha* (obj: GitObjectId): string = $git_oid_tostr_s(obj)
+
+proc `$`* (obj: GitObjectId): string = obj.getSha()
+
+
+proc lookupObjectIdByName* (repo: GitRepository, name: string): GitObjectId =
+    var objId = initGitObjectId()
+    let error = git_reference_name_to_id(objId, repo, cstring(name))
+
+    if error != 0:
+        free(objId)
+        raise newException(CatchableError, "Lookup failed: " & $error.getResultCode)
+
+    result = objId
+
+
 proc lookupObject* (repo: GitRepository, name: string): GitObject = 
     let error = git_revparse_single(addr result, repo, cstring(name))
 
     if error != 0:
         free(result)
         raise newException(CatchableError, "Object lookup failed: " & $error.getResultCode)
-
-proc getSha* (obj: GitObjectId): string =
-    result = $git_oid_tostr_s(obj)
-
-proc `$`* (obj: GitObjectId): string = obj.getSha()-
\ No newline at end of file
diff --git a/showLastCommit.nim b/showLastCommit.nim
@@ -12,7 +12,8 @@ try:
 
     echo "Last commit on HEAD in repo: " & $gitRepository
 
-    let commit = cast[GitCommit](gitRepository.lookupObject("HEAD^{commit}"))
+    let objId = gitRepository.lookupObjectIdByName("HEAD")
+    let commit = gitRepository.lookupCommit(objId)
     let author = commit.getAuthor()
 
     echo "hash: " & $commit.getObjectId()