Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouwe Maan <douwe@selenight.nl>2016-01-28 16:49:21 +0300
committerDouwe Maan <douwe@selenight.nl>2016-01-28 16:49:21 +0300
commitb636f83e5910d10a954464370efe42cc14a097ba (patch)
tree25576ef4d66cff8d40cba4004302a178bfc2ec4e /lib/gitlab/blame.rb
parent4be80f8aa3c585882b4499d6d69dcbf4a97eff29 (diff)
Move blame group logic to dedicated class
Diffstat (limited to 'lib/gitlab/blame.rb')
-rw-r--r--lib/gitlab/blame.rb54
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