wfxr / forgit

:zzz: A utility tool powered by fzf for using git interactively.
MIT License
4.32k stars 136 forks source link

Reduce deferred execution #326

Closed sandr01d closed 3 months ago

sandr01d commented 8 months ago

Check list

Description

We've removed deferred code and replaced it with functions instead. This includes all usages of eval. There are four different types of functions we've added:

  1. Preview functions (e.g. _forgit_add_preview) that are responsible for the fzf preview
  2. Edit functions (e.g. _forgit_edit_add_file) that are responsible for editing files with your $EDITOR from within forgit functions
  3. Git functions (e.g. _forgit_git_add) that make simple git commands reusable
  4. Utility functions (e.g. _forgit_yank_sha or _forgit_quote_files) that were created either out of necessity or implement functions that were previously stored as deferred code in variables.

We've exposed some of these functions as forgit commands. This includes all preview and edit functions and some of the utility functions, so they can be invoked from fzf's subshell. Additionaly some of the git functions were exposed to make it possible to use them with xargs.

In some places were we used deferred code before, we now make use of arrays to allow building commands while having control over globbing and word splitting. We do this for all the git opts environment variables, such as FORGIT_ADD_GIT_OPTS.

In places were we could not avoid deferred execution (e.g. when having to pass arguments to preview functions as we do in _forgit_log) we ensured variables are quoted properly. Variables not quoted properly were one of the main source of bugs previously.

Implementation Details

General

We now make use of the -a flag with read which does exist in bash, but not in zsh. This is fine, since we always use bash for executing /bin/git-forgit since #241.

gbl

It is now possible to pass more than one argument to gbl (e.g. gbl --color-by-age --color-lines). This was a bug previously and the fix is a side effect of us now using an array for the flags instead of relying on eval.

grc

I've removed passing the files variable to the preview, as well as passing parameters to the git log command with $*. These were creating issues in the new implantation and they were actually always empty, because _forgit_revert exits early when any arguments are passed. I copied them by mistake when I implemented _forgit_revert, they did never actually serve any purpose.

glo, grb & gfu

File arguments are now parsed using a different approach using _forgit_quote_files. The only difference in behavior to the sed command we had previously is that the string that is printed out has each file name wrapped in single quotes, so they can be used safely without having to worry about globbing or word splitting. There is no need to quote this string when passing it to a preview function, because the individual file names inside it are already properly quoted. I changed the files variable names to quoted_files to indicate this.

Type of change

Test environment

cjappl commented 3 months ago
Git stash show/git stash push

āŒ You are able to enter into gsp with no changes in the directory, I feel we should early exit/not drop into fzf in this case

I can reproduce this in this branch, but so can I in the master branch, so not a regression. Definitively a bug though, I agree that we should open a separate issue

Filed here https://github.com/wfxr/forgit/issues/369

For the future, definitely does not have to be taken on until this big review lands

Blame

Good intuition. This was a config issue on my side. Fixing my config fixed the issue, this is not a regression or anything to worry about.

I agree. Syntax highlighting would be super useful here.

https://github.com/wfxr/forgit/issues/370

For the future, definitely does not have to be taken on until this big review lands

Would be great if you could play around the environment variables we allow customizing the git commands with, such as FORGIT_BLAME_GIT_OPTS. Also passing arguments to the git commands that support it like gbl --color-by-age.

Will put some time on my calendar to do this tomorrow, same with git fixup.

I have updated my big summary comment above with this info as well.

carlfriedrich commented 3 months ago

In regards to merging, I would strongly recommend we merge this PR (without squashing it) once it's tested. This way, we can be absolutely certain that the final state resembles exactly what we've been testing and we avoid having to rebase and merge 11 PRs and any mistakes that might arise by doing so.

@sandr01d You're probably right, that seems to be the safest way. I think GitHub should actually automatically close the 11 other PRs when we merge this, since they contain the same commits as in this branch.

@cjappl Great test work, thanks a lot! I will do some testing during the next days as well.

cjappl commented 3 months ago

I have completed my testing and deem this good to ship on fish shell and macOS. My complete testing is in a previous comment. (including test cases I didn't do before.

Let me know if there is anything else people can think to test that I have missed.

sandr01d commented 3 months ago

Since we did not find any new bugs I'm planning to merge this on Sunday when I don't hear any objections.

carlfriedrich commented 3 months ago

@sandr01d Didn't have time for testing yet, but if you guys did not find anything then go for it! šŸš€