enter_chroot: fix gitcookies lookup

When the .gitconfig setting uses ~/ in its path, make sure we expand it
as the right source user.

BUG=chromium:1025578
TEST=`cros_sdk` copies in gitcookies now

Change-Id: Ia832db1f8a1e409e4ad449c5440bc6c5b3dc4610
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosutils/+/1959699
Reviewed-by: Mike Nichols <mikenichols@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosutils/+/2340657
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Mike Frysinger <vapier@chromium.org>
diff --git a/sdk_lib/enter_chroot.sh b/sdk_lib/enter_chroot.sh
index 9796402..77254af 100755
--- a/sdk_lib/enter_chroot.sh
+++ b/sdk_lib/enter_chroot.sh
@@ -272,6 +272,12 @@
   fi
 }
 
+git_config() {
+  USER="${SUDO_USER:-${USER}}" \
+  HOME="${SUDO_HOME:-${HOME}}" \
+  git config "$@"
+}
+
 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.
@@ -294,7 +300,8 @@
 
   # Copy the gitcookies file, updating the user's gitconfig to point to it.
   local gitcookies
-  gitcookies="$(git config -f "${chroot_gitconfig}" --get http.cookiefile)"
+  gitcookies="$(git_config --type path -f "${chroot_gitconfig}" \
+                  --get http.cookiefile)"
   if [[ $? -ne 0 ]]; then
     # Try the default location anyway.
     gitcookies="${SUDO_HOME}/.gitcookies"
@@ -378,7 +385,7 @@
     setup_mount "${FLAGS_trunk}" "--rbind" "${CHROOT_TRUNK_DIR}"
 
     debug "Setting up referenced repositories if required."
-    REFERENCE_DIR=$(git config --file  \
+    REFERENCE_DIR=$(git_config --file  \
       "${FLAGS_trunk}/.repo/manifests.git/config" \
       repo.reference)
     if [ -n "${REFERENCE_DIR}" ]; then