class Twig

The main class.

Constants

DEFAULT_GITHUB_API_URI_PREFIX
DEFAULT_GITHUB_URI_PREFIX
DEFAULT_HEADER_COLOR
HOMEPAGE
REF_FORMAT
REF_FORMAT_SEPARATOR
REF_PREFIX
VERSION

Attributes

options[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/twig.rb, line 42
def initialize(options = {})
  self.options = {}

  # Set defaults
  set_option(:github_api_uri_prefix, DEFAULT_GITHUB_API_URI_PREFIX)
  set_option(:github_uri_prefix, DEFAULT_GITHUB_URI_PREFIX)
  set_option(:header_style, DEFAULT_HEADER_COLOR.to_s)

  if options[:read_options]
    read_config_file!
    read_cli_options!(ARGV)
  end
end
repo?() click to toggle source
# File lib/twig.rb, line 37
def self.repo?
  Twig.run('git rev-parse 2>&1')
  $?.success?
end
run(command) click to toggle source
# File lib/twig.rb, line 33
def self.run(command)
  `#{command}`.strip
end

Public Instance Methods

branches() click to toggle source
# File lib/twig.rb, line 70
def branches
  branches = Twig::Branch.all_branches
  now = Time.now
  max_days_old = options[:max_days_old]
  max_seconds_old = max_days_old * 86400 if max_days_old

  branches = branches.select do |branch|
    catch :skip_branch do
      if max_seconds_old
        seconds_old = now.to_i - branch.last_commit_time.to_i
        next if seconds_old > max_seconds_old
      end

      branch_name = branch.name

      (options[:property_except] || {}).each do |property_name, property_value|
        if property_name == :branch
          throw :skip_branch if branch_name =~ property_value
        elsif branch.get_property(property_name.to_s) =~ property_value
          throw :skip_branch
        end
      end

      (options[:property_only] || {}).each do |property_name, property_value|
        if property_name == :branch
          throw :skip_branch if branch_name !~ property_value
        elsif branch.get_property(property_name.to_s) !~ property_value
          throw :skip_branch
        end
      end

      true
    end
  end

  # List least recently modified branches first
  branches = branches.sort_by { |branch| branch.last_commit_time }
  if options[:reverse] != true
    branches.reverse! # List most recently modified branches first
  end

  branches
end
current_branch_name() click to toggle source
# File lib/twig.rb, line 56
def current_branch_name
  # Returns the name of the branch that is currently checked out in Git.
  @_current_branch_name ||= Twig.run('git rev-parse --abbrev-ref HEAD')
end
get_branch_property(branch_name, property_name) click to toggle source
# File lib/twig.rb, line 155
def get_branch_property(branch_name, property_name)
  branch = Branch.new(branch_name)
  branch.get_property(property_name)
end
list_branches() click to toggle source

Actions ###

# File lib/twig.rb, line 134
def list_branches
  if branches.empty?
    msg = 
      if Twig::Branch.all_branches.any?
        "There are no branches matching your selected options.\n" \
        "To list all branches, use `twig --all`."
      else
        'This repository has no branches.'
      end
    return msg
  end

  out = "\n" << branch_list_headers(options)

  branch_lines = branches.inject([]) do |result, branch|
    result << branch_list_line(branch)
  end

  out << branch_lines.join("\n")
end
property_names() click to toggle source
# File lib/twig.rb, line 114
def property_names
  @_property_names ||= begin
    property_names = Twig::Branch.all_property_names
    only_name      = options[:property_only_name]
    except_name    = options[:property_except_name]

    if only_name
      property_names = property_names.select { |name| name =~ only_name }
    end

    if except_name
      property_names = property_names.select { |name| name !~ except_name }
    end

    property_names
  end
end
set_branch_property(branch_name, property_name, value) click to toggle source
# File lib/twig.rb, line 160
def set_branch_property(branch_name, property_name, value)
  branch = Branch.new(branch_name)
  branch.set_property(property_name, value)
end
target_branch() click to toggle source
# File lib/twig.rb, line 66
def target_branch
  Twig::Branch.new(target_branch_name)
end
target_branch_name() click to toggle source
# File lib/twig.rb, line 61
def target_branch_name
  # Returns the name of the branch to work on, e.g., for setting a property.
  options[:branch] || current_branch_name
end
unset_branch_property(branch_name, property_name) click to toggle source
# File lib/twig.rb, line 165
def unset_branch_property(branch_name, property_name)
  branch = Branch.new(branch_name)
  branch.unset_property(property_name)
end