Skip to content

Commit

Permalink
keep reference to parent repo
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbyrne committed Jan 7, 2017
1 parent b9228a8 commit 4bf63fe
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 101 deletions.
2 changes: 1 addition & 1 deletion base/libgit2/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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
Expand Down
6 changes: 3 additions & 3 deletions base/libgit2/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ function GitConfig(path::AbstractString,
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 Down
4 changes: 2 additions & 2 deletions base/libgit2/diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ function diff_tree(repo::GitRepo, tree::GitTree, pathspecs::AbstractString=""; c
finally
!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
7 changes: 3 additions & 4 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 Down
6 changes: 3 additions & 3 deletions base/libgit2/merge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ 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)
ann_ref_ref = Ref{Ptr{Void}}(C_NULL)
@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)
ann_ref_ref = Ref{Ptr{Void}}(C_NULL)
@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)
Expand Down
2 changes: 1 addition & 1 deletion base/libgit2/rebase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 17 additions & 21 deletions base/libgit2/reference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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

"""
Expand All @@ -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)
Expand Down Expand Up @@ -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
close(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)
Expand All @@ -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)
Expand All @@ -158,11 +158,11 @@ function lookup_branch(repo::GitRepo,
return nothing
elseif err != Int(Error.GIT_OK)
if ref_ptr_ptr[] != C_NULL
close(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)
Expand All @@ -174,32 +174,28 @@ function upstream(ref::GitReference)
return nothing
elseif err != Int(Error.GIT_OK)
if ref_ptr_ptr[] != C_NULL
close(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)
Expand All @@ -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])
Expand All @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions base/libgit2/remote.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ 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)
rmt_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
@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)
rmt_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
@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)
rmt_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
@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)
Expand Down
20 changes: 10 additions & 10 deletions base/libgit2/repository.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand All @@ -86,33 +86,33 @@ function revparseid(repo::GitRepo, objname::AbstractString)
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)

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
close(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)
Expand All @@ -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
close(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))
Expand Down
2 changes: 1 addition & 1 deletion base/libgit2/status.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion base/libgit2/tree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit 4bf63fe

Please sign in to comment.