Extract bug/fix from branch only on create

depot_tools supports extracting information from branch name. E.g. if
branch contains fix-\d or bug-\d, commit description will contains
appropriate git footers.

However, such behavior should happen only on initial CL upload. Should
user decide to delete such footer, we shouldn't set bug / fix on any
following PS.

R=ehmaldonado@chromium.org

Bug: 1225663
Change-Id: I66adfdca070083ab66727d132919d47feb7ddd43
Fixed: 1225663
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3010709
Auto-Submit: Josip Sokcevic <sokcevic@google.com>
Reviewed-by: Andy Perelson <ajp@chromium.org>
Commit-Queue: Josip Sokcevic <sokcevic@google.com>
diff --git a/git_cl.py b/git_cl.py
index 6f5ea49..ff66b68 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -1192,7 +1192,7 @@
     return '%s/%s' % (server, issue)
 
   def GetUsePython3(self):
-      return settings.GetUsePython3()
+    return settings.GetUsePython3()
 
   def FetchDescription(self, pretty=False):
     assert self.GetIssue(), 'issue is required to query Gerrit'
@@ -1414,16 +1414,25 @@
       if options.title and options.squash:
         description = options.title + '\n\n' + description
 
-    # Extract bug number from branch name.
     bug = options.bug
     fixed = options.fixed
-    match = re.match(r'(?P<type>bug|fix(?:e[sd])?)[_-]?(?P<bugnum>\d+)',
-                     self.GetBranch())
-    if not bug and not fixed and match:
-      if match.group('type') == 'bug':
-        bug = match.group('bugnum')
-      else:
-        fixed = match.group('bugnum')
+    if not self.GetIssue():
+      # Extract bug number from branch name, but only if issue is being created.
+      # It must start with bug or fix, followed by _ or - and number.
+      # Optionally, it may contain _ or - after number with arbitrary text.
+      # Examples:
+      #   bug-123
+      #   bug_123
+      #   fix-123
+      #   fix-123-some-description
+      match = re.match(
+          r'^(?P<type>bug|fix(?:e[sd])?)[_-]?(?P<bugnum>\d+)([-_]|$)',
+          self.GetBranch())
+      if not bug and not fixed and match:
+        if match.group('type') == 'bug':
+          bug = match.group('bugnum')
+        else:
+          fixed = match.group('bugnum')
 
     change_description = ChangeDescription(description, bug, fixed)