public
Description: Piston is a utility that eases vendor branch management. This repository is a complete reimplementation of Piston to provide different backends, depending on the repositories and working copies you pistonize from.
Homepage: http://piston.rubyforge.org/
Clone URL: git://github.com/francois/piston.git
commit  a2ef04d7498a9f3dd83cc4ee22b14eeb218f9881
tree    374b0c68d01dd9639cc30b3429ae6b59a35c62a7
parent  10863f19e2e48c704983bb1618aacd0f29ebae9c
piston / lib / piston / commands / import.rb
100644 80 lines (63 sloc) 2.723 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require "piston/commands/base"
 
module Piston
  module Commands
    class Import < Piston::Commands::Base
      attr_reader :options
 
      def temp_dir_name(working_copy)
        working_copy.path.parent + ".#{working_copy.path.basename}.tmp"
      end
      
      def repository_type
        options[:repository_type]
      end
      
      def select_repository(repository_url)
        if repository_type then
          logger.info {"Forced repository type to #{repository_type}"}
          repository_class_name = "Piston::#{repository_type.downcase.capitalize}::Repository"
          repository_class = constantize(repository_class_name)
          repository_class.new(repository_url)
        else
          logger.info {"Guessing the repository type"}
          Piston::Repository.guess(repository_url)
        end
      end
 
      def run(repository_url, target_revision, wcdir)
        repository = select_repository(repository_url)
        revision = repository.at(target_revision)
 
        wcdir = wcdir.nil? ? repository.basename : wcdir
        logger.info {"Guessing the working copy type"}
        logger.debug {"repository_url: #{repository_url.inspect}, target_revision: #{target_revision.inspect}, wcdir: #{wcdir.inspect}"}
        working_copy = guess_wc(wcdir)
 
        tmpdir = temp_dir_name(working_copy)
 
        if working_copy.exist? && !force then
          logger.fatal "Path #{working_copy} already exists and --force not given. Aborting..."
          abort
        end
 
        begin
          logger.info {"Checking out the repository"}
          revision.checkout_to(tmpdir)
 
          logger.debug {"Creating the local working copy"}
          working_copy.create
 
          logger.info {"Copying from #{revision}"}
          working_copy.copy_from(revision)
 
          logger.debug {"Remembering values"}
          working_copy.remember({:repository_url => repository.url, :lock => options[:lock], :repository_class => repository.class.name},
                                revision.remember_values)
 
          logger.debug {"Finalizing working copy"}
          working_copy.finalize
 
          logger.info {"Checked out #{repository_url.inspect} #{revision.name} to #{wcdir.inspect}"}
        ensure
          logger.debug {"Removing temporary directory: #{tmpdir}"}
          tmpdir.rmtree rescue nil
        end
      end
      
      protected
      # Copied from 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