diff options
author | Robert Speicher <robert@gitlab.com> | 2016-01-28 23:30:21 +0300 |
---|---|---|
committer | Robert Speicher <robert@gitlab.com> | 2016-01-28 23:30:21 +0300 |
commit | 0e79b5f3967928a576d68d34c0acc1e2aba6c02a (patch) | |
tree | f51bdf5524a64687a4f924f2f256fb68de5189c1 /lib | |
parent | 789e04835257b8d6be19eaab5da0e8f7ebd74153 (diff) | |
parent | 7d07091c6cedb0386017a9f360c971973ac7a920 (diff) |
Merge branch 'highlight-blame' into 'master'
Fix highlighting in blame view.
See merge request !2630
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/blame.rb | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/gitlab/blame.rb b/lib/gitlab/blame.rb new file mode 100644 index 00000000000..313e6b9fc03 --- /dev/null +++ b/lib/gitlab/blame.rb @@ -0,0 +1,54 @@ +module Gitlab + class Blame + attr_accessor :blob, :commit + + def initialize(blob, commit) + @blob = blob + @commit = commit + end + + def groups(highlight: true) + prev_sha = nil + groups = [] + current_group = nil + + i = 0 + blame.each do |commit, line| + commit = Commit.new(commit, project) + + sha = commit.sha + if prev_sha != sha + groups << current_group if current_group + current_group = { commit: commit, lines: [] } + end + + line = highlighted_lines[i].html_safe if highlight + current_group[:lines] << line + + prev_sha = sha + i += 1 + end + groups << current_group if current_group + + groups + end + + private + + def blame + @blame ||= Gitlab::Git::Blame.new(repository, @commit.id, @blob.path) + end + + def highlighted_lines + @highlighted_lines ||= Gitlab::Highlight.highlight(@blob.name, @blob.data).lines + end + + def project + commit.project + end + + def repository + project.repository + end + end +end |