Add 'repo selfupdate' to upgrade only repo

Users may want to upgrade only repo to the latest release, but
leave their working tree state alone and avoid 'repo sync'.

Signed-off-by: Shawn O. Pearce <sop@google.com>
diff --git a/subcmds/selfupdate.py b/subcmds/selfupdate.py
new file mode 100644
index 0000000..de6904c
--- /dev/null
+++ b/subcmds/selfupdate.py
@@ -0,0 +1,59 @@
+#
+# Copyright (C) 2009 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from optparse import SUPPRESS_HELP
+import sys
+
+from command import Command, MirrorSafeCommand
+from subcmds.sync import _PostRepoUpgrade
+from subcmds.sync import _PostRepoFetch
+
+class Selfupdate(Command, MirrorSafeCommand):
+  common = False
+  helpSummary = "Update repo to the latest version"
+  helpUsage = """
+%prog
+"""
+  helpDescription = """
+The '%prog' command upgrades repo to the latest version, if a
+newer version is available.
+
+Normally this is done automatically by 'repo sync' and does not
+need to be performed by an end-user.
+"""
+
+  def _Options(self, p):
+    p.add_option('--no-repo-verify',
+                 dest='no_repo_verify', action='store_true',
+                 help='do not verify repo source code')
+    p.add_option('--repo-upgraded',
+                 dest='repo_upgraded', action='store_true',
+                 help=SUPPRESS_HELP)
+
+  def Execute(self, opt, args):
+    rp = self.manifest.repoProject
+    rp.PreSync()
+
+    if opt.repo_upgraded:
+      _PostRepoUpgrade(self.manifest)
+
+    else:
+      if not rp.Sync_NetworkHalf():
+        print >>sys.stderr, "error: can't update repo"
+        sys.exit(1)
+
+      _PostRepoFetch(rp,
+                     no_repo_verify = opt.no_repo_verify,
+                     verbose = True)
diff --git a/subcmds/sync.py b/subcmds/sync.py
index 79ffcf5..f6eb2a0 100644
--- a/subcmds/sync.py
+++ b/subcmds/sync.py
@@ -20,6 +20,7 @@
 import sys
 
 from git_command import GIT
+from project import HEAD
 from command import Command, MirrorSafeCommand
 from error import RepoChangedException, GitError
 from project import R_HEADS
@@ -99,26 +100,13 @@
     mp.PreSync()
 
     if opt.repo_upgraded:
-      for project in self.manifest.projects.values():
-        if project.Exists:
-          project.PostRepoUpgrade()
+      _PostRepoUpgrade(self.manifest)
 
     all = self.GetProjects(args, missing_ok=True)
 
     if not opt.local_only:
       fetched = self._Fetch(rp, mp, *all)
-
-      if rp.HasChanges:
-        print >>sys.stderr, 'info: A new version of repo is available'
-        print >>sys.stderr, ''
-        if opt.no_repo_verify or _VerifyTag(rp):
-          if not rp.Sync_LocalHalf():
-            sys.exit(1)
-          print >>sys.stderr, 'info: Restarting repo with latest version'
-          raise RepoChangedException(['--repo-upgraded'])
-        else:
-          print >>sys.stderr, 'warning: Skipped upgrade to unverified version'
-
+      _PostRepoFetch(rp, opt.no_repo_verify)
       if opt.network_only:
         # bail out now; the rest touches the working tree
         return
@@ -144,6 +132,27 @@
           sys.exit(1)
     pm.end()
 
+
+def _PostRepoUpgrade(manifest):
+  for project in manifest.projects.values():
+    if project.Exists:
+      project.PostRepoUpgrade()
+
+def _PostRepoFetch(rp, no_repo_verify=False, verbose=False):
+  if rp.HasChanges:
+    print >>sys.stderr, 'info: A new version of repo is available'
+    print >>sys.stderr, ''
+    if no_repo_verify or _VerifyTag(rp):
+      if not rp.Sync_LocalHalf():
+        sys.exit(1)
+      print >>sys.stderr, 'info: Restarting repo with latest version'
+      raise RepoChangedException(['--repo-upgraded'])
+    else:
+      print >>sys.stderr, 'warning: Skipped upgrade to unverified version'
+  else:
+    if verbose:
+      print >>sys.stderr, 'repo version %s is current' % rp.work_git.describe(HEAD)
+
 def _VerifyTag(project):
   gpg_dir = os.path.expanduser('~/.repoconfig/gnupg')
   if not os.path.exists(gpg_dir):