feat: pipe to git commit

Amolith and Crush created

Implements: bug-71272c6
Co-authored-by: Crush <crush@charm.land>

Change summary

main.go | 27 ++++++++++++++++++++++++++-
1 file changed, 26 insertions(+), 1 deletion(-)

Detailed changes

main.go 🔗

@@ -8,6 +8,7 @@ import (
 	"context"
 	"fmt"
 	"os"
+	"os/exec"
 	"runtime/debug"
 	"strings"
 
@@ -75,7 +76,31 @@ formatted-commit -t refactor -s "web/git-bug" -m "fancy shmancy" \
 			commitMsg.WriteString(trailersBlock)
 		}
 
-		fmt.Print(commitMsg.String())
+		gitCmd := exec.Command("git", "commit", "-F", "-")
+		gitCmd.Stdout = os.Stdout
+		gitCmd.Stderr = os.Stderr
+
+		stdin, err := gitCmd.StdinPipe()
+		if err != nil {
+			return fmt.Errorf("failed to create stdin pipe: %w", err)
+		}
+
+		if err := gitCmd.Start(); err != nil {
+			return fmt.Errorf("failed to start git command: %w", err)
+		}
+
+		if _, err := stdin.Write([]byte(commitMsg.String())); err != nil {
+			return fmt.Errorf("failed to write to git stdin: %w", err)
+		}
+
+		if err := stdin.Close(); err != nil {
+			return fmt.Errorf("failed to close stdin: %w", err)
+		}
+
+		if err := gitCmd.Wait(); err != nil {
+			return fmt.Errorf("git commit failed: %w", err)
+		}
+
 		return nil
 	},
 }