diff --git a/CHANGELOG.md b/CHANGELOG.md index c570e416..829dfcd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ # Change Log +## v2.3.2 (2024-11-19) + +[Full Changelog](https://github.com/ruby-git/ruby-git/compare/v2.3.1..v2.3.2) + +Changes since v2.3.1: + +* 7646e38 fix: improve error message for Git::Lib#branches_all + ## v2.3.1 (2024-10-23) [Full Changelog](https://github.com/ruby-git/ruby-git/compare/v2.3.0..v2.3.1) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 83865b85..4128e173 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -362,7 +362,7 @@ def name_rev(commit_ish) # # @example Get the contents of a file with a block # lib.cat_file_contents('README.md') { |f| f.read } # => "This is a README file\n" - # + # # @param object [String] the object whose contents to return # # @return [String] the object contents @@ -641,10 +641,13 @@ def change_head_branch(branch_name) /x def branches_all - command_lines('branch', '-a').map do |line| + lines = command_lines('branch', '-a') + lines.each_with_index.map do |line, line_index| match_data = line.match(BRANCH_LINE_REGEXP) - raise Git::UnexpectedResultError, 'Unexpected branch line format' unless match_data + + raise Git::UnexpectedResultError, unexpected_branch_line_error(lines, line, line_index) unless match_data next nil if match_data[:not_a_branch] || match_data[:detached_ref] + [ match_data[:refname], !match_data[:current].nil?, @@ -654,6 +657,18 @@ def branches_all end.compact end + def unexpected_branch_line_error(lines, line, index) + <<~ERROR + Unexpected line in output from `git branch -a`, line #{index + 1} + + Full output: + #{lines.join("\n ")} + + Line #{index + 1}: + "#{line}" + ERROR + end + def worktrees_all arr = [] directory = '' diff --git a/lib/git/version.rb b/lib/git/version.rb index abc0e3a7..c5710194 100644 --- a/lib/git/version.rb +++ b/lib/git/version.rb @@ -1,5 +1,5 @@ module Git # The current gem version # @return [String] the current gem version. - VERSION='2.3.1' + VERSION='2.3.2' end diff --git a/tests/units/test_lib.rb b/tests/units/test_lib.rb index 74be8dcd..c92959d6 100644 --- a/tests/units/test_lib.rb +++ b/tests/units/test_lib.rb @@ -299,6 +299,39 @@ def test_branches_all assert(branches.select { |b| /master/.match(b[0]) }.size > 0) # has a master branch end + test 'Git::Lib#branches_all with unexpected output from git branches -a' do + # Mock command lines to return unexpected branch data + def @lib.command_lines(*_command) + <<~COMMAND_LINES.split("\n") + * (HEAD detached at origin/master) + this line should result in a Git::UnexpectedResultError + master + remotes/origin/HEAD -> origin/master + remotes/origin/master + COMMAND_LINES + end + + begin + branches = @lib.branches_all + rescue Git::UnexpectedResultError => e + assert_equal(<<~MESSAGE, e.message) + Unexpected line in output from `git branch -a`, line 2 + + Full output: + * (HEAD detached at origin/master) + this line should result in a Git::UnexpectedResultError + master + remotes/origin/HEAD -> origin/master + remotes/origin/master + + Line 2: + " this line should result in a Git::UnexpectedResultError" + MESSAGE + else + raise RuntimeError, 'Expected Git::UnexpectedResultError' + end + end + def test_config_remote config = @lib.config_remote('working') assert_equal('../working.git', config['url'])