-
Notifications
You must be signed in to change notification settings - Fork 136
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
Refactor: Replace deferred code in edit commands with functions #361
Conversation
Forgit allows specifying git options in environment variables that are passed along to the individual git commands. We currently treat those as strings. This commit adds a _forgit_parse_array function and uses it to parse all such environment variables into arrays instead. This will allow us to get rid of deferred code, since we can pass the parsed arrays directly to the git commands and don't have to rely on eval.
Removes the deferred code that is used for creating the fzf preview functions and replaces it with _forgit_*_preview functions instead. These functions are exposed as forgit commands so they can be invoked from the fzf subshell. We split the exposed commands into public_commands and private_commands. The only difference between them is that public_commands are mentioned in the help text. This commit changes the flags variable in _forgit_blame from a string to an array. This is necessary to allow the flags to be passed to _forgit_blame_preview as individual arguments.
We often used deferred code to encapsulate git commands and make them reusable. This change removes deferred code for git commands and replaces it with functions instead. Some of the deferred code was used with xargs, which executes it on a subshell. To avoid having to expose the new git functions the same way we do with the preview functions, the usage of xargs in these cases is replaced with either a loop or a single command when possible.
We used to have a variable that was either undefined or contained a piece of deferred code that piped input through emojify when present on the system. To remove the deferred code here, this commit replaces the _forgit_emojify variable with a function that either pipes the input through emojify or through cat, depending on whether emojify is present.
We were using deferred code in git commands in some places without any reason. Each of these deferred code snippets was only executed a single time, so we can replace them with regular git commands. This commit changes how we handle the FORGIT_LOG_GRAPH_ENABLE environment variable. We previously used a variable that stored the --graph flag as a string and unset it, when FORGIT_LOG_GRAPH_ENABLE was set to anything other than true. We now create an empty array and add the --graph flag to it when FORGIT_LOG_GRAPH_ENABLE is unset or true. Doing it this way allows us to build a command line without having to use eval. The outcome is the same as before.
In _forgit_log and _forgit_enter it is possible to diff a single commit/file by pressing enter. We used to store the code that executes the diffs in variables and passed it to fzf as deferred code. This refactor reduces the amount of deferred code by using functions instead of variables.
@carlfriedrich Scratch my earlier comment about doing |
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.
@sandr01d Thanks for the new PR! Just two small comments, rest LGTM.
bin/git-forgit
Outdated
@@ -258,6 +267,10 @@ _forgit_get_single_file_from_add_line() { | |||
sed -e 's/^\"//' -e 's/\"$//' | |||
} | |||
|
|||
_forgit_edit_add_file() { | |||
$EDITOR "$(echo "$1" | _forgit_get_single_file_from_add_line)" >/dev/tty </dev/tty |
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.
This has been implemented as a one-liner, whereas _forgit_edit_diffed_file()
has split the nearly identical work into three (better readable) lines. We should transform this accordingly to have the same style in both functions.
bin/git-forgit
Outdated
@@ -219,7 +228,7 @@ _forgit_diff() { | |||
$FORGIT_FZF_DEFAULT_OPTS | |||
+m -0 --bind=\"enter:execute($FORGIT diff_enter {} $escaped_commits | $_forgit_enter_pager)\" | |||
--preview=\"$FORGIT diff_view {} $_forgit_preview_context $escaped_commits\" | |||
--bind=\"alt-e:execute-silent($EDITOR \\\"\$\($get_file)\\\" >/dev/tty </dev/tty)+refresh-preview\" | |||
--bind=\"alt-e:execute($FORGIT edit_diffed_file {})+refresh-preview\" |
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.
This should stay execute-silent
, shouldn't it?
_forgit_diff and _forgit_add allow editing the currently previewed file in the EDITOR. This used to be handled entirely using deferred code. This commit replaces the deferred code and binds the commands to functions instead.
Good finds @carlfriedrich, thanks! I've implemented as requested. Rerequesting your review. |
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 for the update!
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!
Changes have been merged with #326 |
Check list
Description
_forgit_diff
and_forgit_add
allow editing the currently previewed file in theEDITOR
. This used to be handled entirely using deferred code. This PR replaces the deferred code and binds the commands to functions instead.Note
This PR belongs to #326 and resulted from discussions in #324.
Type of change
Test environment