From 56f9b3ad0e334695c8944d490ef67c3c3b895fbc Mon Sep 17 00:00:00 2001 From: kshyatt Date: Thu, 19 Jan 2017 08:53:20 -0700 Subject: [PATCH 1/3] upstream and lookup_branch throw if not found --- base/libgit2/libgit2.jl | 36 ++++++++++++++++++------------------ base/libgit2/reference.jl | 20 ++++++++++---------- base/pkg/entry.jl | 10 +++++----- test/libgit2.jl | 8 ++++---- 4 files changed, 37 insertions(+), 37 deletions(-) diff --git a/base/libgit2/libgit2.jl b/base/libgit2/libgit2.jl index 781a3a595f56c..9e35a161f2277 100644 --- a/base/libgit2/libgit2.jl +++ b/base/libgit2/libgit2.jl @@ -219,22 +219,22 @@ function branch!(repo::GitRepo, branch_name::AbstractString, force::Bool=false, # force branch creation set_head::Bool=true) # set as head reference on exit # try to lookup branch first - branch_ref = force ? nothing : lookup_branch(repo, branch_name) - if branch_ref === nothing - branch_rmt_ref = isempty(track) ? nothing : lookup_branch(repo, "$track/$branch_name", true) + branch_ref = Nullable{GitReference}(force ? nothing : lookup_branch(repo, branch_name)) + if isnull(branch_ref) + branch_rmt_ref = Nullable{GitReference}(isempty(track) ? nothing : lookup_branch(repo, "$track/$branch_name", true)) # if commit is empty get head commit oid commit_id = if isempty(commit) - if branch_rmt_ref === nothing + if isnull(branch_rmt_ref) with(head(repo)) do head_ref with(peel(GitCommit, head_ref)) do hrc GitHash(hrc) end end else - tmpcmt = with(peel(GitCommit, branch_rmt_ref)) do hrc + tmpcmt = with(peel(GitCommit, Base.get(branch_rmt_ref))) do hrc GitHash(hrc) end - close(branch_rmt_ref) + close(Base.get(branch_rmt_ref)) tmpcmt end else @@ -244,10 +244,10 @@ function branch!(repo::GitRepo, branch_name::AbstractString, cmt = get(GitCommit, repo, commit_id) new_branch_ref = nothing try - new_branch_ref = create_branch(repo, branch_name, cmt, force=force) + new_branch_ref = Nullable(create_branch(repo, branch_name, cmt, force=force)) finally close(cmt) - new_branch_ref === nothing && throw(GitError(Error.Object, Error.ERROR, "cannot create branch `$branch_name` with `$commit_id`")) + isnull(new_branch_ref) && throw(GitError(Error.Object, Error.ERROR, "cannot create branch `$branch_name` with `$commit_id`")) branch_ref = new_branch_ref end end @@ -257,7 +257,7 @@ function branch!(repo::GitRepo, branch_name::AbstractString, try with(GitConfig, repo) do cfg set!(cfg, "branch.$branch_name.remote", Consts.REMOTE_ORIGIN) - set!(cfg, "branch.$branch_name.merge", name(branch_ref)) + set!(cfg, "branch.$branch_name.merge", name(Base.get(branch_ref))) end catch warn("Please provide remote tracking for branch '$branch_name' in '$(path(repo))'") @@ -266,15 +266,15 @@ function branch!(repo::GitRepo, branch_name::AbstractString, if set_head # checkout selected branch - with(peel(GitTree, branch_ref)) do btree + with(peel(GitTree, Base.get(branch_ref))) do btree checkout_tree(repo, btree) end # switch head to the branch - head!(repo, branch_ref) + head!(repo, Base.get(branch_ref)) end finally - close(branch_ref) + close(Base.get(branch_ref)) end return end @@ -452,14 +452,14 @@ function merge!(repo::GitRepo; else with(head(repo)) do head_ref tr_brn_ref = upstream(head_ref) - if tr_brn_ref === nothing + if isnull(tr_brn_ref) throw(GitError(Error.Merge, Error.ERROR, "There is no tracking information for the current branch.")) end try - [GitAnnotated(repo, tr_brn_ref)] + [GitAnnotated(repo, Base.get(tr_brn_ref))] finally - close(tr_brn_ref) + close(Base.get(tr_brn_ref)) end end end @@ -494,14 +494,14 @@ a `GitError`. This is roughly equivalent to the following command line statement function rebase!(repo::GitRepo, upstream::AbstractString="", newbase::AbstractString="") with(head(repo)) do head_ref head_ann = GitAnnotated(repo, head_ref) - upst_ann = if isempty(upstream) + upst_ann = if isempty(upstream) brn_ref = LibGit2.upstream(head_ref) - if brn_ref === nothing + if isnull(brn_ref) throw(GitError(Error.Rebase, Error.ERROR, "There is no tracking information for the current branch.")) end try - GitAnnotated(repo, brn_ref) + GitAnnotated(repo, get(brn_ref)) finally close(brn_ref) end diff --git a/base/libgit2/reference.jl b/base/libgit2/reference.jl index 5a5677a660bc4..188a7613fa6ac 100644 --- a/base/libgit2/reference.jl +++ b/base/libgit2/reference.jl @@ -169,15 +169,15 @@ function lookup_branch(repo::GitRepo, err = ccall((:git_branch_lookup, :libgit2), Cint, (Ptr{Ptr{Void}}, Ptr{Void}, Ptr{UInt8}, Cint), ref_ptr_ptr, repo.ptr, branch_name, branch_type) - if err == Int(Error.ENOTFOUND) - return nothing - elseif err != Int(Error.GIT_OK) - if ref_ptr_ptr[] != C_NULL + if err != Int(Error.GIT_OK) + if err == Int(Error.ENOTFOUND) + return Nullable{GitReference}() + elseif err != Int(Error.ENOTFOUND) && ref_ptr_ptr[] != C_NULL close(GitReference(repo, ref_ptr_ptr[])) end throw(Error.GitError(err)) end - return GitReference(repo, ref_ptr_ptr[]) + return Nullable{GitReference}(GitReference(repo, ref_ptr_ptr[])) end function upstream(ref::GitReference) @@ -185,15 +185,15 @@ function upstream(ref::GitReference) ref_ptr_ptr = Ref{Ptr{Void}}(C_NULL) err = ccall((:git_branch_upstream, :libgit2), Cint, (Ref{Ptr{Void}}, Ptr{Void},), ref_ptr_ptr, ref.ptr) - if err == Int(Error.ENOTFOUND) - return nothing - elseif err != Int(Error.GIT_OK) - if ref_ptr_ptr[] != C_NULL + if err != Int(Error.GIT_OK) + if err == Int(Error.ENOTFOUND) + return Nullable{GitReference}() + elseif err != Int(Error.ENOTFOUND) && ref_ptr_ptr[] != C_NULL close(GitReference(ref.repo, ref_ptr_ptr[])) end throw(Error.GitError(err)) end - return GitReference(ref.repo, ref_ptr_ptr[]) + return Nullable{GitReference}(GitReference(ref.repo, ref_ptr_ptr[])) end repository(ref::GitReference) = ref.repo diff --git a/base/pkg/entry.jl b/base/pkg/entry.jl index 6444f0393ae5c..9231ea89534f5 100644 --- a/base/pkg/entry.jl +++ b/base/pkg/entry.jl @@ -312,7 +312,7 @@ function pin(pkg::AbstractString, head::AbstractString) end ref = LibGit2.lookup_branch(repo, branch) try - if ref !== nothing + if !isnull(ref) if LibGit2.revparseid(repo, branch) != id throw(PkgError("Package $pkg: existing branch $branch has " * "been edited and doesn't correspond to its original commit")) @@ -320,17 +320,17 @@ function pin(pkg::AbstractString, head::AbstractString) info("Package $pkg: checking out existing branch $branch") else info("Creating $pkg branch $branch") - ref = LibGit2.create_branch(repo, branch, commit) + ref = Nullable(LibGit2.create_branch(repo, branch, commit)) end # checkout selected branch - with(LibGit2.peel(LibGit2.GitTree, ref)) do btree + with(LibGit2.peel(LibGit2.GitTree, get(ref))) do btree LibGit2.checkout_tree(repo, btree) end # switch head to the branch - LibGit2.head!(repo, ref) + LibGit2.head!(repo, get(ref)) finally - close(ref) + close(get(ref)) end finally close(commit) diff --git a/test/libgit2.jl b/test/libgit2.jl index 460405ce44e64..8bf024974d9c5 100644 --- a/test/libgit2.jl +++ b/test/libgit2.jl @@ -324,17 +324,17 @@ mktempdir() do dir @test LibGit2.name(brref) == "refs/heads/master" @test LibGit2.shortname(brref) == master_branch @test LibGit2.ishead(brref) - @test LibGit2.upstream(brref) === nothing + @test isnull(LibGit2.upstream(brref)) @test repo.ptr == LibGit2.repository(brref).ptr @test brnch == master_branch @test LibGit2.headname(repo) == master_branch LibGit2.branch!(repo, test_branch, string(commit_oid1), set_head=false) - @test LibGit2.lookup_branch(repo, test_branch, true) === nothing - tbref = LibGit2.lookup_branch(repo, test_branch, false) + @test isnull(LibGit2.lookup_branch(repo, test_branch, true)) + tbref = Base.get(LibGit2.lookup_branch(repo, test_branch, false)) try @test LibGit2.shortname(tbref) == test_branch - @test LibGit2.upstream(tbref) === nothing + @test isnull(LibGit2.upstream(tbref)) finally close(tbref) end From b46c3912fda116940a2cd8b5cf3c40d577f4196b Mon Sep 17 00:00:00 2001 From: kshyatt Date: Thu, 26 Jan 2017 08:58:08 -0800 Subject: [PATCH 2/3] Add docs --- base/libgit2/reference.jl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/base/libgit2/reference.jl b/base/libgit2/reference.jl index 188a7613fa6ac..1c25596734d4c 100644 --- a/base/libgit2/reference.jl +++ b/base/libgit2/reference.jl @@ -161,6 +161,17 @@ function head!(repo::GitRepo, ref::GitReference) return ref end +""" + lookup_branch(repo::GitRepo, branch_name::AbstractString, remote::Bool=false) -> Nullable{GitReference} + +Determine if the branch specified by `branch_name` exists in the repository `repo`. +If `remote` is `true`, `repo` is assumed to be a remote git repository. Otherwise, it +is part of the local filesystem. + +`lookup_branch` returns a `Nullable`, which will be null if the requested branch does +not exist yet. If the branch does exist, the `Nullable` contains a `GitReference` to +the branch. +""" function lookup_branch(repo::GitRepo, branch_name::AbstractString, remote::Bool=false) @@ -180,6 +191,15 @@ function lookup_branch(repo::GitRepo, return Nullable{GitReference}(GitReference(repo, ref_ptr_ptr[])) end +""" + upstream(ref::GitReference) -> Nullable{GitReference} + +Determine if the branch specified by `ref` has a specified upstream branch. + +`upstream` returns a `Nullable`, which will be null if the requested branch does +not have an upstream counterpart. If the upstream branch does exist, the `Nullable` +contains a `GitReference` to the upstream branch. +""" function upstream(ref::GitReference) isempty(ref) && return nothing ref_ptr_ptr = Ref{Ptr{Void}}(C_NULL) From d0d230617958a9ea12580954af4bce402b457f6b Mon Sep 17 00:00:00 2001 From: kshyatt Date: Thu, 26 Jan 2017 11:00:30 -0800 Subject: [PATCH 3/3] Fix double-nullable --- base/libgit2/libgit2.jl | 6 +++--- base/libgit2/reference.jl | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/base/libgit2/libgit2.jl b/base/libgit2/libgit2.jl index 9e35a161f2277..a71aac856fb3e 100644 --- a/base/libgit2/libgit2.jl +++ b/base/libgit2/libgit2.jl @@ -219,9 +219,9 @@ function branch!(repo::GitRepo, branch_name::AbstractString, force::Bool=false, # force branch creation set_head::Bool=true) # set as head reference on exit # try to lookup branch first - branch_ref = Nullable{GitReference}(force ? nothing : lookup_branch(repo, branch_name)) + branch_ref = force ? Nullable{GitReference}() : lookup_branch(repo, branch_name) if isnull(branch_ref) - branch_rmt_ref = Nullable{GitReference}(isempty(track) ? nothing : lookup_branch(repo, "$track/$branch_name", true)) + branch_rmt_ref = isempty(track) ? Nullable{GitReference}() : lookup_branch(repo, "$track/$branch_name", true) # if commit is empty get head commit oid commit_id = if isempty(commit) if isnull(branch_rmt_ref) @@ -501,7 +501,7 @@ function rebase!(repo::GitRepo, upstream::AbstractString="", newbase::AbstractSt "There is no tracking information for the current branch.")) end try - GitAnnotated(repo, get(brn_ref)) + GitAnnotated(repo, Base.get(brn_ref)) finally close(brn_ref) end diff --git a/base/libgit2/reference.jl b/base/libgit2/reference.jl index 1c25596734d4c..539840f5b868e 100644 --- a/base/libgit2/reference.jl +++ b/base/libgit2/reference.jl @@ -183,7 +183,8 @@ function lookup_branch(repo::GitRepo, if err != Int(Error.GIT_OK) if err == Int(Error.ENOTFOUND) return Nullable{GitReference}() - elseif err != Int(Error.ENOTFOUND) && ref_ptr_ptr[] != C_NULL + end + if ref_ptr_ptr[] != C_NULL close(GitReference(repo, ref_ptr_ptr[])) end throw(Error.GitError(err)) @@ -208,7 +209,8 @@ function upstream(ref::GitReference) if err != Int(Error.GIT_OK) if err == Int(Error.ENOTFOUND) return Nullable{GitReference}() - elseif err != Int(Error.ENOTFOUND) && ref_ptr_ptr[] != C_NULL + end + if ref_ptr_ptr[] != C_NULL close(GitReference(ref.repo, ref_ptr_ptr[])) end throw(Error.GitError(err))