Skip to content

Instantly share code, notes, and snippets.

@Pagliacii
Created October 14, 2023 02:56
Show Gist options
  • Save Pagliacii/bd80f39adf31726b92c534c0974121c5 to your computer and use it in GitHub Desktop.
Save Pagliacii/bd80f39adf31726b92c534c0974121c5 to your computer and use it in GitHub Desktop.
Diff your repo files via FZF
# ----------------------------------
# Function: gdcp
# Description:
# This function allows you to interactively select a file from the list of modified files
# in a Git repository, view the diff, and copy it to the clipboard.
#
# Parameters:
# None
#
# Returns:
# None
#
# Usage:
# gdcp [-h|--help]
#
# Example:
# gdcp
# ----------------------------------
gdcp() {
# Check if the function is called with -h or --help flag
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
echo "Usage: gdcp [-h|--help]"
echo "Interactively select a file from the list of modified files,"
echo "view the diff, and copy it to the clipboard."
echo ""
echo "Options:"
echo " -h, --help Show this help message and exit."
return 0
# Check for invalid parameters
elif [[ -n "$1" ]]; then
echo "Invalid parameter: $1"
echo "Usage: gdcp [-h|--help]"
return 1
fi
# Main functionality of the function
git status -s | cut -d' ' -f3 |
fzf --bind "enter:become(git diff {1} | cb copy)" \
--bind "ctrl-n:preview-down,ctrl-p:preview-up" \
--bind "ctrl-f:preview-page-down,ctrl-b:preview-page-up" \
--preview "git diff {1} | bat --color=always -p" \
--preview-window "right,55%,border-left"
}
@Pagliacii
Copy link
Author

Same function for Mercurial hg:

hdcp() {
    hg status -admnr |
      fzf --bind "enter:become(git diff {1} | cb copy)" \
        --bind "ctrl-n:preview-down,ctrl-p:preview-up" \
        --bind "ctrl-f:preview-page-down,ctrl-b:preview-page-up" \
        --preview "hg diff -g {1} | bat --color=always -p" \
        --preview-window "right,55%,border-left"
}

@Pagliacii
Copy link
Author

More helpful functions for hg:

hglv() {
    hg log --template "{rev} {desc|strip|firstline}\n" -b. |
      fzf --bind "enter:become(hg log -p -r {1} | tail -n +7 |cb copy)" \
        --bind "ctrl-n:preview-down,ctrl-p:preview-up" \
        --bind "ctrl-f:preview-page-down,ctrl-b:preview-page-up" \
        --preview "hg log -p -r {1} | bat --color=always -p -r 7: -l diff" \
        --preview-window "right,55%,border-left"
}

hgadd() {
  hg status -dmnru |
    fzf --bind "enter:become(hg add {1})" \
        --bind "ctrl-n:preview-down,ctrl-p:preview-up" \
        --bind "ctrl-f:preview-page-down,ctrl-b:preview-page-up" \
        --preview "hg diff {1} | bat --color=always -p -l diff" \
        --preview-window "right,55%,border-left"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment