Skip to content

Commit

Permalink
Merge pull request #20116 from JuliaLang/ksh/rebasereset
Browse files Browse the repository at this point in the history
Have rebase and reset return HEAD oid
  • Loading branch information
kshyatt authored Jan 19, 2017
2 parents 71fa0a6 + 307c92d commit 271b318
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
12 changes: 8 additions & 4 deletions base/libgit2/libgit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,12 @@ function reset!(repo::GitRepo, committish::AbstractString, pathspecs::AbstractSt
# do not remove entries in the index matching the provided pathspecs with empty target commit tree
obj === nothing && throw(GitError(Error.Object, Error.ERROR, "`$committish` not found"))
try
reset!(repo, Nullable(obj), pathspecs...)
head = reset!(repo, Nullable(obj), pathspecs...)
return head
finally
close(obj)
end
return head_oid(repo)
end

""" git reset [--soft | --mixed | --hard] <commit> """
Expand All @@ -345,10 +347,12 @@ function reset!(repo::GitRepo, commit::GitHash, mode::Cint = Consts.RESET_MIXED)
# object must exist for reset
obj === nothing && throw(GitError(Error.Object, Error.ERROR, "Commit `$(string(commit))` object not found"))
try
reset!(repo, obj, mode)
head = reset!(repo, obj, mode)
return head
finally
close(obj)
end
return head_oid(repo)
end

""" git cat-file <commit> """
Expand Down Expand Up @@ -513,7 +517,7 @@ function rebase!(repo::GitRepo, upstream::AbstractString="")
close(head_ann)
end
end
return nothing
return head_oid(repo)
end


Expand Down Expand Up @@ -549,7 +553,7 @@ function snapshot(repo::GitRepo)
end

function restore(s::State, repo::GitRepo)
reset!(repo, Consts.HEAD_FILE, "*") # unstage everything
head = reset!(repo, Consts.HEAD_FILE, "*") # unstage everything
with(GitIndex, repo) do idx
read_tree!(idx, s.work) # move work tree to index
opts = CheckoutOptions(
Expand Down
2 changes: 2 additions & 0 deletions base/libgit2/repository.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ function reset!{T<:AbstractString, S<:GitObject}(repo::GitRepo, obj::Nullable{S}
repo.ptr,
isnull(obj) ? C_NULL: Base.get(obj).ptr,
collect(pathspecs))
return head_oid(repo)
end

"""Sets the current head to the specified commit oid and optionally resets the index and working tree to match."""
Expand All @@ -236,6 +237,7 @@ function reset!(repo::GitRepo, obj::GitObject, mode::Cint;
@check ccall((:git_reset, :libgit2), Cint,
(Ptr{Void}, Ptr{Void}, Cint, Ptr{CheckoutOptions}),
repo.ptr, obj.ptr, mode, Ref(checkout_opts))
return head_oid(repo)
end

function clone(repo_url::AbstractString, repo_path::AbstractString,
Expand Down
18 changes: 10 additions & 8 deletions test/libgit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,9 @@ mktempdir() do dir

# because there was not any file we need to reset branch
head_oid = LibGit2.head_oid(repo)
LibGit2.reset!(repo, head_oid, LibGit2.Consts.RESET_HARD)
new_head = LibGit2.reset!(repo, head_oid, LibGit2.Consts.RESET_HARD)
@test isfile(joinpath(test_repo, test_file))
@test new_head == head_oid

# Detach HEAD - no merge
LibGit2.checkout!(repo, string(commit_oid3))
Expand All @@ -483,8 +484,8 @@ mktempdir() do dir
LibGit2.set!(cfg, "user.email", "BBBB@BBBB.COM")

# Try rebasing on master instead
LibGit2.rebase!(repo, master_branch)
@test LibGit2.head_oid(repo) == head_oid
newhead = LibGit2.rebase!(repo, master_branch)
@test newhead == head_oid

# Switch to the master branch
LibGit2.branch!(repo, master_branch)
Expand Down Expand Up @@ -595,14 +596,14 @@ mktempdir() do dir
@test get(st_new) == get(st_stg)

# try to unstage to HEAD
LibGit2.reset!(repo, LibGit2.Consts.HEAD_FILE, test_file)
new_head = LibGit2.reset!(repo, LibGit2.Consts.HEAD_FILE, test_file)
st_uns = LibGit2.status(repo, test_file)
@test get(st_uns) == get(st_mod)

# reset repo
@test_throws LibGit2.Error.GitError LibGit2.reset!(repo, LibGit2.GitHash(), LibGit2.Consts.RESET_HARD)

LibGit2.reset!(repo, LibGit2.head_oid(repo), LibGit2.Consts.RESET_HARD)
new_head = LibGit2.reset!(repo, LibGit2.head_oid(repo), LibGit2.Consts.RESET_HARD)
open(joinpath(test_repo, test_file), "r") do io
@test read(io)[end] != 0x41
end
Expand Down Expand Up @@ -632,7 +633,8 @@ mktempdir() do dir
LibGit2.branch!(repo, "branch/b")

# squash last 2 commits
LibGit2.reset!(repo, oldhead, LibGit2.Consts.RESET_SOFT)
new_head = LibGit2.reset!(repo, oldhead, LibGit2.Consts.RESET_SOFT)
@test new_head == oldhead
LibGit2.commit(repo, "squash file1 and file2")

# add another file
Expand All @@ -646,10 +648,10 @@ mktempdir() do dir

# switch back and rebase
LibGit2.branch!(repo, "branch/a")
LibGit2.rebase!(repo, "branch/b")
newnewhead = LibGit2.rebase!(repo, "branch/b")

# issue #19624
@test LibGit2.head_oid(repo) == newhead
@test newnewhead == newhead
finally
close(repo)
end
Expand Down

0 comments on commit 271b318

Please sign in to comment.