Skip to content

Commit

Permalink
Merge pull request JuliaLang#13804 from JuliaLang/jcb/fixcheckoutmaster
Browse files Browse the repository at this point in the history
Fix bug when cloned package does not have HEAD ref
  • Loading branch information
jakebolewski committed Nov 4, 2015
2 parents 32d0711 + fe3e81e commit a205994
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 7 deletions.
6 changes: 4 additions & 2 deletions base/libgit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ include("libgit2/status.jl")
include("libgit2/tree.jl")
include("libgit2/callbacks.jl")

using .Error

immutable State
head::Oid
index::Oid
Expand Down Expand Up @@ -371,7 +373,7 @@ function merge!(repo::GitRepo;
fheads = fetchheads(repo)
filter!(fh->fh.ismerge, fheads)
if isempty(fheads)
throw(Error.GitError(Error.Merge,Error.ERROR,
throw(GitError(Error.Merge, Error.ERROR,
"There is no fetch reference for this branch."))
end
map(fh->GitAnnotated(repo,fh), fheads)
Expand All @@ -385,7 +387,7 @@ function merge!(repo::GitRepo;
end
else # try to get tracking remote branch for the head
if !isattached(repo)
throw(Error.GitError(Error.Merge, Error.ERROR,
throw(GitError(Error.Merge, Error.ERROR,
"There is no tracking information for the current branch."))
end
with(upstream(head_ref)) do tr_brn_ref
Expand Down
2 changes: 2 additions & 0 deletions base/libgit2/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module Error

export GitError

@enum(Code, GIT_OK = Cint(0), # no error
ERROR = Cint(-01), # generic error
ENOTFOUND = Cint(-03), # requested object could not be found
Expand Down
2 changes: 0 additions & 2 deletions base/libgit2/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ function Base.count(idx::GitIndex)
end

function Base.getindex(idx::GitIndex, i::Csize_t)
# return ccall((:git_index_get_byindex, :libgit2), Ptr{Void},
# (Ptr{Void}, Csize_t), idx.ptr, i)
ie_ptr = ccall((:git_index_get_byindex, :libgit2), Ptr{Void},
(Ptr{Void}, Csize_t), idx.ptr, i-1)
ie_ptr == C_NULL && return nothing
Expand Down
2 changes: 1 addition & 1 deletion base/libgit2/repository.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function cleanup(r::GitRepo)
end
end

function init(path::AbstractString, bare::Cuint = Cuint(0))
function init(path::AbstractString, bare::Bool=false)
repo_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
@check ccall((:git_repository_init, :libgit2), Cint,
(Ptr{Ptr{Void}}, Cstring, Cuint), repo_ptr_ptr, path, bare)
Expand Down
13 changes: 12 additions & 1 deletion base/pkg/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,18 @@ function installed_version(pkg::AbstractString, prepo::LibGit2.GitRepo, avail::D
ispath(pkg,".git") || return typemin(VersionNumber)

# get package repo head hash
head = string(LibGit2.head_oid(prepo))
local head
try
head = string(LibGit2.head_oid(prepo))
catch ex
# refs/heads/master does not exist
if isa(ex,LibGit2.GitError) &&
ex.code == LibGit2.Error.EUNBORNBRANCH
head = ""
else
rethrow(ex)
end
end
isempty(head) && return typemin(VersionNumber)

vers = collect(keys(filter((ver,info)->info.sha1==head, avail)))
Expand Down
2 changes: 1 addition & 1 deletion test/libgit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ temp_dir() do dir

@testset "bare" begin
path = joinpath(dir, "Example.Bare")
repo = LibGit2.init(path, Cuint(1))
repo = LibGit2.init(path, true)
try
@test isdir(path)
@test isfile(joinpath(path, LibGit2.Consts.HEAD_FILE))
Expand Down
11 changes: 11 additions & 0 deletions test/pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,15 @@ temp_pkg_dir() do
Pkg.installed("NOTGIT") == typemin(VersionNumber)
Pkg.installed()["NOTGIT"] == typemin(VersionNumber)
end

begin
# don't bork when a Pkg repo is bare, issue #13804
pth = joinpath(Pkg.dir(), "BAREGIT")
mkdir(pth)
# create a bare repo (isbare = true)
repo = LibGit2.init(pth, true)
@test repo.ptr != C_NULL
finalize(repo)
Pkg.update()
end
end

0 comments on commit a205994

Please sign in to comment.