From 150182149ad3103e141ccc5e1cca88711682f787 Mon Sep 17 00:00:00 2001
From: Oliver Davies <oliver@oliverdavies.dev>
Date: Mon, 25 Nov 2024 23:27:22 +0000
Subject: [PATCH] Add git-squash-merge script

---
 nix/modules/home-manager/git.nix |  5 +++-
 nix/pkgs/default.nix             |  1 +
 nix/pkgs/git-squash-merge.nix    | 40 ++++++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+), 1 deletion(-)
 create mode 100644 nix/pkgs/git-squash-merge.nix

diff --git a/nix/modules/home-manager/git.nix b/nix/modules/home-manager/git.nix
index c54b26af..40bef112 100644
--- a/nix/modules/home-manager/git.nix
+++ b/nix/modules/home-manager/git.nix
@@ -146,7 +146,10 @@
     };
   };
 
-  home.packages = [ pkgs.git-instafix ];
+  home.packages = with pkgs; [
+    git-instafix
+    git-squash-merge
+  ];
 
   home.sessionVariables = {
     GIT_INSTAFIX_UPSTREAM = "origin/main";
diff --git a/nix/pkgs/default.nix b/nix/pkgs/default.nix
index d8cb0376..72f17e2a 100644
--- a/nix/pkgs/default.nix
+++ b/nix/pkgs/default.nix
@@ -5,6 +5,7 @@ let
 in
 {
   build-glove80 = callPackage ./build-glove80.nix { };
+  git-squash-merge = callPackage ./git-squash-merge.nix { };
 
   vimPlugins = prev.vimPlugins // {
     conf-vim = callPackage ./vim-plugins/conf-vim.nix { };
diff --git a/nix/pkgs/git-squash-merge.nix b/nix/pkgs/git-squash-merge.nix
new file mode 100644
index 00000000..bfc1ad4d
--- /dev/null
+++ b/nix/pkgs/git-squash-merge.nix
@@ -0,0 +1,40 @@
+{ pkgs }:
+
+pkgs.writeShellApplication {
+  name = "git-squash-merge";
+
+  runtimeInputs = with pkgs; [
+    bashInteractive
+    coreutils
+    git
+  ];
+
+  text = ''
+    branch_to_merge=$1
+    target_branch=''${2:-main}
+
+    if [[ -z "$branch_to_merge" ]]; then
+      echo "Usage: $0 <branch-to-merge> [target-branch]"
+      exit 1
+    fi
+
+    if ! git checkout "$target_branch"; then
+      echo "Error: Failed to checkout target branch '$target_branch'."
+      exit 1
+    fi
+
+    if ! git merge --squash "$branch_to_merge"; then
+      echo "Error: Squash merge from '$branch_to_merge' to '$target_branch' failed."
+      exit 1
+    fi
+
+    squashed_commit_messages=$(git log "$target_branch..$branch_to_merge" --reverse --pretty=format:"%h %s%n%b%n")
+
+    temp_file=$(mktemp)
+    echo -e "$squashed_commit_messages" > "$temp_file"
+
+    GIT_COMMIT_EDITMSG="$temp_file" git commit --edit
+
+    rm -f "$temp_file"
+  '';
+}