Add Monaco diff-view, the saga ...

I set out to use Monaco to support the diff view. diff2html is lovely,
but there were a ton of usability improvements I wanted to make (line
numbers not making things double spaced, choosing which diff, editing
the right-hand side), and it seemed a dead end. Furthermore, Phabricator
and Gerrit's experience is that diffs should be shown file by file,
because you'll inevitably see a diff with a file that's too large, and
the GitHub PR view often breaks on big changes... so I wanted to show
files diff-by-diff, with "infinite" context when unchanged sections are
expanded. So...

Ultimately, all of this was sketch-coded over maybe 30 Sketch sessions.
I threw away a lot of branches. My git reflog is a superfund site.

Prompting whole-hog didn't work. Or, rather, it made significant
progress, but something very serious wouldn't work, and I couldn't
figure out what, and nor could Sketch.

Instead, I started by adding a new webcomponent that was just a
placeholder. Then, using https://rodydavis.com/posts/lit-monaco-editor,
I nudged Sketch into adding Monaco to it. Sketch pulled out:

   You're right, I should properly read the blog post before implementing the
   solution. Let me check the referenced blog post.

I worked heavily in the demo environment at first, but here I ran into
the issue that we have two different esbuild systems: one is vite and
one is esbuild.go, and they're configured differently enough.

Monaco is unusable and confusingly so when its CSS isn't loaded. The right
way to load it, I've found, is via

  @import url('./static/monaco/min/vs/editor/editor.main.css');

I spent more time than I care to admit noticing that originally
this wasn't relative, and when we use a skaband setting, the
paths need to be relative-aware.

The paths to the various workers need to be similarly correctly placed.

Getting Sketch to build demo data but not put testing code into production
code was tricky. (I threw away a lot of efforts and factories and singletons...)

