bash: less intrusive automatic shell integration

We now use a temporary function (__ghostty_bash_startup) to perform the
bash startup sequence. This gives us a local function scope in which to
store some temporary values (like rcfile). This way, they won't leak
into the sourced files' scopes.

Also, use `~/` instead of `$HOME` for home directory paths as a simpler
shorthand notation.
This commit is contained in:
Jon Parise
2025-01-14 14:12:08 -05:00
parent ff9414d9ea
commit 6af1850ab4

View File

@ -20,14 +20,13 @@
if [[ "$-" != *i* ]] ; then builtin return; fi if [[ "$-" != *i* ]] ; then builtin return; fi
if [ -z "$GHOSTTY_RESOURCES_DIR" ]; then builtin return; fi if [ -z "$GHOSTTY_RESOURCES_DIR" ]; then builtin return; fi
# When automatic shell integration is active, we need to manually # When automatic shell integration is active, we were started in POSIX
# load the normal bash startup files based on the injected state. # mode and need to manually recreate the bash startup sequence.
if [ -n "$GHOSTTY_BASH_INJECT" ]; then if [ -n "$GHOSTTY_BASH_INJECT" ]; then
builtin declare ghostty_bash_inject="$GHOSTTY_BASH_INJECT" # Store a temporary copy of our startup flags and unset these global
builtin unset GHOSTTY_BASH_INJECT ENV # environment variables so we can safely handle reentrancy.
builtin declare __ghostty_bash_flags="$GHOSTTY_BASH_INJECT"
# At this point, we're in POSIX mode and rely on the injected builtin unset ENV GHOSTTY_BASH_INJECT
# flags to guide is through the rest of the startup sequence.
# Restore bash's default 'posix' behavior. Also reset 'inherit_errexit', # Restore bash's default 'posix' behavior. Also reset 'inherit_errexit',
# which doesn't happen as part of the 'posix' reset. # which doesn't happen as part of the 'posix' reset.
@ -40,19 +39,20 @@ if [ -n "$GHOSTTY_BASH_INJECT" ]; then
builtin unset GHOSTTY_BASH_UNEXPORT_HISTFILE builtin unset GHOSTTY_BASH_UNEXPORT_HISTFILE
fi fi
# Manually source the startup files, respecting the injected flags like # Manually source the startup files. See INVOCATION in bash(1) and
# --norc and --noprofile that we parsed with the shell integration code. # run_startup_files() in shell.c in the Bash source code.
# function __ghostty_bash_startup() {
# See also: run_startup_files() in shell.c in the Bash source code builtin local rcfile
if builtin shopt -q login_shell; then if builtin shopt -q login_shell; then
if [[ $ghostty_bash_inject != *"--noprofile"* ]]; then if [[ $__ghostty_bash_flags != *"--noprofile"* ]]; then
[ -r /etc/profile ] && builtin source "/etc/profile" [ -r /etc/profile ] && builtin source "/etc/profile"
for rcfile in "$HOME/.bash_profile" "$HOME/.bash_login" "$HOME/.profile"; do for rcfile in ~/.bash_profile ~/.bash_login ~/.profile; do
[ -r "$rcfile" ] && { builtin source "$rcfile"; break; } [ -r "$rcfile" ] && { builtin source "$rcfile"; break; }
done done
fi fi
else else
if [[ $ghostty_bash_inject != *"--norc"* ]]; then if [[ $__ghostty_bash_flags != *"--norc"* ]]; then
# The location of the system bashrc is determined at bash build # The location of the system bashrc is determined at bash build
# time via -DSYS_BASHRC and can therefore vary across distros: # time via -DSYS_BASHRC and can therefore vary across distros:
# Arch, Debian, Ubuntu use /etc/bash.bashrc # Arch, Debian, Ubuntu use /etc/bash.bashrc
@ -62,13 +62,17 @@ if [ -n "$GHOSTTY_BASH_INJECT" ]; then
for rcfile in /etc/bash.bashrc /etc/bash/bashrc /etc/bashrc; do for rcfile in /etc/bash.bashrc /etc/bash/bashrc /etc/bashrc; do
[ -r "$rcfile" ] && { builtin source "$rcfile"; break; } [ -r "$rcfile" ] && { builtin source "$rcfile"; break; }
done done
if [[ -z "$GHOSTTY_BASH_RCFILE" ]]; then GHOSTTY_BASH_RCFILE="$HOME/.bashrc"; fi if [[ -z "$GHOSTTY_BASH_RCFILE" ]]; then GHOSTTY_BASH_RCFILE=~/.bashrc; fi
[ -r "$GHOSTTY_BASH_RCFILE" ] && builtin source "$GHOSTTY_BASH_RCFILE" [ -r "$GHOSTTY_BASH_RCFILE" ] && builtin source "$GHOSTTY_BASH_RCFILE"
fi fi
fi fi
}
__ghostty_bash_startup
builtin unset -f __ghostty_bash_startup
builtin unset -v __ghostty_bash_flags
builtin unset GHOSTTY_BASH_RCFILE builtin unset GHOSTTY_BASH_RCFILE
builtin unset ghostty_bash_inject rcfile
fi fi
# Sudo # Sudo