Extract bug number from branch name for git cl upload

If you name your branches "bug-123-fix-foobar" or "123-fix-foobar", git cl
upload will automatically populate the "Bug: " line in the cl description with
123.

Bug: 778252
Change-Id: I5bf570d5d44e8681c552a52a8b5ed0c64ce82284
Reviewed-on: https://chromium-review.googlesource.com/738237
Commit-Queue: Aaron Gable <agable@chromium.org>
Reviewed-by: Aaron Gable <agable@chromium.org>
diff --git a/git_cl.py b/git_cl.py
index 178a40f..335b8d0 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -2902,6 +2902,12 @@
     title = options.title
     automatic_title = False
 
+    # Extract bug number from branch name.
+    bug = options.bug
+    match = re.match(r'(?:bug|fix)[_-]?(\d+)', self.GetBranch())
+    if not bug and match:
+      bug = match.group(1)
+
     if options.squash:
       self._GerritCommitMsgHookCheck(offer_removal=not options.force)
       if self.GetIssue():
@@ -2952,7 +2958,7 @@
           confirm_or_exit(action='edit')
           if not options.force:
             change_desc = ChangeDescription(message)
-            change_desc.prompt(bug=options.bug)
+            change_desc.prompt(bug=bug)
             message = change_desc.description
             if not message:
               DieWithError("Description is empty. Aborting...")
@@ -2971,7 +2977,7 @@
         change_desc = ChangeDescription(message)
 
         if not options.force:
-          change_desc.prompt(bug=options.bug)
+          change_desc.prompt(bug=bug)
         # On first upload, patchset title is always this string, while
         # --title flag gets converted to first line of message.
         title = 'Initial upload'
@@ -4868,6 +4874,10 @@
   To unset run:
     git config --unset branch.branch_name.skip-deps-uploads
   Can also set the above globally by using the --global flag.
+
+  If the name of the checked out branch starts with "bug-" or "fix-" followed by
+  a bug number, this bug number is automatically populated in the CL
+  description.
   """
   parser.add_option('--bypass-hooks', action='store_true', dest='bypass_hooks',
                     help='bypass upload presubmit hook')