When I set out to do the git commit selection, I wanted to do a bunch of
backend /git/* handlers. These were easy enough to code in sketch. I had
to convince Sketch to put them in git_tools.go and not in the agent.
It doesn't really matter: these functions to parse git are pretty stateless,
but it's less work to have them separate. Sketch was mediocre at writing
tests for them. Did you know that our container has an older version
of git that doesn't have the same options to decorate ref names? Yeah, nor did
I.

Handling unstaged changes was fun. git diff --raw shows unstaged files
as having identity 0000. Ideally we'd be using jj and there'd be
a synthetic commit, but instead uncommitted-possible files are read
by content.

A real big challenge was getting the Monaco view to use the right vertical and
horizontal space. I did this many, many times. I don't claim to understand flex
and the virtual dom, and :host, and all the interactions. It would fix one
thing and break another. The chat window would shrink. The terminal would
shrink.

Screenshot support was excellent. I eventually added paste support just so
that I could expedite my workflow, and Sketch coded that easily on the first
pass with minor feedback.

I learned the hard way that Safari's support for WebComponents/shadow
dom in its web inspector is rough. See https://fediverse.zachleat.com/@zachleat/114518629612122858

I also learned the hard way that Chrome doesn't use fonts loaded in CSS
in a shadow dom. That's why the codicon font had to be in the global
style sheet.

Kudos to John Reese who kindly allowed me, a long time ago, to adapt a
shell script he had at work to look over diffs into https://github.com/philz/git-vimdiff.
That's the inspiration for having the "new code" be editable when you're
reviewing it; why shouldn't it be!?!

There are a handful of follow up tasks:

* We lose state when we switch to the Chat view and back.
* Need URL-based support for where we are.
* Maybe need shortcut keys to move between diffs and changes.
* Maybe need caching or look-ahead for downloading the next or previous
  file.
* We spend too much vertical real estate on all the diff selections;
  could we scroll it out of the way, collapse it, tighten it, etc.
* The workers sometimes throw errors into the console. I think they're
  harmless and merely need to be caught and suppressed.
* Needing to commit changes when things are saved is weird. Should we
  commit automatically? Amend the previous commit? Have a button for
  that? Show the git dirty state?
* Our JS bundle is big. We could maybe delay loading the monaco bundle
  to help.

Thanks for coming to my TED talk.
diff --git a/git_tools/git_tools.go b/git_tools/git_tools.go
index f8a1807..9ff8a71 100644
--- a/git_tools/git_tools.go
+++ b/git_tools/git_tools.go
@@ -4,7 +4,9 @@
 import (
 	"bufio"
 	"fmt"
+	"os"
 	"os/exec"
+	"path/filepath"
 	"strings"
 )
 
@@ -17,9 +19,18 @@
 	NewHash string `json:"new_hash"`
 	Status  string `json:"status"` // A=added, M=modified, D=deleted, etc.
 } // GitRawDiff returns a structured representation of the Git diff between two commits or references
+// If 'to' is empty, it will show unstaged changes (diff with working directory)
 func GitRawDiff(repoDir, from, to string) ([]DiffFile, error) {
 	// Git command to generate the diff in raw format with full hashes
-	cmd := exec.Command("git", "-C", repoDir, "diff", "--raw", "--abbrev=40", from, to)
+	var cmd *exec.Cmd
+	if to == "" {
+		// If 'to' is empty, show unstaged changes
+		cmd = exec.Command("git", "-C", repoDir, "diff", "--raw", "--abbrev=40", from)
+	} else {
+		// Normal diff between two refs
+		cmd = exec.Command("git", "-C", repoDir, "diff", "--raw", "--abbrev=40", from, to)
+	}
+
 	out, err := cmd.CombinedOutput()
 	if err != nil {
 		return nil, fmt.Errorf("error executing git diff: %w - %s", err, string(out))
@@ -87,15 +98,15 @@
 	return files, nil
 }
 
-// LogEntry represents a single entry in the git log
-type LogEntry struct {
+// GitLogEntry represents a single entry in the git log
+type GitLogEntry struct {
 	Hash    string   `json:"hash"`    // The full commit hash
 	Refs    []string `json:"refs"`    // References (branches, tags) pointing to this commit
 	Subject string   `json:"subject"` // The commit subject/message
 }
 
 // GitRecentLog returns the recent commit log between the initial commit and HEAD
-func GitRecentLog(repoDir string, initialCommitHash string) ([]LogEntry, error) {
+func GitRecentLog(repoDir string, initialCommitHash string) ([]GitLogEntry, error) {
 	// Validate input
 	if initialCommitHash == "" {
 		return nil, fmt.Errorf("initial commit hash must be provided")
@@ -120,7 +131,7 @@
 }
 
 // getGitLog gets the git log with the specified format using the provided fromCommit
-func getGitLog(repoDir string, fromCommit string) ([]LogEntry, error) {
+func getGitLog(repoDir string, fromCommit string) ([]GitLogEntry, error) {
 	// Check if fromCommit~10 exists (10 commits before fromCommit)
 	checkCmd := exec.Command("git", "-C", repoDir, "rev-parse", "--verify", fromCommit+"~10")
 	if err := checkCmd.Run(); err != nil {
@@ -144,8 +155,8 @@
 }
 
 // parseGitLog parses the output of git log with null-separated fields
-func parseGitLog(logOutput string) ([]LogEntry, error) {
-	var entries []LogEntry
+func parseGitLog(logOutput string) ([]GitLogEntry, error) {
+	var entries []GitLogEntry
 	if logOutput == "" {
 		return entries, nil
 	}
@@ -165,7 +176,7 @@
 		// Parse the refs from the decoration
 		refs := parseRefs(decoration)
 
-		entries = append(entries, LogEntry{
+		entries = append(entries, GitLogEntry{
 			Hash:    hash,
 			Refs:    refs,
 			Subject: subject,
@@ -228,3 +239,70 @@
 
 	return refs
 }
+
+// validateRepoPath verifies that a file is tracked by git and within the repository boundaries
+// Returns the full path to the file if valid
+func validateRepoPath(repoDir, filePath string) (string, error) {
+	// First verify that the requested file is tracked by git to prevent
+	// access to files outside the repository
+	cmd := exec.Command("git", "-C", repoDir, "ls-files", "--error-unmatch", filePath)
+	if err := cmd.Run(); err != nil {
+		return "", fmt.Errorf("file not tracked by git or outside repository: %s", filePath)
+	}
+
+	// Construct the full file path
+	fullPath := filepath.Join(repoDir, filePath)
+
+	// Validate that the resolved path is still within the repository directory
+	// to prevent directory traversal attacks (e.g., ../../../etc/passwd)
+	absRepoDir, err := filepath.Abs(repoDir)
+	if err != nil {
+		return "", fmt.Errorf("unable to resolve absolute repository path: %w", err)
+	}
+
+	absFilePath, err := filepath.Abs(fullPath)
+	if err != nil {
+		return "", fmt.Errorf("unable to resolve absolute file path: %w", err)
+	}
+
+	// Check that the absolute file path starts with the absolute repository path
+	if !strings.HasPrefix(absFilePath, absRepoDir+string(filepath.Separator)) {
+		return "", fmt.Errorf("file path outside repository: %s", filePath)
+	}
+
+	return fullPath, nil
+}
+
+// GitCat returns the contents of a file in the repository at the given path
+// This is used to get the current working copy of a file (not using git show)
+func GitCat(repoDir, filePath string) (string, error) {
+	fullPath, err := validateRepoPath(repoDir, filePath)
+	if err != nil {
+		return "", err
+	}
+
+	// Read the file
+	content, err := os.ReadFile(fullPath)
+	if err != nil {
+		return "", fmt.Errorf("error reading file %s: %w", filePath, err)
+	}
+
+	return string(content), nil
+}
+
+// GitSaveFile saves content to a file in the repository, checking first that it's tracked by git
+// This prevents writing to files outside the repository
+func GitSaveFile(repoDir, filePath, content string) error {
+	fullPath, err := validateRepoPath(repoDir, filePath)
+	if err != nil {
+		return err
+	}
+
+	// Write the content to the file
+	err = os.WriteFile(fullPath, []byte(content), 0644)
+	if err != nil {
+		return fmt.Errorf("error writing to file %s: %w", filePath, err)
+	}
+
+	return nil
+}