Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
9b39bfd
Introduce the object registry pattern.
georgebrock Apr 7, 2019
609d7be
Use Registry.env in InputStrategies::Interactive
georgebrock Apr 7, 2019
881b0b0
Use Registry.env in History
georgebrock Apr 7, 2019
273680b
fixup! Use Registry.env in InputStrategies::Interactive
georgebrock Apr 7, 2019
11ce21b
Use Registry.env in TabCompletion::Facade
georgebrock Apr 7, 2019
d1ffa83
Use Registry.env in TabCompletion::AutomatonFactory
georgebrock Apr 7, 2019
ec297e7
Use Registry.env in TabCompletion::DSL
georgebrock Apr 7, 2019
053aaa9
Use Registry.env in TabCompletion::DSL::Parser
georgebrock Apr 7, 2019
b19de01
Use Registry.env in TabCompletion::Matchers::*
georgebrock Apr 7, 2019
64fa06f
Use Registry.env in TabCompletion::AliasExpander
georgebrock Apr 7, 2019
db2932c
Use Registry.env in TabCompletion::VariableCompleter
georgebrock Apr 8, 2019
5188a88
Use Registry.env in PromptColor
georgebrock Apr 8, 2019
9bf7794
Use Registry.env in Prompter
georgebrock Apr 8, 2019
3cf3c67
Use Registry.env in InputStrategies::FileRunner
georgebrock Apr 8, 2019
d4794b8
Meta-programming for registry client methods.
georgebrock Apr 8, 2019
be425c3
Use Registry.env in GitRepository
georgebrock Apr 8, 2019
f9a36ba
Use Registry.env in GitCommandList
georgebrock Apr 8, 2019
1601862
Simplify: remove unused initialize argument.
georgebrock Apr 8, 2019
757c8ba
Add a GitRepository to the Registry.
georgebrock Apr 8, 2019
cda7f59
Use Registry.repo in Environment
georgebrock Apr 8, 2019
49424d8
Remove all the Env -> Repo delegates.
georgebrock Apr 8, 2019
d59529e
fixup! Use Registry.env in InputStrategies::Interactive
georgebrock Apr 9, 2019
7aed129
Move line editor to registry.
georgebrock Apr 9, 2019
69c1aed
Simplify CLI initializer.
georgebrock Apr 9, 2019
281a987
Simplify Interpreter initializer.
georgebrock Apr 13, 2019
22b620a
Simplify interactive input strategy initializer.
georgebrock Apr 13, 2019
37f499c
Update bin/tcviz to use registry.
georgebrock Apr 13, 2019
f5a1cc1
Simplify Environment initializer.
georgebrock Apr 13, 2019
fe43ceb
Use line editor from registry in tab completion.
georgebrock Apr 13, 2019
9d4f645
Remove unused initialize arg.
georgebrock Apr 13, 2019
7c20c56
fixup! Use line editor from registry in tab completion.
georgebrock Apr 13, 2019
d1be651
Stop injecting constants into things.
georgebrock Apr 13, 2019
1c34f25
Use keyword args.
georgebrock Apr 13, 2019
69cd772
fixup! Use line editor from registry in tab completion.
georgebrock Apr 13, 2019
4cb690b
More consistent registry use in specs.
georgebrock Apr 13, 2019
8a77f2b
DRY registry population code.
georgebrock Apr 13, 2019
a2d60a4
Clean up registry interface.
georgebrock Apr 13, 2019
910e689
fixup! Simplify interactive input strategy initializer.
georgebrock Apr 13, 2019
80cca72
fixup! Stop injecting constants into things.
georgebrock Aug 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions bin/gitsh
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ require 'bundler/setup'
$LOAD_PATH.unshift(File.expand_path("../../#{directory}", __FILE__))
end

require 'gitsh/environment'
require 'gitsh/cli'
require 'gitsh/registry'

env = Gitsh::Environment.new(
config_directory: File.expand_path('../../etc', __FILE__),
)
Gitsh::CLI.new(env: env).run
Gitsh::Registry.populate(conf_dir: File.expand_path('../../etc', __FILE__))
Gitsh::CLI.new(ARGV).run
8 changes: 3 additions & 5 deletions bin/tcviz
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ require 'bundler/setup'
$LOAD_PATH.unshift(File.expand_path("../../#{directory}", __FILE__))
end

require 'gitsh/environment'
require 'gitsh/registry'
require 'gitsh/tab_completion/automaton_factory'
require 'gitsh/tab_completion/visualization'

