Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attach finalizers to LibGit2 methods #19660

Merged
merged 7 commits into from
Jan 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1512,4 +1512,8 @@ unsafe_wrap(::Type{String}, p::Cstring, own::Bool=false) = unsafe_wrap(String, c
unsafe_wrap(::Type{String}, p::Cstring, len::Integer, own::Bool=false) =
unsafe_wrap(String, convert(Ptr{UInt8}, p), len, own)

# #19660
@deprecate finalize(sa::LibGit2.StrArrayStruct) close(sa)
@deprecate finalize(sa::LibGit2.Buffer) close(sa)

# End deprecations scheduled for 0.6
4 changes: 2 additions & 2 deletions base/libgit2/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ function mirror_callback(remote::Ptr{Ptr{Void}}, repo_ptr::Ptr{Void},
err != 0 && return Cint(err)

# And set the configuration option to true for the push command
config = GitConfig(GitRepo(repo_ptr))
config = GitConfig(GitRepo(repo_ptr,false))
name_str = unsafe_string(name)
err= try set!(config, "remote.$name_str.mirror", true)
catch -1
finally finalize(config)
finally close(config)
end
err != 0 && return Cint(err)
return Cint(0)
Expand Down
8 changes: 4 additions & 4 deletions base/libgit2/commit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ function commit(repo::GitRepo, msg::AbstractString;
commit_id = commit(repo, refname, msg, auth_sig, comm_sig, tree, parents...)
finally
for parent in parents
finalize(parent)
close(parent)
end
finalize(tree)
finalize(auth_sig)
finalize(comm_sig)
close(tree)
close(auth_sig)
close(comm_sig)
end
return commit_id
end
12 changes: 6 additions & 6 deletions base/libgit2/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ function GitConfig(path::AbstractString,
try
addfile(cfg, path, level, force)
catch ex
finalize(cfg)
throw(ex)
close(cfg)
rethrow(ex)
end
return cfg
end

function GitConfig(r::GitRepo)
function GitConfig(repo::GitRepo)
cfg_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
@check ccall((:git_repository_config, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}), cfg_ptr_ptr, r.ptr)
return GitConfig(cfg_ptr_ptr[])
(Ptr{Ptr{Void}}, Ptr{Void}), cfg_ptr_ptr, repo.ptr)
return GitConfig(repo, cfg_ptr_ptr[])
end

function GitConfig(level::Consts.GIT_CONFIG = Consts.CONFIG_LEVEL_DEFAULT)
Expand All @@ -37,7 +37,7 @@ function GitConfig(level::Consts.GIT_CONFIG = Consts.CONFIG_LEVEL_DEFAULT)
glb_cfg_ptr_ptr, cfg.ptr, Cint(level))
cfg = GitConfig(glb_cfg_ptr_ptr[])
finally
finalize(tmpcfg)
close(tmpcfg)
end
end
return cfg
Expand Down
6 changes: 3 additions & 3 deletions base/libgit2/diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ function diff_tree(repo::GitRepo, tree::GitTree, pathspecs::AbstractString=""; c
diff_ptr_ptr, repo.ptr, tree.ptr, emptypathspec ? C_NULL : Ref(diff_opts))
end
finally
!emptypathspec && finalize(sa)
!emptypathspec && close(sa)
end
return GitDiff(diff_ptr_ptr[])
return GitDiff(repo, diff_ptr_ptr[])
end

function diff_tree(repo::GitRepo, oldtree::GitTree, newtree::GitTree)
diff_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
@check ccall((:git_diff_tree_to_tree, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}, Ptr{Void}, Ptr{Void}, Ptr{DiffOptionsStruct}),
diff_ptr_ptr, repo.ptr, oldtree.ptr, newtree.ptr, C_NULL)
return GitDiff(diff_ptr_ptr[])
return GitDiff(repo, diff_ptr_ptr[])
end

function Base.count(diff::GitDiff)
Expand Down
15 changes: 7 additions & 8 deletions base/libgit2/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function GitIndex(repo::GitRepo)
idx_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
@check ccall((:git_repository_index, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}), idx_ptr_ptr, repo.ptr)
return GitIndex(idx_ptr_ptr[])
return GitIndex(repo, idx_ptr_ptr[])
end

