Last active
November 12, 2019 18:19
-
-
Save benbridge/1f4cb20e959745d49411d2d0bace2296 to your computer and use it in GitHub Desktop.
Run a git command on files with differences selected using fzf.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function run_git_command_on_differences -d "Run a git command on files with differences selected using fzf." | |
# Return if not in a repo | |
# Suppress output to avoid default "no repo" message. | |
if ! git rev-parse --is-inside-work-tree >/dev/null | |
echo "No repo found." | |
return | |
end | |
# Return if no changes are detected (exit code is 0). | |
# Disable all output of the program (--quiet) and exit with 1 if there are | |
# differences and 0 if no differences (--exit-code). | |
if git diff --quiet --exit-code | |
echo "No changes detected." | |
return | |
end | |
# Change grep based on command. | |
# Default filters out any staged changes. | |
set grep_command 'grep "^[ ?]"' | |
if test $argv = "reset" | |
# Only show staged changes if the command is "reset". | |
set grep_command 'grep "^[A-Z]"' | |
end | |
# Only allow the commands in $whitelist. | |
set whitelist "add" "diff" "reset" | |
if not contains $argv $whitelist | |
echo "$argv is not an allowed git command" | |
return | |
end | |
git status --porcelain \ | |
| eval $grep_command \ | |
| fzf --height 15% --reverse --multi \ | |
| cut -c 4- \ | |
| xargs -I % git "$argv" (git rev-parse --show-toplevel)/% | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment