Merge "launcher: Update repo after applying clone.bundle"
diff --git a/docs/manifest-format.txt b/docs/manifest-format.txt
index 1aa9396..7f13820 100644
--- a/docs/manifest-format.txt
+++ b/docs/manifest-format.txt
@@ -50,7 +50,9 @@
     <!ATTLIST url              CDATA #REQUIRED>
 
     <!ELEMENT project (annotation*,
-                       project*)>
+                       project*,
+                       copyfile?,
+                       linkfile?)>
     <!ATTLIST project name        CDATA #REQUIRED>
     <!ATTLIST project path        CDATA #IMPLIED>
     <!ATTLIST project remote      IDREF #IMPLIED>
@@ -68,6 +70,14 @@
     <!ATTLIST annotation value CDATA #REQUIRED>
     <!ATTLIST annotation keep  CDATA "true">
 
+    <!ELEMENT copyfile (EMPTY)>
+    <!ATTLIST src value  CDATA #REQUIRED>
+    <!ATTLIST dest value CDATA #REQUIRED>
+
+    <!ELEMENT linkfile (EMPTY)>
+    <!ATTLIST src value  CDATA #REQUIRED>
+    <!ATTLIST dest value CDATA #REQUIRED>
+
     <!ELEMENT extend-project>
     <!ATTLIST extend-project name CDATA #REQUIRED>
     <!ATTLIST extend-project path CDATA #IMPLIED>
@@ -285,6 +295,21 @@
 "false".  This attribute determines whether or not the annotation will
 be kept when exported with the manifest subcommand.
 
+Element copyfile
+----------------
+
+Zero or more copyfile elements may be specified as children of a
+project element. Each element describes a src-dest pair of files;
+the "src" file will be copied to the "dest" place during 'repo sync'
+command.
+"src" is project relative, "dest" is relative to the top of the tree.
+
+Element linkfile
+----------------
+
+It's just like copyfile and runs at the same time as copyfile but
+instead of copying it creates a symlink.
+
 Element remove-project
 ----------------------
 
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)
diff --git a/manifest_xml.py b/manifest_xml.py
index a7fe8dd..3ac607e 100644
--- a/manifest_xml.py
+++ b/manifest_xml.py
@@ -167,12 +167,13 @@
   def _ParseGroups(self, groups):
     return [x for x in re.split(r'[,\s]+', groups) if x]
 
-  def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
+  def Save(self, fd, peg_rev=False, peg_rev_upstream=True, groups=None):
     """Write the current manifest out to the given file descriptor.
     """
     mp = self.manifestProject
 
-    groups = mp.config.GetString('manifest.groups')
+    if groups is None:
+      groups = mp.config.GetString('manifest.groups')
     if groups:
       groups = self._ParseGroups(groups)
 
diff --git a/project.py b/project.py
index f964b2f..5d8f61e 100644
--- a/project.py
+++ b/project.py
@@ -1275,6 +1275,8 @@
         # Except if the head needs to be detached
         #
         if not syncbuf.detach_head:
+          # The copy/linkfile config may have changed.
+          self._CopyAndLinkFiles()
           return
       else:
         lost = self._revlist(not_rev(revid), HEAD)
@@ -1292,6 +1294,8 @@
     if head == revid:
       # No changes; don't do anything further.
       #
+      # The copy/linkfile config may have changed.
+      self._CopyAndLinkFiles()
       return
 
     branch = self.GetBranch(branch)
diff --git a/subcmds/gitc_init.py b/subcmds/gitc_init.py
index e99affa..c243bb3 100644
--- a/subcmds/gitc_init.py
+++ b/subcmds/gitc_init.py
@@ -19,6 +19,7 @@
 
 import gitc_utils
 from command import RequiresGitcCommand
+from manifest_xml import GitcManifest
 from subcmds import init
 
 
@@ -34,7 +35,7 @@
 
 This command will setup the client directory, initialize repo, just
 like repo init does, and then downloads the manifest collection
-and installs in in the .repo/directory of the GITC client.
+and installs it in the .repo/directory of the GITC client.
 
 Once this is done, a GITC manifest is generated by pulling the HEAD
 SHA for each project and generates the properly formatted XML file
@@ -68,15 +69,16 @@
       os.mkdir(self.client_dir)
     super(GitcInit, self).Execute(opt, args)
 
-    for name, remote in self.manifest.remotes.iteritems():
-      remote.fetchUrl = remote.resolvedFetchUrl
-
+    manifest_file = self.manifest.manifestFile
     if opt.manifest_file:
       if not os.path.exists(opt.manifest_file):
         print('fatal: Specified manifest file %s does not exist.' %
               opt.manifest_file)
         sys.exit(1)
