Skip to content

Commit

Permalink
Refactor: Replace deferred code in edit commands with functions
Browse files Browse the repository at this point in the history
_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.
  • Loading branch information
sandr01d committed Mar 6, 2024
1 parent b1831fe commit 322e722
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions bin/git-forgit
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ _forgit_get_files_from_diff_line() {
sed 's/.*] *//' | sed 's/ -> /\n/' | tr '\n' '\0'
}

_forgit_get_single_file_from_diff_line() {
# Similar to the function above, but only gets a single file from a single line
# Gets the new name of renamed files
sed 's/.*] *//' | sed 's/.*-> //'
}

_forgit_exec_diff() {
_forgit_diff_git_opts=()
_forgit_parse_array _forgit_diff_git_opts "$FORGIT_DIFF_GIT_OPTS"
Expand All @@ -184,6 +190,12 @@ _forgit_diff_view() {
"$FORGIT" exec_diff "${commits[@]}" -U"$diff_context" -- | $_forgit_diff_pager
}

_forgit_edit_diffed_file() {
local input_line=$1
filename=$(echo "$input_line" | _forgit_get_single_file_from_diff_line)
$EDITOR "$filename" >/dev/tty </dev/tty
}

_forgit_diff_enter() {
file=$1
commits=("${@:2}")
Expand All @@ -205,9 +217,6 @@ _forgit_diff() {
files=("$@")
fi
}
# Similar to the line above, but only gets a single file from a single line
# Gets the new name of renamed files
get_file="echo {} | sed 's/.*] *//' | sed 's/.*-> //'"
# Git stashes are named "stash@{x}", which contains the fzf placeholder "{x}".
# In order to support passing stashes as arguments to _forgit_diff, we have to
# prevent fzf from interpreting this substring by escaping the opening bracket.
Expand All @@ -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\"
$FORGIT_DIFF_FZF_OPTS
--prompt=\"${commits[*]} > \"
"
Expand Down Expand Up @@ -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
}

# git add selector
_forgit_add() {
_forgit_inside_work_tree || return 1
Expand All @@ -278,7 +291,7 @@ _forgit_add() {
$FORGIT_FZF_DEFAULT_OPTS
-0 -m --nth 2..,..
--preview=\"$FORGIT add_preview {}\"
--bind=\"alt-e:execute-silent($EDITOR \\\"\$\(echo {} | $extract\)\\\" >/dev/tty </dev/tty)+refresh-preview\"
--bind=\"alt-e:execute-silent($FORGIT edit_add_file {})+refresh-preview\"
$FORGIT_ADD_FZF_OPTS
"
files=$(git -c color.status=always -c status.relativePaths=true status -su |
Expand Down Expand Up @@ -908,6 +921,8 @@ private_commands=(
"log_enter"
"exec_diff"
"diff_view"
"edit_diffed_file"
"edit_add_file"
)

cmd="$1"
Expand Down

0 comments on commit 322e722

Please sign in to comment.