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

Revert "LibGit2: added tree related functions & tests" #17409

Merged
merged 1 commit into from
Jul 14, 2016
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
65 changes: 5 additions & 60 deletions base/libgit2/tree.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# This file is a part of Julia. License is MIT: http://julialang.org/license

"""Traverse the entries in a tree and its subtrees in post or preorder.
"""
Traverse the entries in a tree and its subtrees in post or pre order.

Function parameter should have following signature:

Expand All @@ -15,77 +16,21 @@ function treewalk(f::Function, tree::GitTree, payload=Any[], post::Bool = false)
return cbf_payload
end

"Returns a file name, as a `String`, of a tree entry."
function filename(te::GitTreeEntry)
str = ccall((:git_tree_entry_name, :libgit2), Cstring, (Ptr{Void},), te.ptr)
str == C_NULL && throw(Error.GitError(Error.Tree,
LibGit2.Error.ENOTFOUND, "filename not found"))
return unsafe_string(str)
str != C_NULL && return unsafe_string(str)
return nothing
end

"Returns UNIX file attributes, as a `Cint`, of a tree entry."
function filemode(te::GitTreeEntry)
return ccall((:git_tree_entry_filemode, :libgit2), Cint, (Ptr{Void},), te.ptr)
end

"Returns a `GitAnyObject` which is referenced by a tree entry."

function object(repo::GitRepo, te::GitTreeEntry)
obj_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
@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[])
end

"Returns an object identifier, as a `Oid`, of a tree entry."
function oid(te::GitTreeEntry)
oid_ptr = ccall((:git_tree_entry_id, :libgit2), Ptr{Oid}, (Ptr{Void},), te.ptr)
oid_ptr == C_NULL && throw(Error.GitError(Error.Tree,
LibGit2.Error.ENOTFOUND, "oid not found"))
return Oid(oid_ptr)
end

"""Retrieve a tree entry contained in a tree or in any of its subtrees, given its relative path.

The returned tree entry is owned by the user and must be freed explicitly.
"""
function GitTreeEntry(tree::GitTree, tepath::AbstractString)
te_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
err = ccall((:git_tree_entry_bypath, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ref{Void}, Cstring),
te_ptr_ptr, tree.ptr, tepath)
if err == Cint(Error.ENOTFOUND)
return Nullable{GitTreeEntry}()
elseif err != Cint(Error.GIT_OK)
if te_ptr_ptr[] != C_NULL
finalize(GitTreeEntry(te_ptr_ptr[]))
end
throw(Error.GitError(err))
end
return Nullable(GitTreeEntry(te_ptr_ptr[]))
end

"""Lookup a tree entry by SHA value.

This returns a `GitTreeEntry` that is owned by the `GitTree`.
You don't have to free it, but you must not use it after the `GitTree` is released.
"""
function GitTreeEntry(tree::GitTree, teoid::Oid)
res = ccall((:git_tree_entry_byid, :libgit2), Ptr{Void},
(Ref{Void}, Ref{Oid}),
tree.ptr, Ref(teoid))
res == C_NULL && return Nullable{GitTreeEntry}()
return Nullable(GitTreeEntry(res))
end

"""Lookup a tree entry by its file name.

This returns a `GitTreeEntry` that is owned by the `GitTree`.
You don't have to free it, but you must not use it after the `GitTree` is released.
"""
function lookup(tree::GitTree, fname::AbstractString)
res = ccall((:git_tree_entry_byname, :libgit2), Ptr{Void},
(Ref{Void}, Cstring), tree.ptr, fname)
res == C_NULL && return Nullable{GitTreeEntry}()
return Nullable(GitTreeEntry(res))
end
29 changes: 14 additions & 15 deletions base/libgit2/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -625,25 +625,24 @@ for (typ, ref, sup, fnc) in (
(:GitTag, :Void, :GitObject, nothing)
)

@eval begin
type $typ <: $sup
ptr::Ptr{$ref}
function $typ(ptr::Ptr{$ref})
@assert ptr != C_NULL
obj = new(ptr)
return obj
end
@eval type $typ <: $sup
ptr::Ptr{$ref}
function $typ(ptr::Ptr{$ref})
@assert ptr != C_NULL
obj = new(ptr)
return obj
end
if $fnc !== nothing
function Base.finalize(obj::$typ)
if obj.ptr != C_NULL
ccall(($fnc, :libgit2), Void, (Ptr{$ref},), obj.ptr)
obj.ptr = C_NULL
end
end

if fnc !== nothing
@eval function Base.finalize(obj::$typ)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR but I don't think Base.finalize should be overloaded.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you care to elaborate?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finalize has a well defined meaning of calling the finalizer and remove that from the finalizer list. This overload doesn't do that.

if obj.ptr != C_NULL
ccall(($fnc, :libgit2), Void, (Ptr{$ref},), obj.ptr)
obj.ptr = C_NULL
end
Base.finalize(obj::Nullable{$typ}) = !isnull(obj) && Base.finalize(Base.get(obj))
end
end

end

# Structure has the same layout as SignatureStruct
Expand Down
53 changes: 0 additions & 53 deletions test/libgit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -443,59 +443,6 @@ mktempdir() do dir
end
#end

#@testset "tree" begin
function addfile(root::Cstring, tentry::Ptr{Void}, payload::Ptr{Void})
strarr = unsafe_pointer_to_objref(payload)
sroot = unsafe_string(root)
te = LibGit2.GitTreeEntry(tentry)
fmode = LibGit2.filemode(te)
fname = LibGit2.filename(te)
if fmode == Cint(LibGit2.Consts.FILEMODE_BLOB)
push!(strarr, fname)
end
return zero(Cint)
end

repo = LibGit2.GitRepo(test_repo)
head = LibGit2.head(repo)
ht = LibGit2.peel(LibGit2.GitTree, head)
try
@test isnull(LibGit2.lookup(ht, "no such name"))
tfte = get(LibGit2.lookup(ht, test_file))

# get tree entry by filename
@test isnull(LibGit2.GitTreeEntry(ht, "no such name"))
@test LibGit2.filename(tfte) == test_file
@test LibGit2.filemode(tfte) == Cint(LibGit2.Consts.FILEMODE_BLOB)

tfoid = LibGit2.oid(tfte)

@test isnull(LibGit2.GitTreeEntry(ht, LibGit2.Oid()))
tfte2 = LibGit2.GitTreeEntry(ht, tfoid)
try
@test !isnull(tfte2)
@test LibGit2.filename(get(tfte2)) == test_file

tfcontent = LibGit2.with(LibGit2.object(repo, get(tfte2))) do obj
LibGit2.with(LibGit2.peel(LibGit2.GitBlob, obj)) do blob
unsafe_string(convert(Cstring, LibGit2.content(blob)))
end
end
@test startswith(tfcontent, commit_msg1)
finally
finalize(tfte2)
end

entrs = LibGit2.treewalk(addfile, ht, String[])[]
@test length(find(e->e == test_file, entrs)) == 1

finally
finalize(ht)
finalize(head)
finalize(repo)
end
#end

#@testset "commits with revwalk" begin
repo = LibGit2.GitRepo(test_repo)
cache = LibGit2.GitRepo(cache_repo)
Expand Down