cbuildbot: branch-util --rename-to fixed to work with remote branches.

This refactors the _ProcessCheckout method in the branch-util stage to
be more clear about how it supports the create/delete/rename branch
flows.  It also changes all branch refs to be explicitly remote so that
even if a branch is not present in the local checkout a branch rename
will still work, which was not working before.  Lastly, in cbuildbot the
branch-util flow for deleting or renaming a branch implies both the
--branch and --nobootstrap options, to make sure we only look at the
projects in the manifest for that branch.

BUG=chromium:322100
TEST=unittest

Each of the following branch-util commands, all with common command
prefix: "cbuildbot branch-util --debug --local"
TEST=Create a new branch:
`<prefix> --branch-name test-5000.B --version=5000.0.0`
TEST=Fail to create a new branch that already exists:
`<prefix> --branch-name test-4980.B --version=4980.0.0`
TEST=Force create a branch that already exists:
`<prefix> --branch-name test-4980.B --version=4980.0.0 --force-create`
TEST=Delete a branch that exists:
`<prefix> --branch-name test-4980.B --delete-branch`
TEST=Deleting a branch that does not exist fails:
`<prefix> --branch-name test-foo-4980.B --delete-branch`
TEST=Rename a branch that exists:
`<prefix> --branch-name test-4980.B --rename-to test-foo-4980.B`
TEST=Renaming a branch that does not exist fails:
`<prefix> --branch-name test-foo-4980.B --rename-to test-bar-4980.B`

Change-Id: I53012cea51b5228c5cc4ec9fc14b1a0feae95161
Reviewed-on: https://chromium-review.googlesource.com/181172
Tested-by: Matt Tennant <mtennant@chromium.org>
Reviewed-by: David James <davidjames@chromium.org>
Commit-Queue: Matt Tennant <mtennant@chromium.org>
diff --git a/scripts/cbuildbot.py b/scripts/cbuildbot.py
index 1731b51..37df94e 100644
--- a/scripts/cbuildbot.py
+++ b/scripts/cbuildbot.py
@@ -1318,18 +1318,45 @@
   if constants.BRANCH_UTIL_CONFIG in options.build_targets:
     if options.remote:
       cros_build_lib.Die(
-          'Running branch-util as a remote tryjob is not yet supported.')
+          'Running %s as a remote tryjob is not yet supported.',
+          constants.BRANCH_UTIL_CONFIG)
     if len(options.build_targets) > 1:
       cros_build_lib.Die(
-          'Cannot run branch-util with any other configs.')
+          'Cannot run %s with any other configs.',
+          constants.BRANCH_UTIL_CONFIG)
     if not options.branch_name:
       cros_build_lib.Die(
-          'Must specify --branch-name with the branch-util config.')
-    if not any([options.force_version, options.delete_branch,
-                options.rename_to]):
+          'Must specify --branch-name with the %s config.',
+          constants.BRANCH_UTIL_CONFIG)
+    if options.branch and options.branch != options.branch_name:
       cros_build_lib.Die(
-          'Must specify --version with the branch-util config, unless '
-          'running with --delete-branch or --rename-to.')
+          'If --branch is specified with the %s config, it must'
+          ' have the same value as --branch-name.',
+          constants.BRANCH_UTIL_CONFIG)
+
+    exclusive_opts = {'--version': options.force_version,
+                      '--delete-branch': options.delete_branch,
+                      '--rename-to': options.rename_to,
+                     }
+    if 1 != sum(1 for x in exclusive_opts.values() if x):
+      cros_build_lib.Die('When using the %s config, you must'
+                         ' specifiy one and only one of the following'
+                         ' options: %s.', constants.BRANCH_UTIL_CONFIG,
+                         ', '.join(exclusive_opts.keys()))
+
+    # When deleting or renaming a branch, the --branch and --nobootstrap
+    # options are implied.
+    if options.delete_branch or options.rename_to:
+      if not options.branch:
+        cros_build_lib.Info('Automatically enabling sync to branch %s'
+                            ' for this %s flow.', options.branch_name,
+                            constants.BRANCH_UTIL_CONFIG)
+        options.branch = options.branch_name
+      if options.bootstrap:
+        cros_build_lib.Info('Automatically disabling bootstrap step for'
+                            ' this %s flow.', constants.BRANCH_UTIL_CONFIG)
+        options.bootstrap = False
+
   elif any([options.delete_branch, options.rename_to, options.branch_name]):
     cros_build_lib.Die(
         'Cannot specify --delete-branch, --rename-to or --branch-name when not '