GITC: Fix 'repo start <branch> <repo>/<subdir>'

As soon as we wrote the gitc manifest, the folder for that repo became
empty, causing the next GetProjects lookup to fail. Reorder the
GetProjects calls so that they all happen while we still have the
repository contents available.

If you were already in a subdir, for cases like 'repo start <branch> .',
this would still fail, since the working directory would disappear out
from under you. That's fine most of the time, since we shouldn't be
doing operations based on the local directory, but git has a realpath
function that tries to restore CWD by chdir'ing back to it. So if the
working directory no longer exists, chdir to the topdir before
continuing.

Change-Id: Ibdf6cd37ff6e5a5f8338347c3919175491f7166f
diff --git a/subcmds/start.py b/subcmds/start.py
index 940c341..d1430a9 100644
--- a/subcmds/start.py
+++ b/subcmds/start.py
@@ -57,10 +57,15 @@
         print("error: at least one project must be specified", file=sys.stderr)
         sys.exit(1)
 
+    all_projects = self.GetProjects(projects,
+                                    missing_ok=bool(self.gitc_manifest))
+
+    # This must happen after we find all_projects, since GetProjects may need
+    # the local directory, which will disappear once we save the GITC manifest.
     if self.gitc_manifest:
-      all_projects = self.GetProjects(projects, manifest=self.gitc_manifest,
-                                      missing_ok=True)
-      for project in all_projects:
+      gitc_projects = self.GetProjects(projects, manifest=self.gitc_manifest,
+                                       missing_ok=True)
+      for project in gitc_projects:
         if project.old_revision:
           project.already_synced = True
         else:
@@ -70,8 +75,10 @@
       # Save the GITC manifest.
       gitc_utils.save_manifest(self.gitc_manifest)
 
-    all_projects = self.GetProjects(projects,
-                                    missing_ok=bool(self.gitc_manifest))
+      # Make sure we have a valid CWD
+      if not os.path.exists(os.getcwd()):
+        os.chdir(self.manifest.topdir)
+
     pm = Progress('Starting %s' % nb, len(all_projects))
     for project in all_projects:
       pm.update()