Skip to content

Commit 9c19650

Browse files
committed
Differentiate between options and arguments
i.e., we now use `--help` rather than a `help` sub-command.
1 parent 6b694ef commit 9c19650

File tree

2 files changed

+86
-37
lines changed

2 files changed

+86
-37
lines changed

lib/git_tracker/runner.rb

+58-18
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,81 @@
1+
require "optparse"
12
require "git_tracker/prepare_commit_message"
23
require "git_tracker/hook"
34
require "git_tracker/repository"
45
require "git_tracker/version"
56

67
module GitTracker
7-
module Runner
8-
def self.call(cmd_arg = "help", *args)
9-
command = cmd_arg.tr("-", "_")
8+
class Runner
9+
def self.call(*args, io: $stdout)
10+
args << "--help" if args.empty?
11+
options = {}
1012

11-
abort("[git_tracker] command: '#{cmd_arg}' does not exist.") unless respond_to?(command)
13+
OptionParser.new { |optparse|
14+
optparse.banner = <<~BANNER
15+
git-tracker is a Git hook used during the normal lifecycle of committing,
16+
rebasing, merging, etc… This hook must be initialized into each repository
17+
in which you wish to use it.
1218
13-
send(command, *args)
19+
usage: git-tracker init
20+
BANNER
21+
22+
optparse.on("-h", "--help", "Prints this help") do
23+
io.puts(optparse)
24+
options[:exit] = true
25+
end
26+
27+
optparse.on("-v", "--version", "Prints the git-tracker version number") do
28+
io.puts("git-tracker #{VERSION}")
29+
options[:exit] = true
30+
end
31+
}.parse!(args)
32+
33+
return if options.fetch(:exit, false)
34+
35+
command, *others = args
36+
37+
new(command: command, arguments: others, options: options).call
38+
end
39+
40+
def initialize(command:, arguments:, options:)
41+
@command = command
42+
@arguments = arguments
43+
@options = options
1444
end
1545

16-
def self.prepare_commit_msg(*args)
17-
PrepareCommitMessage.call(*args)
46+
def call
47+
abort("[git_tracker] command: '#{command}' does not exist.") unless sub_command
48+
49+
send(sub_command)
1850
end
1951

20-
def self.init
52+
private
53+
54+
SUB_COMMANDS = {
55+
init: :init,
56+
install: :install,
57+
"prepare-commit-msg": :prepare_commit_msg
58+
}.freeze
59+
private_constant :SUB_COMMANDS
60+
61+
attr_reader :arguments, :command, :options
62+
63+
def init
2164
Hook.init(at: Repository.root)
2265
end
2366

24-
def self.install
25-
warn("`git-tracker install` is deprecated. Please use `git-tracker init`", uplevel: 1)
67+
def install
68+
warn("`git-tracker install` is deprecated. Please use `git-tracker init`.")
2669

2770
init
2871
end
2972

30-
def self.help
31-
puts <<~HELP
32-
git-tracker #{VERSION} is installed.
73+
def prepare_commit_msg
74+
PrepareCommitMessage.call(*arguments)
75+
end
3376

34-
Remember, git-tracker is a hook which Git interacts with during its normal
35-
lifecycle of committing, rebasing, merging, etc. You need to initialize this
36-
hook by running `git-tracker init` from each repository in which you wish to
37-
use it. Cheers!
38-
HELP
77+
def sub_command
78+
@sub_command ||= SUB_COMMANDS.fetch(command.intern, false)
3979
end
4080
end
4181
end

spec/git_tracker/runner_spec.rb

+28-19
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22

33
RSpec.describe GitTracker::Runner do
44
subject(:runner) { described_class }
5-
let(:args) { ["a_file", "the_source", "sha1234"] }
65

76
describe "::call" do
87
include OutputHelper
9-
10-
before do
11-
allow(runner).to receive(:prepare_commit_msg) { true }
12-
end
8+
let(:args) { ["a_file", "the_source", "sha1234"] }
9+
let(:io) { StringIO.new }
1310

1411
it "runs the hook, passing the args" do
15-
expect(runner).to receive(:prepare_commit_msg).with(*args)
12+
expect(GitTracker::PrepareCommitMessage).to receive(:call).with(*args)
1613
runner.call("prepare-commit-msg", *args)
1714
end
1815

@@ -22,27 +19,39 @@
2219
}
2320
expect(errors.chomp).to eq("[git_tracker] command: 'non-existent-hook' does not exist.")
2421
end
25-
end
2622

27-
describe ".prepare_commit_msg" do
28-
it "runs the hook, passing the args" do
29-
expect(GitTracker::PrepareCommitMessage).to receive(:call).with(*args)
30-
runner.prepare_commit_msg(*args)
23+
it "shows the help/banner for the --help option" do
24+
runner.call("--help", io: io)
25+
26+
expect(io.string).to match(/git-tracker is a Git hook used/)
3127
end
32-
end
3328

34-
describe ".init" do
35-
let(:repo_root) { "/path/to/git/repo/root" }
29+
it "shows the version for the --version option" do
30+
runner.call("--version", io: io)
31+
32+
expect(io.string).to match(/git-tracker #{GitTracker::VERSION}/)
33+
end
3634

3735
it "tells the hook to initialize itself" do
36+
repo_root = "/path/to/git/repo/root"
3837
allow(GitTracker::Repository).to receive(:root) { repo_root }
38+
3939
expect(GitTracker::Hook).to receive(:init).with(at: repo_root)
40-
runner.init
40+
41+
runner.call("init")
4142
end
42-
end
4343

44-
it ".help reports that it was run" do
45-
expect(runner).to receive(:puts).with(/git-tracker #{GitTracker::VERSION} is installed\./)
46-
runner.call("help")
44+
it "warns of deprecated install command" do
45+
allow(GitTracker::Hook).to receive(:init)
46+
47+
warnings = capture_stderr {
48+
runner.call("install")
49+
}
50+
51+
aggregate_failures do
52+
expect(warnings.chomp).to match(/git-tracker install.*deprecated.*git-tracker init/)
53+
expect(GitTracker::Hook).to have_received(:init)
54+
end
55+
end
4756
end
4857
end

0 commit comments

Comments
 (0)