sdk_lib: Make git setup more robust.

In particular:
- Move git setup code from make_chroot to enter_chroot. Most of the user
  settings are copied in the enter_chroot script. This increases
  uniformity.
- Determine the location of gitcookies from the gitconfig file for the
  user.
- Update the gitconfig for the user inside the chroot to point to the
  copied gitcookies file.

BUG=chromium:465563
TEST=(1) From scratch, run `cros_sdk --create` and manually verify
         gitconfig, gitcookies, when
         (a) gitcookies doesn't exist.
         (b) gitcookies is at ${HOME}/.gitcookies
         (c) gitcookies is elsewhere with gitconfig pointing to it.
     (2) Run a clobber trybot to ensure sane chroot setup.

Change-Id: Ia7fb9a810c8968f7deb2d49367998b39fd3e8084
Reviewed-on: https://chromium-review.googlesource.com/259825
Reviewed-by: Prathmesh Prabhu <pprabhu@chromium.org>
Commit-Queue: Prathmesh Prabhu <pprabhu@chromium.org>
Tested-by: Prathmesh Prabhu <pprabhu@chromium.org>
diff --git a/sdk_lib/enter_chroot.sh b/sdk_lib/enter_chroot.sh
index 6bbf086..308db28 100755
--- a/sdk_lib/enter_chroot.sh
+++ b/sdk_lib/enter_chroot.sh
@@ -80,12 +80,12 @@
 switch_to_strict_mode
 
 # These config files are to be copied into chroot if they exist in home dir.
+# Additionally, git relevant files are copied by setup_git.
 FILES_TO_COPY_TO_CHROOT=(
   .gdata_cred.txt             # User/password for Google Docs on chromium.org
   .gdata_token                # Auth token for Google Docs on chromium.org
   .disable_build_stats_upload # Presence of file disables command stats upload
   .netrc                      # May contain required source fetching credentials
-  .gitcookies                 # May contain required source fetching credentials
 )
 
 INNER_CHROME_ROOT=$FLAGS_chrome_root_mount  # inside chroot
@@ -234,6 +234,44 @@
   fi
 }
 
+setup_git() {
+  # Copy .gitconfig into chroot so repo and git can be used from inside.
+  # This is required for repo to work since it validates the email address.
+  copy_into_chroot_if_exists "${SUDO_HOME}/.gitconfig" \
+      "/home/${SUDO_USER}/.gitconfig"
+  local -r chroot_gitconfig="${FLAGS_chroot}/home/${SUDO_USER}/.gitconfig"
+
+  # If the user didn't set up their username in their gitconfig, look
+  # at the default git settings for the user.
+  if ! git config -f "${chroot_gitconfig}" user.email >& /dev/null; then
+    local ident=$(cd /; sudo -u ${SUDO_USER} -- git var GIT_COMMITTER_IDENT || \
+                  :)
+    local ident_name=${ident%% <*}
+    local ident_email=${ident%%>*}; ident_email=${ident_email##*<}
+    git config -f "${chroot_gitconfig}" --replace-all user.name \
+        "${ident_name}" || :
+    git config -f "${chroot_gitconfig}" --replace-all user.email \
+        "${ident_email}" || :
+  fi
+
+  # Copy the gitcookies file, updating the user's gitconfig to point to it.
+  local gitcookies
+  gitcookies="$(git config -f "${chroot_gitconfig}" --get http.cookiefile)"
+  if [[ $? -ne 0 ]]; then
+    # Try the default location anyway.
+    gitcookies="${SUDO_HOME}/.gitcookies"
+  fi
+  copy_into_chroot_if_exists "${gitcookies}" "/home/${SUDO_USER}/.gitcookies"
+  local -r chroot_gitcookies="${FLAGS_chroot}/home/${SUDO_USER}/.gitcookies"
+  if [[ -e "${chroot_gitcookies}" ]]; then
+    git config -f "${chroot_gitconfig}" --replace-all http.cookiefile \
+        "/home/${SUDO_USER}/.gitcookies"
+  fi
+  # This line must be at the end because using `git config` changes ownership of
+  # the .gitconfig.
+  chown ${SUDO_UID}:${SUDO_GID} "${chroot_gitconfig}"
+}
+
 setup_env() {
   (
     flock 200
@@ -460,6 +498,7 @@
     for fn in "${FILES_TO_COPY_TO_CHROOT[@]}"; do
       copy_into_chroot_if_exists "${SUDO_HOME}/${fn}" "/home/${SUDO_USER}/${fn}"
     done
+    setup_git
     promote_api_keys
 
     # Fix permissions on shared memory to allow non-root users access to POSIX
diff --git a/sdk_lib/make_chroot.sh b/sdk_lib/make_chroot.sh
index 417eb66..4807dbe 100755
--- a/sdk_lib/make_chroot.sh
+++ b/sdk_lib/make_chroot.sh
@@ -297,25 +297,6 @@
      user_cp -rp "${SUDO_HOME}/.ssh" "$FLAGS_chroot/home/${SUDO_USER}/"
    fi
 
-   if [[ -f ${SUDO_HOME}/.gitconfig ]]; then
-     # Copy .gitconfig into chroot so repo and git can be used from inside.
-     # This is required for repo to work since it validates the email address.
-     echo "Copying ~/.gitconfig into chroot"
-     user_cp "${SUDO_HOME}/.gitconfig" "$FLAGS_chroot/home/${SUDO_USER}/"
-   fi
-
-   # If the user didn't set up their username in their gitconfig, look
-   # at the default git settings for the user.
-   if ! git config -f "${SUDO_HOME}/.gitconfig" user.email >& /dev/null; then
-     ident=$(cd /; sudo -u ${SUDO_USER} -- git var GIT_COMMITTER_IDENT || :)
-     ident_name=${ident%% <*}
-     ident_email=${ident%%>*}; ident_email=${ident_email##*<}
-     gitconfig=${FLAGS_chroot}/home/${SUDO_USER}/.gitconfig
-     git config -f ${gitconfig} --replace-all user.name "${ident_name}" || :
-     git config -f ${gitconfig} --replace-all user.email "${ident_email}" || :
-     chown ${SUDO_UID}:${SUDO_GID} ${FLAGS_chroot}/home/${SUDO_USER}/.gitconfig
-   fi
-
    if [[ -f ${SUDO_HOME}/.cros_chroot_init ]]; then
      sudo -u ${SUDO_USER} -- /bin/bash "${SUDO_HOME}/.cros_chroot_init" \
        "${FLAGS_chroot}"