Skip to content
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

feat: search git-stash follows git-log #310

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions completions/fzf_configure_bindings.fish
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ complete fzf_configure_bindings --long git_status --description "Change the key
complete fzf_configure_bindings --long history --description "Change the key binding for Search History" --condition "not __fish_seen_argument --history"
complete fzf_configure_bindings --long processes --description "Change the key binding for Search Processes" --condition "not __fish_seen_argument --processes"
complete fzf_configure_bindings --long variables --description "Change the key binding for Search Variables" --condition "not __fish_seen_argument --variables"
complete fzf_configure_bindings --long git_stash --description "Change the key binding for Search Git Stash" --condition "not __fish_seen_argument --git_stash"
25 changes: 25 additions & 0 deletions functions/_fzf_search_git_stash.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function _fzf_search_git_stash --description "Search the output of git stash show"
if not git rev-parse --git-dir >/dev/null 2>&1
echo '_fzf_search_git_stash: Not in a git repository.' >&2
else
if not set --query fzf_git_log_format
# %h gives you the abbreviated commit hash, which is useful for saving screen space, but we will have to expand it later below
set fzf_git_log_format '%C(bold blue)%h%C(reset) - %C(cyan)%ad%C(reset) %C(yellow)%d%C(reset) %C(normal)%s%C(reset) %C(dim normal)[%an]%C(reset)'
end
set selected_stash_line (
git stash list --no-show-signature --color=always --format=format:$fzf_git_log_format --date=short | nl -v 0 -w 3 -s ' ' | \
_fzf_wrapper --ansi \
--tiebreak=index \
--prompt="Search Git Stash> " \
--preview='git stash show --color=always --stat --patch {2}' \
--query=(commandline --current-token) \
$fzf_git_log_opts
)
if test $status -eq 0
set stash_index (string trim $selected_stash_line | string split --field 1 ' ')
commandline --current-token --replace stash@\{$stash_index\}
end
end

commandline --function repaint
end
6 changes: 4 additions & 2 deletions functions/fzf_configure_bindings.fish
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function fzf_configure_bindings --description "Installs the default key bindings
# no need to install bindings if not in interactive mode or running tests
status is-interactive || test "$CI" = true; or return

set -f options_spec h/help 'directory=?' 'git_log=?' 'git_status=?' 'history=?' 'processes=?' 'variables=?'
set -f options_spec h/help 'directory=?' 'git_log=?' 'git_status=?' 'history=?' 'processes=?' 'variables=?' 'git_stash=?'
argparse --max-args=0 --ignore-unknown $options_spec -- $argv 2>/dev/null
if test $status -ne 0
echo "Invalid option or a positional argument was provided." >&2
Expand All @@ -16,13 +16,14 @@ function fzf_configure_bindings --description "Installs the default key bindings
else
# Initialize with default key sequences and then override or disable them based on flags
# index 1 = directory, 2 = git_log, 3 = git_status, 4 = history, 5 = processes, 6 = variables
set -f key_sequences \e\cf \e\cl \e\cs \cr \e\cp \cv # \c = control, \e = escape
set -f key_sequences \e\cf \e\cl \e\cs \cr \e\cp \cv \e\ct # \c = control, \e = escape
set --query _flag_directory && set key_sequences[1] "$_flag_directory"
set --query _flag_git_log && set key_sequences[2] "$_flag_git_log"
set --query _flag_git_status && set key_sequences[3] "$_flag_git_status"
set --query _flag_history && set key_sequences[4] "$_flag_history"
set --query _flag_processes && set key_sequences[5] "$_flag_processes"
set --query _flag_variables && set key_sequences[6] "$_flag_variables"
set --query _flag_git_stash && set key_sequences[7] "$_flag_git_stash"

# If fzf bindings already exists, uninstall it first for a clean slate
if functions --query _fzf_uninstall_bindings
Expand All @@ -36,6 +37,7 @@ function fzf_configure_bindings --description "Installs the default key bindings
test -n $key_sequences[4] && bind --mode $mode $key_sequences[4] _fzf_search_history
test -n $key_sequences[5] && bind --mode $mode $key_sequences[5] _fzf_search_processes
test -n $key_sequences[6] && bind --mode $mode $key_sequences[6] "$_fzf_search_vars_command"
test -n $key_sequences[7] && bind --mode $mode $key_sequences[7] _fzf_search_git_stash
end

function _fzf_uninstall_bindings --inherit-variable key_sequences
Expand Down