From f76442837a5b4d8ddf9b7e0c4cc04882e42a8779 Mon Sep 17 00:00:00 2001 From: Ruoyu Zhong Date: Thu, 11 Apr 2024 04:15:59 +0800 Subject: [PATCH] package/scripts/postinstall: avoid writing to `~/.gitconfig` In this attempt we pretend that the "global" configs are in the repository itself. $ XDG_CONFIG_HOME= HOME=$PWD git config --global section.key value $ XDG_CONFIG_HOME= HOME=$PWD git config --global section.key value $ git config --global section.key $ cat $PWD/.gitconfig [section] key = value --- package/scripts/postinstall | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/package/scripts/postinstall b/package/scripts/postinstall index 058370cf34d18..e6e384ea2a6b9 100755 --- a/package/scripts/postinstall +++ b/package/scripts/postinstall @@ -21,15 +21,17 @@ fi # add Git to path export PATH="/Library/Developer/CommandLineTools/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:${PATH}" -# use `git -c key=value` to avoid writing to Git's global config -# https://github.com/Homebrew/brew/issues/17067 -git=(git -c "safe.directory=${homebrew_directory}") - # reset Git repository cd "${homebrew_directory}" +# avoid writing user's global config file by making +# "${homebrew_directory}/.gitconfig" the "global" config +# https://git-scm.com/docs/git-config#SCOPES +git=(env XDG_CONFIG_HOME="" HOME="${homebrew_directory}" git) +"${git[@]}" config --global --add safe.directory "${homebrew_directory}" "${git[@]}" reset --hard "${git[@]}" checkout --force master "${git[@]}" branch | grep -v '\*' | xargs -n 1 "${git[@]}" branch --delete --force || true +rm "${homebrew_directory}/.gitconfig" # move to /usr/local if on x86_64 if [[ $(uname -m) == "x86_64" ]] @@ -39,9 +41,11 @@ then cp -pRL "${homebrew_directory}/.git" "/usr/local/Homebrew/" mv "${homebrew_directory}/cache_api" "/usr/local/Homebrew/" - git=(git -c safe.directory="/usr/local/Homebrew") + git=(env XDG_CONFIG_HOME="" HOME="/usr/local/Homebrew" git) + "${git[@]}" config --global --add safe.directory /usr/local/Homebrew "${git[@]}" -C /usr/local/Homebrew reset --hard "${git[@]}" -C /usr/local/Homebrew checkout --force master + rm /usr/local/Homebrew/.gitconfig else mkdir -vp /usr/local/bin mv "${homebrew_directory}" "/usr/local/Homebrew/"