title: How do I check for valid Git branch names?
body: <p>I'm developing a <a href="https://github.com/alexchamberlain/githooks/blob/master/post-receive.d/jekyll-build"><code>git</code> <code>post-receive</code> hook in Python</a>. Data is supplied on <code>stdin</code> with lines similar to</p>
  
  <pre><code>ef4d4037f8568e386629457d4d960915a85da2ae 61a4033ccf9159ae69f951f709d9c987d3c9f580 refs/heads/master
  </code></pre>
  
  <p>The first hash is the old-ref, the second the new-ref and the third column is the reference being updated.</p>
  
  <p>I want to split this into 3 variables, whilst also validating input. <strong>How do I validate the branch name?</strong></p>
  
  <p>I am currently using the following regular expression</p>
  
  <pre><code>^([0-9a-f]{40}) ([0-9a-f]{40}) refs/heads/([0-9a-zA-Z]+)$
  </code></pre>
  
  <p>This doesn't accept all possible branch names, as set out by <a href="http://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html">man git-check-ref-format</a>. For example, it excludes a branch by the name of <code>build-master</code>, which is valid.</p>
  
  <h3>Bonus marks</h3>
  
  <p>I actually want to exclude any branch that starts with "build-". Can this be done in the same regex?</p>
  
  <h3>Tests</h3>
  
  <p>Given the great answers below, I wrote some tests, which can be found at
  <a href="https://github.com/alexchamberlain/githooks/blob/master/miscellaneous/git-branch-re-test.py">https://github.com/alexchamberlain/githooks/blob/master/miscellaneous/git-branch-re-test.py</a>.</p>
  
  <p><strong>Status: All the regexes below are failing to compile. This could indicate there's a problem with my script or incompatible syntaxes.</strong></p>