Module: Metasploit::Version::Branch

Defined in:
lib/metasploit/version/branch.rb

Overview

Regular expressions for parsing the branch name into its component parts

Constant Summary collapse

JENKINS_PREFIX_REGEXP =

Regular expression that matches, but does not capture an optional prefix of ref/remotes for when a branch name is fully qualified on Jenkins. Remote name after ref/remotes is captured in :remote group.

%r{(?:ref/remotes/(?<remote>[^/]+)/)?}
PRERELEASE_SEGMENT_REGEXP =

Matches runs of alphanumeric characters that are valid in prerelease segments. Prerelease segments must be separated by . or -.

/[0-9a-zA-Z]+/
PRERELEASE_REGEXP =

Version pattern allowed by Rubygems for pre-release: runs of alphanumeric characters separated by . or -. Group is captured to :prerelease name.

/(?<prerelease>#{PRERELEASE_SEGMENT_REGEXP}([-.]#{PRERELEASE_SEGMENT_REGEXP})*)/
STAGING_REGEXP =

Regular expression that matches exactly a staging branch. It may optionally start with ref/remotes, followed by a type of staging, no story ID, and a pre-release name.

Examples:

Staging Branch

'staging/long-running'

Staging Branch on Jenkins

'ref/remotes/origin/staging/long-running'
%r{
  \A
  #{JENKINS_PREFIX_REGEXP}
  (?<type>staging)
  /
  #{PRERELEASE_REGEXP}
  \z
}x
STORY_REGEXP =

Regular expression that matches exactly a chore, feature or bug branch. It may optionally start with ref/remotes, followed by a type of staging, story ID, and a pre-release name.

Examples:

Bug Branch

'bug/MSP-666/nasty'

Bug Branch on Jenkins

'ref/remotes/origin/bug/MSP-666/nasty'

Chore Branch

'chore/MSP-1234/recurring'

Chore Branch on Jenkins

'ref/remotes/origin/chore/MSP-1234/recurring'

Feature Branch

'feature/MSP-31337/cool'

Feature Branch on Jenkins

'ref/remotes/origin/feature/MSP-31337/cool'
%r{
  \A
  #{JENKINS_PREFIX_REGEXP}
  (?<type>bug|chore|feature)
  /
  (?<story>[^/]+)
  /
  #{PRERELEASE_REGEXP}
  \z
}x
VERSION_PRERELEASE_SEGMENT_SEPARATOR_REGEXP =

Regular expression that matches separator used between prerelease segments for gem versions and tag versions.

/\.pre\./
VERSION_REGEXP =

Regular expression that exactly matches a release or pre-release version tag prefixed with v and followed by the major, minor, and patch numbers separated by '.' with an optional prerelease version suffix.

Examples:

Releease Tag

'v1.2.3'

Prerelease Tag

'v1.2.3.pre.cool'
%r{
  \A
  v(?<major>\d+)
  \.
  (?<minor>\d+)
  \.
  (?<patch>\d+)
  (?:
    #{VERSION_PRERELEASE_SEGMENT_SEPARATOR_REGEXP}
    (?<gem_version_prerelease>
      #{PRERELEASE_SEGMENT_REGEXP}
      (#{VERSION_PRERELEASE_SEGMENT_SEPARATOR_REGEXP}#{PRERELEASE_SEGMENT_REGEXP})*
    )
  )?
  \z
}x

Class Method Summary collapse

Class Method Details

.currentString

The current branch name from travis-ci or git.

Returns:

  • (String)


105
106
107
108
109
110
111
112
113
# File 'lib/metasploit/version/branch.rb', line 105

def self.current
  branch = ENV['TRAVIS_BRANCH']

  if branch.nil? || branch.empty?
    branch = `git rev-parse --abbrev-ref HEAD`.strip
  end

  branch
end

.parse(branch) ⇒ 'HEAD', ...

Parses the branch

Parameters:

  • branch (String)

    the branch name

Returns:

  • ('HEAD')

    if branch is 'HEAD' (such as in a detached head state for git)

  • ('master')

    if branch is master

  • (Hash{type: 'staging', prerelease: String})

    if a staging branch

  • (Hash{type: String, story: String, prerelease: String})

    if not a staging branch

  • (Hash{major: Integer, minor: Integer, patch: Integer, prerelease: String})
  • (nil)

    if branch does not match any of the formats



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/metasploit/version/branch.rb', line 124

def self.parse(branch)
  if ['HEAD', 'master'].include? branch
    branch
  else
    match = branch.match(STAGING_REGEXP)

    if match
      {
          prerelease: match[:prerelease],
          type: match[:type]
      }
    else
      match = branch.match(STORY_REGEXP)

      if match
        {
            prerelease: match[:prerelease],
            story: match[:story],
            type: match[:type]
        }
      else
        match = branch.match(VERSION_REGEXP)

        if match
          prerelease = prerelease(match[:gem_version_prerelease])

          {
              major: match[:major].to_i,
              minor: match[:minor].to_i,
              patch: match[:patch].to_i,
              prerelease: prerelease
          }
        end
      end
    end
  end
end

.prerelease(gem_version_prerelease) ⇒ String?

Replaces .pre. in gem_version_prerelease with - to undo conversion to rubygems 1.8.6 compatible pre-release version.

Returns:

  • (String)

    unless gem_version_prerelease is nil.

  • (nil)

    if gem_version_prerelease is nil.



167
168
169
170
171
# File 'lib/metasploit/version/branch.rb', line 167

def self.prerelease(gem_version_prerelease)
  unless gem_version_prerelease.nil?
    gem_version_prerelease.gsub('.pre.', '-')
  end
end