git-compose can now be cancelled with :q! in neovim

This commit is contained in:
2024-11-17 11:20:31 -05:00
parent 6f06d5ed44
commit 76be210ec3

View File

@@ -6,6 +6,8 @@ pkgs.writeShellApplication {
gawk gawk
]; ];
text = '' text = ''
set -e
toplevel=$(git rev-parse --show-toplevel 2>/dev/null) toplevel=$(git rev-parse --show-toplevel 2>/dev/null)
if [ -z "$toplevel" ]; then if [ -z "$toplevel" ]; then
@@ -16,7 +18,6 @@ pkgs.writeShellApplication {
( (
cd "$toplevel" || { echo "Failed to change to repo root"; exit 1; } cd "$toplevel" || { echo "Failed to change to repo root"; exit 1; }
git reset > /dev/null 2>&1
unstaged=$(git status --porcelain | awk '{print $2}') || { echo "Failed to get git status"; exit 1; } unstaged=$(git status --porcelain | awk '{print $2}') || { echo "Failed to get git status"; exit 1; }
if [ -z "$unstaged" ]; then if [ -z "$unstaged" ]; then
@@ -24,11 +25,14 @@ pkgs.writeShellApplication {
exit 0 exit 0
fi fi
git reset > /dev/null 2>&1
tmpfile=$(mktemp) || { echo "Failed to create a temporary file"; exit 1; } tmpfile=$(mktemp) || { echo "Failed to create a temporary file"; exit 1; }
trap 'rm -f "$tmpfile"' EXIT trap 'rm -f "$tmpfile"' EXIT
{ filecontent=$(
echo "# Compose your commits here" echo "# Compose your commits here"
echo "# To cancel this operation, exit without saving (e.g., :q!)"
echo "#" echo "#"
echo "# Format should be as follows:" echo "# Format should be as follows:"
echo "# <some_commit_message>" echo "# <some_commit_message>"
@@ -41,10 +45,16 @@ pkgs.writeShellApplication {
echo "# using the given commit message at the top of the group" echo "# using the given commit message at the top of the group"
echo echo
echo "$unstaged" echo "$unstaged"
} >> "$tmpfile" )
echo "$filecontent" > "$tmpfile"
nvim -c 'setfiletype gitcommit' "$tmpfile" nvim -c 'setfiletype gitcommit' "$tmpfile"
if [ "$(cat "$tmpfile")" = "$filecontent" ]; then
echo "No changes found; cancelling composition."
exit 1
fi
collecting=false collecting=false
msg="" msg=""
lines=() lines=()
@@ -63,14 +73,14 @@ pkgs.writeShellApplication {
collecting=true collecting=true
msg="$line" msg="$line"
else else
lines+=("$line") lines+=("./$line")
fi fi
done < "$tmpfile" done < "$tmpfile"
# Final cleanup # Final cleanup
if [ -n "$msg" ] && [ ''${#lines[@]} -gt 0 ]; then if [ -n "$msg" ] && [ ''${#lines[@]} -gt 0 ]; then
git add "''${lines[@]}" git add "''${lines[@]}"
git commit -m "$msg" git commit -m "$msg"
fi fi
) )
''; '';