ctucx.git: nimgit

[nimlang] nim-wrapper for libgit2

commit 0d029fbdf90feb11f139f84b86be63aee65e6805
parent 4634d09b6989f2c481b3ab8f500f356483a540aa
Author: Leah (ctucx) <leah@ctu.cx>
Date: Tue, 16 Mar 2021 15:27:57 +0100

free on error
4 files changed, 20 insertions(+), 4 deletions(-)
diff --git a/nimgit/commit.nim b/nimgit/commit.nim
@@ -1,23 +1,28 @@
 import nimgit2
-import types, utils
+import types, free, utils
 
 proc lookupCommit* (repo: GitRepository, oid: ptr git_oid): GitCommit =
     let error = git_commit_lookup(addr result, repo, oid)
 
     if error != 0:
+        free(result)
         raise newException(CatchableError, "Commit lookup failed: " & $error.getResultCode)
 
+
 proc getObjectId* (commit: GitCommit): GitObjectId = 
     result = git_commit_id(commit)
 
+
 proc getSummary* (commit: GitCommit): string =
     result = $git_commit_summary(commit)
 
+
 proc getMessage* (commit: GitCommit): string =
     result = $git_commit_message(commit)
 
 proc getShortMessage* (commit: GitCommit): string = commit.getSummary()
 
+
 proc getAuthor* (commit: GitCommit): GitSignature =
     let author = git_commit_author(commit)
 

@@ -25,6 +30,7 @@ proc getAuthor* (commit: GitCommit): GitSignature =
     result.email = $author.email
     result.when  = parseGitTime(author.when)
 
+
 proc getCommitter* (commit: GitCommit): GitSignature =
     let author = git_commit_committer(commit)
 

@@ -32,8 +38,10 @@ proc getCommitter* (commit: GitCommit): GitSignature =
     result.email = $author.email
     result.when  = parseGitTime(author.when)
 
+
 proc getParrent* (commit: GitCommit): GitCommit =
     let error = git_commit_parent(addr result, commit, 0)
 
     if error != 0:
+        free(result)
         raise newException(CatchableError, "parrent lookup failed: " & $error.getResultCode)
diff --git a/nimgit/objects.nim b/nimgit/objects.nim
@@ -1,5 +1,5 @@
 import nimgit2
-import types
+import types, free
 
 proc initGitObjectId* (): GitObjectId = cast[GitObjectId](sizeof(git_oid).alloc)
 

@@ -7,6 +7,7 @@ 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 =
diff --git a/nimgit/reference.nim b/nimgit/reference.nim
@@ -49,17 +49,19 @@ proc getGitReferenceNames* (repo: GitRepository): seq[string] =
     let error = git_reference_list(addr gitRefsArr, repo)
 
     if error != 0:
+        free(addr gitRefsArr)
         raise newException(CatchableError, "Cannot get reference-names: " & $error.getResultCode)
 
     result = cstringArrayToSeq(cast[cstringArray](gitRefsArr.strings), gitRefsArr.count)
     
-    git_strarray_dispose(addr gitRefsArr);
+    free(addr gitRefsArr);
 
 
 proc lookupGitReference* (repo: GitRepository, refName: string): GitReference =     
     let error = git_reference_lookup(addr result, repo, cstring(refName))
 
     if error != 0:
+        free(result)
         raise newException(CatchableError, "Lookup failed: " & $error.getResultCode)
 
 

@@ -69,11 +71,13 @@ proc createRevisionWalker* (repo: GitRepository, sort: git_sort_t = GIT_SORT_TOP
     error = git_revwalk_new(addr result, repo)
 
     if error != 0:
+        free(result)
         raise newException(CatchableError, "Cannot create RevWalker: " & $error.getResultCode)
 
     error = git_revwalk_sorting(result, cast[cuint](sort))
 
     if error != 0:
+        free(result)
         raise newException(CatchableError, "Cannot set sorting: " & $error.getResultCode)
 
 

@@ -84,6 +88,7 @@ proc createRevisionWalker* (repo: GitRepository, reference: string, sort: git_so
     error = git_revwalk_push_ref(gitWalker, "HEAD")
 
     if error != 0:
+        free(gitWalker)
         raise newException(CatchableError, "Cannot push: " & $error.getResultCode)
 
     result = gitWalker
diff --git a/nimgit/repository.nim b/nimgit/repository.nim
@@ -1,16 +1,18 @@
 import nimgit2
-import types
+import types, free
 
 proc openGitRepository* (path: string): GitRepository =
     let error = git_repository_open(addr result, path)
 
     if error != 0:
+        free(result)
         raise newException(CatchableError, "Cannot open repository: " & $error.getResultCode)
 
 proc getHead* (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 getPath* (repo: GitRepository): string =