Add --upstream option to gclient to scrape git checkouts back to upstream state.

This will be used on the bots when calling apply_patch, which commits to git
repos.

[email protected],[email protected]
BUG=

Review URL: https://chromiumcodereview.appspot.com/18006002

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@209040 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/gclient_scm.py b/gclient_scm.py
index 2cdee7d..3e8f59c 100644
--- a/gclient_scm.py
+++ b/gclient_scm.py
@@ -430,7 +430,10 @@
 
     # This is a big hammer, debatable if it should even be here...
     if options.force or options.reset:
-      self._Run(['reset', '--hard', 'HEAD'], options)
+      target = 'HEAD'
+      if options.upstream and upstream_branch:
+        target = upstream_branch
+      self._Run(['reset', '--hard', target], options)
 
     if current_type == 'detached':
       # case 0
@@ -578,6 +581,10 @@
       return self.update(options, [], file_list)
 
     default_rev = "refs/heads/master"
+    if options.upstream:
+      if self._GetCurrentBranch():
+        upstream_branch = scm.GIT.GetUpstreamBranch(self.checkout_path)
+        default_rev = upstream_branch or default_rev
     _, deps_revision = gclient_utils.SplitUrlRevision(self.url)
     if not deps_revision:
       deps_revision = default_rev