diff --git a/base/deprecated.jl b/base/deprecated.jl index a265fef82d2d8..b498c0fbd96c7 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -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 diff --git a/base/libgit2/callbacks.jl b/base/libgit2/callbacks.jl index 13f2f88e168de..f46b232ce1435 100644 --- a/base/libgit2/callbacks.jl +++ b/base/libgit2/callbacks.jl @@ -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) diff --git a/base/libgit2/commit.jl b/base/libgit2/commit.jl index 87fbe402114d2..4da704383d2d2 100644 --- a/base/libgit2/commit.jl +++ b/base/libgit2/commit.jl @@ -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 diff --git a/base/libgit2/config.jl b/base/libgit2/config.jl index 8826f5c2bfccb..534025b043aa5 100644 --- a/base/libgit2/config.jl +++ b/base/libgit2/config.jl @@ -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) @@ -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 diff --git a/base/libgit2/diff.jl b/base/libgit2/diff.jl index 02aef2ac3e87e..a1c5669bb8bdf 100644 --- a/base/libgit2/diff.jl +++ b/base/libgit2/diff.jl @@ -18,9 +18,9 @@ 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) @@ -28,7 +28,7 @@ function diff_tree(repo::GitRepo, oldtree::GitTree, newtree::GitTree) @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) diff --git a/base/libgit2/index.jl b/base/libgit2/index.jl index 6ce6d3a886680..d9682b5d057c8 100644 --- a/base/libgit2/index.jl +++ b/base/libgit2/index.jl @@ -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) @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/base/libgit2/libgit2.jl b/base/libgit2/libgit2.jl index e419b29cb39a9..2212a286ef86a 100644 --- a/base/libgit2/libgit2.jl +++ b/base/libgit2/libgit2.jl @@ -63,7 +63,7 @@ function iscommit(id::AbstractString, repo::GitRepo) if c === nothing res = false else - finalize(c) + close(c) end catch res = false @@ -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 @@ -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 @@ -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 @@ -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 @@ -194,7 +194,7 @@ function branch(repo::GitRepo) try branch(head_ref) finally - finalize(head_ref) + close(head_ref) end end @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) @@ -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 @@ -563,7 +568,7 @@ function transact(f::Function, repo::GitRepo) restore(state, repo) rethrow() finally - finalize(repo) + close(repo) end end diff --git a/base/libgit2/merge.jl b/base/libgit2/merge.jl index db64522ba55e4..2769572553d77 100644 --- a/base/libgit2/merge.jl +++ b/base/libgit2/merge.jl @@ -5,7 +5,7 @@ function GitAnnotated(repo::GitRepo, commit_id::Oid) @check ccall((:git_annotated_commit_lookup, :libgit2), Cint, (Ptr{Ptr{Void}}, Ptr{Void}, Ptr{Oid}), ann_ptr_ptr, repo.ptr, Ref(commit_id)) - return GitAnnotated(ann_ptr_ptr[]) + return GitAnnotated(repo, ann_ptr_ptr[]) end function GitAnnotated(repo::GitRepo, ref::GitReference) @@ -13,7 +13,7 @@ function GitAnnotated(repo::GitRepo, ref::GitReference) @check ccall((:git_annotated_commit_from_ref, :libgit2), Cint, (Ptr{Ptr{Void}}, Ptr{Void}, Ptr{Void}), ann_ref_ref, repo.ptr, ref.ptr) - return GitAnnotated(ann_ref_ref[]) + return GitAnnotated(repo, ann_ref_ref[]) end function GitAnnotated(repo::GitRepo, fh::FetchHead) @@ -21,7 +21,7 @@ function GitAnnotated(repo::GitRepo, fh::FetchHead) @check ccall((:git_annotated_commit_from_fetchhead, :libgit2), Cint, (Ptr{Ptr{Void}}, Ptr{Void}, Cstring, Cstring, Ptr{Oid}), ann_ref_ref, repo.ptr, fh.name, fh.url, Ref(fh.oid)) - return GitAnnotated(ann_ref_ref[]) + return GitAnnotated(repo, ann_ref_ref[]) end function GitAnnotated(repo::GitRepo, comittish::AbstractString) @@ -31,7 +31,7 @@ function GitAnnotated(repo::GitRepo, comittish::AbstractString) cmt === nothing && return nothing return GitAnnotated(repo, Oid(cmt)) finally - finalize(obj) + close(obj) end end @@ -65,10 +65,10 @@ function ffmerge!(repo::GitRepo, ann::GitAnnotated) else GitReference(repo, cmt_oid, fullname(head_ref), msg=msg) end - finalize(new_head_ref) + close(new_head_ref) end finally - finalize(cmt) + close(cmt) end return true end diff --git a/base/libgit2/rebase.jl b/base/libgit2/rebase.jl index cf6645693d57d..8a80b7ca9afa6 100644 --- a/base/libgit2/rebase.jl +++ b/base/libgit2/rebase.jl @@ -9,7 +9,7 @@ function GitRebase(repo::GitRepo, branch::GitAnnotated, upstream::GitAnnotated; Ptr{Void}, Ptr{RebaseOptions}), rebase_ptr_ptr, repo.ptr, branch.ptr, upstream.ptr, isnull(onto) ? C_NULL : Base.get(onto).ptr, Ref(opts)) - return GitRebase(rebase_ptr_ptr[]) + return GitRebase(repo, rebase_ptr_ptr[]) end function Base.count(rb::GitRebase) diff --git a/base/libgit2/reference.jl b/base/libgit2/reference.jl index b3513f568d0fc..71ceb09195e71 100644 --- a/base/libgit2/reference.jl +++ b/base/libgit2/reference.jl @@ -5,7 +5,7 @@ function GitReference(repo::GitRepo, refname::AbstractString) @check ccall((:git_reference_lookup, :libgit2), Cint, (Ptr{Ptr{Void}}, Ptr{Void}, Cstring), ref_ptr_ptr, repo.ptr, refname) - return GitReference(ref_ptr_ptr[]) + return GitReference(repo, ref_ptr_ptr[]) end function GitReference(repo::GitRepo, obj_oid::Oid, refname::AbstractString = Consts.HEAD_FILE; @@ -15,7 +15,7 @@ function GitReference(repo::GitRepo, obj_oid::Oid, refname::AbstractString = Con (Ptr{Ptr{Void}}, Ptr{Void}, Ptr{UInt8}, Ptr{Oid}, Cint, Cstring), ref_ptr_ptr, repo.ptr, refname, Ref(obj_oid), Cint(force), isempty(msg) ? C_NULL : msg) - return GitReference(ref_ptr_ptr[]) + return GitReference(repo, ref_ptr_ptr[]) end """ @@ -34,7 +34,7 @@ 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[]) + return GitReference(repo, head_ptr_ptr[]) end function shortname(ref::GitReference) @@ -108,11 +108,11 @@ function peel{T <: GitObject}(::Type{T}, ref::GitReference) return Oid() elseif err != Int(Error.GIT_OK) if obj_ptr_ptr[] != C_NULL - finalize(GitAnyObject(obj_ptr_ptr[])) + close(GitAnyObject(ref.repo, obj_ptr_ptr[])) end throw(Error.GitError(err)) end - return T(obj_ptr_ptr[]) + return T(ref.repo, obj_ptr_ptr[]) end function ref_list(repo::GitRepo) @@ -132,7 +132,7 @@ function create_branch(repo::GitRepo, @check ccall((:git_branch_create, :libgit2), Cint, (Ptr{Ptr{Void}}, Ptr{Void}, Cstring, Ptr{Void}, Cint), ref_ptr_ptr, repo.ptr, bname, commit_obj.ptr, Cint(force)) - return GitReference(ref_ptr_ptr[]) + return GitReference(repo, ref_ptr_ptr[]) end function delete_branch(branch::GitReference) @@ -158,11 +158,11 @@ function lookup_branch(repo::GitRepo, return nothing elseif err != Int(Error.GIT_OK) if ref_ptr_ptr[] != C_NULL - finalize(GitReference(ref_ptr_ptr[])) + close(GitReference(repo, ref_ptr_ptr[])) end throw(Error.GitError(err)) end - return GitReference(ref_ptr_ptr[]) + return GitReference(repo, ref_ptr_ptr[]) end function upstream(ref::GitReference) @@ -174,32 +174,28 @@ function upstream(ref::GitReference) return nothing elseif err != Int(Error.GIT_OK) if ref_ptr_ptr[] != C_NULL - finalize(GitReference(ref_ptr_ptr[])) + close(GitReference(ref.repo, ref_ptr_ptr[])) end throw(Error.GitError(err)) end - return GitReference(ref_ptr_ptr[]) + return GitReference(ref.repo, ref_ptr_ptr[]) end -function owner(ref::GitReference) - repo_ptr = ccall((:git_reference_owner, :libgit2), Ptr{Void}, - (Ptr{Void},), ref.ptr) - return GitRepo(repo_ptr) -end +owner(ref::GitReference) = ref.repo function target!(ref::GitReference, new_oid::Oid; msg::AbstractString="") ref_ptr_ptr = Ref{Ptr{Void}}(C_NULL) @check ccall((:git_reference_set_target, :libgit2), Cint, (Ptr{Ptr{Void}}, Ptr{Void}, Ptr{Oid}, Cstring), ref_ptr_ptr, ref.ptr, Ref(new_oid), isempty(msg) ? C_NULL : msg) - return GitReference(ref_ptr_ptr[]) + return GitReference(ref.repo, ref_ptr_ptr[]) end -function GitBranchIter(r::GitRepo, flags::Cint=Cint(Consts.BRANCH_LOCAL)) +function GitBranchIter(repo::GitRepo, flags::Cint=Cint(Consts.BRANCH_LOCAL)) bi_ptr = Ref{Ptr{Void}}(C_NULL) @check ccall((:git_branch_iterator_new, :libgit2), Cint, - (Ptr{Ptr{Void}}, Ptr{Void}, Cint), bi_ptr, r.ptr, flags) - return GitBranchIter(bi_ptr[]) + (Ptr{Ptr{Void}}, Ptr{Void}, Cint), bi_ptr, repo.ptr, flags) + return GitBranchIter(repo, bi_ptr[]) end function Base.start(bi::GitBranchIter) @@ -209,7 +205,7 @@ function Base.start(bi::GitBranchIter) (Ptr{Ptr{Void}}, Ptr{Cint}, Ptr{Void}), ref_ptr_ptr, btype, bi.ptr) err != Int(Error.GIT_OK) && return (nothing, -1, true) - return (GitReference(ref_ptr_ptr[]), btype[], false) + return (GitReference(bi.repo, ref_ptr_ptr[]), btype[], false) end Base.done(bi::GitBranchIter, state) = Bool(state[3]) @@ -221,7 +217,7 @@ function Base.next(bi::GitBranchIter, state) (Ptr{Ptr{Void}}, Ptr{Cint}, Ptr{Void}), ref_ptr_ptr, btype, bi.ptr) err != Int(Error.GIT_OK) && return (state[1:2], (nothing, -1, true)) - return (state[1:2], (GitReference(ref_ptr_ptr[]), btype[], false)) + return (state[1:2], (GitReference(bi.repo, ref_ptr_ptr[]), btype[], false)) end Base.iteratorsize(::Type{GitBranchIter}) = Base.SizeUnknown() diff --git a/base/libgit2/remote.jl b/base/libgit2/remote.jl index 3000ba8c9e0f2..16a342405b1e3 100644 --- a/base/libgit2/remote.jl +++ b/base/libgit2/remote.jl @@ -5,7 +5,7 @@ function GitRemote(repo::GitRepo, rmt_name::AbstractString, rmt_url::AbstractStr @check ccall((:git_remote_create, :libgit2), Cint, (Ptr{Ptr{Void}}, Ptr{Void}, Cstring, Cstring), rmt_ptr_ptr, repo.ptr, rmt_name, rmt_url) - return GitRemote(rmt_ptr_ptr[]) + return GitRemote(repo, rmt_ptr_ptr[]) end function GitRemote(repo::GitRepo, rmt_name::AbstractString, rmt_url::AbstractString, fetch_spec::AbstractString) @@ -13,7 +13,7 @@ function GitRemote(repo::GitRepo, rmt_name::AbstractString, rmt_url::AbstractStr @check ccall((:git_remote_create_with_fetchspec, :libgit2), Cint, (Ptr{Ptr{Void}}, Ptr{Void}, Cstring, Cstring, Cstring), rmt_ptr_ptr, repo.ptr, rmt_name, rmt_url, fetch_spec) - return GitRemote(rmt_ptr_ptr[]) + return GitRemote(repo, rmt_ptr_ptr[]) end function GitRemoteAnon(repo::GitRepo, url::AbstractString) @@ -21,7 +21,7 @@ function GitRemoteAnon(repo::GitRepo, url::AbstractString) @check ccall((:git_remote_create_anonymous, :libgit2), Cint, (Ptr{Ptr{Void}}, Ptr{Void}, Cstring), rmt_ptr_ptr, repo.ptr, url) - return GitRemote(rmt_ptr_ptr[]) + return GitRemote(repo, rmt_ptr_ptr[]) end function get(::Type{GitRemote}, repo::GitRepo, rmt_name::AbstractString) @@ -29,7 +29,7 @@ function get(::Type{GitRemote}, repo::GitRepo, rmt_name::AbstractString) @check ccall((:git_remote_lookup, :libgit2), Cint, (Ptr{Ptr{Void}}, Ptr{Void}, Cstring), rmt_ptr_ptr, repo.ptr, rmt_name) - return GitRemote(rmt_ptr_ptr[]) + return GitRemote(repo, rmt_ptr_ptr[]) end function url(rmt::GitRemote) @@ -45,7 +45,7 @@ function fetch_refspecs(rmt::GitRemote) (Ptr{LibGit2.StrArrayStruct}, Ptr{Void}), sa_ref, rmt.ptr) convert(Vector{AbstractString}, sa_ref[]) finally - finalize(sa_ref[]) + close(sa_ref[]) end end @@ -56,7 +56,7 @@ function push_refspecs(rmt::GitRemote) (Ptr{LibGit2.StrArrayStruct}, Ptr{Void}), sa_ref, rmt.ptr) convert(Vector{AbstractString}, sa_ref[]) finally - finalize(sa_ref[]) + close(sa_ref[]) end end @@ -71,7 +71,7 @@ function fetch{T<:AbstractString}(rmt::GitRemote, refspecs::Vector{T}; (Ptr{Void}, Ptr{StrArrayStruct}, Ptr{FetchOptions}, Cstring), rmt.ptr, no_refs ? C_NULL : Ref(sa), Ref(options), msg) finally - !no_refs && finalize(sa) + !no_refs && close(sa) end end @@ -85,6 +85,6 @@ function push{T<:AbstractString}(rmt::GitRemote, refspecs::Vector{T}; (Ptr{Void}, Ptr{StrArrayStruct}, Ptr{PushOptions}), rmt.ptr, no_refs ? C_NULL : Ref(sa), Ref(options)) finally - !no_refs && finalize(sa) + !no_refs && close(sa) end end diff --git a/base/libgit2/repository.jl b/base/libgit2/repository.jl index ea81c04fe499c..60fe43f0968c7 100644 --- a/base/libgit2/repository.jl +++ b/base/libgit2/repository.jl @@ -6,7 +6,7 @@ function GitRepo(path::AbstractString) (Ptr{Ptr{Void}}, Cstring), repo_ptr_ptr, path) if err != Int(Error.GIT_OK) if repo_ptr_ptr[] != C_NULL - finalize(GitRepo(repo_ptr_ptr[])) + close(GitRepo(repo_ptr_ptr[])) end throw(Error.GitError(err)) end @@ -21,7 +21,7 @@ function GitRepoExt(path::AbstractString, flags::Cuint = Cuint(Consts.REPOSITORY repo_ptr_ptr, path, flags, separator) if err != Int(Error.GIT_OK) if repo_ptr_ptr[] != C_NULL - finalize(GitRepo(repo_ptr_ptr[])) + close(GitRepo(repo_ptr_ptr[])) end throw(Error.GitError(err)) end @@ -46,7 +46,7 @@ function head_oid(repo::GitRepo) try return Oid(head_ref) finally - finalize(head_ref) + close(head_ref) end end @@ -74,7 +74,7 @@ function revparse(repo::GitRepo, objname::AbstractString) err = ccall((:git_revparse_single, :libgit2), Cint, (Ptr{Ptr{Void}}, Ptr{Void}, Cstring), obj_ptr_ptr, repo.ptr, objname) err != 0 && return nothing - return GitAnyObject(obj_ptr_ptr[]) + return GitAnyObject(repo, obj_ptr_ptr[]) end """ Returns id of a found object """ @@ -82,11 +82,11 @@ function revparseid(repo::GitRepo, objname::AbstractString) obj = revparse(repo, objname) obj === nothing && return Oid() oid = Oid(obj.ptr) - finalize(obj) + close(obj) return oid end -function get{T <: GitObject}(::Type{T}, r::GitRepo, oid::Oid, oid_size::Int=OID_HEXSZ) +function get{T <: GitObject}(::Type{T}, repo::GitRepo, oid::Oid, oid_size::Int=OID_HEXSZ) id_ptr = Ref(oid) obj_ptr_ptr = Ref{Ptr{Void}}(C_NULL) git_otype = getobjecttype(T) @@ -94,25 +94,25 @@ function get{T <: GitObject}(::Type{T}, r::GitRepo, oid::Oid, oid_size::Int=OID_ err = if oid_size != OID_HEXSZ ccall((:git_object_lookup_prefix, :libgit2), Cint, (Ptr{Ptr{Void}}, Ptr{Void}, Ptr{Oid}, Csize_t, Cint), - obj_ptr_ptr, r.ptr, id_ptr, Csize_t(oid_size), git_otype) + obj_ptr_ptr, repo.ptr, id_ptr, Csize_t(oid_size), 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) + obj_ptr_ptr, repo.ptr, id_ptr, git_otype) end if err == Int(Error.ENOTFOUND) return nothing elseif err != Int(Error.GIT_OK) if obj_ptr_ptr[] != C_NULL - finalize(GitAnyObject(obj_ptr_ptr[])) + close(GitAnyObject(repo, obj_ptr_ptr[])) end throw(Error.GitError(err)) end - return T(obj_ptr_ptr[]) + return T(repo, obj_ptr_ptr[]) end -function get{T <: GitObject}(::Type{T}, r::GitRepo, oid::AbstractString) - return get(T, r, Oid(oid), length(oid)) +function get{T <: GitObject}(::Type{T}, repo::GitRepo, oid::AbstractString) + return get(T, repo, Oid(oid), length(oid)) end function gitdir(repo::GitRepo) @@ -134,11 +134,11 @@ function peel(obj::GitObject, obj_type::Cint) return Oid() elseif err != Int(Error.GIT_OK) if peeled_ptr_ptr[] != C_NULL - finalize(GitAnyObject(peeled_ptr_ptr[])) + close(GitAnyObject(obj.repo, peeled_ptr_ptr[])) end throw(Error.GitError(err)) end - return git_otype(peeled_ptr_ptr[]) + return git_otype(obj.repo, peeled_ptr_ptr[]) end peel{T <: GitObject}(::Type{T}, obj::GitObject) = peel(obj, getobjecttype(T)) diff --git a/base/libgit2/signature.jl b/base/libgit2/signature.jl index 69f477b70d1a3..644dc57619ad6 100644 --- a/base/libgit2/signature.jl +++ b/base/libgit2/signature.jl @@ -16,14 +16,14 @@ function Signature(name::AbstractString, email::AbstractString) (Ptr{Ptr{SignatureStruct}}, Cstring, Cstring), sig_ptr_ptr, name, email) sig = GitSignature(sig_ptr_ptr[]) s = Signature(sig.ptr) - finalize(sig) + close(sig) return s end function Signature(repo::GitRepo) sig = default_signature(repo) s = Signature(sig.ptr) - finalize(sig) + close(sig) return s end diff --git a/base/libgit2/status.jl b/base/libgit2/status.jl index 8adf8ede7c0bb..889f609d9b25f 100644 --- a/base/libgit2/status.jl +++ b/base/libgit2/status.jl @@ -5,7 +5,7 @@ function GitStatus(repo::GitRepo; status_opts=StatusOptions()) @check ccall((:git_status_list_new, :libgit2), Cint, (Ptr{Ptr{Void}}, Ptr{Void}, Ptr{StatusOptions}), stat_ptr_ptr, repo.ptr, Ref(status_opts)) - return GitStatus(stat_ptr_ptr[]) + return GitStatus(repo, stat_ptr_ptr[]) end function Base.length(status::GitStatus) diff --git a/base/libgit2/tree.jl b/base/libgit2/tree.jl index 321bd83708d2c..a814840c98ea5 100644 --- a/base/libgit2/tree.jl +++ b/base/libgit2/tree.jl @@ -32,5 +32,5 @@ function object(repo::GitRepo, te::GitTreeEntry) @check ccall((:git_tree_entry_to_object, :libgit2), Cint, (Ptr{Ptr{Void}}, Ptr{Void}, Ref{Void}), obj_ptr_ptr, repo.ptr, te.ptr) - return GitAnyObject(obj_ptr_ptr[]) + return GitAnyObject(repo, obj_ptr_ptr[]) end diff --git a/base/libgit2/types.jl b/base/libgit2/types.jl index 34ea1ffd425a0..d42ca80708710 100644 --- a/base/libgit2/types.jl +++ b/base/libgit2/types.jl @@ -45,7 +45,7 @@ Matches the [`git_strarray`](https://libgit2.github.com/libgit2/#HEAD/type/git_s strings::Ptr{Cstring} count::Csize_t end -function Base.finalize(sa::StrArrayStruct) +function Base.close(sa::StrArrayStruct) sa_ptr = Ref(sa) ccall((:git_strarray_free, :libgit2), Void, (Ptr{StrArrayStruct},), sa_ptr) return sa_ptr[] @@ -62,7 +62,7 @@ Matches the [`git_buf`](https://libgit2.github.com/libgit2/#HEAD/type/git_buf) s asize::Csize_t size::Csize_t end -function Base.finalize(buf::Buffer) +function Base.close(buf::Buffer) buf_ptr = Ref(buf) ccall((:git_buf_free, :libgit2), Void, (Ptr{Buffer},), buf_ptr) return buf_ptr[] @@ -398,53 +398,94 @@ abstract AbstractGitObject Base.isempty(obj::AbstractGitObject) = (obj.ptr == C_NULL) abstract GitObject <: AbstractGitObject -function Base.finalize(obj::GitObject) - if obj.ptr != C_NULL - ccall((:git_object_free, :libgit2), Void, (Ptr{Void},), obj.ptr) - obj.ptr = C_NULL - end -end -# Common types -for (typ, ref, sup, fnc) in ( - (:GitRemote, :Void, :AbstractGitObject, :(:git_remote_free)), - (:GitRevWalker, :Void, :AbstractGitObject, :(:git_revwalk_free)), - (:GitConfig, :Void, :AbstractGitObject, :(:git_config_free)), - (:GitReference, :Void, :AbstractGitObject, :(:git_reference_free)), - (:GitDiff, :Void, :AbstractGitObject, :(:git_diff_free)), - (:GitIndex, :Void, :AbstractGitObject, :(:git_index_free)), - (:GitRepo, :Void, :AbstractGitObject, :(:git_repository_free)), - (:GitAnnotated, :Void, :AbstractGitObject, :(:git_annotated_commit_free)), - (:GitRebase, :Void, :AbstractGitObject, :(:git_rebase_free)), - (:GitStatus, :Void, :AbstractGitObject, :(:git_status_list_free)), - (:GitBranchIter, :Void, :AbstractGitObject, :(:git_branch_iterator_free)), - (:GitTreeEntry, :Void, :AbstractGitObject, :(:git_tree_entry_free)), - (:GitSignature, :SignatureStruct, :AbstractGitObject, :(:git_signature_free)), - (:GitAnyObject, :Void, :GitObject, nothing), - (:GitCommit, :Void, :GitObject, nothing), - (:GitBlob, :Void, :GitObject, nothing), - (:GitTree, :Void, :GitObject, nothing), - (:GitTag, :Void, :GitObject, nothing) - ) - - @eval type $typ <: $sup - ptr::Ptr{$ref} - function $typ(ptr::Ptr{$ref}) - @assert ptr != C_NULL - obj = new(ptr) - return obj +for (typ, reporef, sup, cname) in [ + (:GitRepo, nothing, :AbstractGitObject, :git_repository), + (:GitTreeEntry, nothing, :AbstractGitObject, :git_tree_entry), + (:GitConfig, :Nullable, :AbstractGitObject, :git_config), + (:GitIndex, :Nullable, :AbstractGitObject, :git_index), + (:GitRemote, :GitRepo, :AbstractGitObject, :git_remote), + (:GitRevWalker, :GitRepo, :AbstractGitObject, :git_revwalk), + (:GitReference, :GitRepo, :AbstractGitObject, :git_reference), + (:GitDiff, :GitRepo, :AbstractGitObject, :git_diff), + (:GitAnnotated, :GitRepo, :AbstractGitObject, :git_annotated_commit), + (:GitRebase, :GitRepo, :AbstractGitObject, :git_rebase), + (:GitStatus, :GitRepo, :AbstractGitObject, :git_status_list), + (:GitBranchIter, :GitRepo, :AbstractGitObject, :git_branch_iterator), + (:GitAnyObject, :GitRepo, :GitObject, :git_object), + (:GitCommit, :GitRepo, :GitObject, :git_commit), + (:GitBlob, :GitRepo, :GitObject, :git_blob), + (:GitTree, :GitRepo, :GitObject, :git_tree), + (:GitTag, :GitRepo, :GitObject, :git_tag)] + + if reporef === nothing + @eval type $typ <: $sup + ptr::Ptr{Void} + function $typ(ptr::Ptr{Void},fin=true) + @assert ptr != C_NULL + obj = new(ptr) + if fin + finalizer(obj, Base.close) + end + return obj + end end - end - - if fnc !== nothing - @eval function Base.finalize(obj::$typ) - if obj.ptr != C_NULL - ccall(($fnc, :libgit2), Void, (Ptr{$ref},), obj.ptr) - obj.ptr = C_NULL + elseif reporef == :Nullable + @eval type $typ <: $sup + nrepo::Nullable{GitRepo} + ptr::Ptr{Void} + function $typ(repo::GitRepo, ptr::Ptr{Void}) + @assert ptr != C_NULL + obj = new(Nullable(repo), ptr) + finalizer(obj, Base.close) + return obj + end + function $typ(ptr::Ptr{Void}) + @assert ptr != C_NULL + obj = new(Nullable{GitRepo}(), ptr) + finalizer(obj, Base.close) + return obj + end + end + elseif reporef == :GitRepo + @eval type $typ <: $sup + repo::GitRepo + ptr::Ptr{Void} + function $typ(repo::GitRepo, ptr::Ptr{Void}) + @assert ptr != C_NULL + obj = new(repo, ptr) + finalizer(obj, Base.close) + return obj end end end + @eval function Base.close(obj::$typ) + if obj.ptr != C_NULL + ccall(($(string(cname, :_free)), :libgit2), Void, (Ptr{Void},), obj.ptr) + obj.ptr = C_NULL + end + end +end +""" + LibGit2.GitSignature + +This is a Julia wrapper around a pointer to a [`git_signature`](https://libgit2.github.com/libgit2/#HEAD/type/git_signature) object. +""" +type GitSignature <: AbstractGitObject + ptr::Ptr{SignatureStruct} + function GitSignature(ptr::Ptr{SignatureStruct}) + @assert ptr != C_NULL + obj = new(ptr) + finalizer(obj, Base.close) + return obj + end +end +function Base.close(obj::GitSignature) + if obj.ptr != C_NULL + ccall((:git_signature_free, :libgit2), Void, (Ptr{SignatureStruct},), obj.ptr) + obj.ptr = C_NULL + end end # Structure has the same layout as SignatureStruct @@ -461,7 +502,7 @@ function with(f::Function, obj) try f(obj) finally - finalize(obj) + close(obj) end end diff --git a/base/libgit2/walker.jl b/base/libgit2/walker.jl index 1d564c3c406db..fa878a414a4c6 100644 --- a/base/libgit2/walker.jl +++ b/base/libgit2/walker.jl @@ -1,10 +1,10 @@ # This file is a part of Julia. License is MIT: http://julialang.org/license -function GitRevWalker(r::GitRepo) +function GitRevWalker(repo::GitRepo) w_ptr = Ref{Ptr{Void}}(C_NULL) @check ccall((:git_revwalk_new, :libgit2), Cint, - (Ptr{Ptr{Void}}, Ptr{Void}), w_ptr, r.ptr) - return GitRevWalker(w_ptr[]) + (Ptr{Ptr{Void}}, Ptr{Void}), w_ptr, repo.ptr) + return GitRevWalker(repo, w_ptr[]) end function Base.start(w::GitRevWalker) @@ -48,11 +48,7 @@ function Base.sort!(w::GitRevWalker; by::Cint = Consts.SORT_NONE, rev::Bool=fals return w end -function repository(w::GitRevWalker) - ptr = ccall((:git_revwalk_repository, :libgit2), Ptr{Void}, (Ptr{Void},), w.ptr) - ptr != C_NULL && return GitRepo(ptr) - return nothing -end +repository(w::GitRevWalker) = w.repo function Base.map(f::Function, walker::GitRevWalker; oid::Oid=Oid(), diff --git a/base/pkg/cache.jl b/base/pkg/cache.jl index fe0247dc4dc3e..2dbeb216085ae 100644 --- a/base/pkg/cache.jl +++ b/base/pkg/cache.jl @@ -66,7 +66,7 @@ function prefetch(pkg::AbstractString, url::AbstractString, sha1s::Vector) end sha1s[!in_cache] finally - finalize(repo) # closing repo opened/created above + close(repo) # closing repo opened/created above end end prefetch(pkg::AbstractString, url::AbstractString, sha1::AbstractString...) = diff --git a/base/pkg/entry.jl b/base/pkg/entry.jl index b39e415544ae4..278b98b915775 100644 --- a/base/pkg/entry.jl +++ b/base/pkg/entry.jl @@ -176,7 +176,7 @@ function status(io::IO, pkg::AbstractString, ver::VersionNumber, fix::Bool) catch err print_with_color(Base.error_color(), io, " broken-repo (unregistered)") finally - finalize(prepo) + close(prepo) end else print_with_color(Base.warn_color(), io, "non-repo (unregistered)") @@ -330,10 +330,10 @@ function pin(pkg::AbstractString, head::AbstractString) # switch head to the branch LibGit2.head!(repo, ref) finally - finalize(ref) + close(ref) end finally - finalize(commit) + close(commit) end end should_resolve && resolve() diff --git a/base/pkg/read.jl b/base/pkg/read.jl index 4542d3d30d2c0..70f6f4b81ce3a 100644 --- a/base/pkg/read.jl +++ b/base/pkg/read.jl @@ -96,7 +96,7 @@ function isfixed(pkg::AbstractString, prepo::LibGit2.GitRepo, avail::Dict=availa end end finally - cache_has_head && LibGit2.finalize(crepo) + cache_has_head && LibGit2.close(crepo) end return res end @@ -160,7 +160,7 @@ function installed_version(pkg::AbstractString, prepo::LibGit2.GitRepo, avail::D string(base) == head && push!(descendants,ver) end finally - cache_has_head && LibGit2.finalize(crepo) + cache_has_head && LibGit2.close(crepo) end both = sort!(intersect(ancestors,descendants)) isempty(both) || warn("$pkg: some versions are both ancestors and descendants of head: $both") diff --git a/base/pkg/write.jl b/base/pkg/write.jl index ff9d9d9ed15e6..c762b5598b3a5 100644 --- a/base/pkg/write.jl +++ b/base/pkg/write.jl @@ -43,7 +43,7 @@ function install(pkg::AbstractString, sha1::AbstractString) fetch(repo, pkg, sha1) checkout(repo, pkg, sha1) finally - finalize(repo) + close(repo) end end diff --git a/test/libgit2-online.jl b/test/libgit2-online.jl index 06847ba5fbbc4..590555a8958c9 100644 --- a/test/libgit2-online.jl +++ b/test/libgit2-online.jl @@ -16,7 +16,7 @@ mktempdir() do dir @test isdir(repo_path) @test isdir(joinpath(repo_path, ".git")) finally - finalize(repo) + close(repo) end end diff --git a/test/libgit2.jl b/test/libgit2.jl index 05007b489e329..236a44c309c68 100644 --- a/test/libgit2.jl +++ b/test/libgit2.jl @@ -43,7 +43,7 @@ end arr = convert(Vector{AbstractString}, sa1) @test arr[1] == p1 finally - finalize(sa1) + close(sa1) end sa2 = LibGit2.StrArrayStruct(p1, p2) @@ -55,9 +55,9 @@ end arr2 = convert(Vector{AbstractString}, sa3) @test arr1[1] == arr2[1] @test arr1[2] == arr2[2] - finalize(sa3) + close(sa3) finally - finalize(sa2) + close(sa2) end end @@ -65,7 +65,7 @@ end sig = LibGit2.Signature("AAA", "AAA@BBB.COM", round(time(), 0), 0) git_sig = convert(LibGit2.GitSignature, sig) sig2 = LibGit2.Signature(git_sig) - finalize(git_sig) + close(git_sig) @test sig.name == sig2.name @test sig.email == sig2.email @test sig.time == sig2.time @@ -159,7 +159,7 @@ mktempdir() do dir @test LibGit2.get(cfg, "tmp.int64", Int64(0)) == Int64(1) @test LibGit2.get(cfg, "tmp.bool", false) == true finally - finalize(cfg) + close(cfg) end end @@ -172,7 +172,7 @@ mktempdir() do dir # set a remote branch branch = "upstream" - LibGit2.GitRemote(repo, branch, repo_url) |> finalize + LibGit2.GitRemote(repo, branch, repo_url) |> close config = joinpath(cache_repo, ".git", "config") lines = split(open(readstring, config, "r"), "\n") @@ -181,9 +181,9 @@ mktempdir() do dir remote = LibGit2.get(LibGit2.GitRemote, repo, branch) @test LibGit2.url(remote) == repo_url @test LibGit2.isattached(repo) - finalize(remote) + close(remote) finally - finalize(repo) + close(repo) end end @@ -195,7 +195,7 @@ mktempdir() do dir @test isfile(joinpath(path, LibGit2.Consts.HEAD_FILE)) @test LibGit2.isattached(repo) finally - finalize(repo) + close(repo) end path = joinpath("garbagefakery", "Example.Bare") @@ -220,7 +220,7 @@ mktempdir() do dir @test LibGit2.isattached(repo) @test LibGit2.remotes(repo) == ["origin"] finally - finalize(repo) + close(repo) end end @testset "bare with remote callback" begin @@ -235,10 +235,10 @@ mktempdir() do dir @test LibGit2.isattached(repo) @test LibGit2.remotes(repo) == ["origin"] finally - finalize(rmt) + close(rmt) end finally - finalize(repo) + close(repo) end end @testset "normal" begin @@ -249,7 +249,7 @@ mktempdir() do dir @test LibGit2.isattached(repo) @test LibGit2.isorphan(repo) finally - finalize(repo) + close(repo) end end end @@ -303,10 +303,10 @@ mktempdir() do dir @test cmtr.email == test_sig.email @test LibGit2.message(cmt) == commit_msg1 finally - finalize(cmt) + close(cmt) end finally - finalize(repo) + close(repo) close(repo_file) end end @@ -334,17 +334,17 @@ mktempdir() do dir @test LibGit2.shortname(tbref) == test_branch @test LibGit2.upstream(tbref) === nothing finally - finalize(tbref) + close(tbref) end finally - finalize(brref) + close(brref) end branches = map(b->LibGit2.shortname(b[1]), LibGit2.GitBranchIter(repo)) @test master_branch in branches @test test_branch in branches finally - finalize(repo) + close(repo) end end @@ -366,7 +366,7 @@ mktempdir() do dir @test sig.email == "BBBB@BBBB.COM" end finally - finalize(repo) + close(repo) end end @@ -402,7 +402,7 @@ mktempdir() do dir @test tag2 ∈ tags @test tag1 ∉ tags finally - finalize(repo) + close(repo) end end @@ -426,7 +426,7 @@ mktempdir() do dir @test status[1].status == LibGit2.Consts.STATUS_WT_NEW close(repo_file) finally - finalize(repo) + close(repo) end end end @@ -467,7 +467,7 @@ mktempdir() do dir LibGit2.branch!(repo, master_branch) finally - finalize(repo) + close(repo) end end @@ -494,11 +494,11 @@ mktempdir() do dir try @test_throws LibGit2.Error.GitError LibGit2.upstream(tag2ref) finally - finalize(tag2ref) + close(tag2ref) end finally - finalize(repo) + close(repo) end end @@ -521,8 +521,8 @@ mktempdir() do dir @test cache_oids[i] == test_oids[i] end finally - finalize(repo) - finalize(cache) + close(repo) + close(cache) end end end @@ -584,7 +584,7 @@ mktempdir() do dir @test read(io)[end] != 0x41 end finally - finalize(repo) + close(repo) end end @@ -628,7 +628,7 @@ mktempdir() do dir # issue #19624 @test LibGit2.head_oid(repo) == newhead finally - finalize(repo) + close(repo) end end @@ -649,7 +649,7 @@ mktempdir() do dir @test !isfile(joinpath(test_repo, "BBB")) @test isfile(joinpath(test_repo, test_file)) finally - finalize(repo) + close(repo) end end @@ -750,7 +750,7 @@ mktempdir() do dir println(f, err) end finally - finalize(repo) + close(repo) end """ # We try to be helpful by desperately looking for diff --git a/test/pkg.jl b/test/pkg.jl index 9ee7e3975ba3d..7b0ecb8bdffc8 100644 --- a/test/pkg.jl +++ b/test/pkg.jl @@ -330,7 +330,7 @@ temp_pkg_dir() do # create a bare repo (isbare = true) repo = LibGit2.init(pth, true) @test repo.ptr != C_NULL - finalize(repo) + close(repo) Pkg.update() end