repo_sync_manifest: Add -g cherry-pick support.

Add support for cherry-picking Gerrit CLs into the checkout. This
allows them to be cherry-picked in for cases that were not previously
possible, such as based on version numbers or build specs.

This CL does NOT include support for extracting changes to cherry-pick
in from buildspec manifests.

BUG=chromium:864816
TEST=run_tests

Change-Id: I030b4ca9044ecaa3596a56b5e444a0e99df2fd99
Reviewed-on: https://chromium-review.googlesource.com/1330170
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: Don Garrett <dgarrett@chromium.org>
Reviewed-by: Alec Thilenius <athilenius@google.com>
diff --git a/scripts/repo_sync_manifest.py b/scripts/repo_sync_manifest.py
index dfdd0aa..5e2f180 100644
--- a/scripts/repo_sync_manifest.py
+++ b/scripts/repo_sync_manifest.py
@@ -20,10 +20,12 @@
 from __future__ import print_function
 
 from chromite.cbuildbot import manifest_version
+from chromite.cbuildbot import patch_series
 from chromite.cbuildbot import repository
 from chromite.lib import commandline
 from chromite.lib import cros_logging as logging
 from chromite.lib import config_lib
+from chromite.lib import gerrit
 from chromite.lib import osutils
 
 
@@ -59,6 +61,16 @@
            'manifest-versions-internal to manifest-versions for buildspecs. '
            'Not usable with --manifest.')
 
+  patch_group = parser.add_argument_group(
+      'Patch',
+      description='Which patches should be included with the build?')
+  patch_group.add_argument(
+      '-g', '--gerrit-patches', action='split_extend', default=[],
+      metavar='Id1 *int_Id2...IdN',
+      help='Space-separated list of short-form Gerrit '
+           "Change-Id's or change numbers to patch. "
+           "Please prepend '*' to internal Change-Id's")
+
   resources_group = parser.add_argument_group(
       'Resources',
       description='External resources that might be needed.')
@@ -187,4 +199,14 @@
 
   repo.Sync(local_manifest=local_manifest, detach=True)
 
-  # TODO: Cherry-pick in changes.
+  if options.gerrit_patches:
+    patches = gerrit.GetGerritPatchInfo(options.gerrit_patches)
+    # TODO: Extract patches from manifest synced.
+
+    helper_pool = patch_series.HelperPool.SimpleCreate(
+        cros_internal=not options.external, cros=True)
+
+    series = patch_series.PatchSeries(
+        path=options.repo_root, helper_pool=helper_pool, forced_manifest=None)
+
+    series.Apply(patches)