Skip to content

Commit 7646e38

Browse files
committed
fix: improve error message for Git::Lib#branches_all
When the output from `git branch -a` can not be parsed, return an error that shows the complete output, the line containing the error, and the line with the error.
1 parent affe1a0 commit 7646e38

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

lib/git/lib.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def name_rev(commit_ish)
362362
#
363363
# @example Get the contents of a file with a block
364364
# lib.cat_file_contents('README.md') { |f| f.read } # => "This is a README file\n"
365-
#
365+
#
366366
# @param object [String] the object whose contents to return
367367
#
368368
# @return [String] the object contents
@@ -641,10 +641,13 @@ def change_head_branch(branch_name)
641641
/x
642642

643643
def branches_all
644-
command_lines('branch', '-a').map do |line|
644+
lines = command_lines('branch', '-a')
645+
lines.each_with_index.map do |line, line_index|
645646
match_data = line.match(BRANCH_LINE_REGEXP)
646-
raise Git::UnexpectedResultError, 'Unexpected branch line format' unless match_data
647+
648+
raise Git::UnexpectedResultError, unexpected_branch_line_error(lines, line, line_index) unless match_data
647649
next nil if match_data[:not_a_branch] || match_data[:detached_ref]
650+
648651
[
649652
match_data[:refname],
650653
!match_data[:current].nil?,
@@ -654,6 +657,18 @@ def branches_all
654657
end.compact
655658
end
656659

660+
def unexpected_branch_line_error(lines, line, index)
661+
<<~ERROR
662+
Unexpected line in output from `git branch -a`, line #{index + 1}
663+
664+
Full output:
665+
#{lines.join("\n ")}
666+
667+
Line #{index + 1}:
668+
"#{line}"
669+
ERROR
670+
end
671+
657672
def worktrees_all
658673
arr = []
659674
directory = ''

tests/units/test_lib.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,39 @@ def test_branches_all
299299
assert(branches.select { |b| /master/.match(b[0]) }.size > 0) # has a master branch
300300
end
301301

302+
test 'Git::Lib#branches_all with unexpected output from git branches -a' do
303+
# Mock command lines to return unexpected branch data
304+
def @lib.command_lines(*_command)
305+
<<~COMMAND_LINES.split("\n")
306+
* (HEAD detached at origin/master)
307+
this line should result in a Git::UnexpectedResultError
308+
master
309+
remotes/origin/HEAD -> origin/master
310+
remotes/origin/master
311+
COMMAND_LINES
312+
end
313+
314+
begin
315+
branches = @lib.branches_all
316+
rescue Git::UnexpectedResultError => e
317+
assert_equal(<<~MESSAGE, e.message)
318+
Unexpected line in output from `git branch -a`, line 2
319+
320+
Full output:
321+
* (HEAD detached at origin/master)
322+
this line should result in a Git::UnexpectedResultError
323+
master
324+
remotes/origin/HEAD -> origin/master
325+
remotes/origin/master
326+
327+
Line 2:
328+
" this line should result in a Git::UnexpectedResultError"
329+
MESSAGE
330+
else
331+
raise RuntimeError, 'Expected Git::UnexpectedResultError'
332+
end
333+
end
334+
302335
def test_config_remote
303336
config = @lib.config_remote('working')
304337
assert_equal('../working.git', config['url'])

0 commit comments

Comments
 (0)