Skip to content

Commit

Permalink
batdiff: Add '--staged' flag
Browse files Browse the repository at this point in the history
  • Loading branch information
eth-p committed May 16, 2023
1 parent 7107e16 commit 7aa73f8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/batdiff.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This script supports using [delta](https://github.com/dandavison/delta) as an al

batdiff [OPTIONS] FILE
batdiff [OPTIONS] FILE OTHER_FILE
batdiff --staged

## Environment

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



Expand Down
27 changes: 24 additions & 3 deletions src/batdiff.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ SUPPORTS_DELTA=false
BAT_VERSION="$(bat_version)"
BAT_ARGS=()
DELTA_ARGS=()
GIT_ARGS=()

FILES=()
OPT_TABS=
OPT_CONTEXT=2
OPT_STAGED=false
OPT_ALL_CHANGES=false

# Set options based on bat version.
Expand All @@ -61,6 +63,7 @@ while shiftopt; do

# Script options
--all) OPT_ALL_CHANGES=true ;;
--staged) OPT_STAGED=true; GIT_ARGS+=("--staged") ;;
--delta) BATDIFF_USE_DELTA=true ;;

# ???
Expand Down Expand Up @@ -95,6 +98,9 @@ if [[ -n "$OPT_TABS" ]]; then
DELTA_ARGS+=("--tabs=${OPT_TABS}")
fi

# Append arguments for git.
GIT_ARGS+=(-U"$OPT_CONTEXT")

# -----------------------------------------------------------------------------
# Printing:
# -----------------------------------------------------------------------------
Expand All @@ -107,11 +113,26 @@ print_bat_diff() {
return $?
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.
if "$SUPPORTS_BAT_DIFF"; then
"$EXECUTABLE_BAT" --diff --diff-context="$OPT_CONTEXT" "${files[0]}" "${BAT_ARGS[@]}"
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
}

Expand All @@ -125,7 +146,7 @@ print_delta_diff() {
fi

# 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
Expand Down Expand Up @@ -167,7 +188,7 @@ main() {
if [[ -f "$file" ]]; then
print_diff "$file"
fi
done < <("${EXECUTABLE_GIT}" diff --name-only --diff-filter=d)
done < <("${EXECUTABLE_GIT}" diff "${GIT_ARGS[@]}" --name-only --diff-filter=d)
return
fi

Expand Down

0 comments on commit 7aa73f8

Please sign in to comment.