Skip to content

Commit

Permalink
feat: add git add --patch interactive command (#53)
Browse files Browse the repository at this point in the history
I use --patch quite a lot when doing commits. This adds patch support as a separate command.
  • Loading branch information
iloveitaly authored Jul 12, 2023
1 parent a9a030d commit 8aab155
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
15 changes: 15 additions & 0 deletions lib/modules/helpers/status.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 -- "$@"
}
Expand Down Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions lib/modules/status.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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}"'
Expand All @@ -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" \
Expand All @@ -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
Expand Down

0 comments on commit 8aab155

Please sign in to comment.