batdiff: Add '--staged' flag

This commit is contained in:
Ethan P 2023-05-15 20:25:23 -07:00
parent 7107e1603f
commit 7aa73f8989
No known key found for this signature in database
GPG Key ID: B29B90B1B228FEBC
2 changed files with 26 additions and 3 deletions

View File

@ -11,6 +11,7 @@ This script supports using [delta](https://github.com/dandavison/delta) as an al
batdiff [OPTIONS] FILE batdiff [OPTIONS] FILE
batdiff [OPTIONS] FILE OTHER_FILE batdiff [OPTIONS] FILE OTHER_FILE
batdiff --staged
## Environment ## Environment
@ -29,6 +30,7 @@ This script supports using [delta](https://github.com/dandavison/delta) as an al
| | `--paging=["never"/"always"]` | Enable/disable paging. | | | `--paging=["never"/"always"]` | Enable/disable paging. |
| | `--pager=[PAGER]` | Specify the pager to use. | | | `--pager=[PAGER]` | Specify the pager to use. |
| | `--terminal-width=[COLS]` | Generate output for the specified terminal width. | | | `--terminal-width=[COLS]` | Generate output for the specified terminal width. |
| | `--staged` | Show staged changes. |

View File

@ -34,10 +34,12 @@ SUPPORTS_DELTA=false
BAT_VERSION="$(bat_version)" BAT_VERSION="$(bat_version)"
BAT_ARGS=() BAT_ARGS=()
DELTA_ARGS=() DELTA_ARGS=()
GIT_ARGS=()
FILES=() FILES=()
OPT_TABS= OPT_TABS=
OPT_CONTEXT=2 OPT_CONTEXT=2
OPT_STAGED=false
OPT_ALL_CHANGES=false OPT_ALL_CHANGES=false
# Set options based on bat version. # Set options based on bat version.
@ -61,6 +63,7 @@ while shiftopt; do
# Script options # Script options
--all) OPT_ALL_CHANGES=true ;; --all) OPT_ALL_CHANGES=true ;;
--staged) OPT_STAGED=true; GIT_ARGS+=("--staged") ;;
--delta) BATDIFF_USE_DELTA=true ;; --delta) BATDIFF_USE_DELTA=true ;;
# ??? # ???
@ -95,6 +98,9 @@ if [[ -n "$OPT_TABS" ]]; then
DELTA_ARGS+=("--tabs=${OPT_TABS}") DELTA_ARGS+=("--tabs=${OPT_TABS}")
fi fi
# Append arguments for git.
GIT_ARGS+=(-U"$OPT_CONTEXT")
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Printing: # Printing:
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
@ -107,11 +113,26 @@ print_bat_diff() {
return $? return $?
fi fi
# Diff staged git file.
if "$OPT_STAGED"; then
if false && "$SUPPORTS_DELTA"; then
# bat doesn't support diffing staged changes against the HEAD.
# Delta is better suited for printing diffs in this case.
print_delta_diff "$@"
else
difftext="$("$EXECUTABLE_GIT" diff "${GIT_ARGS[@]}" "${files[0]}")"
if [[ "${#difftext}" -gt 0 ]]; then
"$EXECUTABLE_BAT" --language=diff --file-name="${files[0]}" - "${BAT_ARGS[@]}" <<< "$difftext"
fi
fi
return $?
fi
# Diff git file. # Diff git file.
if "$SUPPORTS_BAT_DIFF"; then if "$SUPPORTS_BAT_DIFF"; then
"$EXECUTABLE_BAT" --diff --diff-context="$OPT_CONTEXT" "${files[0]}" "${BAT_ARGS[@]}" "$EXECUTABLE_BAT" --diff --diff-context="$OPT_CONTEXT" "${files[0]}" "${BAT_ARGS[@]}"
else else
"$EXECUTABLE_GIT" diff -U"$OPT_CONTEXT" "${files[0]}" | "$EXECUTABLE_BAT" --language=diff - "${BAT_ARGS[@]}" "$EXECUTABLE_GIT" diff "${GIT_ARGS[@]}" "${files[0]}" | "$EXECUTABLE_BAT" --language=diff - "${BAT_ARGS[@]}"
fi fi
} }
@ -125,7 +146,7 @@ print_delta_diff() {
fi fi
# Diff git file. # Diff git file.
"$EXECUTABLE_GIT" diff -U"$OPT_CONTEXT" "${files[0]}" | "$EXECUTABLE_DELTA" "${DELTA_ARGS[@]}" "$EXECUTABLE_GIT" diff "${GIT_ARGS[@]}" "${files[0]}" | "$EXECUTABLE_DELTA" "${DELTA_ARGS[@]}"
} }
if [[ "$BATDIFF_USE_DELTA" = "true" && "$SUPPORTS_DELTA" = "true" ]]; then if [[ "$BATDIFF_USE_DELTA" = "true" && "$SUPPORTS_DELTA" = "true" ]]; then
@ -167,7 +188,7 @@ main() {
if [[ -f "$file" ]]; then if [[ -f "$file" ]]; then
print_diff "$file" print_diff "$file"
fi fi
done < <("${EXECUTABLE_GIT}" diff --name-only --diff-filter=d) done < <("${EXECUTABLE_GIT}" diff "${GIT_ARGS[@]}" --name-only --diff-filter=d)
return return
fi fi