diff --git a/bin/homesick b/bin/homesick index 711405d..aef6100 100755 --- a/bin/homesick +++ b/bin/homesick @@ -2,7 +2,7 @@ require 'pathname' lib = Pathname.new(__FILE__).dirname.join('..', 'lib').expand_path -$LOAD_PATH.unshift lib.to_s +$LOAD_PATH.unshift lib.to_s require 'homesick' diff --git a/lib/homesick.rb b/lib/homesick.rb index 6c82838..e1a7ebb 100644 --- a/lib/homesick.rb +++ b/lib/homesick.rb @@ -12,7 +12,7 @@ class Homesick < Thor GITHUB_NAME_REPO_PATTERN = /\A([A-Za-z_-]+\/[A-Za-z_-]+)\Z/ SUBDIR_FILENAME = '.homesick_subdir' - def initialize(args=[], options={}, config={}) + def initialize(args = [], options = {}, config = {}) super self.shell = Homesick::Shell.new end @@ -33,7 +33,7 @@ class Homesick < Thor elsif uri =~ GITHUB_NAME_REPO_PATTERN destination = Pathname.new($1) git_clone "git://github.com/#{$1}.git", :destination => destination - elsif uri =~ /\/([^\/]*?)(\.git)?\Z/ + elsif uri =~ /%r([^%r]*?)(\.git)?\Z/ destination = Pathname.new($1) git_clone uri elsif uri =~ /[^:]+:([^:]+)(\.git)?\Z/ @@ -67,7 +67,7 @@ class Homesick < Thor desc 'pull CASTLE', 'Update the specified castle' method_option :all, :type => :boolean, :default => false, :required => false, :desc => 'Update all cloned castles' - def pull(name='') + def pull(name = '') if options[:all] inside_each_castle do |castle| shell.say castle.to_s.gsub(repos_dir.to_s + '/', '') + ':' @@ -149,9 +149,7 @@ class Homesick < Thor end # are we tracking something nested? Add the parent dir to the manifest - unless relative_dir.eql?(Pathname.new('.')) - subdir_add(castle, relative_dir) - end + subdir_add(castle, relative_dir) unless relative_dir.eql?(Pathname.new('.')) end desc 'list', 'List cloned castles' @@ -208,7 +206,9 @@ class Homesick < Thor dirs = Pathname.glob("#{repos_dir}/**/.git", File::FNM_DOTMATCH) # reject paths that lie inside another castle, like git submodules return dirs.reject do |dir| - dirs.any? {|other| dir != other && dir.fnmatch(other.parent.join('*').to_s) } + dirs.any? do |other| + dir != other && dir.fnmatch(other.parent.join('*').to_s) + end end end @@ -262,7 +262,9 @@ class Homesick < Thor def subdir_add(castle, path) subdir_filepath = subdir_file(castle) File.open(subdir_filepath, 'a+') do |subdir| - subdir.puts path unless subdir.readlines.inject(false) { |memo, line| line.eql?("#{path.to_s}\n") || memo } + subdir.puts path unless subdir.readlines.reduce(false) do |memo, line| + line.eql?("#{path.to_s}\n") || memo + end end inside castle_dir(castle) do @@ -308,7 +310,7 @@ class Homesick < Thor def symlink_each(castle, basedir, subdirs) absolute_basedir = Pathname.new(basedir).expand_path inside basedir do - files = Pathname.glob('{.*,*}').reject{|a| ['.', '..'].include?(a.to_s)} + files = Pathname.glob('{.*,*}').reject{ |a| ['.', '..'].include?(a.to_s) } files.each do |path| absolute_path = path.expand_path castle_home = castle_dir(castle) diff --git a/lib/homesick/actions.rb b/lib/homesick/actions.rb index 3911fdd..ef4ad24 100644 --- a/lib/homesick/actions.rb +++ b/lib/homesick/actions.rb @@ -20,7 +20,7 @@ class Homesick path = Pathname.new(path) inside path do - unless path.join('.git').exist? + if !path.join('.git').exist? say_status 'git init', '' unless options[:quiet] system 'git init >/dev/null' unless options[:pretend] else @@ -33,7 +33,7 @@ class Homesick existing_remote = `git config remote.#{name}.url`.chomp existing_remote = nil if existing_remote == '' - unless existing_remote + if !existing_remote say_status 'git remote', "add #{name} #{url}" unless options[:quiet] system "git remote add #{name} #{url}" unless options[:pretend] else diff --git a/spec/homesick_spec.rb b/spec/homesick_spec.rb index bc4e7c4..fc92849 100644 --- a/spec/homesick_spec.rb +++ b/spec/homesick_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'spec_helper' describe 'homesick' do let(:home) { create_construct } @@ -30,7 +30,12 @@ describe 'homesick' do it 'should not symlink' do homesick.should_not_receive(:git_clone) - homesick.clone @existing_dir.to_s rescue nil + begin + homesick.clone @existing_dir.to_s + fail 'homesick.clone should raise' + rescue => e + e.message.should match 'Castle already cloned to' + end end it 'should raise an error' do @@ -80,7 +85,12 @@ describe 'homesick' do it 'should not try to clone a malformed uri like malformed' do homesick.should_not_receive(:git_clone) - homesick.clone 'malformed' rescue nil + begin + homesick.clone 'malformed' + fail 'homesick.clone should raise' + rescue => e + e.message.should match 'Unknown URI format: malformed' + end end it 'should throw an exception when trying to clone a malformed uri like malformed' do @@ -129,7 +139,6 @@ describe 'homesick' do end it 'can override existing directory' do - somewhere_else = create_construct existing_dotdir = home.directory('.vim') dotdir = castle.directory('.vim') @@ -168,22 +177,22 @@ describe 'homesick' do end end - context "with '.config' and '.config/appA' in .homesick_subdir" do - let(:castle) { given_castle('glencairn', ['.config', '.config/appA']) } - it 'can symlink under both of .config and .config/appA' do + context "with '.config' and '.config/someapp' in .homesick_subdir" do + let(:castle) { given_castle('glencairn', ['.config', '.config/someapp']) } + it 'can symlink under both of .config and .config/someapp' do config_dir = castle.directory('.config') config_dotfile = config_dir.file('.some_dotfile') - appA_dir = config_dir.directory('appA') - appA_dotfile = appA_dir.file('.some_appfile') + someapp_dir = config_dir.directory('someapp') + someapp_dotfile = someapp_dir.file('.some_appfile') homesick.symlink('glencairn') home_config_dir = home.join('.config') - home_appA_dir = home_config_dir.join('appA') + home_someapp_dir = home_config_dir.join('someapp') home_config_dir.symlink?.should be == false home_config_dir.join('.some_dotfile').readlink.should be == config_dotfile - home_appA_dir.symlink?.should be == false - home_appA_dir.join('.some_appfile').readlink.should == appA_dotfile + home_someapp_dir.symlink?.should be == false + home_someapp_dir.join('.some_appfile').readlink.should == someapp_dotfile end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 97110d7..d231ec8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -16,13 +16,13 @@ RSpec.configure do |config| homesick.stub(:say_status) end - def given_castle(path, subdirs=[]) + def given_castle(path, subdirs = []) name = Pathname.new(path).basename castles.directory(path) do |castle| Dir.chdir(castle) do system 'git init >/dev/null 2>&1' system "git remote add origin git://github.com/technicalpickles/#{name}.git >/dev/null 2>&1" - if subdirs then + if subdirs subdir_file = castle.join(Homesick::SUBDIR_FILENAME) subdirs.each do |subdir| system "echo #{subdir} >> #{subdir_file}"