env = Gitsh::Environment.new(
config_directory: File.expand_path('../../etc', __FILE__),
)
automaton = Gitsh::TabCompletion::AutomatonFactory.build(env)
Gitsh::Registry.populate(conf_dir: File.expand_path('../../etc', __FILE__))
automaton = Gitsh::TabCompletion::AutomatonFactory.build
viz = Gitsh::TabCompletion::Visualization.new(automaton)

$stdout.puts viz.to_dot
Expand Down
2 changes: 1 addition & 1 deletion lib/gitsh/arguments/subshell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Gitsh
module Arguments
class Subshell
def initialize(command, options = {})
def initialize(command)
@command = command
end

Expand Down
20 changes: 9 additions & 11 deletions lib/gitsh/cli.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
require 'optparse'
require 'gitsh/environment'
require 'gitsh/exit_statuses'
require 'gitsh/input_strategies/file'
require 'gitsh/input_strategies/interactive'
require 'gitsh/interpreter'
require 'gitsh/registry'
require 'gitsh/version'

module Gitsh
class CLI
def initialize(opts={})
@env = opts.fetch(:env, Environment.new)
@unparsed_args = opts.fetch(:args, ARGV).clone
@interactive_input_strategy = opts.fetch(:interactive_input_strategy) do
InputStrategies::Interactive.new(env: @env)
end
extend Registry::Client
use_registry_for :env

def initialize(args)
@unparsed_args = args.clone
end

def run
Expand All @@ -27,18 +26,17 @@ def run

private

attr_reader :env, :unparsed_args, :script_file_argument,
:interactive_input_strategy
attr_reader :unparsed_args, :script_file_argument

def interpreter
Interpreter.new(env: env, input_strategy: input_strategy)
end

def input_strategy
if script_file
InputStrategies::File.new(env: env, path: script_file)
InputStrategies::File.new(path: script_file)
else
interactive_input_strategy
InputStrategies::Interactive.new
end
end

Expand Down
10 changes: 3 additions & 7 deletions lib/gitsh/commands/git_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@
module Gitsh
module Commands
class GitCommand
def initialize(command, arg_values, options = {})
def initialize(command, arg_values)
@command = command
@arg_values = arg_values
@shell_command_runner = options.fetch(
:shell_command_runner,
ShellCommandRunner,
)
end

def execute(env)
shell_command_runner.run(command_with_arguments(env), env)
ShellCommandRunner.run(command_with_arguments(env), env)
end

private

attr_reader :command, :arg_values, :shell_command_runner
attr_reader :command, :arg_values

def command_with_arguments(env)
if autocorrect_enabled?(env) && command == 'git'
Expand Down
8 changes: 2 additions & 6 deletions lib/gitsh/commands/shell_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ class ShellCommand
def initialize(command, arg_values, options = {})
@command = command
@arg_values = arg_values
@shell_command_runner = options.fetch(
:shell_command_runner,
ShellCommandRunner,
)
end

def execute(env)
shell_command_runner.run(command_with_arguments, env)
ShellCommandRunner.run(command_with_arguments, env)
end

private

attr_reader :command, :arg_values, :shell_command_runner
attr_reader :command, :arg_values