function read!(idx::GitIndex, force::Bool = false)
Expand All @@ -25,9 +25,8 @@ function write_tree!(idx::GitIndex)
end

function owner(idx::GitIndex)
repo_ptr = ccall((:git_index_owner, :libgit2), Ptr{Void},
(Ptr{Void},), idx.ptr)
return GitRepo(repo_ptr)
isnull(idx.nrepo) && throw(GitError(Error.Index, Error.ENOTFOUND, "Index does not have an owning repository."))
return Base.get(idx.nrepo)
end

function read_tree!(idx::GitIndex, tree_id::Oid)
Expand All @@ -37,7 +36,7 @@ function read_tree!(idx::GitIndex, tree_id::Oid)
@check ccall((:git_index_read_tree, :libgit2), Cint,
(Ptr{Void}, Ptr{Void}), idx.ptr, tree.ptr)
finally
finalize(tree)
close(tree)
end
end

Expand All @@ -49,7 +48,7 @@ function add!{T<:AbstractString}(idx::GitIndex, files::T...;
(Ptr{Void}, Ptr{StrArrayStruct}, Cuint, Ptr{Void}, Ptr{Void}),
idx.ptr, Ref(sa), flags, C_NULL, C_NULL)
finally
finalize(sa)
close(sa)
end
end

Expand All @@ -60,7 +59,7 @@ function update!{T<:AbstractString}(idx::GitIndex, files::T...)
(Ptr{Void}, Ptr{StrArrayStruct}, Ptr{Void}, Ptr{Void}),
idx.ptr, Ref(sa), C_NULL, C_NULL)
finally
finalize(sa)
close(sa)
end
end

Expand All @@ -71,7 +70,7 @@ function remove!{T<:AbstractString}(idx::GitIndex, files::T...)
(Ptr{Void}, Ptr{StrArrayStruct}, Ptr{Void}, Ptr{Void}),
idx.ptr, Ref(sa), C_NULL, C_NULL)
finally
finalize(sa)
close(sa)
end
end

