Skip to content
Snippets Groups Projects
Select Git revision
  • move-gl-dropdown
  • improve-table-pagination-spec
  • move-markdown-preview
  • winh-fix-merge-request-spec
  • master default
  • index-namespaces-lower-name
  • winh-single-karma-test
  • 10-3-stable
  • 36782-replace-team-user-role-with-add_role-user-in-specs
  • winh-modal-internal-state
  • tz-ide-file-icons
  • 38869-milestone-select
  • update-autodevops-template
  • jivl-activate-repo-cookie-preferences
  • qa-add-deploy-key
  • docs-move-article-ldap
  • 40780-choose-file
  • 22643-manual-job-page
  • refactor-cluster-show-page-conservative
  • dm-sidekiq-versioning
  • v10.4.0.pre
  • v10.3.0
  • v10.3.0-rc5
  • v10.3.0-rc4
  • v10.3.0-rc3
  • v10.3.0-rc2
  • v10.2.5
  • v10.3.0-rc1
  • v10.0.7
  • v10.1.5
  • v10.2.4
  • v10.2.3
  • v10.2.2
  • v10.2.1
  • v10.3.0.pre
  • v10.2.0
  • v10.2.0-rc4
  • v10.2.0-rc3
  • v10.1.4
  • v10.2.0-rc2
40 results

user_uses_slash_commands_spec.rb

Forked from GitLab.org / GitLab FOSS
Source project has a limited visibility.
commit_range.rb NaN GiB
# CommitRange makes it easier to work with commit ranges
#
# Examples:
#
#   range = CommitRange.new('f3f85602...e86e1013')
#   range.exclude_start?  # => false
#   range.reference_title # => "Commits f3f85602 through e86e1013"
#   range.to_s            # => "f3f85602...e86e1013"
#
#   range = CommitRange.new('f3f856029bc5f966c5a7ee24cf7efefdd20e6019..e86e1013709735be5bb767e2b228930c543f25ae')
#   range.exclude_start?  # => true
#   range.reference_title # => "Commits f3f85602^ through e86e1013"
#   range.to_param        # => {from: "f3f856029bc5f966c5a7ee24cf7efefdd20e6019^", to: "e86e1013709735be5bb767e2b228930c543f25ae"}
#   range.to_s            # => "f3f85602..e86e1013"
#
#   # Assuming `project` is a Project with a repository containing both commits:
#   range.project = project
#   range.valid_commits? # => true
#
class CommitRange
  include ActiveModel::Conversion
  include Referable

  attr_reader :sha_from, :notation, :sha_to

  # Optional Project model
  attr_accessor :project

  # See `exclude_start?`
  attr_reader :exclude_start

  # The beginning and ending SHA sums can be between 6 and 40 hex characters,
  # and the range selection can be double- or triple-dot.
  PATTERN = /\h{6,40}\.{2,3}\h{6,40}/

  # Initialize a CommitRange
  #
  # range_string - The String commit range.
  # project      - An optional Project model.
  #
  # Raises ArgumentError if `range_string` does not match `PATTERN`.
  def initialize(range_string, project = nil)
    range_string.strip!

    unless range_string.match(/\A#{PATTERN}\z/)
      raise ArgumentError, "invalid CommitRange string format: #{range_string}"
    end

    @exclude_start = !range_string.include?('...')
    @sha_from, @notation, @sha_to = range_string.split(/(\.{2,3})/, 2)

    @project = project
  end

  def inspect
    %(#<#{self.class}:#{object_id} #{to_s}>)
  end

  def to_s
    "#{sha_from[0..7]}#{notation}#{sha_to[0..7]}"
  end

  def to_reference(from_project = nil)
    if cross_project_reference?(from_project)
      "#{project.to_reference}@#{to_s}"
    else
      to_s
    end
  end

  # Returns a String for use in a link's title attribute
  def reference_title
    "Commits #{suffixed_sha_from} through #{sha_to}"
  end

  # Return a Hash of parameters for passing to a URL helper
  #
  # See `namespace_project_compare_url`
  def to_param
    { from: suffixed_sha_from, to: sha_to }
  end

  def exclude_start?
    exclude_start
  end

  # Check if both the starting and ending commit IDs exist in a project's
  # repository
  #
  # project - An optional Project to check (default: `project`)
  def valid_commits?(project = project)
    return nil   unless project.present?
    return false unless project.valid_repo?

    commit_from.present? && commit_to.present?
  end

  def persisted?
    true
  end

  def commit_from
    @commit_from ||= project.repository.commit(suffixed_sha_from)
  end

  def commit_to
    @commit_to ||= project.repository.commit(sha_to)
  end

  private

  def suffixed_sha_from
    sha_from + (exclude_start? ? '^' : '')
  end
end