summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeah (ctucx) <leah@ctu.cx>2021-03-17 02:22:07 +0100
committerLeah (ctucx) <leah@ctu.cx>2021-03-17 02:22:07 +0100
commit74b4e93e6e52dfd54c58f09b4b39971d826ccd3b (patch)
tree5722c23c1ec2bd0caf4790e79a9a69b915175b3f
parente3cce2406706cbbcc2fa2defe16a50d220b3f101 (diff)
downloadnimgit-74b4e93e6e52dfd54c58f09b4b39971d826ccd3b.tar.gz
nimgit-74b4e93e6e52dfd54c58f09b4b39971d826ccd3b.tar.bz2
nimgit-74b4e93e6e52dfd54c58f09b4b39971d826ccd3b.zip
split objects.nim
-rw-r--r--nimgit/objects.nim33
-rw-r--r--nimgit/oid.nim28
2 files changed, 38 insertions, 23 deletions
diff --git a/nimgit/objects.nim b/nimgit/objects.nim
index e44d6f6..93792e8 100644
--- a/nimgit/objects.nim
+++ b/nimgit/objects.nim
@@ -1,24 +1,5 @@
import nimgit2
-import types, free
-
-proc initGitObjectId* (): GitObjectId = cast[GitObjectId](sizeof(git_oid).alloc)
-
-proc fromString* (str: string): GitObjectId =
- result = initGitObjectId()
- let error = git_oid_fromstr(result, cstring(str))
-
- if error != 0:
- free(result)
- raise newException(CatchableError, "Cannot parse string to GitObjectId: " & $error.getResultCode)
-
-
-proc toString* (obj: GitObjectId): string = $git_oid_tostr_s(obj)
-
-proc `$`* (obj: GitObjectId): string = obj.toString()
-
-
-proc `==`* (a: GitObjectId, b: GitObjectId): bool = cast[bool](git_oid_equal(a, b))
-
+import types, free, oid
proc lookupObjectIdByName* (repo: GitRepository, name: string): GitObjectId =
result = initGitObjectId()
@@ -30,8 +11,14 @@ proc lookupObjectIdByName* (repo: GitRepository, name: string): GitObjectId =
proc lookupObject* (repo: GitRepository, name: string): GitObject =
- let error = git_revparse_single(addr result, repo, cstring(name))
+ let error = git_revparse_single(addr result, repo, cstring(name)).getResultCode
- if error != 0:
+ if error != grcOk:
free(result)
- raise newException(CatchableError, "Object lookup failed: " & $error.getResultCode)
+ case error:
+ of grcNotFound: raise newException(CatchableError, "Object not found.")
+ of grcAmbiguous: raise newException(CatchableError, "Object lookup was ambiguous")
+ of grcInvalidSpec: raise newException(CatchableError, "Object lookup was ambiguous")
+ else: raise newException(CatchableError, "Object lookup failed: " & $error)
+
+proc kind* (obj: GitObject): GitObjectKind = GitObjectKind(git_object_type(obj) - GIT_OBJECT_ANY) \ No newline at end of file
diff --git a/nimgit/oid.nim b/nimgit/oid.nim
new file mode 100644
index 0000000..76808e6
--- /dev/null
+++ b/nimgit/oid.nim
@@ -0,0 +1,28 @@
+import nimgit2
+import types, free
+
+proc initGitObjectId* (): GitObjectId = cast[GitObjectId](sizeof(git_oid).alloc)
+
+proc fromString* (str: string): GitObjectId =
+ result = initGitObjectId()
+ let error = git_oid_fromstr(result, cstring(str))
+
+ if error != 0:
+ free(result)
+ raise newException(CatchableError, "Cannot parse string to GitObjectId: " & $error.getResultCode)
+
+
+proc toString* (obj: GitObjectId): string = $git_oid_tostr_s(obj)
+
+proc isZero (obj: GitObjectId): bool = cast[bool](git_oid_is_zero(obj))
+
+proc `$`* (obj: GitObjectId): string = obj.toString()
+
+proc `==`* (a: GitObjectId, b: GitObjectId): bool =
+ if a.isNil or b.isNil:
+ result = false
+ elif a.isZero or b.isZero:
+ result = false
+ else:
+ result = cast[bool](git_oid_equal(a, b))
+