Sha256: 182f9f9bb84f2e4ef1f2f4c1f221f382891bde88e8c380d55a56572a09bce7e0

Contents?: true

Size: 965 Bytes

Versions: 6

Compression:

Stored size: 965 Bytes

Contents

# frozen_string_literal: true

require 'git'
require 'open3'

class TagExtractor
  attr_reader :git, :shas_names

  def initialize(git: nil)
    @git = git || Git.open(Dir.pwd)
  end

  def recent_tag_names
    if head_changed?
      tag_names = []
      # TODO unlimit tags when arg is given
      cmd = "git describe --tags $(git rev-list --tags --max-count=1000)"
      _stdin, stdout, _stderr, _wait_thr = Open3.popen3(cmd)
      stdout.each(sep="\n") do |line|
        # reject rev without a tag
        tag_names << line.chomp unless line =~ /-[\d]+-[a-z0-9]{8,}$/
      end
      @tag_names = tag_names
    else
      @tag_names
    end
  end

  private

  def shas_names
    if head_changed?
      @shas_names = git.tags.each_with_object({}) { |t, sum| (sum[t.sha] ||= []) << t.name }
    else
      @shas_names
    end
  end

  def head_changed?
    changed = (@head_sha != git.log(1).first.sha)
    @head_sha = git.log(1).first.sha
    changed
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
git_selector-0.1.5 lib/tag_extractor.rb
git_selector-0.1.4 lib/tag_extractor.rb
git_selector-0.1.3 lib/tag_extractor.rb
git_selector-0.1.2 lib/tag_extractor.rb
git_selector-0.1.1 lib/tag_extractor.rb
git_selector-0.1.0 lib/tag_extractor.rb