[git-cl] allow setting the bug during force mode

Bug: 1001709
Change-Id: I53e7699cb38d810d0a8c88d40907bf249174ad8b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1804603
Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Anthony Polito <apolito@google.com>
diff --git a/git_cl.py b/git_cl.py
index cf784bb..09d9084 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -2377,26 +2377,28 @@
           # Change-Id. Thus, just create a new footer, but let user verify the
           # new description.
           message = '%s\n\nChange-Id: %s' % (message, change_id)
-          print(
-              'WARNING: change %s has Change-Id footer(s):\n'
-              '  %s\n'
-              'but change has Change-Id %s, according to Gerrit.\n'
-              'Please, check the proposed correction to the description, '
-              'and edit it if necessary but keep the "Change-Id: %s" footer\n'
-              % (self.GetIssue(), '\n  '.join(footer_change_ids), change_id,
-                 change_id))
-          confirm_or_exit(action='edit')
+          change_desc = ChangeDescription(message, bug=bug)
           if not options.force:
-            change_desc = ChangeDescription(message)
-            change_desc.prompt(bug=bug)
-            message = change_desc.description
-            if not message:
-              DieWithError("Description is empty. Aborting...")
+            print(
+                'WARNING: change %s has Change-Id footer(s):\n'
+                '  %s\n'
+                'but change has Change-Id %s, according to Gerrit.\n'
+                'Please, check the proposed correction to the description, '
+                'and edit it if necessary but keep the "Change-Id: %s" footer\n'
+                % (self.GetIssue(), '\n  '.join(footer_change_ids), change_id,
+                   change_id))
+            confirm_or_exit(action='edit')
+            change_desc.prompt()
+
+          message = change_desc.description
+          if not message:
+            DieWithError("Description is empty. Aborting...")
+
           # Continue the while loop.
         # Sanity check of this code - we should end up with proper message
         # footer.
         assert [change_id] == git_footers.get_footer_change_id(message)
-        change_desc = ChangeDescription(message)
+        change_desc = ChangeDescription(message, bug=bug)
       else:  # if not self.GetIssue()
         if options.message:
           message = options.message
@@ -2404,10 +2406,10 @@
           message = _create_description_from_log(git_diff_args)
           if options.title:
             message = options.title + '\n\n' + message
-        change_desc = ChangeDescription(message)
-
+        change_desc = ChangeDescription(message, bug=bug)
         if not options.force:
-          change_desc.prompt(bug=bug)
+          change_desc.prompt()
+
         # On first upload, patchset title is always this string, while
         # --title flag gets converted to first line of message.
         title = 'Initial upload'
@@ -2441,7 +2443,7 @@
         ref_to_push = RunGit(['commit-tree', tree, '-p', parent,
                               '-F', desc_tempfile.name]).strip()
         os.remove(desc_tempfile.name)
-    else:
+    else:  # if not options.squash
       change_desc = ChangeDescription(
           options.message or _create_description_from_log(git_diff_args))
       if not change_desc.description:
@@ -2792,8 +2794,14 @@
   COLON_SEPARATED_HASH_TAG = r'^([a-zA-Z0-9_\- ]+):'
   BAD_HASH_TAG_CHUNK = r'[^a-zA-Z0-9]+'
 
-  def __init__(self, description):
+  def __init__(self, description, bug=None):
     self._description_lines = (description or '').strip().splitlines()
+    if bug:
+      regexp = re.compile(self.BUG_LINE)
+      prefix = settings.GetBugPrefix()
+      if not any((regexp.match(line) for line in self._description_lines)):
+        values = list(_get_bug_line_values(prefix, bug))
+        self.append_footer('Bug: %s' % ', '.join(values))
 
   @property               # www.logilab.org/ticket/89786
   def description(self):  # pylint: disable=method-hidden
@@ -2888,7 +2896,7 @@
         return
     self.append_footer('Cq-Do-Not-Cancel-Tryjobs: true')
 
-  def prompt(self, bug=None, git_footer=True):
+  def prompt(self):
     """Asks the user to update the description."""
     self.set_description([
       '# Enter a description of the change.',
@@ -2897,19 +2905,13 @@
       '#--------------------This line is 72 characters long'
       '--------------------',
     ] + self._description_lines)
-
     regexp = re.compile(self.BUG_LINE)
     prefix = settings.GetBugPrefix()
     if not any((regexp.match(line) for line in self._description_lines)):
-      values = list(_get_bug_line_values(prefix, bug or '')) or [prefix]
-      if git_footer:
-        self.append_footer('Bug: %s' % ', '.join(values))
-      else:
-        for value in values:
-          self.append_footer('BUG=%s' % value)
+      self.append_footer('Bug: %s' % prefix)
 
     content = gclient_utils.RunEditor(self.description, True,
-                                      git_editor=settings.GetGitEditor())
+                                        git_editor=settings.GetGitEditor())
     if not content:
       DieWithError('Running editor failed')
     lines = content.splitlines()