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 13  
14 14 if commit
15 15 commit = Commit.new(commit)
16   - commit = CommitDecorator.decorate(commit)
17 16 line_notes = project.notes.for_commit_id(commit.id).inline
18 17  
19 18 result[:commit] = commit
... ...
app/mailers/emails/notes.rb
... ... @@ -3,7 +3,6 @@ module Emails
3 3 def note_commit_email(recipient_id, note_id)
4 4 @note = Note.find(note_id)
5 5 @commit = @note.noteable
6   - @commit = CommitDecorator.decorate(@commit)
7 6 @project = @note.project
8 7 mail(to: recipient(recipient_id), subject: subject("note for commit #{@commit.short_id}", @commit.title))
9 8 end
... ...
app/models/commit.rb
... ... @@ -10,10 +10,6 @@ class Commit
10 10  
11 11 attr_accessor :raw
12 12  
13   - def self.decorate(commits)
14   - commits.map { |c| Commit.new(c) }
15   - end
16   -
17 13 def initialize(raw_commit)
18 14 raise "Nil as raw commit passed" unless raw_commit
19 15  
... ... @@ -24,7 +20,54 @@ class Commit
24 20 @raw.id
25 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 64 def method_missing(m, *args, &block)
28 65 @raw.send(m, *args, &block)
29 66 end
  67 +
  68 + def respond_to?(method)
  69 + return true if @raw.respond_to?(method)
  70 +
  71 + super
  72 + end
30 73 end
... ...
app/models/merge_request.rb
... ... @@ -152,7 +152,17 @@ class MergeRequest &lt; ActiveRecord::Base
152 152 end
153 153  
154 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 166 end
157 167  
158 168 def probably_merged?
... ... @@ -171,7 +181,6 @@ class MergeRequest &lt; ActiveRecord::Base
171 181 def unmerged_commits
172 182 self.project.repository.
173 183 commits_between(self.target_branch, self.source_branch).
174   - map {|c| Commit.new(c)}.
175 184 sort_by(&:created_at).
176 185 reverse
177 186 end
... ...
app/models/project.rb
... ... @@ -142,7 +142,7 @@ class Project &lt; ActiveRecord::Base
142 142  
143 143 def repository
144 144 if path
145   - @repository ||= Gitlab::Git::Repository.new(path_with_namespace, default_branch)
  145 + @repository ||= Repository.new(path_with_namespace, default_branch)
146 146 else
147 147 nil
148 148 end
... ...
app/models/tree.rb
... ... @@ -26,4 +26,12 @@ class Tree
26 26 def empty?
27 27 data.blank?
28 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 37 end
... ...
lib/extracts_path.rb
... ... @@ -101,10 +101,8 @@ module ExtractsPath
101 101 # It is used "@project.repository.commits(@ref, @path, 1, 0)",
102 102 # because "@project.repository.commit(@ref)" returns wrong commit when @ref is tag name.
103 103 @commit = @project.repository.commits(@ref, @path, 1, 0).first
104   - @commit = CommitDecorator.decorate(@commit)
105 104  
106 105 @tree = Tree.new(@commit.tree, @ref, @path)
107   - @tree = TreeDecorator.new(@tree)
108 106  
109 107 raise InvalidPathError if @tree.invalid?
110 108 rescue RuntimeError, NoMethodError, InvalidPathError
... ...