Class: SemVerComponents::LocalGit

Inherits:
Object
  • Object
show all
Defined in:
lib/sem_ver_components/local_git.rb

Instance Method Summary collapse

Constructor Details

#initialize(git_repo, git_from, git_to) ⇒ LocalGit

Constructor

Parameters
  • git_repo (String): The git repository to analyze

  • git_from (String or nil): The git from ref

  • git_to (String): The git to ref



15
16
17
18
19
20
# File 'lib/sem_ver_components/local_git.rb', line 15

def initialize(git_repo, git_from, git_to)
  @git_repo = git_repo
  @git_from = git_from
  @git_to = git_to
  @git = Git.open(@git_repo)
end

Instance Method Details

#analyze_commitsObject

Semantically analyze commits.

Result
  • Array< Hash<Symbol, Object> >: The commits information:

    • components_bump_levels (Hash<String or nil, Integer>): Set of bump levels (0: patch, 1: minor, 2: major) per component name (nil for global)

    • commit (Git::Object::Commit): Corresponding git commit



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sem_ver_components/local_git.rb', line 38

def analyze_commits
  git_log.between(git_from.nil? ? git_log.last.sha : git_from, git_to).map do |git_commit|
    # Analyze the message
    # Always consider a minimum of global patch bump per commit.
    components_bump_levels = { nil => [0] }
    git_commit.message.scan(/\[([^\]]+)\]/).flatten(1).each do |commit_label|
      commit_type, component = commit_label =~ /^(.+)\((.+)\)$/ ? [$1, $2] : [commit_label, nil]
      components_bump_levels[component] = [] unless components_bump_levels.key?(component)
      components_bump_levels[component] <<
        case commit_type.downcase
        when 'break', 'breaking', 'major'
          2
        when 'feat', 'feature', 'minor'
          1
        else
          0
        end
    end
    {
      commit: git_commit,
      components_bump_levels: Hash[components_bump_levels.map { |component, component_bump_levels| [component, component_bump_levels.max] }]
    }
  end
end

#git_logObject

Get full the git log. Keep a cache of it.

Result
  • Array< Git::Object::Commit >: Full git log



27
28
29
30
# File 'lib/sem_ver_components/local_git.rb', line 27

def git_log
  @git_log = @git.log(nil) unless defined?(@git_log)
  @git_log
end