def command_with_arguments
[
Expand Down
72 changes: 19 additions & 53 deletions lib/gitsh/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@
require 'gitsh/git_repository'
require 'gitsh/line_editor'
require 'gitsh/magic_variables'
require 'gitsh/registry'

module Gitsh
class Environment
extend Registry::Client
use_registry_for :repo

DEFAULT_GIT_COMMAND = '/usr/bin/env git'.freeze
DEFAULT_CONFIG_DIRECTORY = '/usr/local/etc/gitsh'.freeze

attr_reader :input_stream, :output_stream, :error_stream, :config_directory

def initialize(options={})
@input_stream = options.fetch(:input_stream, $stdin)
@output_stream = options.fetch(:output_stream, $stdout)
@error_stream = options.fetch(:error_stream, $stderr)
@repo = options.fetch(:repository_factory, GitRepository).new(self)
def initialize(
input_stream: $stdin, output_stream: $stdout, error_stream: $stderr,
config_directory: DEFAULT_CONFIG_DIRECTORY
)
@input_stream = input_stream
@output_stream = output_stream
@error_stream = error_stream
@config_directory = config_directory
@variables = Hash.new
@magic_variables = options.fetch(:magic_variables) { MagicVariables.new(@repo) }
@config_directory = options.fetch(
:config_directory,
DEFAULT_CONFIG_DIRECTORY,
)
end

def initialize_copy(original)
Expand Down Expand Up @@ -83,54 +85,18 @@ def tty?
input_stream.tty?
end

def repo_branches
repo.branches
end

def repo_tags
repo.tags
end

def repo_remotes
repo.remotes
end

def repo_heads
repo.heads
end

def repo_current_head
repo.current_head
end

def repo_status
repo.status
end

def repo_config_color(name, default)
if color_override = fetch(name) { false }
repo.color(color_override)
else
repo.config_color(name, default)
end
end

def git_commands
repo.commands
end

def git_aliases
(repo.aliases + local_aliases).sort
def local_aliases
variables.keys.
select { |key| key.to_s.start_with?('alias.') }.
map { |key| key.to_s.sub('alias.', '') }
end

private

attr_reader :variables, :magic_variables, :repo
attr_reader :variables

def local_aliases
variables.keys.
select { |key| key.to_s.start_with?('alias.') }.
map { |key| key.to_s.sub('alias.', '') }
def magic_variables
@_magic_variables ||= MagicVariables.new
end
end
end
8 changes: 4 additions & 4 deletions lib/gitsh/file_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def self.run(opts)
new(opts).run
end

def initialize(opts)
@env = opts.fetch(:env)
@path = opts.fetch(:path)
def initialize(env:, path:)
@env = env
@path = path
end

def run
Expand All @@ -25,7 +25,7 @@ def interpreter
end

def input_strategy
InputStrategies::File.new(env: env, path: path)
InputStrategies::File.new(path: path)
end
end
end
9 changes: 4 additions & 5 deletions lib/gitsh/git_command_list.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require 'gitsh/registry'

module Gitsh
class GitCommandList
def initialize(env)
@env = env
end
extend Registry::Client
use_registry_for :env

def to_a
try_using(commands_from_list_cmds) do
Expand All @@ -14,8 +15,6 @@ def to_a

private

attr_accessor :env

def try_using(result, default: [])
if result && result.any?
result
Expand Down
13 changes: 5 additions & 8 deletions lib/gitsh/git_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
require 'shellwords'
require 'gitsh/git_repository/status'
require 'gitsh/git_command_list'
require 'gitsh/registry'

module Gitsh
class GitRepository
def initialize(env, options={})
@env = env
@status_factory = options.fetch(:status_factory, Status)
end
extend Registry::Client
use_registry_for :env

def git_dir
git_output('rev-parse --git-dir')
Expand All @@ -23,7 +22,7 @@ def current_head
end

def status
status_factory.new(git_output('status --porcelain'), git_dir)
Status.new(git_output('status --porcelain'), git_dir)
end

def branches
Expand All @@ -41,7 +40,7 @@ def heads
end

def commands
GitCommandList.new(env).to_a
GitCommandList.new.to_a
end

def aliases
Expand Down Expand Up @@ -103,8 +102,6 @@ def merge_base(commit1, commit2)

private

attr_reader :env, :status_factory

def current_branch_name
branch_name = git_output('symbolic-ref HEAD --short')
unless branch_name.empty?
Expand Down
16 changes: 11 additions & 5 deletions lib/gitsh/history.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
require 'gitsh/registry'

module Gitsh
class History
extend Registry::Client
use_registry_for :env, :line_editor

DEFAULT_HISTORY_FILE = "#{Dir.home}/.gitsh_history"
DEFAULT_HISTORY_SIZE = 500

def initialize(env, line_editor)
@env = env
@line_editor = line_editor
def self.load
new.load
end

def self.save
new.save
end

def load
Expand All @@ -25,8 +33,6 @@ def save

private

attr_reader :env, :line_editor

def history_file_exists?
File.exist?(history_file_path)
end
Expand Down
11 changes: 7 additions & 4 deletions lib/gitsh/input_strategies/file.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
require 'gitsh/error'
require 'gitsh/registry'

module Gitsh
module InputStrategies
class File
extend Registry::Client
use_registry_for :env

STDIN_PLACEHOLDER = '-'.freeze

def initialize(opts)
@env = opts[:env]
@path = opts.fetch(:path)
def initialize(path:)
@path = path
end

def setup
Expand Down Expand Up @@ -40,7 +43,7 @@ def handle_parse_error(message)

private

attr_reader :env, :file, :path
attr_reader :file, :path

def open_file
if path == STDIN_PLACEHOLDER
Expand Down
Loading