Allow insteadof to persist for manifest_version usage.

The sync'ing workflow through here is a fair bit convoluted- at the
very least for a fresh checkout the previous version of PrepForChanges
would sync against http, than force later syncs to hit gerrit directly.

With recent changes (Ic049a8), the pushinsteadof and insteadof url
manipulations were gutted due to changes in the mirror urls used-
thus this code wound up accessing http rather than ssh.

Hack it to allow it's previous insteadof overrides to persist for
the duration of it's operations; this should restore it, and will
ultimately be removed once I've had enough time to fully spelunk the
sucker and refactor it.

(cherry picked from commit 5dea008f81270bae4f2b930719cf8e6b1c1dddbc)
(original CL Icbe20fcab363af0ddda83d8a3385f296c7571d00)

BUG=chromium-os:24709
TEST=Run manifest_version_unittest.py, examine output.

Change-Id: I59e1ad8bb53c977d425390a3d911cc53f96f52c1
Reviewed-on: https://gerrit.chromium.org/gerrit/13824
Tested-by: Brian Harring <ferringb@chromium.org>
Reviewed-by: David McMahon <djmm@chromium.org>
diff --git a/buildbot/cbuildbot_config.py b/buildbot/cbuildbot_config.py
index 686b102..8ce9cfa 100644
--- a/buildbot/cbuildbot_config.py
+++ b/buildbot/cbuildbot_config.py
@@ -75,7 +75,7 @@
 upload_symbols -- Do we upload debug symbols.
 
 git_url -- git repository URL for our manifests.
-           External: http://git.chromium.org/chromiumos/manifest.git
+           External: http://git.chromium.org/git/chromiumos/manifest
            Internal:
                ssh://gerrit-int.chromium.org:29419/chromeos/manifest-internal
 
@@ -153,8 +153,7 @@
               constants.MANIFEST_VERSIONS_INT_SUFFIX)
   else:
     if read_only:
-      return (constants.GIT_HTTP_URL + constants.MANIFEST_VERSIONS_SUFFIX +
-              constants.GIT_HTTP_SUFFIX)
+      return constants.GIT_HTTP_URL + constants.MANIFEST_VERSIONS_SUFFIX
     else:
       return constants.GERRIT_SSH_URL + constants.MANIFEST_VERSIONS_SUFFIX
 
diff --git a/buildbot/constants.py b/buildbot/constants.py
index c6dc034..527c845 100644
--- a/buildbot/constants.py
+++ b/buildbot/constants.py
@@ -9,7 +9,7 @@
 SOURCE_ROOT = os.path.join(os.path.dirname(__file__), '..', '..')
 CROSUTILS_LIB_DIR = os.path.join(SOURCE_ROOT, 'src/scripts/lib')
 
-REPO_URL = 'http://git.chromium.org/external/repo.git'
+REPO_URL = 'http://git.chromium.org/git/external/repo'
 
 GERRIT_PORT = '29418'
 GERRIT_INT_PORT = '29419'
@@ -20,12 +20,10 @@
 
 GERRIT_SSH_URL = 'ssh://%s:%s' % (GERRIT_HOST, GERRIT_PORT)
 GERRIT_INT_SSH_URL = 'ssh://%s:%s' % (GERRIT_INT_HOST, GERRIT_INT_PORT)
-GERRIT_HTTP_URL = 'http://%s' % GERRIT_HOST
-GERRIT_HTTP_SUFFIX = '.git'
-GIT_HTTP_URL = 'http://%s' % GIT_HOST
-GIT_HTTP_SUFFIX = '.git'
+GERRIT_HTTP_URL = 'https://%s' % GERRIT_HOST
+GIT_HTTP_URL = 'http://%s/git' % GIT_HOST
 
-MANIFEST_URL = GIT_HTTP_URL + '/chromiumos/manifest' + GIT_HTTP_SUFFIX
+MANIFEST_URL = GIT_HTTP_URL + '/chromiumos/manifest'
 MANIFEST_INT_URL = GERRIT_INT_SSH_URL + '/chromeos/manifest-internal'
 
 # TODO(sosa): Move to manifest-versions-external once its created
