Avoid use of regex in bash hook output#1043
Avoid use of regex in bash hook output#1043zimbatm merged 1 commit intodirenv:masterfrom scop:refactor/bash-hook-no-regex
Conversation
| return $previous_exit_status; | ||
| }; | ||
| if ! [[ "${PROMPT_COMMAND:-}" =~ _direnv_hook ]]; then | ||
| if [[ ";${PROMPT_COMMAND-};" != *";_direnv_hook;"* ]]; then |
There was a problem hiding this comment.
| if [[ ";${PROMPT_COMMAND-};" != *";_direnv_hook;"* ]]; then | |
| if [[ ";${PROMPT_COMMAND-};" != *"_direnv_hook"* ]]; then |
The item might be alone in the list or at the start of the list.
There was a problem hiding this comment.
The added semicolons we have around the strings both on the left and right sides of the != cover those cases, too.
If it's alone there, the test becomes [[ ";_direnv_hook;" != *";_direnv_hook;"* ]] which is false as wanted; if it's the first followed by something else it becomes [[ ";_direnv_hook;something_else;" != *";_direnv_hook;"* ]] which is also false as wanted.
Removing the semicolons per the suggestion would cause undesired results e.g. if $PROMPT_COMMAND would be this_is_not_the_direnv_hook_we_are_looking_for. Theoretical, but we could just do the right thing for that, too :)
|
I just wanted to make a PR for this because the current implementation also fails when you have something like this in your This is a contrived example of course, but if we'd move the exclamation mark inside the If this PR gets merged, the issue also goes away, since there is no negation anymore then. |
|
Rebased. Is it still worth merging? |
|
Sure, thanks. |
|
only took a year to get merge :-p |
Not that this would be performance critical, but in principle.
Also makes the match test more exact, rather than hitting anything with a
_direnv_hooksubstring.