BigW Consortium Gitlab

commit.rb 1.76 KB
Newer Older
1
class Commit
2
  include ActiveModel::Conversion
3
  include StaticModel
4
  extend ActiveModel::Naming
Saito committed
5

6 7 8 9 10
  # Safe amount of files with diffs in one commit to render
  # Used to prevent 500 error on huge commits by suppressing diff
  #
  DIFF_SAFE_SIZE = 100

11 12 13 14
  def self.decorate(commits)
    commits.map { |c| self.new(c) }
  end

15
  attr_accessor :raw
16

17
  def initialize(raw_commit)
18 19
    raise "Nil as raw commit passed" unless raw_commit

20
    @raw = raw_commit
21
  end
22

23 24 25 26
  def id
    @raw.id
  end

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  # 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.
  # In case this first line is longer than 80 characters, it is cut off
  # after 70 characters and ellipses (`&hellp;`) are appended.
  def title
    title = safe_message

    return no_commit_message if title.blank?

    title_end = title.index(/\n/)
    if (!title_end && title.length > 80) || (title_end && title_end > 80)
      title[0..69] << "&hellip;".html_safe
    else
      title.split(/\n/, 2).first
    end
  end

  # Returns the commits description
  #
  # cut off, ellipses (`&hellp;`) are prepended to the commit message.
  def description
    description = safe_message

    title_end = description.index(/\n/)
    if (!title_end && description.length > 80) || (title_end && title_end > 80)
      "&hellip;".html_safe << description[70..-1]
    else
      description.split(/\n/, 2)[1].try(:chomp)
    end
  end

68 69
  def method_missing(m, *args, &block)
    @raw.send(m, *args, &block)
70
  end
71 72 73 74 75 76

  def respond_to?(method)
    return true if @raw.respond_to?(method)

    super
  end
77
end