File tree 2 files changed +27
-2
lines changed
2 files changed +27
-2
lines changed Original file line number Diff line number Diff line change @@ -357,7 +357,11 @@ def change_head_branch(branch_name)
357
357
)
358
358
359
359
# The branch's full refname
360
- (?<refname>[^[[:blank:]]]+)
360
+ (?:
361
+ (?<not_a_branch>\( not[[:blank:]]a[[:blank:]]branch\) ) |
362
+ (?:\( HEAD[[:blank:]]detached[[:blank:]]at[[:blank:]](?<detached_ref>[^\) ]+)\) ) |
363
+ (?<refname>[^[[:blank:]]]+)
364
+ )
361
365
362
366
# Optional symref
363
367
# If this ref is a symbolic reference, this is the ref referenced
@@ -371,13 +375,14 @@ def branches_all
371
375
command_lines ( 'branch' , '-a' ) . map do |line |
372
376
match_data = line . match ( BRANCH_LINE_REGEXP )
373
377
raise GitExecuteError , 'Unexpected branch line format' unless match_data
378
+ next nil if match_data [ :not_a_branch ] || match_data [ :detached_ref ]
374
379
[
375
380
match_data [ :refname ] ,
376
381
!match_data [ :current ] . nil? ,
377
382
!match_data [ :worktree ] . nil? ,
378
383
match_data [ :symref ]
379
384
]
380
- end
385
+ end . compact
381
386
end
382
387
383
388
def worktrees_all
Original file line number Diff line number Diff line change @@ -50,6 +50,26 @@ def setup
50
50
end
51
51
end
52
52
53
+ test 'Git::Base#branchs with detached head' do
54
+ in_temp_dir do
55
+ git = Git . init ( '.' , initial_branch : 'master' )
56
+ File . write ( 'file1.txt' , 'hello world' )
57
+ git . add ( 'file1.txt' )
58
+ git . commit ( 'Initial commit' )
59
+ git . add_tag ( 'v1.0.0' )
60
+ File . write ( 'file2.txt' , 'hello world' )
61
+ git . add ( 'file2.txt' )
62
+ git . commit ( 'Second commit' )
63
+
64
+ # This will put us in a detached head state
65
+ git . checkout ( 'v1.0.0' )
66
+
67
+ branches = assert_nothing_raised { git . branches }
68
+ assert_equal ( 1 , branches . size )
69
+ assert_equal ( 'master' , branches . first . name )
70
+ end
71
+ end
72
+
53
73
def test_branches_local
54
74
bs = @git . branches . local
55
75
assert ( bs . size > 4 )
You can’t perform that action at this time.
0 commit comments