-      self.manifest.Override(opt.manifest_file)
-    gitc_utils.generate_gitc_manifest(self.client_dir, self.manifest)
+      manifest_file = opt.manifest_file
+
+    manifest = GitcManifest(self.repodir, opt.gitc_client)
+    manifest.Override(manifest_file)
+    gitc_utils.generate_gitc_manifest(None, manifest)
     print('Please run `cd %s` to view your GITC client.' %
           os.path.join(gitc_utils.GITC_FS_ROOT_DIR, opt.gitc_client))
diff --git a/subcmds/start.py b/subcmds/start.py
index 188fd7c..940c341 100644
--- a/subcmds/start.py
+++ b/subcmds/start.py
@@ -57,7 +57,6 @@
         print("error: at least one project must be specified", file=sys.stderr)
         sys.exit(1)
 
-    proj_name_to_gitc_proj_dict = {}
     if self.gitc_manifest:
       all_projects = self.GetProjects(projects, manifest=self.gitc_manifest,
                                       missing_ok=True)
@@ -67,7 +66,6 @@
         else:
           project.already_synced = False
           project.old_revision = project.revisionExpr
-        proj_name_to_gitc_proj_dict[project.name] = project
         project.revisionExpr = None
       # Save the GITC manifest.
       gitc_utils.save_manifest(self.gitc_manifest)
@@ -77,9 +75,10 @@
     pm = Progress('Starting %s' % nb, len(all_projects))
     for project in all_projects:
       pm.update()
+
       if self.gitc_manifest:
-        gitc_project = proj_name_to_gitc_proj_dict[project.name]
-        # Sync projects that have already been opened.
+        gitc_project = self.gitc_manifest.paths[project.relpath]
+        # Sync projects that have not been opened.
         if not gitc_project.already_synced:
           proj_localdir = os.path.join(self.gitc_manifest.gitc_client_dir,
                                        project.relpath)
@@ -89,7 +88,7 @@
           project.Sync_NetworkHalf()
           sync_buf = SyncBuffer(self.manifest.manifestProject.config)
           project.Sync_LocalHalf(sync_buf)
-          project.revisionExpr = gitc_project.old_revision
+          project.revisionId = gitc_project.old_revision
 
       # If the current revision is a specific SHA1 then we can't push back
       # to it; so substitute with dest_branch if defined, or with manifest
@@ -100,6 +99,7 @@
           branch_merge = project.dest_branch
         else:
           branch_merge = self.manifest.default.revisionExpr
+
       if not project.StartBranch(nb, branch_merge=branch_merge):
         err.append(project)
     pm.end()
diff --git a/subcmds/sync.py b/subcmds/sync.py
index 2e9bbe7..a99d7e7 100644
--- a/subcmds/sync.py
+++ b/subcmds/sync.py
@@ -75,6 +75,7 @@
 from project import SyncBuffer
 from progress import Progress
 from wrapper import Wrapper
+from manifest_xml import GitcManifest
 
 _ONE_DAY_S = 24 * 60 * 60
 
@@ -670,32 +671,39 @@
       if opt.jobs is None:
         self.jobs = self.manifest.default.sync_j
 
-    # TODO (sbasi) - Add support for manifest changes, aka projects
-    # have been added or deleted from the manifest.
     if self.gitc_manifest:
       gitc_manifest_projects = self.GetProjects(args,
-                                                manifest=self.gitc_manifest,
                                                 missing_ok=True)
       gitc_projects = []
       opened_projects = []
       for project in gitc_manifest_projects:
-        if not project.old_revision:
-          gitc_projects.append(project)
+        if project.relpath in self.gitc_manifest.paths and \
+           self.gitc_manifest.paths[project.relpath].old_revision:
+          opened_projects.append(project.relpath)
         else:
-          opened_projects.append(project)
+          gitc_projects.append(project.relpath)
 
-      if gitc_projects and not opt.local_only:
+      if not args:
+        gitc_projects = None
+
+      if gitc_projects != [] and not opt.local_only:
         print('Updating GITC client: %s' % self.gitc_manifest.gitc_client_name)
-        gitc_utils.generate_gitc_manifest(self.gitc_manifest.gitc_client_dir,
-                                          self.gitc_manifest,
+        manifest = GitcManifest(self.repodir, self.gitc_manifest.gitc_client_name)
+        if manifest_name:
+          manifest.Override(manifest_name)
+        else:
+          manifest.Override(self.manifest.manifestFile)
+        gitc_utils.generate_gitc_manifest(self.gitc_manifest,
+                                          manifest,
                                           gitc_projects)
         print('GITC client successfully synced.')
 
       # The opened projects need to be synced as normal, therefore we
       # generate a new args list to represent the opened projects.
-      args = []
-      for proj in opened_projects:
-        args.append(os.path.relpath(proj.worktree, cwd))
+      # TODO: make this more reliable -- if there's a project name/path overlap,
+      # this may choose the wrong project.
+      args = [os.path.relpath(self.manifest.paths[p].worktree, os.getcwd())
+              for p in opened_projects]
       if not args:
         return
     all_projects = self.GetProjects(args,