Class: Twig

Inherits:
Object
  • Object
show all
Includes:
Cli, Display, Options
Defined in:
lib/twig.rb,
lib/twig/cli.rb,
lib/twig/util.rb,
lib/twig/branch.rb,
lib/twig/github.rb,
lib/twig/system.rb,
lib/twig/display.rb,
lib/twig/options.rb,
lib/twig/version.rb,
lib/twig/cli/help.rb,
lib/twig/homepage.rb,
lib/twig/commit_time.rb,
lib/twig/subcommands.rb

Overview

The main class.

Defined Under Namespace

Modules: Cli, Display, Options, Subcommands, System, Util Classes: Branch, CommitTime, GithubRepo

Constant Summary collapse

DEFAULT_GITHUB_API_URI_PREFIX =
'https://api.github.com'
DEFAULT_GITHUB_URI_PREFIX =
'https://github.com'
DEFAULT_HEADER_COLOR =
:blue
REF_FORMAT_SEPARATOR =
'|'
REF_FORMAT =
%w[refname:short committerdate:iso].
map { |field| '%(' + field + ')' }.
join(REF_FORMAT_SEPARATOR)
REF_PREFIX =
'refs/heads/'
VERSION =
'1.7.1'
HOMEPAGE =
'http://rondevera.github.io/twig/'

Constants included from Options

Options::CONFIG_PATH, Options::DEPRECATED_CONFIG_PATH, Options::MIN_PROPERTY_WIDTH

Constants included from Display

Display::COLORS, Display::CURRENT_BRANCH_INDICATOR, Display::DEFAULT_BRANCH_COLUMN_WIDTH, Display::DEFAULT_PROPERTY_COLUMN_WIDTH, Display::EMPTY_BRANCH_PROPERTY_INDICATOR, Display::WEIGHTS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Options

#parse_config_file, #read_config_file!, #readable_config_file_path, #set_header_style_option, #set_option, #set_property_width_option, #unset_option

Methods included from Display

#branch_list_headers, #branch_list_line, #branches_json, #column, #column_gutter, #date_time_column_width, #format_string, #format_strings?, #property_column_width, unformat_string

Methods included from Cli

#abort_for_option_exception, #get_branch_property_for_cli, prompt_with_choices, #read_cli_args!, #read_cli_options!, #run_pager, #set_branch_property_for_cli, #unset_branch_property_for_cli

Constructor Details

#initialize(options = {}) ⇒ Twig

Returns a new instance of Twig.



42
43
44
45
46
47
48
49
50
51
52
53
54
# 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

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



22
23
24
# File 'lib/twig.rb', line 22

def options
  @options
end

Class Method Details

.repo?Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/twig.rb', line 37

def self.repo?
  Twig.run('git rev-parse 2>&1')
  $?.success?
end

.run(command) ⇒ Object



33
34
35
# File 'lib/twig.rb', line 33

def self.run(command)
  `#{command}`.strip
end

Instance Method Details

#branchesObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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_nameObject



56
57
58
59
# 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) ⇒ Object



155
156
157
158
# 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_branchesObject

Actions ###



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# 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_namesObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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) ⇒ Object



160
161
162
163
# 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_branchObject



66
67
68
# File 'lib/twig.rb', line 66

def target_branch
  Twig::Branch.new(target_branch_name)
end

#target_branch_nameObject



61
62
63
64
# 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) ⇒ Object



165
166
167
168
# 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