-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
repository.jl
72 lines (63 loc) · 2.32 KB
/
repository.jl
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
function GitRepo(path::AbstractString)
repo_ptr_ptr = Ptr{Void}[0]
err = ccall((:git_repository_open, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{UInt8}), repo_ptr_ptr, path)
if err != GitErrorConst.GIT_OK
if repo_ptr_ptr[] != C_NULL
finalize(GitRepo(repo_ptr_ptr[]))
end
throw(GitError(err))
end
return GitRepo(repo_ptr_ptr[])
end
function close(r::GitRepo)
if r.ptr != C_NULL
ccall((:git_repository__cleanup, :libgit2), Void, (Ptr{Void},), r.ptr)
end
end
function init(path::AbstractString, bare::Cuint = Cuint(0))
repo_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
@check ccall((:git_repository_init, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{UInt8}, Cuint), repo_ptr_ptr, path, bare)
return GitRepo(repo_ptr_ptr[])
end
function head(repo::GitRepo)
head_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
@check ccall((:git_repository_head, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}), head_ptr_ptr, repo.ptr)
return GitReference(head_ptr_ptr[])
end
head_oid(repo::GitRepo) = Oid(head(repo))
function isbare(repo::GitRepo)
return ccall((:git_repository_is_bare, :libgit2), Cint, (Ptr{Void},), repo.ptr) == 1
end
function revparse(repo::GitRepo, obj::AbstractString)
obj_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
@check ccall((:git_revparse_single, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}, Ptr{UInt8}), obj_ptr_ptr, repo.ptr, obj)
return Oid(obj_ptr_ptr[])
end
function get{T <: GitObject}(::Type{T}, r::GitRepo, oid::Oid, prefix::Bool=false)
id_ptr = Ref(oid)
obj_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
git_otype = if T == GitCommit
GitConst.OBJ_COMMIT
elseif T == GitTree
GitConst.OBJ_TREE
else
error("Type $T is not supported")
end
@check if prefix
ccall((:git_object_lookup_prefix, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}, Ptr{Oid}, Csize_t, Cint),
obj_ptr_ptr, r.ptr, id_ptr, len, git_otype)
else
ccall((:git_object_lookup, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}, Ptr{Oid}, Cint),
obj_ptr_ptr, r.ptr, id_ptr, git_otype)
end
return T(obj_ptr_ptr[])
end
function get{T <: GitObject}(::Type{T}, r::GitRepo, oid::AbstractString)
return get(T, r, Oid(oid), length(oid) != OID_HEXSZ)
end