GITC: Add repo start support.

Add repo start support for GITC checkouts. If the user is in
the GITC FS view, they can now run repo start to check out
the sources and create a new working branch.

When "repo start" is called on a GITC project, the revision
tag is set to an empty string and saved in a new tag:
old-revision. This tells the GITC filesystem to display the
local copy of the sources when being viewed. The local copy
is created by pulling the project sources and the new branch
is created based off the original project revision.

Updated main.py to setup each command's gitc_manifest when
appropriate.

Updated repo sync's logic to sync opened projects and
updating the GITC manifest file for the rest.

Change-Id: I7e4809d1c4fc43c69b26f2f1bebe45aab0cae628
diff --git a/gitc_utils.py b/gitc_utils.py
index ef028b0..4d8d536 100644
--- a/gitc_utils.py
+++ b/gitc_utils.py
@@ -16,6 +16,7 @@
 from __future__ import print_function
 import os
 import sys
+import time
 
 import git_command
 import git_config
@@ -26,6 +27,18 @@
 GITC_FS_ROOT_DIR = '/gitc/manifest-rw/'
 NUM_BATCH_RETRIEVE_REVISIONID = 300
 
+def parse_clientdir(gitc_fs_path):
+  """Parse a path in the GITC FS and return its client name.
+
+  @param gitc_fs_path: A subdirectory path within the GITC_FS_ROOT_DIR.
+
+  @returns: The GITC client name
+  """
+  if (gitc_fs_path == GITC_FS_ROOT_DIR or
+      not gitc_fs_path.startswith(GITC_FS_ROOT_DIR)):
+    return None
+  return gitc_fs_path.split(GITC_FS_ROOT_DIR)[1].split('/')[0]
+
 def _set_project_revisions(projects):
   """Sets the revisionExpr for a list of projects.
 
@@ -50,19 +63,37 @@
       sys.exit(1)
     proj.revisionExpr = gitcmd.stdout.split('\t')[0]
 
-def generate_gitc_manifest(client_dir, manifest):
+def generate_gitc_manifest(client_dir, manifest, projects=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.
   """
   print('Generating GITC Manifest by fetching revision SHAs for each '
         'project.')
+  if projects is None:
+    projects = manifest.projects
   index = 0
-  while index < len(manifest.projects):
+  while index < len(projects):
     _set_project_revisions(
-        manifest.projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)])
+        projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)])
     index += NUM_BATCH_RETRIEVE_REVISIONID
   # Save the manifest.
+  save_manifest(manifest, client_dir=client_dir)
+
+def save_manifest(manifest, client_dir=None):
+  """Save the manifest file in the client_dir.
+
+  @param client_dir: Client directory to save the manifest in.
+  @param manifest: Manifest object to save.
+  """
+  if not client_dir:
+    client_dir = manifest.gitc_client_dir
   with open(os.path.join(client_dir, '.manifest'), 'w') as f:
     manifest.Save(f)
+  # 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