commit.rb 6.29 KB
Newer Older
1
class Commit
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
2
  extend ActiveModel::Naming
3 4

  include ActiveModel::Conversion
5
  include Participable
6
  include Mentionable
7 8
  include Referable
  include StaticModel
9

10
  attr_mentionable :safe_message, pipeline: :single_line
11
  participant :author, :committer, :notes
Saito's avatar
Saito committed
12

13 14
  attr_accessor :project

15
  DIFF_SAFE_LINES = Gitlab::Git::DiffCollection::DEFAULT_LIMITS[:max_lines]
16

17
  # Commits above this size will not be rendered in HTML
18 19
  DIFF_HARD_LIMIT_FILES = 1000
  DIFF_HARD_LIMIT_LINES = 50000
20

21
  class << self
22
    def decorate(commits, project)
23 24 25 26
      commits.map do |commit|
        if commit.kind_of?(Commit)
          commit
        else
27
          self.new(commit, project)
28 29
        end
      end
30
    end
31

32 33
    # Calculate number of lines to render for diffs
    def diff_line_count(diffs)
34
      diffs.reduce(0) { |sum, d| sum + Gitlab::Git::Util.count_lines(d.diff) }
35
    end
36

37
    # Truncate sha to 8 characters
38
    def truncate_sha(sha)
39
      sha[0..7]
40
    end
41 42 43 44 45 46 47

    def max_diff_options
      {
        max_files: DIFF_HARD_LIMIT_FILES,
        max_lines: DIFF_HARD_LIMIT_LINES,
      }
    end
48 49
  end

50
  attr_accessor :raw
Nihad Abbasov's avatar
Nihad Abbasov committed
51

52
  def initialize(raw_commit, project)
53 54
    raise "Nil as raw commit passed" unless raw_commit

55
    @raw = raw_commit
56
    @project = project
57
  end
58

59 60 61 62
  def id
    @raw.id
  end

Robert Speicher's avatar
Robert Speicher committed
63 64 65 66
  def ==(other)
    (self.class === other) && (raw == other.raw)
  end

67 68 69 70 71 72
  def self.reference_prefix
    '@'
  end

  # Pattern used to extract commit references from text
  #
73
  # The SHA can be between 7 and 40 hex characters.
74 75 76
  #
  # This pattern supports cross-project references.
  def self.reference_pattern
