ctucx.git: nimgit

[nimlang] nim-wrapper for libgit2

commit 360711e20d8e71b7dd56a45091eec950ada13f4f
parent 1c90db75a8328034f81e809909f9c84ddd79a273
Author: Leah (ctucx) <leah@ctu.cx>
Date: Fri, 19 Mar 2021 13:51:34 +0100

add copy proc for GitBlob, GitCommit, GitObject, GitReference, GitTag, GitTree, GitTreeEntry
7 files changed, 54 insertions(+), 0 deletions(-)
diff --git a/nimgit/blob.nim b/nimgit/blob.nim
@@ -8,6 +8,15 @@ proc lookupBlob* (repo: GitRepository, id: GitObjectId): GitBlob =
         free(result)
         raise newException(CatchableError, "Blob lookup failed: " & $error.getResultCode)
 
+
+proc copy* (blob: GitBlob): GitBlob =
+    let error = git_blob_dup(addr result, blob)
+
+    if error != 0:
+        free(result)
+        raise newException(CatchableError, "Cannot copy GitBlob: " & $error.getResultCode)
+
+
 proc type* (obj: GitBlob): GitObjectKind = cast[GitObject](obj).type
 
 proc owner* (blob: GitBlob): GitRepository = git_blob_owner(blob)
diff --git a/nimgit/commit.nim b/nimgit/commit.nim
@@ -9,6 +9,15 @@ proc lookupCommit* (repo: GitRepository, oid: GitObjectId): GitCommit =
         free(result)
         raise newException(CatchableError, "Commit lookup failed: " & $error.getResultCode)
 
+
+proc copy* (commit: GitCommit): GitCommit =
+    let error = git_commit_dup(addr result, commit)
+
+    if error != 0:
+        free(result)
+        raise newException(CatchableError, "Cannot copy GitCommit: " & $error.getResultCode)
+
+
 proc type* (obj: GitCommit): GitObjectKind = cast[GitObject](obj).type
 
 proc owner* (commit: GitCommit): GitRepository = git_commit_owner(commit)
diff --git a/nimgit/objects.nim b/nimgit/objects.nim
@@ -23,6 +23,15 @@ proc lookupObject* (repo: GitRepository, name: string): GitObject =
 
 proc type* (obj: GitObject): GitObjectKind = GitObjectKind(git_object_type(obj) - GIT_OBJECT_ANY)
 
+
+proc copy* (obj: GitObject): GitObject =
+    let error = git_object_dup(addr result, obj)
+
+    if error != 0:
+        free(result)
+        raise newException(CatchableError, "Cannot copy GitObject: " & $error.getResultCode)
+
+
 proc shortId* (obj: GitObject): string =
     var buffer: git_buf
 
diff --git a/nimgit/reference.nim b/nimgit/reference.nim
@@ -20,6 +20,13 @@ proc repo* (reference: GitReference): GitRepository = reference.owner()
 
 proc getType* (reference: GitReference): GitReferenceType = cast[GitReferenceType](git_reference_type(reference))
 
+proc copy* (reference: GitReference): GitReference =
+    let error = git_reference_dup(addr result, reference)
+
+    if error != 0:
+        free(result)
+        raise newException(CatchableError, "Cannot copy GitReference: " & $error.getResultCode)
+
 proc resolveTarget(reference: GitReference): GitReference =
     if reference.getType() != referenceSymbolic:
         raise newException(ValueError, "Not a symbolic reference!")
diff --git a/nimgit/tag.nim b/nimgit/tag.nim
@@ -22,6 +22,12 @@ proc message* (tag: GitTag): string = $git_tag_message(tag)
 
 proc tagger* (tag: GitTag): GitSignature = git_tag_tagger(tag).parseGitSignature
 
+proc copy* (tag: GitTag): GitTag =
+    let error = git_tag_dup(addr result, tag)
+
+    if error != 0:
+        free(result)
+        raise newException(CatchableError, "Cannot copy GitTag: " & $error.getResultCode)
 
 proc target* (tag: GitTag): GitObject =
     let error = git_tag_target(addr result, tag)
diff --git a/nimgit/tree.nim b/nimgit/tree.nim
@@ -24,6 +24,13 @@ proc entry* (tree: GitTree, id: GitObjectId): GitTreeEntry = git_tree_entry_byid
 
 #proc entry* (tree: GitTree, name: string): GitTreeEntry = git_tree_entry_byname(tree, cstring(name))
 
+proc copy* (tree: GitTree): GitTree =
+    let error = git_tree_dup(addr result, tree)
+
+    if error != 0:
+        free(result)
+        raise newException(CatchableError, "Cannot copy GitTree: " & $error.getResultCode)
+
 proc entry* (tree: GitTree, path: string): GitTreeEntry =
     let error = git_tree_entry_bypath(addr result, tree, cstring(path)).getResultCode
 
diff --git a/nimgit/treeEntry.nim b/nimgit/treeEntry.nim
@@ -11,6 +11,13 @@ proc mode* (entry: GitTreeEntry): int = cast[int](git_tree_entry_filemode(entry)
 
 proc modeStr* (entry: GitTreeEntry): string = filemodeStr(entry.mode)
 
+proc copy* (entry: GitTreeEntry): GitTreeEntry =
+    let error = git_tree_entry_dup(addr result, entry)
+
+    if error != 0:
+        free(result)
+        raise newException(CatchableError, "Cannot copy GitTreeEntry: " & $error.getResultCode)
+
 proc getObject* (repo: GitRepository, entry: GitTreeEntry): GitBlob =
     var obj: GitObject
     let error = git_tree_entry_to_object(addr obj, repo, entry)