Skip to content

Commit

Permalink
added ability to paste SHA instead of line #. added specs. some refac…
Browse files Browse the repository at this point in the history
…toring.
  • Loading branch information
Charles Finkel authored and charleseff committed Jan 29, 2012
1 parent 52d4450 commit d27abff
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 37 deletions.
4 changes: 3 additions & 1 deletion Rakefile
Expand Up @@ -3,7 +3,9 @@ require "bundler/gem_tasks"
require 'rubygems'
require 'cucumber'
require 'cucumber/rake/task'
require 'rspec/core/rake_task'

Cucumber::Rake::Task.new(:features)
RSpec::Core::RakeTask.new(:spec)

task :default => :features
task :default => [:spec, :features]
6 changes: 1 addition & 5 deletions bin/git-blame-game
@@ -1,10 +1,6 @@
#!/usr/bin/env ruby
$:.unshift(File.dirname(__FILE__) + '/../lib') unless $:.include?(File.dirname(__FILE__) + '/../lib')

require "rubygems"
require 'colorize'
require 'optparse'
require 'git-blame-game/git_blame_game'
require 'git-blame-game'

options = {}
OptionParser.new do |opts|
Expand Down
58 changes: 51 additions & 7 deletions features/blaming.feature
Expand Up @@ -16,7 +16,7 @@ Feature: Blaming
"""

Scenario: Specifying a file that doesn't exist:
When I run `bin/git-blame-game file/that/doesnt/exist.rb`
When I run `bin/git-blame-game file/that/doesnt/exist.rb`
Then it should fail with:
"""
fatal: no such path file/that/doesnt/exist.rb in HEAD
Expand All @@ -33,12 +33,13 @@ Feature: Blaming
de2a1d78 (Carmen Cummings 2012-01-14 14:49:00 -0800 4) end
de2a1d78 (Carmen Cummings 2012-01-14 14:49:00 -0800 5) end
Which line are you concerned with? (1 to 5) >
Which line are you concerned with?
Enter a number from 1 to 5 or paste the SHA you want to show >
"""
When I type "foobar"
Then the next bit of output should contain, ignoring spaces:
"""
Invalid input. Enter a number from 1 to 5 >
Invalid input. Enter a number from 1 to 5 or paste the SHA you want to show >
"""
When I type "3"
Then the next bit of output should contain, ignoring spaces:
Expand Down Expand Up @@ -86,7 +87,8 @@ Feature: Blaming
de2a1d78 (Carmen Cummings 2012-01-14 14:49:00 -0800 4) end
de2a1d78 (Carmen Cummings 2012-01-14 14:49:00 -0800 5) end
Which line are you concerned with? (1 to 5) >
Which line are you concerned with?
Enter a number from 1 to 5 or paste the SHA you want to show >
"""
When I type "3"
Then the next bit of output should contain, ignoring spaces:
Expand Down Expand Up @@ -143,7 +145,8 @@ Feature: Blaming
^f603a9a (Alice Amos 2012-01-14 14:46:18 -0800 4)
^f603a9a (Alice Amos 2012-01-14 14:46:18 -0800 5) puts add_4(9) # should be 13
Which line are you concerned with? (1 to 5) >
Which line are you concerned with?
Enter a number from 1 to 5 or paste the SHA you want to show >
"""
When I type "2"
Then the next bit of output should contain, ignoring spaces:
Expand Down Expand Up @@ -192,7 +195,8 @@ Feature: Blaming
^f603a9a (Alice Amos 2012-01-14 14:46:18 -0800 4)
^f603a9a (Alice Amos 2012-01-14 14:46:18 -0800 5) puts add_4(9) # should be 13
Which line are you concerned with? (1 to 5) >
Which line are you concerned with?
Enter a number from 1 to 5 or paste the SHA you want to show >
"""
When I type "2"
Then the next bit of output should contain, ignoring spaces:
Expand Down Expand Up @@ -228,4 +232,44 @@ Feature: Blaming
Date: Sat Jan 14 14:46:53 2012 -0800
being bad
"""
"""

Scenario: Entering the SHA instead of the number
Given I cd to "test/fixtures/sample_git_repo"
When I run `../../../bin/git-blame-game add.rb` interactively
Then the next bit of output should contain, ignoring spaces:
"""
de2a1d78 (Carmen Cummings 2012-01-14 14:49:00 -0800 1) module Add
5087eab5 (Danny Dover 2012-01-14 14:50:06 -0800 2) def add_4(y)
5087eab5 (Danny Dover 2012-01-14 14:50:06 -0800 3) y + 5
de2a1d78 (Carmen Cummings 2012-01-14 14:49:00 -0800 4) end
de2a1d78 (Carmen Cummings 2012-01-14 14:49:00 -0800 5) end
Which line are you concerned with?
Enter a number from 1 to 5 or paste the SHA you want to show >
"""
When I type "5087eab5"
Then the next bit of output should contain, ignoring spaces:
"""
commit 5087eab56af9b0901a1b190de14f29867307c140
Author: Danny Dover <developers+danny@foo.com>
Date: Sat Jan 14 14:50:06 2012 -0800
I like y's better
diff --git a/add.rb b/add.rb
index 44be98f..898a812 100644
--- a/add.rb
+++ b/add.rb
@@ -1,5 +1,5 @@
module Add
- def add_4(x)
- x + 5
+ def add_4(y)
+ y + 5
end
end
\ No newline at end of file
Do you need to git blame chain further? (y/n) >
"""
6 changes: 4 additions & 2 deletions lib/git-blame-game.rb
@@ -1,2 +1,4 @@
# Load nothing - just keep the file here to keep bundler happy.

