depot_tools: Send the remote tracked ref to Rietveld via upload.py.

This change goes hand in hand with the corresponding Rietveld change here:
https://codereview.chromium.org/773083004/
The motivation for both CLs came from the discussion in the internal CL: https://chromereviews.googleplex.com/115567013/

AFAIK either change can be submitted first without breaking anything in the other framework.

Observe the "Tracked Ref" field in the below CLs-

Tracking a remote ref:
* https://skia-codereview-staging.appspot.com/8861001 (Tracking skiabot-test's refs/heads/master)
* https://skia-codereview-staging.appspot.com/851002 (Tracking skiabot-test's refs/diff/test1)
* https://skia-codereview-staging.appspot.com/2891001 (Tracking Chromium's refs/heads/master)
* https://skia-codereview-staging.appspot.com/1931003 (Tracking Chromium's refs/branch-heads/1916)

Tracking a local branch which in turn tracks a remote ref:
* https://skia-codereview-staging.appspot.com/3891002 (Transitively tracking skiabot-test's refs/heads/master)
* https://skia-codereview-staging.appspot.com/4921001 (Transitively tracking Chromium's refs/branch-heads/1916)

CL when no target_ref is specified in depot_tools/third_party/upload.py:
* https://skia-codereview-staging.appspot.com/3871003 (CL with missing target_ref should default to /refs/heads/master)


Try the above links with and without the 'Deprecated UI' checked in https://skia-codereview-staging.appspot.com/settings


BUG=435702

Review URL: https://codereview.chromium.org/781523002

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@293334 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/git_cl.py b/git_cl.py
index 778c468..de9433f 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -1747,6 +1747,21 @@
                       + cl.GetUpstreamBranch().split('/')[-1])
   if remote_url:
     upload_args.extend(['--base_url', remote_url])
+    remote, remote_branch = cl.GetRemoteBranch()
+    if remote and remote_branch:
+      # Create the true path to the remote branch.
+      # Does the following translation:
+      # * refs/remotes/origin/refs/diff/test -> refs/diff/test
+      # * refs/remotes/origin/master -> refs/heads/master
+      # * refs/remotes/branch-heads/test -> refs/branch-heads/test
+      if remote_branch.startswith('refs/remotes/%s/refs/' % remote):
+        remote_branch = remote_branch.replace('refs/remotes/%s/' % remote, '')
+      elif remote_branch.startswith('refs/remotes/%s/' % remote):
+        remote_branch = remote_branch.replace('refs/remotes/%s/' % remote,
+                                              'refs/heads/')
+      elif remote_branch.startswith('refs/remotes/branch-heads'):
+        remote_branch = remote_branch.replace('refs/remotes/', 'refs/')
+      upload_args.extend(['--target_ref', remote_branch])
 
   project = settings.GetProject()
   if project:
diff --git a/third_party/upload.py b/third_party/upload.py
index 6b71432..1940fd3 100755
--- a/third_party/upload.py
+++ b/third_party/upload.py
@@ -650,6 +650,10 @@
                  help="Base URL path for files (listed as \"Base URL\" when "
                  "viewing issue).  If omitted, will be guessed automatically "
                  "for SVN repos and left blank for others.")
+group.add_option("--target_ref", action="store", dest="target_ref",
+                 default=None,
+                 help="The target ref that is transitively tracked by the "
+                 "local branch this patch comes from.")
 group.add_option("--download_base", action="store_true",
                  dest="download_base", default=False,
                  help="Base files will be downloaded by the server "
@@ -2605,6 +2609,8 @@
     form_fields.append(("cc", options.cc))
   if options.project:
     form_fields.append(("project", options.project))
+  if options.target_ref:
+    form_fields.append(("target_ref", options.target_ref))
 
   # Process --message, --title and --file.
   message = options.message or ""