Sha256: e8e5b75787895aba48a08a61c91a6718bb5b533ba2991cb0f3d81dc2cfa6d9db

Contents?: true

Size: 1.94 KB

Versions: 24

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module OhlohScm
  class BranchNumber
    def initialize(branch_number)
      @num = branch_number.split('.').collect(&:to_i)
      # Accomodate CVS magic branch numbers by swapping the magic zero
      # That is, 1.1.0.2 => 1.1.2.0
      @num[-1], @num[-2] = @num[-2], @num[-1] if (@num.size > 2) && @num[-2].zero?
    end

    # Returns true if <branch_number> is an ancestor of this object,
    # or if this object follows <branch_number> on the same line.
    def on_same_line?(branch_number)
      b = branch_number.to_a

      # b has been branched more times than this object.
      return false if b.size > @num.size
      if b.size == @num.size
        # b and a have the same number of branch events.
        # If either one inherits from the other then they
        # are on the same line.
        return (inherits_from?(branch_number) || branch_number.inherits_from?(self))
      end
      # b has not been branched as often as this object.
      # That's OK if b is an ancestor of this object.
      return inherits_from?(branch_number) if b.size < @num.size
    end

    def to_a
      @num
    end

    protected

    # Returns true if <branch_number> is an ancestor of this object.
    # Also returns true if <branch_number> is the same as this object.
    # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
    def inherits_from?(branch_number)
      b = branch_number.to_a

      return false if b.size > @num.size

      return false if b.size == 2 && descendant?(b)

      unless b.size == 2
        0.upto(b.size - 2) do |i|
          return false if b[i] != @num[i]
        end
        return false if b[-1] > @num[b.size - 1]
      end
      true
    end
    # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity

    private

    def descendant?(branch_number)
      return true if branch_number[0] > @num[0] ||
                     ((branch_number[0] == @num[0]) && (branch_number[1] > @num[1]))
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
ohloh_scm-4.0.5 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-4.0.4 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-4.0.3 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-4.0.2 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-4.0.1 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-3.0.21 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-3.0.19 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-3.0.17 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-3.0.16 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-3.0.15 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-3.0.14 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-3.0.13 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-3.0.12 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-3.0.11 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-3.0.10 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-3.0.9 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-3.0.8 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-3.0.7 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-3.0.6 lib/ohloh_scm/parser/branch_number.rb
ohloh_scm-3.0.5 lib/ohloh_scm/parser/branch_number.rb