Skip to content

Commit

Permalink
Minor LibGit2 cleanup (#35466)
Browse files Browse the repository at this point in the history
* Fix spelling of committish

The official git glossary (https://git-scm.com/docs/gitglossary),
spells this commit-ish or committish. Since we can't do the former,
use the latter consistently. We had three different spellings.

* Minor LibGit2 API improvement

Use the _bypath function in GitTree getindex rather than opencoding
it using the tree walker.
  • Loading branch information
Keno authored May 29, 2020
1 parent 7c980c6 commit 3c63934
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 45 deletions.
2 changes: 1 addition & 1 deletion stdlib/LibGit2/src/LibGit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ function merge!(repo::GitRepo;
"There is no fetch reference for this branch."))
end
Base.map(fh->GitAnnotated(repo,fh), fheads)
else # merge commitish
else # merge committish
[GitAnnotated(repo, committish)]
end
else
Expand Down
2 changes: 2 additions & 0 deletions stdlib/LibGit2/src/commit.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

repository(c::GitCommit) = c.owner

"""
message(c::GitCommit, raw::Bool=false)
Expand Down
6 changes: 3 additions & 3 deletions stdlib/LibGit2/src/merge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
GitAnnotated(repo::GitRepo, commit_id::GitHash)
GitAnnotated(repo::GitRepo, ref::GitReference)
GitAnnotated(repo::GitRepo, fh::FetchHead)
GitAnnotated(repo::GitRepo, comittish::AbstractString)
GitAnnotated(repo::GitRepo, committish::AbstractString)
An annotated git commit carries with it information about how it was looked up and
why, so that rebase or merge operations have more information about the context of
Expand Down Expand Up @@ -40,8 +40,8 @@ function GitAnnotated(repo::GitRepo, fh::FetchHead)
return GitAnnotated(repo, ann_ref_ref[])
end

function GitAnnotated(repo::GitRepo, comittish::AbstractString)
obj = GitObject(repo, comittish)
function GitAnnotated(repo::GitRepo, committish::AbstractString)
obj = GitObject(repo, committish)
cmt = peel(GitCommit, obj)
return GitAnnotated(repo, GitHash(cmt))
end
Expand Down
20 changes: 10 additions & 10 deletions stdlib/LibGit2/src/repository.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,32 +263,32 @@ end
peel(obj::GitObject) = peel(GitObject, obj)

"""
LibGit2.GitDescribeResult(commitish::GitObject; kwarg...)
LibGit2.GitDescribeResult(committish::GitObject; kwarg...)
Produce a `GitDescribeResult` of the `commitish` `GitObject`, which
Produce a `GitDescribeResult` of the `committish` `GitObject`, which
contains detailed information about it based on the keyword argument:
* `options::DescribeOptions=DescribeOptions()`
A git description of a `commitish` object looks for the tag (by default, annotated,
although a search of all tags can be performed) which can be reached from `commitish`
which is most recent. If the tag is pointing to `commitish`, then only the tag is
A git description of a `committish` object looks for the tag (by default, annotated,
although a search of all tags can be performed) which can be reached from `committish`
which is most recent. If the tag is pointing to `committish`, then only the tag is
included in the description. Otherwise, a suffix is included which contains the
number of commits between `commitish` and the most recent tag. If there is no such
number of commits between `committish` and the most recent tag. If there is no such
tag, the default behavior is for the description to fail, although this can be
changed through `options`.
Equivalent to `git describe <commitish>`. See [`DescribeOptions`](@ref) for more
Equivalent to `git describe <committish>`. See [`DescribeOptions`](@ref) for more
information.
"""
function GitDescribeResult(commitish::GitObject;
function GitDescribeResult(committish::GitObject;
options::DescribeOptions=DescribeOptions())
ensure_initialized()
result_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
@check ccall((:git_describe_commit, :libgit2), Cint,
(Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{DescribeOptions}),
result_ptr_ptr, commitish.ptr, Ref(options))
return GitDescribeResult(commitish.owner, result_ptr_ptr[])
result_ptr_ptr, committish.ptr, Ref(options))
return GitDescribeResult(committish.owner, result_ptr_ptr[])
end

"""
Expand Down
63 changes: 32 additions & 31 deletions stdlib/LibGit2/src/tree.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

function GitTree(c::GitCommit)
tree_out = Ref{Ptr{Cvoid}}(C_NULL)
@check ccall((:git_commit_tree, :libgit2), Cint, (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}), tree_out, c)
GitTree(repository(c), tree_out[])
end

