From 368c48e11628f1af1f780d87ec06044e242e62eb Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Sat, 31 Mar 2012 19:00:12 +0200 Subject: [PATCH] changes --- lib/space/helpers.rb | 6 ++++- lib/space/models/bundle.rb | 8 +++---- lib/space/models/command.rb | 4 ++-- lib/space/models/commands.rb | 4 ++-- lib/space/models/git.rb | 31 ++++++++++++++++++++---- spec/models/bundle_spec.rb | 5 ++-- spec/models/git_spec.rb | 46 ++++++++++++++++++++++++++++++++++-- 7 files changed, 86 insertions(+), 18 deletions(-) diff --git a/lib/space/helpers.rb b/lib/space/helpers.rb index dd4410c..95a79c0 100644 --- a/lib/space/helpers.rb +++ b/lib/space/helpers.rb @@ -19,7 +19,11 @@ def repo_selected? end def git_status - "Git: #{format_boolean(git.clean?)}" + "Git: #{format_boolean(git.clean?)}#{git_ahead if git.ahead?}" + end + + def git_ahead + " #{git.ahead} commits ahead".ansi(:cyan) end def bundle_status diff --git a/lib/space/models/bundle.rb b/lib/space/models/bundle.rb index 0630bcf..5f857df 100644 --- a/lib/space/models/bundle.rb +++ b/lib/space/models/bundle.rb @@ -4,7 +4,7 @@ class Bundle COMMANDS = { :check => 'bundle check', - :list => 'bundle list', + :list => 'bundle list | grep %s', :config => 'bundle config' } @@ -21,9 +21,9 @@ def info end def deps - result(:list).split("\n").map do |dep| - dep =~ /(#{App.name}.*) \(\d\.\d\.\d (.+)\)/ - Dependency.new($1, $2) if Repos.names.include?($1) + result(:list, :name => App.name).split("\n").map do |dep| + matches = dep.strip.match /^\* (?[\S]+) \(\d+\.\d+\.\d+(?: (?.+))?\)/ + Dependency.new(matches[:name], matches[:ref]) end.compact end diff --git a/lib/space/models/command.rb b/lib/space/models/command.rb index d944bae..889a3f3 100644 --- a/lib/space/models/command.rb +++ b/lib/space/models/command.rb @@ -9,8 +9,8 @@ def initialize(path, command) @command = command end - def result - @result ||= chdir { strip_ansi(`#{command}`) } + def result(args = {}) + @result ||= chdir { strip_ansi(`#{command % args}`) } end def reset diff --git a/lib/space/models/commands.rb b/lib/space/models/commands.rb index ecc8c8c..6ddbbfc 100644 --- a/lib/space/models/commands.rb +++ b/lib/space/models/commands.rb @@ -6,8 +6,8 @@ def initialize(path) @path = path end - def result(command) - commands[command].result + def result(command, args = {}) + commands[command].result(args) end def commands diff --git a/lib/space/models/git.rb b/lib/space/models/git.rb index 6e4b2c8..ce67ba0 100644 --- a/lib/space/models/git.rb +++ b/lib/space/models/git.rb @@ -1,17 +1,18 @@ +# On branch master +# # Your branch is ahead of 'origin/master' by 1 commit. +# # +# nothing to commit (working directory clean) + module Space class Git include Commands COMMANDS = { - :status => 'git status -s', + :status => 'git status', :branch => 'git branch --no-color', :commit => 'git log -1 head' } - def clean? - result(:status).empty? - end - def branch result(:branch) =~ /^\* (.+)/ && $1.strip end @@ -19,5 +20,25 @@ def branch def commit result(:commit) =~ /^commit (\S{7})/ && $1 end + + def status + dirty? ? :dirty : (ahead? ? :ahead : :clean) + end + + def ahead? + ahead > 0 + end + + def ahead + result(:status) =~ /Your branch is ahead of .* by (\d+) commits?\./ ? $1.to_i : 0 + end + + def dirty? + !clean? + end + + def clean? + result(:status).include?('nothing to commit (working directory clean)') + end end end diff --git a/spec/models/bundle_spec.rb b/spec/models/bundle_spec.rb index 5de5b8b..bb31c06 100644 --- a/spec/models/bundle_spec.rb +++ b/spec/models/bundle_spec.rb @@ -3,10 +3,11 @@ describe Bundle do let(:app) { stub('app', :name => 'travis') } let(:repo) { stub('repo', :name => 'travis-ci') } - let(:bundle) { Bundle.new(app, 'path/to/repo') } + let(:bundle) { Bundle.new('path/to/repo') } before :each do app.stubs(:repos).returns([repo]) + App.stubs(:name).returns('travis') end describe 'clean?' do @@ -30,7 +31,7 @@ describe 'deps' do it 'returns dependencies listend in `bundle list` that match the app name' do - bundle.stubs(:result).with(:list).returns(" * ansi (1.4.2)\n * travis-ci (0.0.1 123456)") + bundle.stubs(:result).with(:list, :name => 'travis').returns(" * travis-ci (0.0.1 123456)") dep = bundle.deps.first [dep.name, dep.ref].should == ['travis-ci', '123456'] end diff --git a/spec/models/git_spec.rb b/spec/models/git_spec.rb index ba69daa..0c1633e 100644 --- a/spec/models/git_spec.rb +++ b/spec/models/git_spec.rb @@ -1,9 +1,51 @@ require 'spec_helper' describe Git do + let(:git) { Git.new('path/to/somewhere') } + + describe 'ahead?' do + it 'returns true if ahead is greater than 0' do + git.stubs(:ahead).returns(1) + git.ahead?.should be_true + end + + it 'returns false if ahead equals 0' do + git.stubs(:ahead).returns(0) + git.ahead?.should be_false + end + end + + describe 'ahead' do + it "returns 2 if `git status` contains: ahead of '...' by 2 commits." do + git.stubs(:result).with(:status).returns %(Your branch is ahead of 'origin/master' by 2 commits.\n\nnothing to commit (working directory clean)) + git.ahead.should == 2 + end + + it "returns 1 if `git status` contains: ahead of '...' by 1 commit." do + git.stubs(:result).with(:status).returns %(Your branch is ahead of 'origin/master' by 1 commit.\n\nnothing to commit (working directory clean)) + git.ahead.should == 1 + end + + it "returns 0 if `git status` does not contain a line about commits ahead of a remote branch." do + git.stubs(:result).with(:status).returns %(Your branch is nothing to commit (working directory clean)) + git.ahead.should == 0 + end + end + + describe 'status' do + + end + describe 'clean?' do - it 'returns true if `git status -s` is empty' - it 'returns false if `git status -s` is not empty' + it 'returns true if `git status` contains: nothing to commit' do + git.stubs(:result).with(:status).returns 'nothing to commit (working directory clean)' + git.clean?.should be_true + end + + it 'returns false if `git status` does not contain: nothing to commit' do + git.stubs(:result).with(:status).returns 'Changes not staged for commit:' + git.clean?.should be_false + end end describe 'branch' do