title: Using capistrano to deploy from different git branches
body: <p>I am using capistrano to deploy a RoR application. The codebase is in a git repository, and branching is widely used in development. Capistrano uses <code>deploy.rb</code> file for it's settings, one of them being the branch to deploy from. </p>
  
  <p>My problem is this: let's say I create a new branch <em>A</em> from <em>master</em>. The deploy file will reference <em>master</em> branch. I edit that, so <em>A</em> can be deployed to test environment. I finish working on the feature, and merge branch <em>A</em> into <em>master</em>. Since the <code>deploy.rb</code> file from <em>A</em> is fresher, it gets merged in and now the <code>deploy.rb</code> in <em>master</em> branch references <em>A</em>. Time to edit again.</p>
  
  <p>That's a lot of seemingly unnecessary manual editing - the parameter should always match current branch name. On top of that, it is easy to forget to edit the settings each and every time.</p>
  
  <p>What would be the best way to automate this process?</p>
  
  <p><strong>Edit:</strong> Turns out <a href="http://web.archive.org/web/20121006054725/http://www.harukizaemon.com/2008/05/deploying-branches-with-capistrano.html" rel="noreferrer">someone already had done exactly what I needed</a>:</p>
  
  <blockquote>
    <p>This morning I had occasion to deploy a branch of a git repository to
    a staging server but hadn’t the foggiest idea how. A quick search
    through the capistrano source code revealed that I could use set
    <code>:branch "branch_name"</code> in my deploy script. I tried it and it worked.
    I then figured I would need to make a similar change across all my
    branches. Of course, I’m a lazy sod and wondered if there wasn’t a
    better way.</p>
    
    <p>If you’re not familiar with git, the output of the git branch command
    is a list of branches with an asterisk marking the one currently
    checked out on your local machine. For example:</p>
  
  <pre><code>&gt; git branch
  * drupal_authentication
  fragment_caching
  master
  </code></pre>
    
    <p>So, I figured, what if I just parsed the output and searched for the
    branch marked as current:</p>
  
  <pre><code>set :branch, $1 if `git branch` =~ /\* (\S+)\s/m
  </code></pre>
    
    <p>Now I’m able to deploy whatever branch is current on my local machine
    from a single, shared, deploy script.</p>
  </blockquote>