-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Pager variables are not evaluated #381
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remind me again what the initial goal of striking eval
from the program was?
I think this approach is fine, especially if we can say that we didn't "give up" that initial goal by re-introducing it here. Code seems good and encapsulated, and I'm generally for this change 👍 (interested to hear what others think)
|
@sandr01d Thank you for your recent series of outstanding contributions to forgit. I see your points, and agree with most of them. My only concern is, for a terminal utils like forgit, do we need to pursue extreme security to the point of giving up the simplicity and efficiency of the code? |
My comment regarding security was meant as one of the reasons why we've removed |
No, I'm referring to the changes made in this PR. #326 looks good to me. As you said, solving this problem is difficult to bypass When I talk about efficiency, I don’t just mean this part of the function, but also the loading speed of the git-forgit script. After all, we need to completely parse this script every time we use a forgit function. I'm worried that its continued expansion will make users experience more obvious delays. Of course, just this one commit will not immediately cause a problem, but we should still be aware of it. Another point I think of is lazy evaluation should already be achieved every time a forgit command calls the |
Just adding my two cents here: I am more conservative in this case. I like the previous implementation better, since it is really easy to read. The change adds complexitiy to the code that is not needed IMO. What about calling |
That sounds like a good approach to me @carlfriedrich, not sure exactly what it would look like though. Would you be open to create a PR for this? In this case we could close this one. |
Another option is moving all the pager logic into another script named like declare -A PAGERS
PAGERS["core"]=${FORGIT_PAGER:-$(git config core.pager || echo cat)}
PAGERS["show"]=${FORGIT_SHOW_PAGER:-$(git config pager.show || echo "${PAGERS[core]}")}
PAGERS["diff"]=${FORGIT_DIFF_PAGER:-$(git config pager.diff || echo "${PAGERS[core]}")}
PAGERS["blame"]=${FORGIT_BLAME_PAGER:-$(git config pager.blame || echo "${PAGERS[core]}")}
PAGERS["ignore"]=${FORGIT_IGNORE_PAGER:-$(hash bat &>/dev/null && echo 'bat -l gitignore --color=always' || echo cat)}
PAGERS["enter"]=${FORGIT_ENTER_PAGER:-"LESS='-r' less"}
pager="${1:core}"
if [[ -z "${PAGERS[$pager]}" ]]; then
echo "pager not found: $pager" >&2
exit 1
fi
eval "${PAGERS[$pager]}" Defining a function in Then we use ++FORGIT_PAGER="${FORGIT%/*}/forgit-pager"
...
--preview_cmd="echo {} | ... | $_forgit_show_pager"
++preview_cmd="echo {} | ... | $FORGIT_PAGER show"
... |
@wfxr I like that approach, we would even have only one single |
@carlfriedrich Yes, it can be implemented in the existing |
I would prefer keeping this in a single script, if possible. I think it simplifies things especially with regards to our packaging and making sure everything works when installed.
Not the end of the world if we split it though, just my 2cents!
…On Sat, Apr 6, 2024 at 1:59 AM, Wenxuan ***@***.***(mailto:On Sat, Apr 6, 2024 at 1:59 AM, Wenxuan <<a href=)> wrote:
> ***@***.***(https://github.com/wfxr) I like that approach, we would even have only one single eval there. I do not see any advantage of putting that into a dedicated script, though, and would prefer a function.
***@***.***(https://github.com/carlfriedrich) Yes, it can be implemented in the existing git-forgit script. Perhaps one advantage of creating a dedicate script is that it can reduce some lines in git-forgit. But it's not that important, both ways are OK for me.
—
Reply to this email directly, [view it on GitHub](#381 (comment)), or [unsubscribe](https://github.com/notifications/unsubscribe-auth/ADMDXY4MJL4PBOIXGRGKQ3TY362QNAVCNFSM6AAAAABFUFA7ICVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANBRGAZDENZQHE).
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
This reverts commit 70fac98.
I definitively like @wfxr's approach. Unfortunately associative arrays are only available in bash 4.0+, so the code won't work on macOS as is. I've pushed an approach that follows the same idea without using associative arrays. Also had to adjust it a bit to make it work with variables being passed along to the ignore pager. WDYT? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great solution, thanks a lot @sandr01d. Looks good to me code-wise, just a very tiny comment.
bin/git-forgit
Outdated
} | ||
|
||
_forgit_get_pager() { | ||
case "${1:-core}" in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a local
here as well please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, updated accordingly. Let me know if it works for you like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks a lot!
Check list
Description
Our pager variables, e.g.
$_forgit_blame_pager
used to be in deferred code blocks that were evaluated witheval
. Since we've removed the usage ofeval
in #326, variables in the pagers no longer get expanded, as is the case with #380.In this specific case, evaluation is what we want and deferred code is unavoidable if we do not want to cut down on existing functionality, since the pagers are defined outside forgit (either in the git config or in an environment variable). The most straight forward approach to solving this would probably be to introduce
eval
everywhere we use the pager variables, e.g.However, this solution is ugly in my eyes and I'm concerned it reintroduces the anti-pattern of
eval
/deferred code that we got rid of in #326.So here is an attempt to solve this issue while keeping the amount of deferred code and usage of
eval
as low as possible.The code here is not final by any means and not tested thoroughly. This is a draft to start a discussion on how we want to solve this issue. Would highly appreciate your thoughts on this @wfxr, @cjappl & @carlfriedrich.
Type of change
Test environment