Skip to content

Commit

Permalink
package/scripts/postinstall: avoid writing to ~/.gitconfig
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ZhongRuoyu committed Apr 10, 2024
1 parent 4c24f91 commit f764428
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions package/scripts/postinstall
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]]
Expand All @@ -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/"
Expand Down

0 comments on commit f764428

Please sign in to comment.