77
    @reference_pattern ||= %r{
78
      (?:#{Project.reference_pattern}#{reference_prefix})?
79
      (?<commit>\h{7,40})
80
    }x
81 82
  end

83
  def self.link_reference_pattern
84
    @link_reference_pattern ||= super("commit", /(?<commit>\h{7,40})/)
85 86
  end

87 88
  def to_reference(from_project = nil)
    if cross_project_reference?(from_project)
89 90 91 92 93 94 95
      project.to_reference + self.class.reference_prefix + self.id
    else
      self.id
    end
  end

  def reference_link_text(from_project = nil)
96
    if cross_project_reference?(from_project)
97
      project.to_reference + self.class.reference_prefix + self.short_id
98
    else
99
      self.short_id
100 101 102
    end
  end

103 104 105 106 107
  def diff_line_count
    @diff_line_count ||= Commit::diff_line_count(self.diffs)
    @diff_line_count
  end

108 109 110 111 112 113 114 115 116 117 118 119
  # Returns a string describing the commit for use in a link title
  #
  # Example
  #
  #   "Commit: Alex Denisov - Project git clone panel"
  def link_title
    "Commit: #{author_name} - #{title}"
  end

  # Returns the commits title.
  #
  # Usually, the commit title is the first line of the commit message.
120 121
  # In case this first line is longer than 100 characters, it is cut off
  # after 80 characters and ellipses (`&hellp;`) are appended.
122 123 124 125 126
  def title
    title = safe_message

    return no_commit_message if title.blank?

127
    title_end = title.index("\n")
128
    if (!title_end && title.length > 100) || (title_end && title_end > 100)
129
      title[0..79] << "…"
130
    else
131
      title.split("\n", 2).first
132 133 134 135 136 137 138
    end
  end

  # Returns the commits description
  #
  # cut off, ellipses (`&hellp;`) are prepended to the commit message.
  def description
139
    title_end = safe_message.index("\n")
140 141
    @description ||=
      if (!title_end && safe_message.length > 100) || (title_end && title_end > 100)
142
        "…" << safe_message[80..-1]
143 144 145
      else
        safe_message.split("\n", 2)[1].try(:chomp)
      end
146
  end
147

148 149
  def description?
    description.present?
150 151
  end

Valery Sizov's avatar
Valery Sizov committed
152
  def hook_attrs(with_changed_files: false)
Valery Sizov's avatar
Valery Sizov committed
153
    data = {
Kirill Zaitsev's avatar
Kirill Zaitsev committed
154 155 156
      id: id,
      message: safe_message,
      timestamp: committed_date.xmlschema,
157
      url: Gitlab::UrlBuilder.build(self),
Kirill Zaitsev's avatar
Kirill Zaitsev committed
158 159 160 161 162
      author: {
        name: author_name,
        email: author_email
      }
    }
Valery Sizov's avatar
Valery Sizov committed
163 164

    if with_changed_files
Valery Sizov's avatar
Valery Sizov committed
165
      data.merge!(repo_changes)
Valery Sizov's avatar
Valery Sizov committed
166 167 168
    end

    data
Kirill Zaitsev's avatar
Kirill Zaitsev committed
169 170
  end

171 172
  # Discover issues should be closed when this commit is pushed to a project's
  # default branch.
173
  def closes_issues(current_user = self.committer)
174
    Gitlab::ClosingIssueExtractor.new(project, current_user).closed_by_message(safe_message)
175 176
  end

177
  def author
178
    @author ||= User.find_by_any_email(author_email.downcase)
179 180 181
  end

  def committer
182
    @committer ||= User.find_by_any_email(committer_email.downcase)
183 184
  end

185 186 187 188 189 190 191 192
  def parents
    @parents ||= parent_ids.map { |id| project.commit(id) }
  end

  def parent
    @parent ||= project.commit(self.parent_id) if self.parent_id
  end

193
  def notes
194 195 196
    project.notes.for_commit_id(self.id)
  end

197 198
  def method_missing(m, *args, &block)
    @raw.send(m, *args, &block)
199
  end
200

201 202
  def respond_to_missing?(method, include_private = false)
    @raw.respond_to?(method, include_private) || super
203
  end
204

205 206 207 208 209
  # Truncate sha to 8 characters
  def short_id
    @raw.short_id(7)
  end

210 211 212 213 214 215 216
  def ci_commit
    project.ci_commit(sha)
  end

  def status
    ci_commit.try(:status) || :not_found
  end
217

218
  def revert_branch_name
219
    "revert-#{short_id}"
220
  end
221 222 223 224
  
  def cherry_pick_branch_name
    project.repository.next_branch("cherry-pick-#{short_id}", mild: true)
  end
225

226 227 228 229 230 231 232 233
  def revert_description
    if merged_merge_request
      "This reverts merge request #{merged_merge_request.to_reference}"
    else
      "This reverts commit #{sha}"
    end
  end

234
  def revert_message
235
    %Q{Revert "#{title.strip}"\n\n#{revert_description}}
236
  end
237

238
  def reverts_commit?(commit)
239
    description? && description.include?(commit.revert_description)
240 241
  end

242
  def merge_commit?
243 244 245
    parents.size > 1
  end

246 247 248
  def merged_merge_request
    return @merged_merge_request if defined?(@merged_merge_request)

249
    @merged_merge_request = project.merge_requests.find_by(merge_commit_sha: id) if merge_commit?
250 251
  end

252 253
  def has_been_reverted?(current_user = nil, noteable = self)
    Gitlab::ReferenceExtractor.lazily do
254
      noteable.notes.system.flat_map do |note|
255 256 257 258 259
        note.all_references(current_user).commits
      end
    end.any? { |commit_ref| commit_ref.reverts_commit?(self) }
  end

260 261 262 263
  def change_type_title
    merged_merge_request ? 'merge request' : 'commit'
  end

264 265 266 267 268
  private

  def repo_changes
    changes = { added: [], modified: [], removed: [] }

Valery Sizov's avatar
Valery Sizov committed
269 270 271 272 273 274 275
    diffs.each do |diff|
      if diff.deleted_file
        changes[:removed] << diff.old_path
      elsif diff.renamed_file || diff.new_file
        changes[:added] << diff.new_path
      else
        changes[:modified] << diff.new_path
276 277 278 279 280
      end
    end

    changes
  end
281
end