Sha256: 7016fd2abd2491082601c0c837c674b4d7cb4d0785904c28f54ac2fd80bfc453

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require 'git'
require 'open3'

class TreeishExtractor
  attr_reader :git

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

  def recent_tag_names
    cmd = 'git tag --sort=-committerdate'
    get_treeishes(cmd) do |line, names|
      names << {
        value: line.strip,
        name: line.strip
      }
    end
  end

  def recent_branch_names
    cmd = 'git branch --sort=-committerdate'
    get_treeishes(cmd) do |line, names|
      next if REJECT_STRATEGY.call(line, names).nil?
      names << {
        value: VALUE_STRATEGY.call(line, names),
        name: NAME_STRATEGY.call(line, names)
      }
    end
  end

  private

  REJECT_STRATEGY = lambda do |line, _|
    # 'HEAD detached' means the last commit is detached from its HEAD.
    # We don't need this information and remove it.
    line unless line =~ /HEAD detached/
  end
  VALUE_STRATEGY = lambda do |line, _|
    line.sub('*', '').strip
  end
  NAME_STRATEGY = lambda do |line, _|
    if line.match(/\*\s/)
      line.strip
    else
      "  #{line.sub('*', '').strip}"
    end
  end

  def get_treeishes(cmd)
    names = []
    _stdin, stdout, _stderr, _wait_thr = Open3.popen3(cmd)
    stdout.each(sep="\n") do |line|
      yield(line, names)
    end
    names
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gnav-0.4.0 lib/treeish_extractor.rb
gnav-0.3.2 lib/treeish_extractor.rb
gnav-0.3.1 lib/treeish_extractor.rb
gnav-0.3.0 lib/treeish_extractor.rb