Module: CapistranoMisc::Misc::Branch

Defined in:
lib/capistrano-misc/misc/branch.rb

Class Method Summary collapse

Class Method Details

.load_into(configuration) ⇒ Object



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
# File 'lib/capistrano-misc/misc/branch.rb', line 3

def self.load_into(configuration)
  configuration.load do
    namespace :misc do
      desc 'Select branch to deploy from console'
      task :branch do
        branch = fetch(:branch, nil)
        next if branch.is_a?(String)
      
        regexp = branch.is_a?(Regexp) ? branch : /.*/
        branches = run_locally(%Q|#{scm} fetch && #{scm} branch -a|).split("\n").map{ |s| s.sub(/^[ *]*/, "") }
        branches = branches.grep(regexp)
      
        options = ['0: Use revision number']
        options.concat branches.map.with_index{|revision, idx|"#{idx+1}: #{revision}"}
        options.concat ['q: Quit']
        while
          s = Capistrano::CLI.ui.ask(%Q|Choose branch from \n#{options.join("\n")}\n or type tag to deploy (make sure to push the tag first): |).strip
          if s =~ /^(q|quit)$/i
            exit
          elsif s.to_i.to_s != s
            next
          end
          break
        end
      
        case idx = s.to_i
        when 0
          tag = Capistrano::CLI.ui.ask('Type revision or tag to deploy: ').strip
        else
          tag = branches[idx-1]
        end
      
        if tag.nil? || tag !~ regexp
          logger.important "err :: The branch you specified is not allowed to deploy"
          exit
        end
      
        set :branch, tag
      end
    end
  end
end