From f4d45b048ede5e66bc2d16c9eefb387619566089 Mon Sep 17 00:00:00 2001 From: Josh Nichols Date: Fri, 18 Apr 2008 23:03:59 -0400 Subject: [PATCH 1/3] Added support for specifying what type of repository you are trying to import. --- lib/piston/cli.rb | 9 ++++++++- lib/piston/commands/import.rb | 31 +++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/piston/cli.rb b/lib/piston/cli.rb index b5aec6d..c2c880d 100644 --- a/lib/piston/cli.rb +++ b/lib/piston/cli.rb @@ -65,6 +65,12 @@ def target_revision default false description "Automatically lock down the revision/commit to protect against blanket updates" end + + option("repository-type") do + argument :required + default nil + description "Force a specific repository type, for when it's not possible to guess" + end logger_level Logger::DEBUG def run @@ -78,7 +84,8 @@ def run :verbose => params["verbose"].value, :quiet => params["quiet"].value, :force => params["force"].value, - :dry_run => params["dry-run"].value) + :dry_run => params["dry-run"].value, + :repository_type => params["repository-type"].value) cmd.run(params[:repository].value, self.target_revision, params[:directory].value) end end diff --git a/lib/piston/commands/import.rb b/lib/piston/commands/import.rb index abb1e37..f3b2111 100644 --- a/lib/piston/commands/import.rb +++ b/lib/piston/commands/import.rb @@ -8,10 +8,26 @@ class Import < Piston::Commands::Base def temp_dir_name(working_copy) working_copy.path.parent + ".#{working_copy.path.basename}.tmp" end + + def repository_type + options[:repository_type] + end + + def determine_repository(repository_url) + if repository_type + logger.info {"Forced repository type to #{repository_type}"} + repository_class_name = "Piston::#{repository_type.downcase.capitalize}::Repository" + repository_class = constantize(repository_class_name) + repository = repository_class.new(repository_url) + else + logger.info {"Guessing the repository type"} + repository = Piston::Repository.guess(repository_url) + end + repository + end def run(repository_url, target_revision, wcdir) - logger.info {"Guessing the repository type"} - repository = Piston::Repository.guess(repository_url) + repository = determine_repository(repository_url) revision = repository.at(target_revision) wcdir = wcdir.nil? ? repository.basename : wcdir @@ -48,6 +64,17 @@ def run(repository_url, target_revision, wcdir) tmpdir.rmtree rescue nil end end + + protected + # riped out of activesupport + def constantize(camel_cased_word) + unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word + raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!" + end + + Object.module_eval("::#{$1}", __FILE__, __LINE__) + end + end end end From 54a0436ca6787bb99a5950c9587f3ad4a6654f5e Mon Sep 17 00:00:00 2001 From: Josh Nichols Date: Fri, 18 Apr 2008 23:34:45 -0400 Subject: [PATCH 2/3] Repositories know what 'type' of repository they handle, ie Piston::Svn::Repository handles 'git'. When don't know how to handle a repository, suggests to use --repository-type and gives valid types. --- lib/piston/cli.rb | 1 + lib/piston/git/repository.rb | 4 ++++ lib/piston/repository.rb | 8 +++++++- lib/piston/svn/repository.rb | 4 ++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/piston/cli.rb b/lib/piston/cli.rb index c2c880d..a664598 100644 --- a/lib/piston/cli.rb +++ b/lib/piston/cli.rb @@ -86,6 +86,7 @@ def run :force => params["force"].value, :dry_run => params["dry-run"].value, :repository_type => params["repository-type"].value) + cmd.run(params[:repository].value, self.target_revision, params[:directory].value) end end diff --git a/lib/piston/git/repository.rb b/lib/piston/git/repository.rb index 3c6c777..4daafbc 100644 --- a/lib/piston/git/repository.rb +++ b/lib/piston/git/repository.rb @@ -27,6 +27,10 @@ def client def git(*args) client.git(*args) end + + def repository_type + 'git' + end end def git(*args) diff --git a/lib/piston/repository.rb b/lib/piston/repository.rb index dac3886..d83dc03 100644 --- a/lib/piston/repository.rb +++ b/lib/piston/repository.rb @@ -17,7 +17,13 @@ def guess(url) handler.understands_url?(url) end - raise UnhandledUrl, "No internal handlers found for #{url.inspect}. Do you want to help ?" if handler.nil? + if handler.nil? + supported_types = handlers.collect do |handler| + handler.repository_type + end + message = "No internal handlers found for #{url.inspect}. You should check out --repository-type. Supported types are: #{supported_types.join(', ')}" + raise UnhandledUrl, message + end handler.new(url) end diff --git a/lib/piston/svn/repository.rb b/lib/piston/svn/repository.rb index b20ad6c..bc3a70e 100644 --- a/lib/piston/svn/repository.rb +++ b/lib/piston/svn/repository.rb @@ -29,6 +29,10 @@ def client def svn(*args) client.svn(*args) end + + def repository_type + 'svn' + end end def svn(*args) From ba77f51f2c8ac783fe93227ef66bd08e52552bcc Mon Sep 17 00:00:00 2001 From: Josh Nichols Date: Sat, 19 Apr 2008 17:50:02 -0400 Subject: [PATCH 3/3] Added nice error message for when can't guess the type of a repository. --- lib/piston/cli.rb | 17 +++++++++++++++-- lib/piston/repository.rb | 12 +++--------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/piston/cli.rb b/lib/piston/cli.rb index a664598..286f231 100644 --- a/lib/piston/cli.rb +++ b/lib/piston/cli.rb @@ -86,8 +86,21 @@ def run :force => params["force"].value, :dry_run => params["dry-run"].value, :repository_type => params["repository-type"].value) - - cmd.run(params[:repository].value, self.target_revision, params[:directory].value) + + begin + cmd.run(params[:repository].value, self.target_revision, params[:directory].value) + rescue Piston::Repository::UnhandledUrl => e + supported_types = Piston::Repository.handlers.collect do |handler| + handler.repository_type + end + puts "Unsure how to handle:" + puts "\t#{params[:repository].value.inspect}." + puts "You should try using --repository-type. Supported types are:" + supported_types.each do |type| + puts "\t#{type}" + end + exit_failure! + end end end diff --git a/lib/piston/repository.rb b/lib/piston/repository.rb index d83dc03..1add9aa 100644 --- a/lib/piston/repository.rb +++ b/lib/piston/repository.rb @@ -16,14 +16,9 @@ def guess(url) logger.debug {"Asking #{handler}"} handler.understands_url?(url) end - - if handler.nil? - supported_types = handlers.collect do |handler| - handler.repository_type - end - message = "No internal handlers found for #{url.inspect}. You should check out --repository-type. Supported types are: #{supported_types.join(', ')}" - raise UnhandledUrl, message - end + + raise UnhandledUrl unless handler + handler.new(url) end @@ -35,7 +30,6 @@ def add_handler(handler) def handlers @@handlers end - private :handlers end attr_reader :url