require 'rubygems'
require 'colorize'
require 'optparse'
require 'git-blame-game/git_blame_game'
48 changes: 26 additions & 22 deletions lib/git-blame-game/git_blame_game.rb
Expand Up @@ -17,13 +17,10 @@ def loop
cmd = "git blame #{@sha} -- #{@path_to_file}"
system cmd
exit $?.exitstatus unless $?.success?
out = `#{cmd}`
git_blame_out = `#{cmd}`

lines = out.split("\n")
count = lines.count

line = prompt_for_line(count)
sha_to_show = lines[line-1][GIT_BLAME_REGEX, 1]
sha_list = get_sha_list(git_blame_out)
sha_to_show = prompt_for_sha(sha_list)

p_flush("\n")
system "git show #{sha_to_show}"
Expand All @@ -37,21 +34,25 @@ def loop
true
end

def get_sha_list(git_blame_out)
git_blame_out.strip.split("\n").map { |line| line[GIT_BLAME_REGEX, 1] }
end

def p_flush(str)
$stdout.print str
$stdout.flush
end

def prompt_for_continue(sha)
p_flush "\n" + gbc_color("Do you need to git blame chain further? (y/n) >") + ' '
p_flush "\n" + color("Do you need to git blame chain further? (y/n) >") + ' '
input = $stdin.gets.strip.downcase
until %w{y n}.include?(input)
p_flush "\n" + gbc_color("Invalid input. Enter y or n >") + ' '
p_flush "\n" + color("Invalid input. Enter y or n >") + ' '
input = $stdin.gets.strip.downcase
end

if input == 'n'
p_flush "\n" + gbc_color("The responsible commit is:") + "\n\n"
p_flush "\n" + color("The responsible commit is:") + "\n\n"
system "git log #{sha} -n 1"
exit 0
end
Expand All @@ -64,29 +65,32 @@ def prompt_for_file(files_changed)
$stdout.flush
end

p_flush "\n" + gbc_color("Enter the number (from 1 to #{files_changed.size}) of the file to git blame chain into >") + ' '
p_flush "\n" + color("Enter the number (from 1 to #{files_changed.size}) of the file to git blame chain into >") + ' '
until (input = $stdin.gets.strip.to_i) >= 1 && input <= files_changed.size
p_flush "\n" + gbc_color("Invalid input. Enter a number from 1 to #{files_changed.size} >") + ' '
p_flush "\n" + color("Invalid input. Enter a number from 1 to #{files_changed.size} >") + ' '
end

return files_changed[input-1]

end

def prompt_for_line(count)
p_flush "\n" + gbc_color("Which line are you concerned with? (1 to #{count}) >") + ' '
until (input = $stdin.gets.strip.to_i) >= 1 && input <= count
p_flush "\n" + gbc_color("Invalid input. Enter a number from 1 to #{count} >") + ' '
end
input
end
def prompt_for_sha(shas)
p_flush "\n" + color("Which line are you concerned with?\n" + "Enter a number from 1 to #{shas.count} or paste the SHA you want to show >") + ' '
while true
input = $stdin.gets.strip
# sha was entered, return it:
return input if shas.include? input

def run_idempotent_git_command(cmd)
system cmd
`#{cmd}`
if input =~ /\A\d+\Z/
input = input.to_i
return shas[input - 1] if input <= shas.count && input >= 1
end

p_flush "\n" + color("Invalid input. Enter a number from 1 to #{shas.count} or paste the SHA you want to show >") + ' '
end
end

def gbc_color(s)
def color(s)
s.colorize(:color => :light_white, :background => :magenta)
end
end
47 changes: 47 additions & 0 deletions spec/git-blame-game/git_blame_game_spec.rb
@@ -0,0 +1,47 @@
require 'spec_helper'

describe GitBlameGame do

let(:git_blame_game) { GitBlameGame.new('some_file') }
let(:sha_list) { %w(de2a1d78 5087eab5 5087eab5 de2a1d78 de2a1d78) }

before do
$stdout.stub(:print)
end

describe "#get_sha_list" do
let(:git_blame_out) {
<<-END.gsub(/^[ \t]+/m, '')
de2a1d78 (Carmen Cummings 2012-01-14 14:49:00 -0800 1) module Add
5087eab5 (Danny Dover 2012-01-14 14:50:06 -0800 2) def add_4(y)
5087eab5 (Danny Dover 2012-01-14 14:50:06 -0800 3) y + 5
de2a1d78 (Carmen Cummings 2012-01-14 14:49:00 -0800 4) end
de2a1d78 (Carmen Cummings 2012-01-14 14:49:00 -0800 5) end
END
}

it "should return a list of shas" do
git_blame_game.get_sha_list(git_blame_out).should == sha_list
end
end

describe "prompt_for_sha" do
before do
$stdin.should_receive(:gets).and_return(input)
end
context "when user enters a correct sha" do
let(:input) { '5087eab5' }
it "should return the correct sha" do
git_blame_game.prompt_for_sha(sha_list).should == '5087eab5'
end
end
context "when user enters a correct number" do
let(:input) { '1' }
it "should return the correct sha" do
git_blame_game.prompt_for_sha(sha_list).should == 'de2a1d78'
end
end
context "when user enters an incorrect value"
end

end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
@@ -0,0 +1 @@
require 'git-blame-game'

0 comments on commit d27abff

Please sign in to comment.