Expand Down
75 changes: 40 additions & 35 deletions base/libgit2/libgit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function iscommit(id::AbstractString, repo::GitRepo)
if c === nothing
res = false
else
finalize(c)
close(c)
end
catch
res = false
Expand All @@ -84,11 +84,11 @@ function isdiff(repo::GitRepo, treeish::AbstractString, paths::AbstractString=""
try
diff = diff_tree(repo, tree, paths)
result = count(diff) > 0
finalize(diff)
close(diff)
catch err
result = true
finally
finalize(tree)
close(tree)
end
return result
end
Expand All @@ -110,10 +110,10 @@ function diff_files(repo::GitRepo, branch1::AbstractString, branch2::AbstractStr
push!(files, unsafe_string(delta.new_file.path))
end
end
finalize(diff)
close(diff)
finally
finalize(tree1)
finalize(tree2)
close(tree1)
close(tree2)
end
return files
end
Expand Down Expand Up @@ -163,7 +163,7 @@ function fetch{T<:AbstractString, P<:AbstractCredentials}(repo::GitRepo;
fo = FetchOptions(callbacks=RemoteCallbacks(credentials_cb(), payload))
fetch(rmt, refspecs, msg="from $(url(rmt))", options = fo)
finally
finalize(rmt)
close(rmt)
end
end

Expand All @@ -184,7 +184,7 @@ function push{T<:AbstractString, P<:AbstractCredentials}(repo::GitRepo;
push_opts=PushOptions(callbacks=RemoteCallbacks(credentials_cb(), payload))
push(rmt, refspecs, force=force, options=push_opts)
finally
finalize(rmt)
close(rmt)
end
end

Expand All @@ -194,7 +194,7 @@ function branch(repo::GitRepo)
try
branch(head_ref)
finally
finalize(head_ref)
close(head_ref)
end
end

Expand All @@ -220,7 +220,7 @@ function branch!(repo::GitRepo, branch_name::AbstractString,
tmpcmt = with(peel(GitCommit, branch_rmt_ref)) do hrc
Oid(hrc)
end
finalize(branch_rmt_ref)
close(branch_rmt_ref)
tmpcmt
end
else
Expand All @@ -232,7 +232,7 @@ function branch!(repo::GitRepo, branch_name::AbstractString,
try
new_branch_ref = create_branch(repo, branch_name, cmt, force=force)
finally
finalize(cmt)
close(cmt)
new_branch_ref === nothing && throw(GitError(Error.Object, Error.ERROR, "cannot create branch `$branch_name` with `$commit_id`"))
branch_ref = new_branch_ref
end
Expand Down Expand Up @@ -260,7 +260,7 @@ function branch!(repo::GitRepo, branch_name::AbstractString,
head!(repo, branch_ref)
end
finally
finalize(branch_ref)
close(branch_ref)
end
return
end
Expand Down Expand Up @@ -296,15 +296,15 @@ function checkout!(repo::GitRepo, commit::AbstractString = "";
obj_oid = Oid(peeled)
ref = GitReference(repo, obj_oid, force=force,
msg="libgit2.checkout: moving from $head_name to $(string(obj_oid))")
finalize(ref)
close(ref)

# checkout commit
checkout_tree(repo, peeled, options = opts)
finally
finalize(peeled)
close(peeled)
end
finally
finalize(obj)
close(obj)
end
end

Expand Down Expand Up @@ -335,7 +335,7 @@ function reset!(repo::GitRepo, committish::AbstractString, pathspecs::AbstractSt
try
reset!(repo, Nullable(obj), pathspecs...)
finally
finalize(obj)
close(obj)
end
end

Expand All @@ -347,7 +347,7 @@ function reset!(repo::GitRepo, commit::Oid, mode::Cint = Consts.RESET_MIXED)
try
reset!(repo, obj, mode)
finally
finalize(obj)
close(obj)
end
end

Expand Down Expand Up @@ -433,24 +433,26 @@ function merge!(repo::GitRepo;
return true
else
with(head(repo)) do head_ref
with(upstream(head_ref)) do tr_brn_ref
if tr_brn_ref === nothing
throw(GitError(Error.Merge, Error.ERROR,
"There is no tracking information for the current branch."))
end
tr_brn_ref = upstream(head_ref)
if tr_brn_ref === nothing
throw(GitError(Error.Merge, Error.ERROR,
"There is no tracking information for the current branch."))
end
try
[GitAnnotated(repo, tr_brn_ref)]
finally
close(tr_brn_ref)
end
end
end
end
end

try
merge!(repo, upst_anns, fastforward,
merge_opts=merge_opts,
checkout_opts=checkout_opts)
finally
map(finalize, upst_anns)
map(close, upst_anns)
end
end

Expand All @@ -474,12 +476,15 @@ function rebase!(repo::GitRepo, upstream::AbstractString="")
with(head(repo)) do head_ref
head_ann = GitAnnotated(repo, head_ref)
upst_ann = if isempty(upstream)
with(LibGit2.upstream(head_ref)) do brn_ref
if brn_ref === nothing
throw(GitError(Error.Rebase, Error.ERROR,
"There is no tracking information for the current branch."))
end
brn_ref = LibGit2.upstream(head_ref)
if brn_ref === nothing
throw(GitError(Error.Rebase, Error.ERROR,
"There is no tracking information for the current branch."))
end
try
GitAnnotated(repo, brn_ref)
finally
close(brn_ref)
end
else
GitAnnotated(repo, upstream)
Expand All @@ -497,15 +502,15 @@ function rebase!(repo::GitRepo, upstream::AbstractString="")
abort(rbs)
rethrow(err)
finally
finalize(rbs)
close(rbs)
end
finally
#!isnull(onto_ann) && finalize(get(onto_ann))
finalize(sig)
#!isnull(onto_ann) && close(get(onto_ann))
close(sig)
end
finally
finalize(upst_ann)
finalize(head_ann)
close(upst_ann)
close(head_ann)
end
end
return nothing
Expand Down Expand Up @@ -563,7 +568,7 @@ function transact(f::Function, repo::GitRepo)
restore(state, repo)
rethrow()
finally
finalize(repo)
close(repo)
end
end

Expand Down
Loading