BigW Consortium Gitlab

commit.rb 7.19 KB
Newer Older
1
class Commit
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
Yorick Peterse committed
11 12 13 14

  participant :author
  participant :committer
  participant :notes_with_associations
Saito committed
15

16 17
  attr_accessor :project

18
  DIFF_SAFE_LINES = Gitlab::Git::DiffCollection::DEFAULT_LIMITS[:max_lines]
19

20
  # Commits above this size will not be rendered in HTML
21 22
  DIFF_HARD_LIMIT_FILES = 1000
  DIFF_HARD_LIMIT_LINES = 50000
23

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

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

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

    def max_diff_options
      {
        max_files: DIFF_HARD_LIMIT_FILES,
        max_lines: DIFF_HARD_LIMIT_LINES,
      }
    end
51 52
  end

53
  attr_accessor :raw
54

55
  def initialize(raw_commit, project)
56 57
    raise "Nil as raw commit passed" unless raw_commit

58
    @raw = raw_commit
59
    @project = project
60
  end
61

62 63 64 65
  def id
    @raw.id
  end

Robert Speicher committed
66 67 68 69
  def ==(other)
    (self.class === other) && (raw == other.raw)
  end

70 71 72 73 74 75
  def self.reference_prefix
    '@'
  end

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

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

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

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

106 107 108 109 110
  def diff_line_count
    @diff_line_count ||= Commit::diff_line_count(self.diffs)
    @diff_line_count
  end

111 112 113 114 115 116 117 118 119 120 121 122
  # 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.
123 124
  # In case this first line is longer than 100 characters, it is cut off
  # after 80 characters and ellipses (`&hellp;`) are appended.
125 126 127 128 129
  def title
    title = safe_message

    return no_commit_message if title.blank?

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

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

151 152
  def description?
    description.present?
153 154
  end

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

    if with_changed_files
Valery Sizov committed
168
      data.merge!(repo_changes)
Valery Sizov committed
169 170 171
    end

    data
Kirill Zaitsev committed
172 173
  end

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

180
  def author
181
    @author ||= User.find_by_any_email(author_email.downcase)
182 183 184
  end

  def committer
185
    @committer ||= User.find_by_any_email(committer_email.downcase)
186 187
  end

188 189 190 191 192 193 194 195
  def parents
    @parents ||= parent_ids.map { |id| project.commit(id) }
  end

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

196
  def notes
197 198 199
    project.notes.for_commit_id(self.id)
  end

Yorick Peterse committed
200
  def notes_with_associations
201
    notes.includes(:author)
Yorick Peterse committed
202 203
  end

204 205
  def method_missing(m, *args, &block)
    @raw.send(m, *args, &block)
206
  end
207

208 209
  def respond_to_missing?(method, include_private = false)
    @raw.respond_to?(method, include_private) || super
210
  end
211

212 213 214 215 216
  # Truncate sha to 8 characters
  def short_id
    @raw.short_id(7)
  end

217 218 219 220 221 222 223
  def diff_refs
    Gitlab::Diff::DiffRefs.new(
      base_sha: self.parent_id || self.sha,
      head_sha: self.sha
    )
  end

224 225
  def pipelines
    @pipeline ||= project.pipelines.where(sha: sha)
226 227 228
  end

  def status
229
    return @status if defined?(@status)
230
    @status ||= pipelines.status
231
  end
232

233
  def revert_branch_name
234
    "revert-#{short_id}"
235
  end
Yorick Peterse committed
236

237 238 239
  def cherry_pick_branch_name
    project.repository.next_branch("cherry-pick-#{short_id}", mild: true)
  end
240

241 242 243 244 245 246 247 248
  def revert_description
    if merged_merge_request
      "This reverts merge request #{merged_merge_request.to_reference}"
    else
      "This reverts commit #{sha}"
    end
  end

249
  def revert_message
250
    %Q{Revert "#{title.strip}"\n\n#{revert_description}}
251
  end
252

253
  def reverts_commit?(commit)
254
    description? && description.include?(commit.revert_description)
255 256
  end

257
  def merge_commit?
258 259 260
    parents.size > 1
  end

261 262 263
  def merged_merge_request
    return @merged_merge_request if defined?(@merged_merge_request)

264
    @merged_merge_request = project.merge_requests.find_by(merge_commit_sha: id) if merge_commit?
265 266
  end

267
  def has_been_reverted?(current_user = nil, noteable = self)
Yorick Peterse committed
268 269 270 271 272 273 274
    ext = all_references(current_user)

    noteable.notes_with_associations.system.each do |note|
      note.all_references(current_user, extractor: ext)
    end

    ext.commits.any? { |commit_ref| commit_ref.reverts_commit?(self) }
275 276
  end

277 278 279 280
  def change_type_title
    merged_merge_request ? 'merge request' : 'commit'
  end

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
  # Get the URI type of the given path
  #
  # Used to build URLs to files in the repository in GFM.
  #
  # path - String path to check
  #
  # Examples:
  #
  #   uri_type('doc/README.md') # => :blob
  #   uri_type('doc/logo.png')  # => :raw
  #   uri_type('doc/api')       # => :tree
  #   uri_type('not/found')     # => :nil
  #
  # Returns a symbol
  def uri_type(path)
    entry = @raw.tree.path(path)
    if entry[:type] == :blob
      blob = Gitlab::Git::Blob.new(name: entry[:name])
      blob.image? ? :raw : :blob
    else
      entry[:type]
    end
  rescue Rugged::TreeError
    nil
  end

307 308 309 310 311
  private

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

Valery Sizov committed
312 313 314 315 316 317 318
    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
319 320 321 322 323
      end
    end

    changes
  end
324
end