Skip to content

Commit 5db6c10

Browse files
committed
option parsin, --help / --version / --sha + file:line support
fixes #7
1 parent a16a21a commit 5db6c10

9 files changed

+82
-7
lines changed

Gemfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
source "http://rubygems.org"
22

33
gemspec
4+
45
gem 'debugger'
56
gem 'bump'
67
gem 'rake'
7-
8+
gem 'minitest'
9+
gem 'minitest-rg'

Gemfile.lock

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
git_spelunk (0.2.2)
4+
git_spelunk (0.2.0)
55
grit
66

77
GEM
@@ -20,7 +20,10 @@ GEM
2020
diff-lcs (~> 1.1)
2121
mime-types (~> 1.15)
2222
posix-spawn (~> 0.3.6)
23-
mime-types (1.25)
23+
mime-types (1.25.1)
24+
minitest (5.1.0)
25+
minitest-rg (5.0.0)
26+
minitest (~> 5.0)
2427
posix-spawn (0.3.6)
2528
rake (10.1.0)
2629

@@ -31,4 +34,6 @@ DEPENDENCIES
3134
bump
3235
debugger
3336
git_spelunk!
37+
minitest
38+
minitest-rg
3439
rake

bin/git-spelunk

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
#!/usr/bin/env ruby
2+
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
23
require 'git_spelunk'
4+
require 'optparse'
35

4-
file_context = GitSpelunk::FileContext.new(ARGV[0], {:sha => ARGV[1], :line_number => ARGV[2]})
6+
def parse_options(argv)
7+
options = {}
8+
OptionParser.new do |opts|
9+
opts.banner = <<-BANNER.gsub(/^ {6}/, "")
10+
An interactive tool for exploring blame history.
11+
12+
Usage:
13+
git-spelunk some_file.rb
14+
git-spelunk some_file.rb:123
15+
16+
Options:
17+
BANNER
18+
opts.on("-s", "--sha SHA", String, "Start on this sha") { |sha| options[:sha] = sha }
19+
opts.on("-h", "--help", "Show this.") { puts opts; exit }
20+
opts.on("-v", "--version", "Show Version"){ puts GitSpelunk::VERSION; exit}
21+
end.parse!(argv)
22+
options
23+
end
24+
25+
options = parse_options(ARGV)
26+
27+
raise "Too many arguments" if ARGV.size != 1
28+
29+
file = ARGV[0]
30+
31+
file, line = file.split(':',2) if file.include?(':')
32+
options[:line_number] = line
33+
34+
file_context = GitSpelunk::FileContext.new(file, options)
535
ui = GitSpelunk::UI.new(file_context)
636
ui.run

git_spelunk.gemspec

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
2+
require "git_spelunk/version"
3+
14
Gem::Specification.new do |s|
25
s.name = "git_spelunk"
3-
s.version = "0.2.2"
6+
s.version = GitSpelunk::VERSION
47
s.platform = Gem::Platform::RUBY
58
s.authors = ["Ben Osheroff", "Saroj Yadav"]
69
s.email = ["ben@zendesk.com", "saroj@zendesk.com"]

lib/git_spelunk.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'debugger'
1+
require 'git_spelunk/version'
22
require 'git_spelunk/ui'
33
require 'git_spelunk/file_context'
44
require 'git_spelunk/offset'

lib/git_spelunk/grit_patches.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@ class String
55
def getord(offset); self[offset].ord; end
66
end
77

8-
PACK_IDX_SIGNATURE = "\377tOc".force_encoding(Encoding::ASCII_8BIT)
8+
begin
9+
old, $VERBOSE = $VERBOSE, nil
10+
PACK_IDX_SIGNATURE = "\377tOc".force_encoding(Encoding::ASCII_8BIT)
11+
ensure
12+
$VERBOSE = old
13+
end
914
end

lib/git_spelunk/version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module GitSpelunk
2+
VERSION = "0.2.2"
3+
end

test/integration_test.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require_relative "test_helper"
2+
3+
describe "CLI" do
4+
def sh(cmd, options={})
5+
result = `#{cmd} 2>&1`
6+
raise "FAILED #{cmd} --> #{result}" if $?.success? != !options[:fail]
7+
result
8+
end
9+
10+
def spelunk(command, options={})
11+
sh "#{File.expand_path("../../bin/git-spelunk", __FILE__)} #{command}", options
12+
end
13+
14+
it "shows version" do
15+
version = /\A\d+\.\d+\.\d+\Z/m
16+
spelunk("--version").must_match version
17+
spelunk("-v").must_match version
18+
end
19+
20+
it "shows help" do
21+
spelunk("--help").must_include "Usage"
22+
spelunk("-h").must_include "Usage"
23+
end
24+
end

test/test_helper.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'bundler/setup'
2+
require 'minitest/autorun'
3+
require 'minitest/rg'

0 commit comments

Comments
 (0)