diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 108b1035..4a685944 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -357,7 +357,11 @@ def change_head_branch(branch_name) ) # The branch's full refname - (?[^[[:blank:]]]+) + (?: + (?\(not[[:blank:]]a[[:blank:]]branch\)) | + (?:\(HEAD[[:blank:]]detached[[:blank:]]at[[:blank:]](?[^\)]+)\)) | + (?[^[[:blank:]]]+) + ) # Optional symref # If this ref is a symbolic reference, this is the ref referenced @@ -371,13 +375,14 @@ def branches_all command_lines('branch', '-a').map do |line| match_data = line.match(BRANCH_LINE_REGEXP) raise GitExecuteError, 'Unexpected branch line format' unless match_data + next nil if match_data[:not_a_branch] || match_data[:detached_ref] [ match_data[:refname], !match_data[:current].nil?, !match_data[:worktree].nil?, match_data[:symref] ] - end + end.compact end def worktrees_all diff --git a/tests/units/test_branch.rb b/tests/units/test_branch.rb index c7a12aee..08707b63 100644 --- a/tests/units/test_branch.rb +++ b/tests/units/test_branch.rb @@ -50,6 +50,26 @@ def setup end end + test 'Git::Base#branchs with detached head' do + in_temp_dir do + git = Git.init('.', initial_branch: 'master') + File.write('file1.txt', 'hello world') + git.add('file1.txt') + git.commit('Initial commit') + git.add_tag('v1.0.0') + File.write('file2.txt', 'hello world') + git.add('file2.txt') + git.commit('Second commit') + + # This will put us in a detached head state + git.checkout('v1.0.0') + + branches = assert_nothing_raised { git.branches } + assert_equal(1, branches.size) + assert_equal('master', branches.first.name) + end + end + def test_branches_local bs = @git.branches.local assert(bs.size > 4)