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_test.go b/git_tools/git_tools_test.go
index 5be6b0f..e800a0c 100644
--- a/git_tools/git_tools_test.go
+++ b/git_tools/git_tools_test.go
@@ -363,3 +363,79 @@
})
}
}
+
+func TestGitSaveFile(t *testing.T) {
+ // Create a temporary directory for the test repository
+ tmpDir, err := os.MkdirTemp("", "gitsave-test-")
+ if err != nil {
+ t.Fatalf("Failed to create temp dir: %v", err)
+ }
+ defer os.RemoveAll(tmpDir)
+
+ // Initialize a git repository
+ cmd := exec.Command("git", "init")
+ cmd.Dir = tmpDir
+ output, err := cmd.CombinedOutput()
+ if err != nil {
+ t.Fatalf("Failed to initialize git repo: %v, output: %s", err, output)
+ }
+
+ // Create and add a test file to the repo
+ testFilePath := "test-file.txt"
+ testFileContent := "initial content"
+ testFileFull := filepath.Join(tmpDir, testFilePath)
+
+ err = os.WriteFile(testFileFull, []byte(testFileContent), 0644)
+ if err != nil {
+ t.Fatalf("Failed to write test file: %v", err)
+ }
+
+ // Add the file to git
+ cmd = exec.Command("git", "add", testFilePath)
+ cmd.Dir = tmpDir
+ output, err = cmd.CombinedOutput()
+ if err != nil {
+ t.Fatalf("Failed to add test file to git: %v, output: %s", err, output)
+ }
+
+ // Commit the file
+ cmd = exec.Command("git", "commit", "-m", "Initial commit")
+ cmd.Dir = tmpDir
+ cmd.Env = append(os.Environ(),
+ "GIT_AUTHOR_NAME=Test",
+ "GIT_AUTHOR_EMAIL=test@example.com",
+ "GIT_COMMITTER_NAME=Test",
+ "GIT_COMMITTER_EMAIL=test@example.com")
+ output, err = cmd.CombinedOutput()
+ if err != nil {
+ t.Fatalf("Failed to commit test file: %v, output: %s", err, output)
+ }
+
+ // Test successful save
+ newContent := "updated content"
+ err = GitSaveFile(tmpDir, testFilePath, newContent)
+ if err != nil {
+ t.Errorf("GitSaveFile failed: %v", err)
+ }
+
+ // Verify the file was updated
+ content, err := os.ReadFile(testFileFull)
+ if err != nil {
+ t.Fatalf("Failed to read updated file: %v", err)
+ }
+ if string(content) != newContent {
+ t.Errorf("File content not updated correctly; got %q, want %q", string(content), newContent)
+ }
+
+ // Test saving a file outside the repo
+ err = GitSaveFile(tmpDir, "../outside.txt", "malicious content")
+ if err == nil {
+ t.Error("GitSaveFile should have rejected a path outside the repository")
+ }
+
+ // Test saving a file not tracked by git
+ err = GitSaveFile(tmpDir, "untracked.txt", "untracked content")
+ if err == nil {
+ t.Error("GitSaveFile should have rejected an untracked file")
+ }
+}