Module: Git

Defined in:
lib/get/commons/git.rb

Overview

Utility module

Constant Summary collapse

CONVENTIONAL_COMMIT_REGEX =

Groups: 1 = type, 2 = scope with (), 3 = scope, 4 = breaking change, 5 = summary

%r{^(\w+)(\(([\w/-]+)\))?(!)?:(.*)}
DEFAULT_RELEASE_VERSION =
'0.1.0'
FULL_SEMANTIC_VERSION_REGEX =

Groups: 1 = full stable version ; 2,3,4 = major,minor,patch 5 = prerelease ; 6 = metadata

/
  ^((0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)) # Stable version, major, minor, patch
  (?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))? # prerelease
  (?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$ # metadata
/x

Class Method Summary collapse

Class Method Details

.in_repo?Boolean

Check if the command is called while in a git repository. If the command fails, it is assumed to not be in a git repository.

Returns:

  • (Boolean)


38
39
40
# File 'lib/get/commons/git.rb', line 38

def self.in_repo?
  CommandIssuer.run('git', 'rev-parse', '--is-inside-work-tree').exit_status.zero?
end

.last_releaseObject

Returns the last release and caches it for the next calls.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/get/commons/git.rb', line 71

def self.last_release
  @last_release ||=
    CommandIssuer.run('git', '--no-pager', 'tag', '--list', '--merged')
                 .then do |value|
      unless value.output.empty?
        value.output
             .split("\n")
             .select { |str| str.match(FULL_SEMANTIC_VERSION_REGEX)[5].nil? }
             .map { |str| str.sub('+', '_') }
             .sort
             .map { |str| str.sub('_', '+') }
             .last
      end
    end
end

.last_versionObject

Returns the last version and caches it for the next calls.



64
65
66
67
68
# File 'lib/get/commons/git.rb', line 64

def self.last_version
  @last_version ||=
    CommandIssuer.run('git', 'describe', '--tags', '--abbrev=0')
                 .then { |result| result.output.strip if result.exit_status.zero? }
end

.with_commit_list_from(version = nil, &block) ⇒ Object

Run a block of code with the list of commits from the given version as an argument. If the block is not given, this method is a nop.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/get/commons/git.rb', line 44

def self.with_commit_list_from(version = nil, &block)
  return unless block_given?

  command_result = CommandIssuer.run(
    'git',
    '--no-pager',
    'log',
    '--oneline',
    '--pretty=format:%s',
    version.nil? ? '' : "^#{version} HEAD"
  )
  commits_from_version = if command_result.exit_status.zero?
                           command_result.output.split("\n")
                         else
                           []
                         end
  block.call(commits_from_version)
end