GITC: Always update the gitc manifest from the repo manifest

This way any changes made to the main manifest are reflected in the gitc
manifest. It's also necessary to use both manifests to sync since the
information required to update the gitc manifest is actually in the repo
manifest.

This also fixes a few issues that came up when testing. notdefault
groups weren't being saved to the gitc manifest in a method that matched
'sync'. The merge branch wasn't always being set to the correct value
either.

Change-Id: I435235cb5622a048ffad0059affd32ecf71f1f5b
diff --git a/gitc_utils.py b/gitc_utils.py
index d082c8d..dd38f89 100644
--- a/gitc_utils.py
+++ b/gitc_utils.py
@@ -15,6 +15,8 @@
 
 from __future__ import print_function
 import os
+import platform
+import re
 import sys
 import time
 
@@ -22,7 +24,6 @@
 import git_config
 import wrapper
 
-
 GITC_FS_ROOT_DIR = '/gitc/manifest-rw/'
 NUM_BATCH_RETRIEVE_REVISIONID = 300
 
@@ -65,26 +66,82 @@
       sys.exit(1)
     proj.revisionExpr = gitcmd.stdout.split('\t')[0]
 
-def generate_gitc_manifest(client_dir, manifest, projects=None):
+def _manifest_groups(manifest):
+  """Returns the manifest group string that should be synced
+
+  This is the same logic used by Command.GetProjects(), which is used during
+  repo sync
+
+  @param manifest: The XmlManifest object
+  """
+  mp = manifest.manifestProject
+  groups = mp.config.GetString('manifest.groups')
+  if not groups:
+    groups = 'default,platform-' + platform.system().lower()
+  return groups
+
+def generate_gitc_manifest(gitc_manifest, manifest, paths=None):
   """Generate a manifest for shafsd to use for this GITC client.
 
-  @param client_dir: GITC client directory to install the .manifest file in.
-  @param manifest: XmlManifest object representing the repo manifest.
-  @param projects: List of projects we want to update, this must be a sublist
-                   of manifest.projects to work properly. If not provided,
-                   manifest.projects is used.
+  @param gitc_manifest: Current gitc manifest, or None if there isn't one yet.
+  @param manifest: A GitcManifest object loaded with the current repo manifest.
+  @param paths: List of project paths we want to update.
   """
+
   print('Generating GITC Manifest by fetching revision SHAs for each '
         'project.')
-  if projects is None:
-    projects = manifest.projects
+  if paths is None:
+    paths = manifest.paths.keys()
+
+  groups = [x for x in re.split(r'[,\s]+', _manifest_groups(manifest)) if x]
+
+  # Convert the paths to projects, and filter them to the matched groups.
+  projects = [manifest.paths[p] for p in paths]
+  projects = [p for p in projects if p.MatchesGroups(groups)]
+
+  if gitc_manifest is not None:
+    for path, proj in manifest.paths.iteritems():
+      if not proj.MatchesGroups(groups):
+        continue
+
+      if not proj.upstream and not git_config.IsId(proj.revisionExpr):
+        proj.upstream = proj.revisionExpr
+
+      if not path in gitc_manifest.paths:
+        # Any new projects need their first revision, even if we weren't asked
+        # for them.
+        projects.append(proj)
+      elif not path in paths:
+        # And copy revisions from the previous manifest if we're not updating
+        # them now.
+        gitc_proj = gitc_manifest.paths[path]
+        if gitc_proj.old_revision:
+          proj.revisionExpr = None
+          proj.old_revision = gitc_proj.old_revision
+        else:
+          proj.revisionExpr = gitc_proj.revisionExpr
+
   index = 0
   while index < len(projects):
     _set_project_revisions(
         projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)])
     index += NUM_BATCH_RETRIEVE_REVISIONID
+
+  if gitc_manifest is not None:
+    for path, proj in gitc_manifest.paths.iteritems():
+      if proj.old_revision and path in paths:
+        # If we updated a project that has been started, keep the old-revision
+        # updated.
+        repo_proj = manifest.paths[path]
+        repo_proj.old_revision = repo_proj.revisionExpr
+        repo_proj.revisionExpr = None
+
+  # Convert URLs from relative to absolute.
+  for name, remote in manifest.remotes.iteritems():
+    remote.fetchUrl = remote.resolvedFetchUrl
+
   # Save the manifest.
-  save_manifest(manifest, client_dir=client_dir)
+  save_manifest(manifest)
 
 def save_manifest(manifest, client_dir=None):
   """Save the manifest file in the client_dir.
@@ -95,7 +152,7 @@
   if not client_dir:
     client_dir = manifest.gitc_client_dir
   with open(os.path.join(client_dir, '.manifest'), 'w') as f:
-    manifest.Save(f)
+    manifest.Save(f, groups=_manifest_groups(manifest))
   # TODO(sbasi/jorg): Come up with a solution to remove the sleep below.
   # Give the GITC filesystem time to register the manifest changes.
-  time.sleep(3)
\ No newline at end of file
+  time.sleep(3)