Commit bbfbff3add4c78ce1256ac3bbe787cc6eb9fe1b9

Authored by Dmitriy Zaporozhets
1 parent b53557ac

Extend models functionality with old decorator methods. Use Repository model

app/contexts/commit_load_context.rb
@@ -13,7 +13,6 @@ class CommitLoadContext < BaseContext @@ -13,7 +13,6 @@ class CommitLoadContext < BaseContext
13 13
14 if commit 14 if commit
15 commit = Commit.new(commit) 15 commit = Commit.new(commit)
16 - commit = CommitDecorator.decorate(commit)  
17 line_notes = project.notes.for_commit_id(commit.id).inline 16 line_notes = project.notes.for_commit_id(commit.id).inline
18 17
19 result[:commit] = commit 18 result[:commit] = commit
app/mailers/emails/notes.rb
@@ -3,7 +3,6 @@ module Emails @@ -3,7 +3,6 @@ module Emails
3 def note_commit_email(recipient_id, note_id) 3 def note_commit_email(recipient_id, note_id)
4 @note = Note.find(note_id) 4 @note = Note.find(note_id)
5 @commit = @note.noteable 5 @commit = @note.noteable
6 - @commit = CommitDecorator.decorate(@commit)  
7 @project = @note.project 6 @project = @note.project
8 mail(to: recipient(recipient_id), subject: subject("note for commit #{@commit.short_id}", @commit.title)) 7 mail(to: recipient(recipient_id), subject: subject("note for commit #{@commit.short_id}", @commit.title))
9 end 8 end
app/models/commit.rb
@@ -10,10 +10,6 @@ class Commit @@ -10,10 +10,6 @@ class Commit
10 10
11 attr_accessor :raw 11 attr_accessor :raw
12 12
13 - def self.decorate(commits)  
14 - commits.map { |c| Commit.new(c) }  
15 - end  
16 -  
17 def initialize(raw_commit) 13 def initialize(raw_commit)
18 raise "Nil as raw commit passed" unless raw_commit 14 raise "Nil as raw commit passed" unless raw_commit
19 15
@@ -24,7 +20,54 @@ class Commit @@ -24,7 +20,54 @@ class Commit
24 @raw.id 20 @raw.id
25 end 21 end
26 22
  23 + # Returns a string describing the commit for use in a link title
  24 + #
  25 + # Example
  26 + #
  27 + # "Commit: Alex Denisov - Project git clone panel"
  28 + def link_title
  29 + "Commit: #{author_name} - #{title}"
  30 + end
  31 +
  32 + # Returns the commits title.
  33 + #
  34 + # Usually, the commit title is the first line of the commit message.
  35 + # In case this first line is longer than 80 characters, it is cut off
  36 + # after 70 characters and ellipses (`&hellp;`) are appended.
  37 + def title
  38 + title = safe_message
  39 +
  40 + return no_commit_message if title.blank?
  41 +
  42 + title_end = title.index(/\n/)
  43 + if (!title_end && title.length > 80) || (title_end && title_end > 80)
  44 + title[0..69] << "&hellip;".html_safe
  45 + else
  46 + title.split(/\n/, 2).first
  47 + end
  48 + end
  49 +
  50 + # Returns the commits description
  51 + #
  52 + # cut off, ellipses (`&hellp;`) are prepended to the commit message.
  53 + def description
  54 + description = safe_message
  55 +
  56 + title_end = description.index(/\n/)
  57 + if (!title_end && description.length > 80) || (title_end && title_end > 80)
  58 + "&hellip;".html_safe << description[70..-1]
  59 + else
  60 + description.split(/\n/, 2)[1].try(:chomp)
  61 + end
  62 + end
  63 +
27 def method_missing(m, *args, &block) 64 def method_missing(m, *args, &block)
28 @raw.send(m, *args, &block) 65 @raw.send(m, *args, &block)
29 end 66 end
  67 +
  68 + def respond_to?(method)
  69 + return true if @raw.respond_to?(method)
  70 +
  71 + super
  72 + end
30 end 73 end
app/models/merge_request.rb
@@ -152,7 +152,17 @@ class MergeRequest &lt; ActiveRecord::Base @@ -152,7 +152,17 @@ class MergeRequest &lt; ActiveRecord::Base
152 end 152 end
153 153
154 def commits 154 def commits
155 - st_commits || [] 155 + if st_commits.present?
  156 + # check if merge request commits are valid
  157 + if st_commits.first.respond_to?(:short_id)
  158 + st_commits
  159 + else
  160 + # if commits are invalid - simply reload it from repo
  161 + reloaded_commits
  162 + end
  163 + else
  164 + []
  165 + end
156 end 166 end
157 167
158 def probably_merged? 168 def probably_merged?
@@ -171,7 +181,6 @@ class MergeRequest &lt; ActiveRecord::Base @@ -171,7 +181,6 @@ class MergeRequest &lt; ActiveRecord::Base
171 def unmerged_commits 181 def unmerged_commits
172 self.project.repository. 182 self.project.repository.
173 commits_between(self.target_branch, self.source_branch). 183 commits_between(self.target_branch, self.source_branch).
174 - map {|c| Commit.new(c)}.  
175 sort_by(&:created_at). 184 sort_by(&:created_at).
176 reverse 185 reverse
177 end 186 end
app/models/project.rb
@@ -142,7 +142,7 @@ class Project &lt; ActiveRecord::Base @@ -142,7 +142,7 @@ class Project &lt; ActiveRecord::Base
142 142
143 def repository 143 def repository
144 if path 144 if path
145 - @repository ||= Gitlab::Git::Repository.new(path_with_namespace, default_branch) 145 + @repository ||= Repository.new(path_with_namespace, default_branch)
146 else 146 else
147 nil 147 nil
148 end 148 end
app/models/tree.rb
@@ -26,4 +26,12 @@ class Tree @@ -26,4 +26,12 @@ class Tree
26 def empty? 26 def empty?
27 data.blank? 27 data.blank?
28 end 28 end
  29 +
  30 + def up_dir?
  31 + path.present?
  32 + end
  33 +
  34 + def readme
  35 + @readme ||= contents.find { |c| c.is_a?(Grit::Blob) and c.name =~ /^readme/i }
  36 + end
29 end 37 end
lib/extracts_path.rb
@@ -101,10 +101,8 @@ module ExtractsPath @@ -101,10 +101,8 @@ module ExtractsPath
101 # It is used "@project.repository.commits(@ref, @path, 1, 0)", 101 # It is used "@project.repository.commits(@ref, @path, 1, 0)",
102 # because "@project.repository.commit(@ref)" returns wrong commit when @ref is tag name. 102 # because "@project.repository.commit(@ref)" returns wrong commit when @ref is tag name.
103 @commit = @project.repository.commits(@ref, @path, 1, 0).first 103 @commit = @project.repository.commits(@ref, @path, 1, 0).first
104 - @commit = CommitDecorator.decorate(@commit)  
105 104
106 @tree = Tree.new(@commit.tree, @ref, @path) 105 @tree = Tree.new(@commit.tree, @ref, @path)
107 - @tree = TreeDecorator.new(@tree)  
108 106
109 raise InvalidPathError if @tree.invalid? 107 raise InvalidPathError if @tree.invalid?
110 rescue RuntimeError, NoMethodError, InvalidPathError 108 rescue RuntimeError, NoMethodError, InvalidPathError