This repository has been archived on 2024-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/bin/vdiff

41 lines
948 B
Plaintext
Raw Normal View History

2020-02-12 11:13:46 -06:00
#!/usr/bin/env bash
repo_root="$(git rev-parse --show-toplevel)"
2020-02-12 11:24:16 -06:00
if ! pushd "$repo_root" &> /dev/null; then
echo "Repo doesn't exist!"
exit 2
fi
2020-02-12 11:13:46 -06:00
td="$(mktemp -p "$pdir" -d "vdiff.XXXXXXXX")"
trap "rm -rf \"$td\"" EXIT
files="$(git diff --name-only "$@")"
args=()
vcmd=""
for f in $files; do
d="$(dirname "$td/$f")"
rfn="$(basename "$f")"
fn="$rfn._@HEAD"
cfn="$rfn"
mkdir -p "$d"
2020-02-21 12:39:47 -06:00
# TODO: show at ref if provided
2020-02-12 11:13:46 -06:00
git --no-pager show HEAD:"$f" > "$d/$fn" 2>/dev/null || \
echo "<FILE CREATED>" > "$d/$fn"
cp "$f" "$d/$cfn" 2>/dev/null || echo "<FILE DELETED>" > "$d/$cfn"
if [ -z "$args" ]; then
args+=($d/$fn -c)
vcmd="vert diffsplit $d/$cfn"
else
vcmd="$vcmd | tabnew | e $d/$fn | vert diffsplit $d/$cfn"
fi
done
remaps="nnoremap <leader>k :tabnext<CR> | nnoremap <leader>j :tabprev<CR>"
nvim "${args[@]}" "$vcmd | tabnext | $remaps"
2020-02-12 11:24:16 -06:00
if ! popd &> /dev/null; then
echo "Could not return to original directory"
exit 4
fi