Skip to content

Commit

Permalink
import Base for finalizes
Browse files Browse the repository at this point in the history
better warning information
added `libgit2` tags
  • Loading branch information
wildart authored and jakebolewski committed Sep 29, 2015
1 parent dcac546 commit 52d72cb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
24 changes: 11 additions & 13 deletions base/pkg/libgit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ include("libgit2/remote.jl")
include("libgit2/strarray.jl")
include("libgit2/index.jl")
include("libgit2/merge.jl")
include("libgit2/tag.jl")

immutable State
head::Oid
Expand All @@ -32,12 +33,6 @@ function normalize_url(url::AbstractString)
m == nothing ? url : "git://github.com/$(m.captures[1]).git"
end

function gitdir(d, repo::GitRepo)
g = joinpath(d,".git")
isdir(g) && return g
path(repo)
end

"""Return HEAD Oid as string"""
function head(pkg::AbstractString)
with(GitRepo, pkg) do repo
Expand Down Expand Up @@ -109,7 +104,7 @@ function merge_base(one::AbstractString, two::AbstractString, repo::GitRepo)
moid_ptr, repo.ptr, oid1_ptr, oid2_ptr)
moid_ptr[]
catch e
warn("merge_base: ", e.msg)
#warn("Pkg:",path(repo),"=>",e.msg)
Oid()
end
return moid
Expand Down Expand Up @@ -172,7 +167,7 @@ function fetch(repo::GitRepo, remote::AbstractString="origin";
try
fetch(rmt)
catch err
warn("'fetch' thrown exception: $err")
warn("fetch: $err")
finally
finalize(rmt)
end
Expand Down Expand Up @@ -223,9 +218,13 @@ function branch!(repo::GitRepo, branch_name::AbstractString,
end
try
if !isempty(track) # setup tracking #TODO: what is branch tracks other then "origin" remote
with(GitConfig, repo) do cfg
set!(cfg, "branch.$branch_name.remote", GitConst.REMOTE_ORIGIN)
set!(cfg, "branch.$branch_name.merge", name(branch_ref))
try
with(GitConfig, repo) do cfg
set!(cfg, "branch.$branch_name.remote", GitConst.REMOTE_ORIGIN)
set!(cfg, "branch.$branch_name.merge", name(branch_ref))
end
catch
warn("Please provide remote tracking for branch '$branch_name' in '$(path(repo))'")
end
end

Expand Down Expand Up @@ -337,9 +336,8 @@ function snapshot(repo::GitRepo)
head = Oid(repo, GitConst.HEAD_FILE)
index = with(GitIndex, repo) do idx; write_tree!(idx) end
work = try
dir = splitdir(path(repo)[1:end-1])[1] # remove `/.git` part
with(GitIndex, repo) do idx
content = readdir(dir)
content = readdir(path(repo))
if length(content) > 1
files = [utf8(bytestring(c))::UTF8String for c in content]
push!(files, utf8("."))
Expand Down
6 changes: 0 additions & 6 deletions base/pkg/libgit2/strarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,4 @@ function Base.convert(::Type{Vector{AbstractString}}, sa::StrArrayStruct)
arr[i] = bytestring(unsafe_load(sa.strings, i))
end
return arr
end

function finalize(sa::StrArrayStruct)
sa_ptr = Ref(sa)
ccall((:git_strarray_free, :libgit2), Void, (Ptr{StrArrayStruct},), sa_ptr)
return sa_ptr[]
end
27 changes: 27 additions & 0 deletions base/pkg/libgit2/tag.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function tags(repo::GitRepo)
with(StrArrayStruct()) do sa
sa_ref = Ref(sa)
@check ccall((:git_tag_list, :libgit2), Cint,
(Ptr{StrArrayStruct}, Ptr{Void}), sa_ref, repo.ptr)
convert(Vector{AbstractString}, sa_ref[])
end
end

function tag_delete(repo::GitRepo, tag::AbstractString)
@check ccall((:git_tag_delete, :libgit2), Cint,
(Ptr{Void}, Ptr{UInt8}, ), repo.ptr, tag)
end

function tag_create(repo::GitRepo, tag::AbstractString, commit::AbstractString;
msg::AbstractString = "",
force::Bool = false)
oid_ptr = Ref(Oid())
with(get(GitCommit, repo, commit)) do commit_obj
with(default_signature(repo)) do sig
@check ccall((:git_tag_create, :libgit2), Cint,
(Ptr{Oid}, Ptr{Void}, Ptr{UInt8}, Ptr{Void}, Ptr{SignatureStruct}, Ptr{UInt8}, Cint),
oid_ptr, repo.ptr, tag, commit_obj.ptr, sig.ptr, msg, Cint(force))
end
end
return oid_ptr[]
end
10 changes: 8 additions & 2 deletions base/pkg/libgit2/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ immutable StrArrayStruct
count::Csize_t
end
StrArrayStruct() = StrArrayStruct(Ptr{UInt8}(0), zero(Csize_t))
function Base.finalize(sa::StrArrayStruct)
sa_ptr = Ref(sa)
ccall((:git_strarray_free, :libgit2), Void, (Ptr{StrArrayStruct},), sa_ptr)
return sa_ptr[]
end


immutable CheckoutOptionsStruct
version::Cuint
Expand Down Expand Up @@ -202,7 +208,7 @@ abstract AbstractGitObject
Base.isempty(obj::AbstractGitObject) = (obj.ptr == C_NULL)

abstract GitObject <: AbstractGitObject
function finalize(obj::GitObject)
function Base.finalize(obj::GitObject)
if obj.ptr != C_NULL
ccall((:git_object_free, :libgit2), Void, (Ptr{Void},), obj.ptr)
obj.ptr = C_NULL
Expand Down Expand Up @@ -235,7 +241,7 @@ for (typ, ref, sup, fnc) in (
end

if fnc != nothing
@eval function finalize(obj::$typ)
@eval function Base.finalize(obj::$typ)
if obj.ptr != C_NULL
ccall(($fnc, :libgit2), Void, (Ptr{$ref},), obj.ptr)
obj.ptr = C_NULL
Expand Down

0 comments on commit 52d72cb

Please sign in to comment.