Add upgrade hooks to support removing chrome projects from manifest.

See the bug for details.

BUG=chromium-os:32963.
TEST=Locally, remote trybots.

Change-Id: I33f5c42b36f3e06139036c299c2fc2c2ff026411
Reviewed-on: https://gerrit.chromium.org/gerrit/28543
Reviewed-by: David James <davidjames@chromium.org>
Commit-Ready: Ryan Cui <rcui@chromium.org>
Tested-by: Ryan Cui <rcui@chromium.org>
diff --git a/chroot_version_hooks.d/44_fix_gerrit_chrome b/chroot_version_hooks.d/44_fix_gerrit_chrome
new file mode 100644
index 0000000..2f902f8
--- /dev/null
+++ b/chroot_version_hooks.d/44_fix_gerrit_chrome
@@ -0,0 +1,30 @@
+# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# Move users of CHROME_ORIGIN=GERRIT_SOURCE to the new gerrit-source.xml
+# manifest, as well as setting up the chromium/src repository properly for
+# submodules. crosbug.com/32963.
+
+# We only want to run this hook once.  Since this also gets called from
+# enter_chroot, look for the marker.
+MARKER="/tmp/44_fix_gerrit_chrome"
+if [ -e "${MARKER}" ]; then
+  exit 0
+fi
+
+chrome_workon="=chromeos-base/chromeos-chrome-9999"
+cros_workon_dir="${HOME}/trunk/.config/cros_workon/*"
+
+if grep -q "${chrome_workon}" ${cros_workon_dir} &> /dev/null; then
+  repo selfupdate && repo init -m gerrit-source.xml
+fi
+
+(
+  cd "${HOME}/trunk/chromium/src"
+  ignore_cmd='git config -f $toplevel/.git/config submodule.$name.ignore all'
+  git submodule foreach "${ignore_cmd}" &> /dev/null
+)
+
+touch "${MARKER}"
+exit 0
diff --git a/sdk_lib/enter_chroot.sh b/sdk_lib/enter_chroot.sh
index 86c7f61..53728e5 100755
--- a/sdk_lib/enter_chroot.sh
+++ b/sdk_lib/enter_chroot.sh
@@ -608,6 +608,17 @@
   cmd=( sudo -i -u "$USER" )
 fi
 
+# TODO(rcui): Remove this hook on 8/26/2012.
+# crosbug.com/32963
+if [ $FLAGS_early_make_chroot -eq $FLAGS_FALSE ]; then
+  chroot_script_root="\${HOME}/trunk/src/scripts"
+  hook_rel_path="chroot_version_hooks.d/44_fix_gerrit_chrome"
+  gerrit_chrome_hook="${chroot_script_root}/${hook_rel_path}"
+  entry_hook=(eval "source \"${gerrit_chrome_hook}\"")
+  sudo -- chroot "${FLAGS_chroot}" "${cmd[@]}" "${CHROOT_PASSTHRU[@]}" \
+                 "${entry_hook[@]}"
+fi
+
 sudo -- chroot "${FLAGS_chroot}" "${cmd[@]}" "${CHROOT_PASSTHRU[@]}" "$@"
 
 # Remove trap and explicitly unmount