Skip to content

Commit 4409ef2

Browse files
jcouballmblythe86
andauthored
Fix Git::Branch#update_ref (#626)
* Branch-related bugfixes Fixes both #599 and #600 Also fixes argument name of update_ref. I'm assuming it's supposed to be analogous to the command line `git update-ref`, which doesn't directly use a branch name. Signed-off-by: Matthew Blythe <mblythester+git@gmail.com> * Add test for Branch#update_ref Signed-off-by: James Couball <jcouball@yahoo.com> --------- Signed-off-by: Matthew Blythe <mblythester+git@gmail.com> Signed-off-by: James Couball <jcouball@yahoo.com> Co-authored-by: Matthew Blythe <mblythester+git@gmail.com>
1 parent 4c5a6ac commit 4409ef2

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

lib/git/branch.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ def merge(branch = nil, message = nil)
7878
end
7979

8080
def update_ref(commit)
81-
@base.lib.update_ref(@full, commit)
81+
if @remote
82+
@base.lib.update_ref("refs/remotes/#{@remote.name}/#{@name}", commit)
83+
else
84+
@base.lib.update_ref("refs/heads/#{@name}", commit)
85+
end
8286
end
8387

8488
def to_a
@@ -114,7 +118,7 @@ def determine_current
114118
# param [String] name branch full name.
115119
# return [<Git::Remote,NilClass,String>] an Array containing the remote and branch names.
116120
def parse_name(name)
117-
if name.match(/^(?:remotes)?\/([^\/]+)\/(.+)/)
121+
if name.match(/^(?:remotes\/)?([^\/]+)\/(.+)/)
118122
return [Git::Remote.new(@base, $1), $2]
119123
end
120124

lib/git/lib.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,8 +974,8 @@ def commit_tree(tree, opts = {})
974974
command('commit-tree', *arr_opts, redirect: "< #{escape t.path}")
975975
end
976976

977-
def update_ref(branch, commit)
978-
command('update-ref', branch, commit)
977+
def update_ref(ref, commit)
978+
command('update-ref', ref, commit)
979979
end
980980

981981
def checkout_index(opts = {})

tests/units/test_branch.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,22 @@ def test_branch_create_and_switch
9898
assert(git.branch('other_branch').current)
9999
end
100100
end
101+
102+
def test_branch_update_ref
103+
in_temp_dir do |path|
104+
git = Git.init
105+
File.write('foo','rev 1')
106+
git.add('foo')
107+
git.commit('rev 1')
108+
git.branch('testing').create
109+
File.write('foo','rev 2')
110+
git.add('foo')
111+
git.commit('rev 2')
112+
git.branch('testing').update_ref(git.revparse('HEAD'))
113+
114+
# Expect the call to Branch#update_ref to pass the full ref name for the
115+
# of the testing branch to Lib#update_ref
116+
assert_equal(git.revparse('HEAD'), git.revparse('refs/heads/testing'))
117+
end
118+
end
101119
end

0 commit comments

Comments
 (0)