commit.rb 5.33 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
11
  participant :author, :committer, :notes
Saito's avatar
Saito committed
12

13 14
  attr_accessor :project

15
  # Safe amount of changes (files and lines) in one commit to render
16 17
  # Used to prevent 500 error on huge commits by suppressing diff
  #
18
  # User can force display of diff above this size
19 20
  DIFF_SAFE_FILES  = 100 unless defined?(DIFF_SAFE_FILES)
  DIFF_SAFE_LINES  = 5000 unless defined?(DIFF_SAFE_LINES)
21

22
  # Commits above this size will not be rendered in HTML
23 24
  DIFF_HARD_LIMIT_FILES = 1000 unless defined?(DIFF_HARD_LIMIT_FILES)
  DIFF_HARD_LIMIT_LINES = 50000 unless defined?(DIFF_HARD_LIMIT_LINES)
25

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

37 38
    # Calculate number of lines to render for diffs
    def diff_line_count(diffs)
39
      diffs.reduce(0) { |sum, d| sum + d.diff.lines.count }
40
    end
41

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

48
  attr_accessor :raw
Nihad Abbasov's avatar
Nihad Abbasov committed
49

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

53
    @raw = raw_commit
54
    @project = project
55
  end
56

57 58 59 60
  def id
    @raw.id
  end

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

65 66 67 68 69 70 71 72 73 74
  def self.reference_prefix
    '@'
  end

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

81 82 83 84
  def self.link_reference_pattern
    super("commit", /(?<commit>\h{6,40})/)
  end

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

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

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

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

    return no_commit_message if title.blank?

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

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

146 147
  def description?
    description.present?
148 149
  end

Valery Sizov's avatar
Valery Sizov committed
150
  def hook_attrs(with_changed_files: false)
Kirill Zaitsev's avatar
Kirill Zaitsev committed
151 152
    path_with_namespace = project.path_with_namespace

Valery Sizov's avatar
Valery Sizov committed
153
    data = {
Kirill Zaitsev's avatar
Kirill Zaitsev committed
154 155 156 157 158 159 160 161 162
      id: id,
      message: safe_message,
      timestamp: committed_date.xmlschema,
      url: "#{Gitlab.config.gitlab.url}/#{path_with_namespace}/commit/#{id}",
      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 219 220 221 222

  private

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

Valery Sizov's avatar
Valery Sizov committed
223 224 225 226 227 228 229
    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
230 231 232 233 234
      end
    end

    changes
  end
235
end