diff --git a/lib/gitlab/blame.rb b/lib/gitlab/blame.rb index ac39daf375f66dae2aa30138d3cbb1b7124e1586..c2ddca469b90bc7deed4e0b2d1f3b3360cb526a3 100644 --- a/lib/gitlab/blame.rb +++ b/lib/gitlab/blame.rb @@ -29,7 +29,7 @@ def groups(highlight: true) current_group = { commit: commit, lines: [], previous_path: previous_path, span: span, lineno: i + 1 } end - current_group[:lines] << (highlight ? highlighted_lines[i].html_safe : line) + current_group[:lines] << (highlight && highlighted_lines[i] ? highlighted_lines[i].html_safe : line) prev_sha = commit.sha i += 1 diff --git a/spec/lib/gitlab/blame_spec.rb b/spec/lib/gitlab/blame_spec.rb index bfe2b7d1360a5a7855372778ad28854c39904456..493fe453f850d2b8ba2a1540223ecba960e040b4 100644 --- a/spec/lib/gitlab/blame_spec.rb +++ b/spec/lib/gitlab/blame_spec.rb @@ -67,6 +67,17 @@ expect(groups[1][:lines][0]).to match(/LC4.*Popen/) expect(groups[1][:lines][1]).to match(/LC5.*extend/) end + + context 'when highlighed lines are misaligned' do + let(:raw_blob) { Gitlab::Git::Blob.new(data: "Test\r\nopen3", path: path, size: 6) } + let(:blob) { Blob.new(raw_blob) } + + it 'returns the correct lines' do + expect(groups.count).to eq(2) + expect(groups[0][:lines][0]).to match(/LC1.*Test/) + expect(groups[0][:lines][1]).to match(/LC2.*open3/) + end + end end end