loop: auto-commit changes when saving in diff view

What I've done is create a git commit whenever the user edits things,
and amend if possible. The existing detection logic pushes the commits
to the host, BUT, I had to do some plumbing to make that happen. The
agent state machine would be out of sorts if I did this (since we're
doing things outside of the loop), but, I didn't tell it, so... it's ok!

If the user has the agent running when editing, everyone can get
confused. There's no atomicity for the git operations, etc.
I suspect in practice this will all be as fine as everything else is.

I'm not running the autoformatters. That's a weird editing experience.

The alternative was to do what the diff comments does, and let the agent
deal with the changes by sending it a message. I chose not to do that:
first of all, I want the push to happen fast, since I don't like losing
user data. Second, the latency on an operation is distracting sometimes,
and sometimes what I do next is just cherrypick my changes over, and I'm
not interested in the pedantry of the agent and the code formatters and
so forth. If they were faster, maybe.

Co-Authored-By: sketch <hello@sketch.dev>
Change-ID: sec50af415124810bk
diff --git a/git_tools/git_tools.go b/git_tools/git_tools.go
index b206c05..fc59e0d 100644
--- a/git_tools/git_tools.go
+++ b/git_tools/git_tools.go
@@ -3,6 +3,7 @@
 
 import (
 	"bufio"
+	"context"
 	"fmt"
 	"os"
 	"os/exec"
@@ -306,3 +307,41 @@
 
 	return nil
 }
+
+// AutoCommitDiffViewChanges automatically commits changes to the specified file
+// If the last commit message is exactly "User changes from diff view.", it amends the commit
+// Otherwise, it creates a new commit
+func AutoCommitDiffViewChanges(ctx context.Context, repoDir, filePath string) error {
+	// Check if the last commit has the expected message
+	cmd := exec.CommandContext(ctx, "git", "log", "-1", "--pretty=%s")
+	cmd.Dir = repoDir
+	output, err := cmd.Output()
+	commitMsg := strings.TrimSpace(string(output))
+
+	// Check if we should amend or create a new commit
+	const expectedMsg = "User changes from diff view."
+	amend := err == nil && commitMsg == expectedMsg
+
+	// Add the file to git
+	cmd = exec.CommandContext(ctx, "git", "add", filePath)
+	cmd.Dir = repoDir
+	if err := cmd.Run(); err != nil {
+		return fmt.Errorf("error adding file to git: %w", err)
+	}
+
+	// Commit the changes
+	if amend {
+		// Amend the previous commit
+		cmd = exec.CommandContext(ctx, "git", "commit", "--amend", "--no-edit")
+	} else {
+		// Create a new commit
+		cmd = exec.CommandContext(ctx, "git", "commit", "-m", expectedMsg, filePath)
+	}
+	cmd.Dir = repoDir
+
+	if err := cmd.Run(); err != nil {
+		return fmt.Errorf("error committing changes: %w", err)
+	}
+
+	return nil
+}