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 import sequtils
import nimgit2
import types, free, objects, treeEntry
proc lookupTree* (repo: GitRepository, id: GitObjectId): GitTree =
let error = git_tree_lookup(addr result, repo, id)
if error != 0:
free(result)
raise newException(CatchableError, "Tree lookup failed: " & $error.getResultCode)
proc type* (obj: GitTree): GitObjectKind = cast[GitObject](obj).type
proc owner* (tree: GitTree): GitRepository = git_tree_owner(tree)
proc id* (tree: GitTree): GitObjectId = git_tree_id(tree)
proc shortId* (tree: GitTree): string = cast[GitObject](tree).shortId()
proc len* (tree: GitTree): int = cast[int](git_tree_entrycount(tree))
proc entry* (tree: GitTree, id: int): GitTreeEntry = git_tree_entry_byindex(tree, cuint(id))
proc entry* (tree: GitTree, id: GitObjectId): GitTreeEntry = git_tree_entry_byid(tree, id)
#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
if error != grcOk:
free(result)
if error != grcNotFound:
raise newException(CatchableError, "TreeEntry lookup failed: " & $error)
else:
raise newException(CatchableError, "TreeEntry not found")
iterator items* (tree: GitTree): GitTreeEntry =
var counter: int
let treeLen = tree.len
while counter < treeLen:
yield tree.entry(counter).copy()
inc(counter)
proc walk* (tree: GitTree, path: string = ""): seq[(string, GitTreeEntry)] =
for entry in tree:
result.add((path, entry))
if entry.type == goTree:
let subtree = tree.owner.lookupTree(entry.id)
result = result.concat(subtree.walk(path & entry.name & "/"))
free(subtree)
proc entries* (tree: GitTree): seq[string] =
for element in tree.walk:
let (dir, entry) = element
result.add(dir&entry.name)
free(entry)