"""
treewalk(f, tree::GitTree, post::Bool=false)
Expand Down Expand Up @@ -122,14 +128,14 @@ tree_entry = tree[1]
blob = LibGit2.GitBlob(tree_entry)
```
"""
function GitObject(e::GitTreeEntry) end
GitObject(e::GitTreeEntry)
function (::Type{T})(te::GitTreeEntry) where T<:GitObject
ensure_initialized()
repo = repository(te)
obj_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
@check ccall((:git_tree_entry_to_object, :libgit2), Cint,
(Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ref{Nothing}),
obj_ptr_ptr, repo.ptr, te.ptr)
(Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{Cvoid}),
obj_ptr_ptr, repo, te)
return T(repo, obj_ptr_ptr[])
end

Expand All @@ -146,6 +152,23 @@ function Base.show(io::IO, tree::GitTree)
println(io, "Number of entries: ", count(tree))
end

function _getindex(tree::GitTree, target::AbstractString)
if basename(target) == ""
# get rid of any trailing separator
target = dirname(target)
end
if isempty(target) || target == "/"
return tree
end

entry = Ref{Ptr{Cvoid}}(C_NULL)
err = ccall((:git_tree_entry_bypath, :libgit2), Cint, (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Cstring), entry, tree, target)
err == Int(Error.ENOTFOUND) && return nothing
err < 0 && throw(Error.GitError(err))
entry = GitTreeEntry(tree, entry[], true #= N.B.: Most other lookups need false here =#)
return GitObject(entry)
end

"""
getindex(tree::GitTree, target::AbstractString) -> GitObject
Expand All @@ -161,33 +184,11 @@ runtests = subtree["runtests.jl"]
```
"""
function Base.getindex(tree::GitTree, target::AbstractString)
if basename(target) == ""
# get rid of any trailing separator
target = dirname(target)
end
if target == "" || target == "/"
return tree
end
e = _getindex(tree, target)
e === nothing && throw(KeyError(target))
return e
end

local oid = nothing
function _getindex_callback(root::String, entry::GitTreeEntry)::Cint
path = joinpath(root, filename(entry))
if path == target
# we found the target, save the oid and stop the walk
oid = entryid(entry)
# workaround for issue: https://github.com/libgit2/libgit2/issues/4693
ensure_initialized()
ccall((:giterr_set_str, :libgit2), Cvoid,
(Cint, Cstring), Cint(Error.Callback),
"git_tree_walk callback returned -1")
return -1
elseif entrytype(entry) == GitTree && !startswith(target, path)
# this subtree isn't relevant, so skip it
return 1
end
return 0
end
treewalk(_getindex_callback, tree)
oid === nothing && throw(KeyError(target))
return GitObject(repository(tree), oid)
function Base.haskey(tree::GitTree, target::AbstractString)
return _getindex(tree, target) !== nothing
end
2 changes: 2 additions & 0 deletions stdlib/LibGit2/src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@ for (typ, owntyp, sup, cname) in [
return obj
end
end
@eval Base.unsafe_convert(::Type{Ptr{Cvoid}}, x::$typ) = x.ptr
else
@eval mutable struct $typ <: $sup
owner::$owntyp
Expand All @@ -1024,6 +1025,7 @@ for (typ, owntyp, sup, cname) in [
return obj
end
end
@eval Base.unsafe_convert(::Type{Ptr{Cvoid}}, x::$typ) = x.ptr
if isa(owntyp, Expr) && owntyp.args[1] === :Union && owntyp.args[3] === :Nothing
@eval begin
$typ(ptr::Ptr{Cvoid}, fin::Bool=true) = $typ(nothing, ptr, fin)
Expand Down

0 comments on commit 3c63934

Please sign in to comment.