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 import nimgit2
import types, free
proc getName* (reference: GitReference): string = $git_reference_name(reference)
proc getShortName* (reference: GitReference): string = $git_reference_shorthand(reference)
proc getShorthand* (reference: GitReference): string = reference.getShortName()
proc `$`* (reference: GitReference): string = reference.getName()
proc isTag* (reference: GitReference): bool = cast[bool](git_reference_is_tag(reference))
proc isNote* (reference: GitReference): bool = cast[bool](git_reference_is_note(reference))
proc owner* (reference: GitReference): GitRepository = git_reference_owner(reference)
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!")
let error = git_reference_resolve(addr result, reference)
if error != 0:
free(result)
raise newException(CatchableError, "Cannot lookup symbolic reference: " & $error.getResultCode)
proc getTarget* (reference: GitReference): GitObjectId =
case reference.getType():
of referenceDirect:
result = git_reference_target(reference)
of referenceSymbolic:
var targetReference = reference.resolveTarget()
result = git_reference_target(targetReference)
free(targetReference)
proc getSymbolicTarget* (reference: GitReference): string =
if reference.getType() != referenceSymbolic:
raise newException(ValueError, "Not a symbolic reference!")
result = $git_reference_symbolic_target(reference)
proc getGitReferenceNames* (repo: GitRepository): seq[string] =
var gitRefsArr: git_strarray
let error = git_reference_list(addr gitRefsArr, repo)
if error != 0:
free(addr gitRefsArr)
raise newException(CatchableError, "Cannot get reference-names: " & $error.getResultCode)
if gitRefsArr.count == 0'u:
result = newSeq[string]()
else:
result = cstringArrayToSeq(cast[cstringArray](gitRefsArr.strings), gitRefsArr.count)
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)