From 8aab155445207088f690437246902079f0858d60 Mon Sep 17 00:00:00 2001 From: Michael Bianco Date: Wed, 12 Jul 2023 11:48:30 -0600 Subject: [PATCH] feat: add git add --patch interactive command (#53) I use --patch quite a lot when doing commits. This adds patch support as a separate command. --- lib/modules/helpers/status.sh | 15 +++++++++++++++ lib/modules/status.sh | 14 ++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/modules/helpers/status.sh b/lib/modules/helpers/status.sh index 54a2d71..499d789 100644 --- a/lib/modules/helpers/status.sh +++ b/lib/modules/helpers/status.sh @@ -43,6 +43,19 @@ gf_helper_status_add() { gf_command_logged git add -- "$@" } +gf_helper_status_add_patch() { + if [ "$#" = 0 ]; then + gf_log_error 'tried to git add --patch with no file(s)' + else + gf_interactive_command_logged git add --patch -- "$@" < /dev/tty + fi + + # if there's more to commit, loop right back into the status view + if [ -n "$(git status -s)" ]; then + gf_interactive_command_logged git fuzzy status + fi +} + gf_helper_status_reset() { gf_command_logged git reset -- "$@" } @@ -71,6 +84,8 @@ gf_helper_status_edit() { gf_helper_status_commit() { # shellcheck disable=2086 gf_interactive_command_logged git commit + + # if there's more to commit, loop right back into the status view if [ -n "$(git status -s)" ]; then gf_interactive_command_logged git fuzzy status fi diff --git a/lib/modules/status.sh b/lib/modules/status.sh index eb3ba79..e107a7a 100644 --- a/lib/modules/status.sh +++ b/lib/modules/status.sh @@ -2,6 +2,7 @@ # shellcheck disable=2016 GIT_FUZZY_STATUS_ADD_KEY="${GIT_FUZZY_STATUS_ADD_KEY:-Alt-S}" +GIT_FUZZY_STATUS_ADD_PATCH_KEY="${GIT_FUZZY_STATUS_ADD_PATCH_KEY:-Alt-P}" GIT_FUZZY_STATUS_EDIT_KEY="${GIT_FUZZY_STATUS_EDIT_KEY:-Alt-E}" GIT_FUZZY_STATUS_COMMIT_KEY="${GIT_FUZZY_STATUS_COMMIT_KEY:-Alt-C}" GIT_FUZZY_STATUS_RESET_KEY="${GIT_FUZZY_STATUS_RESET_KEY:-Alt-R}" @@ -11,7 +12,8 @@ GF_STATUS_HEADER=' Type to filter. '"${WHITE}Enter${NORMAL} to ${GREEN}ACCEPT${NORMAL}"' '"${GRAY}-- (${NORMAL}*${GRAY}) editor: ${MAGENTA}${EDITOR} ${NORMAL}${GF_EDITOR_ARGS}${NORMAL}"' - '" * ${GREEN}${BOLD}edit ✎${NORMAL} ${WHITE}$GIT_FUZZY_STATUS_EDIT_KEY${NORMAL}"' + '""' + '" ${GREEN}stage -p ${BOLD}⇡ ${NORMAL}${WHITE}${GIT_FUZZY_STATUS_ADD_PATCH_KEY}${NORMAL} * ${GREEN}${BOLD}edit ✎${NORMAL} ${WHITE}$GIT_FUZZY_STATUS_EDIT_KEY${NORMAL}"' '"${GREEN}all ☑${NORMAL} ${WHITE}${GIT_FUZZY_SELECT_ALL_KEY}${NORMAL} ${GREEN}stage ${BOLD}⇡${NORMAL} ${WHITE}$GIT_FUZZY_STATUS_ADD_KEY${NORMAL} ${RED}${BOLD}discard ✗${NORMAL} ${WHITE}$GIT_FUZZY_STATUS_DISCARD_KEY${NORMAL}"' '"${GREEN}none ☐${NORMAL} ${WHITE}${GIT_FUZZY_SELECT_NONE_KEY}${NORMAL} ${GREEN}reset ${RED}${BOLD}⇣${NORMAL} ${WHITE}$GIT_FUZZY_STATUS_RESET_KEY${NORMAL} * ${RED}${BOLD}commit ${NORMAL}${RED}⇧${NORMAL} ${WHITE}$GIT_FUZZY_STATUS_COMMIT_KEY${NORMAL}"' @@ -23,11 +25,10 @@ fi gf_fzf_status() { RELOAD="reload:git fuzzy helper status_menu_content" - # doesn't work gf_fzf -m --header "$GF_STATUS_HEADER" \ --header-lines=2 \ - --expect="$(lowercase "$GIT_FUZZY_STATUS_EDIT_KEY"),$(lowercase "$GIT_FUZZY_STATUS_COMMIT_KEY")" \ + --expect="$(lowercase "$GIT_FUZZY_STATUS_EDIT_KEY"),$(lowercase "$GIT_FUZZY_STATUS_COMMIT_KEY"),$(lowercase "$GIT_FUZZY_STATUS_ADD_PATCH_KEY")" \ --nth=2 \ --preview 'git fuzzy helper status_preview_content {1} {2} {4}' \ --bind "$(lowercase "$GIT_FUZZY_STATUS_ADD_KEY"):execute-silent(git fuzzy helper status_add {+2..})+down+$RELOAD" \ @@ -39,10 +40,15 @@ gf_status_interpreter() { CONTENT="$(cat -)" HEAD="$(echo "$CONTENT" | head -n1)" TAIL="$(echo "$CONTENT" | tail -n +2)" + if [ "$(lowercase "$HEAD")" = "$(lowercase "$GIT_FUZZY_STATUS_EDIT_KEY")" ]; then - eval "git fuzzy helper status_edit $(echo "$TAIL" | cut -c4- | join_lines_quoted)" + local selected_file=$(echo "$TAIL" | cut -c4- | join_lines_quoted) + eval "git fuzzy helper status_edit $selected_file" elif [ "$(lowercase "$HEAD")" = "$(lowercase "$GIT_FUZZY_STATUS_COMMIT_KEY")" ]; then eval "git fuzzy helper status_commit" + elif [ "$(lowercase "$HEAD")" = "$(lowercase "$GIT_FUZZY_STATUS_ADD_PATCH_KEY")" ]; then + local selected_file=$(echo "$TAIL" | cut -c4- | join_lines_quoted) + eval "git fuzzy helper status_add_patch $selected_file" else echo "$TAIL" | cut -c4- fi