From 76be210ec3f827b2fb50f790acff3349d88b9eef Mon Sep 17 00:00:00 2001 From: pagedmov Date: Sun, 17 Nov 2024 11:20:31 -0500 Subject: [PATCH] git-compose can now be cancelled with :q! in neovim --- overlay/scripts/commands/git-compose.nix | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/overlay/scripts/commands/git-compose.nix b/overlay/scripts/commands/git-compose.nix index d7471f6..ed7e8b2 100644 --- a/overlay/scripts/commands/git-compose.nix +++ b/overlay/scripts/commands/git-compose.nix @@ -6,6 +6,8 @@ pkgs.writeShellApplication { gawk ]; text = '' + set -e + toplevel=$(git rev-parse --show-toplevel 2>/dev/null) if [ -z "$toplevel" ]; then @@ -16,7 +18,6 @@ pkgs.writeShellApplication { ( 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; } if [ -z "$unstaged" ]; then @@ -24,11 +25,14 @@ pkgs.writeShellApplication { exit 0 fi + git reset > /dev/null 2>&1 + tmpfile=$(mktemp) || { echo "Failed to create a temporary file"; exit 1; } trap 'rm -f "$tmpfile"' EXIT - { + filecontent=$( echo "# Compose your commits here" + echo "# To cancel this operation, exit without saving (e.g., :q!)" echo "#" echo "# Format should be as follows:" echo "# " @@ -41,10 +45,16 @@ pkgs.writeShellApplication { echo "# using the given commit message at the top of the group" echo echo "$unstaged" - } >> "$tmpfile" + ) + echo "$filecontent" > "$tmpfile" nvim -c 'setfiletype gitcommit' "$tmpfile" + if [ "$(cat "$tmpfile")" = "$filecontent" ]; then + echo "No changes found; cancelling composition." + exit 1 + fi + collecting=false msg="" lines=() @@ -63,14 +73,14 @@ pkgs.writeShellApplication { collecting=true msg="$line" else - lines+=("$line") + lines+=("./$line") fi done < "$tmpfile" # Final cleanup if [ -n "$msg" ] && [ ''${#lines[@]} -gt 0 ]; then - git add "''${lines[@]}" - git commit -m "$msg" + git add "''${lines[@]}" + git commit -m "$msg" fi ) '';