diff --git a/buildbot/manifest_version.py b/buildbot/manifest_version.py
index e615e6f..2fac8d0 100644
--- a/buildbot/manifest_version.py
+++ b/buildbot/manifest_version.py
@@ -69,6 +69,14 @@
   """
   _GitCleanDirectory(git_repo)
   try:
+    # TODO(ferringb): Rework this whole codepath- the level of syncs in use
+    # make this likely to be working accidentally rather than intentionally.
+    # See http://crosbug.com/24709 for details.
+    cros_lib.RunCommand(['git',
+                         'config',
+                         'url.%s.insteadof' % constants.GERRIT_SSH_URL,
+                         constants.GIT_HTTP_URL], cwd=git_repo)
+
     if repository.InARepoRepository(git_repo):
       cros_lib.RunCommand(['repo', 'abandon', _PUSH_BRANCH, '.'],
                           cwd=git_repo, error_ok=True)
@@ -98,13 +106,6 @@
     cros_lib.RunCommand(['git', 'config', 'push.default', 'tracking'],
                         cwd=git_repo)
 
-    # TODO Test fix for chromium-os:16249
-    # repository.FixExternalRepoPushUrls(git_repo)
-    cros_lib.RunCommand(['git',
-                         'config',
-                         'url.ssh://gerrit.chromium.org:29418.insteadof',
-                         'http://git.chromium.org'], cwd=git_repo)
-
   except cros_lib.RunCommandError, e:
     err_msg = 'Failed to prep for edit in %s with %s' % (git_repo, e.message)
     logging.error(err_msg)
@@ -483,7 +484,9 @@
     """Returns the current version info from the version file.
     Args:
     """
-    self.cros_source.Sync(repository.RepoRepository.DEFAULT_MANIFEST)
+    # TODO(ferringb): Gut cleanup=False hack- see http://crosbug.com/24709.
+    self.cros_source.Sync(repository.RepoRepository.DEFAULT_MANIFEST,
+                          cleanup=False)
     version_file_path = self.cros_source.GetRelativePath(constants.VERSION_FILE)
     return VersionInfo(version_file=version_file_path,
                        incr_type=self.incr_type)
@@ -514,7 +517,9 @@
                  self.build_name, version))
       version = version_info.IncrementVersion(message, dry_run=self.dry_run)
       logging.debug('Incremented version number to  %s', version)
-      self.cros_source.Sync(repository.RepoRepository.DEFAULT_MANIFEST)
+      # TODO(ferringb): Gut cleanup=False hack- see http://crosbug.com/24709.
+      self.cros_source.Sync(repository.RepoRepository.DEFAULT_MANIFEST,
+                            cleanup=False)
 
     spec_file = '%s.xml' % os.path.join(self.all_specs_dir, version)
     if not os.path.exists(os.path.dirname(spec_file)):
diff --git a/buildbot/manifest_version_unittest.py b/buildbot/manifest_version_unittest.py
index 6d32247..ba820da 100755
--- a/buildbot/manifest_version_unittest.py
+++ b/buildbot/manifest_version_unittest.py
@@ -17,6 +17,7 @@
   import constants
   sys.path.append(constants.SOURCE_ROOT)
 
+from chromite.buildbot import cbuildbot_config
 from chromite.buildbot import manifest_version
 from chromite.buildbot import repository
 from chromite.lib import cros_build_lib as cros_lib
@@ -79,14 +80,15 @@
   def testPushGitChanges(self):
     """Tests if we can append to an authors file and push it using dryrun."""
     if not os.path.exists(GIT_DIR): os.makedirs(GIT_DIR)
-    cros_lib.RunCommand(
-        ('repo init -u http://git.chromium.org/chromiumos/manifest.git '
-         '-m minilayout.xml').split(), cwd=GIT_DIR, input='\n\ny\n')
+    init_cmd = ['repo', 'init', '-u', constants.MANIFEST_URL, '--repo-url',
+                constants.REPO_URL, '-m', 'minilayout.xml']
+    cros_lib.RunCommand(init_cmd, cwd=GIT_DIR, input='\n\ny\n')
     cros_lib.RunCommand(('repo sync --jobs 8').split(), cwd=GIT_DIR)
     git_dir = os.path.join(GIT_DIR, GIT_TEST_PATH)
-    cros_lib.RunCommand(
-        ('git config url.ssh://gerrit.chromium.org:29418.insteadof'
-         ' http://git.chromium.org').split(), cwd=git_dir)
+    cros_lib.RunCommand(['git',
+                         'config',
+                         'url.%s.insteadof' % constants.GERRIT_SSH_URL,
+                         constants.GIT_HTTP_URL], cwd=git_dir)
 
     manifest_version._PrepForChanges(git_dir, dry_run=True)
 
@@ -101,13 +103,14 @@
   def testPushGitChangesWithRealPrep(self):
     """Another push test that tests push but on non-repo does it on a branch."""
     git_dir = tempfile.mktemp('manifest_dir')
-    cros_lib.RunCommand(
-        ['git', 'clone',
-         'http://git.chromium.org/chromiumos/manifest-versions.git', git_dir])
+    manifest_versions_url = cbuildbot_config.GetManifestVersionsRepoUrl(
+        internal_build=False, read_only=True)
+    cros_lib.RunCommand(['git', 'clone', manifest_versions_url, git_dir])
     try:
-      cros_lib.RunCommand(
-          ('git config url.ssh://gerrit.chromium.org:29418.insteadof'
-           ' http://git.chromium.org').split(), cwd=git_dir)
+      cros_lib.RunCommand(['git',
+                           'config',
+                           'url.%s.insteadof' % constants.GERRIT_SSH_URL,
+                           constants.GIT_HTTP_URL], cwd=git_dir)
 
       manifest_version._PrepForChanges(git_dir, dry_run=False)
 
@@ -330,7 +333,8 @@
             self.build_name, FAKE_VERSION_STRING),
         dry_run=True).AndReturn(FAKE_VERSION_STRING_NEXT)
 
-    repository.RepoRepository.Sync('default')
+    # TODO(ferringb): Gut the cleanup=False added for 24709.
+    repository.RepoRepository.Sync('default', cleanup=False)
     repository.RepoRepository.ExportManifest(mox.IgnoreArg())
 
     self.mox.ReplayAll()
diff --git a/buildbot/repository.py b/buildbot/repository.py
index 1c6a8b1..32512f8 100755
--- a/buildbot/repository.py
+++ b/buildbot/repository.py
@@ -140,7 +140,7 @@
                          constants.REPO_URL],
                          cwd=os.path.join(self.directory, '.repo/repo'))
 
-  def Sync(self, local_manifest=None):
+  def Sync(self, local_manifest=None, cleanup=True):
     """Sync/update the source.  Changes manifest if specified.
 
     local_manifest:  If set, checks out source to manifest.  DEFAULT_MANIFEST
@@ -157,7 +157,9 @@
 
       self._ReinitializeIfNecessary(local_manifest)
 
-      FixBrokenExistingRepos(self.directory)
+      if cleanup:
+        FixBrokenExistingRepos(self.directory)
+
       cros_lib.OldRunCommand(['repo', 'sync', '--jobs', '4'],
                              cwd=self.directory, num_retries=2)