Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/osx/Installer.Mac/scripts/postinstall
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,22 @@ mkdir -p /usr/local/bin
/bin/ln -Fs "$INSTALL_DESTINATION/git-credential-manager" /usr/local/bin/git-credential-manager

# Configure GCM for the current user (running as the current user to avoid root
# from taking ownership of ~/.gitconfig)
sudo -u ${USER} "$INSTALL_DESTINATION/git-credential-manager" configure
# from taking ownership of ~/.gitconfig).
# Determine the real (non-root) user via multiple methods since the installer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

USER/LOGNAME [..] when run via Homebrew which explicitly clears those environment variables

So in my testing I do NOT find this to be the case. If I modify the GCM Cask with a locally built pkg file that has env > /tmp/pkgenv.log in the postinstall script I see the following variables set (amongst others):

USER=mattche
HOME=/Users/mattche
LOGNAME=root
COMMAND_LINE_INSTALL=1

Specifically I see USER is set to the correct local username (mattche in my case). It is also not possible to run brew as sudo, at least with the latest version (I'm on 5.1.0).

I also tested the output of env from postinstall when installing the pkg file directly using

  1. Finder
USER=mattche
HOME=/Users/mattche
LOGNAME=root
  1. Terminal with sudo installer -target / -pkg gcm.pkg
USER=root
HOME=/Users/mattche
LOGNAME=root
COMMAND_LINE_INSTALL=1

Note that I do not see SUDO_USER set in any of these instances.

# may be invoked with USER/LOGNAME unset (e.g. when run via Homebrew which
# explicitly clears those environment variables).
# 1. SUDO_USER - set by sudo, works for both GUI (Homebrew) and SSH scenarios.
# 2. stat /dev/console - works for GUI sessions when not running under sudo.
REAL_USER="${SUDO_USER}"
if [ -z "${REAL_USER}" ] || [ "${REAL_USER}" = "root" ]; then
REAL_USER=$(stat -f%Su /dev/console 2>/dev/null)
fi

if [ -n "${REAL_USER}" ] && [ "${REAL_USER}" != "root" ]; then
sudo -u "${REAL_USER}" "$INSTALL_DESTINATION/git-credential-manager" configure
else
echo "warning: unable to determine the installing user; GCM has not been configured automatically." >&2
echo "warning: run 'git-credential-manager configure' to configure GCM for your user." >&2
fi

exit 0
Loading