Skip to content

Fix parsing of symbolic refs in Git::Lib#branches_all #640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,37 @@ def change_head_branch(branch_name)
command('symbolic-ref', 'HEAD', "refs/heads/#{branch_name}")
end

BRANCH_LINE_REGEXP = /
^
# Prefix indicates if this branch is checked out. The prefix is one of:
(?:
(?<current>\*[[:blank:]]) | # Current branch (checked out in the current worktree)
(?<worktree>\+[[:blank:]]) | # Branch checked out in a different worktree
[[:blank:]]{2} # Branch not checked out
)

# The branch's full refname
(?<refname>[^[[:blank:]]]+)

# Optional symref
# If this ref is a symbolic reference, this is the ref referenced
(?:
[[:blank:]]->[[:blank:]](?<symref>.*)
)?
$
/x

def branches_all
arr = []
command_lines('branch', '-a').each do |b|
current = (b[0, 2] == '* ')
arr << [b.gsub('* ', '').strip, current]
command_lines('branch', '-a').map do |line|
match_data = line.match(BRANCH_LINE_REGEXP)
raise GitExecuteError, 'Unexpected branch line format' unless match_data
[
match_data[:refname],
!match_data[:current].nil?,
!match_data[:worktree].nil?,
match_data[:symref]
]
end
arr
end

def worktrees_all
Expand Down
25 changes: 22 additions & 3 deletions tests/units/test_branch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,28 @@ def setup
end
end

def test_branches_all
assert(@git.branches[:master].is_a?(Git::Branch))
assert(@git.branches.size > 5)
test 'Git::Base#branches' do
in_temp_dir do
remote_git = Git.init('remote_git', initial_branch: 'master')
File.write('remote_git/file.txt', 'hello world')
remote_git.add('file.txt')
remote_git.commit('Initial commit')
remote_branches = remote_git.branches
assert_equal(1, remote_branches.size)
assert(remote_branches.first.current)
assert_equal('master', remote_branches.first.name)

# Test that remote tracking branches are handled correctly
#
local_git = Git.clone('remote_git/.git', 'local_git')
local_branches = assert_nothing_raised { local_git.branches }
assert_equal(3, local_branches.size)
assert(remote_branches.first.current)
local_branch_refs = local_branches.map(&:full)
assert_include(local_branch_refs, 'master')
assert_include(local_branch_refs, 'remotes/origin/master')
assert_include(local_branch_refs, 'remotes/origin/HEAD')
end
end

def test_branches_local
Expand Down