Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

bash: improve prior_trap processing #5142

Merged
merged 1 commit into from
Jan 16, 2025

Conversation

jparise
Copy link
Collaborator

@jparise jparise commented Jan 16, 2025

We use trap to bootstrap our installation function (__bp_install). We remove our code upon first execution but need to restore any preexisting trap calls. We previously used sed to process the trap string, but that had two downsides:

  1. sed is an external command dependency. It needs to exist on the system, and we need to invoke it in a subshell (which has some runtime cost).

  2. The regular expression pattern was imperfect and didn't handle trickier cases like ' characters in the trap string:

     $ (trap "echo 'hello'" DEBUG; trap -p DEBUG)
     hello
     trap -- 'echo '\''hello'\''' DEBUG
    

This change removes the dependency on sed by locally evaluating the trap string and extracting any prior trap. This works reliably because we control the format our trap string, which looks like this (with newlines expanded):

__bp_trap_string="$(trap -p DEBUG)"
trap - DEBUG
__bp_install

Upstream: rcaloras/bash-preexec#170

We use `trap` to bootstrap our installation function (__bp_install). We
remove our code upon first execution but need to restore any preexisting
trap calls. We previously used `sed` to process the trap string, but
that had two downsides:

1. `sed` is an external command dependency. It needs to exist on the
    system, and we need to invoke it in a subshell (which has some
    runtime cost).
2. The regular expression pattern was imperfect and didn't handle
    trickier cases like `'` characters in the trap string:

        $ (trap "echo 'hello'" DEBUG; trap -p DEBUG)
        hello
        trap -- 'echo '\''hello'\''' DEBUG

This change removes the dependency on `sed` by locally evaluating the
trap string and extracting any prior trap. This works reliably because
we control the format our trap string, which looks like this (with
newlines expanded):

    __bp_trap_string="$(trap -p DEBUG)"
    trap - DEBUG
    __bp_install
@jparise jparise mentioned this pull request Jan 16, 2025
@mitchellh mitchellh merged commit b4a3ca9 into ghostty-org:main Jan 16, 2025
30 checks passed
@github-actions github-actions bot added this to the 1.1.0 milestone Jan 16, 2025
@jparise jparise deleted the bash-preexec-prior_trap branch January 16, 2025 21:52
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants