Skip to content

Instantly share code, notes, and snippets.

@rondy
Created March 30, 2012 22:32
Show Gist options
  • Select an option

  • Save rondy/2256526 to your computer and use it in GitHub Desktop.

Select an option

Save rondy/2256526 to your computer and use it in GitHub Desktop.

Revisions

  1. rondy revised this gist May 19, 2012. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ def ask
    end

    def invalid_branch?(branch)
    !git_branch.all.include? branch
    !git_branch.exists? branch
    end

    def git_branch
    @@ -42,6 +42,10 @@ def all
    branches.collect(&removing_asterisk_from_current_branch)
    end

    def exists?(branch)
    branches.include? branch
    end

    private

    # => ["* master", "feature_1"]
    @@ -52,4 +56,4 @@ def branches
    def removing_asterisk_from_current_branch
    lambda { |branch| branch.gsub(/^\* /,"") }
    end
    end
    end
  2. rondy revised this gist May 19, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,10 @@
    end

    def prompt_branch_name
    DeployBranchName.new.prompt
    BranchedDeploy.new.prompt
    end

    class DeployBranchName
    class BranchedDeploy
    def prompt
    begin
    branch = ask
  3. rondy created this gist Mar 30, 2012.
    55 changes: 55 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    namespace :deploy do
    task :set_branch do
    set :branch, prompt_branch_name
    end
    before "deploy:update" , "deploy:set_branch"
    end

    def prompt_branch_name
    DeployBranchName.new.prompt
    end

    class DeployBranchName
    def prompt
    begin
    branch = ask
    end while invalid_branch? branch
    branch
    end

    private

    # => * Type branch name to deploy: (master, feature_1) |master|
    def ask
    Capistrano::CLI.ui.ask(" * Type branch name to deploy (#{git_branch.all.join(', ')}):") { |q| q.default = git_branch.current }
    end

    def invalid_branch?(branch)
    !git_branch.all.include? branch
    end

    def git_branch
    @git_branch ||= GitBranch.new
    end
    end

    class GitBranch
    def current
    branches.detect { |branch| branch.start_with? "*" }[2..-1]
    end

    def all
    branches.collect(&removing_asterisk_from_current_branch)
    end

    private

    # => ["* master", "feature_1"]
    def branches
    @branches ||= `git branch`.split("\n").map(&:strip)
    end

    def removing_asterisk_from_current_branch
    lambda { |branch| branch.gsub(/^\* /,"") }
    end
    end