Skip to content

feat: Add diff mode#112

Open
maratori wants to merge 1 commit intogoogle:mainfrom
maratori:diff
Open

feat: Add diff mode#112
maratori wants to merge 1 commit intogoogle:mainfrom
maratori:diff

Conversation

@maratori
Copy link
Copy Markdown

@maratori maratori commented Jan 9, 2026

This PR adds new --mode diff and closes #111

Example:

--- goldens/separator.in
+++ goldens/separator.in
@@ -1,17 +1,17 @@
 Comma separator:
 static class Foo {
   // keep-sorted-test start
-  "Foo",
+  "Bar",
-  "Bar",
+  "Baz",
-  "Baz"
+  "Foo"
   // keep-sorted-test end
 }
 
 Comma separator with comments:
-// keep-sorted-test start sticky_comments=yes
-"Foo",
+// keep-sorted-test start sticky_comments=yes
 // Sticky comment
 "Bar",
-"Baz"
+"Baz",
+"Foo"
 // Trailing comments
 // keep-sorted-test end

@JeffFaer JeffFaer self-requested a review February 22, 2026 05:51
Comment thread cmd/cmd.go Outdated
Comment thread goldens/golden_test.go Outdated
Comment thread goldens/golden_test.go Outdated
if err != nil {
t.Errorf("Had trouble running keep-sorted --mode diff: %v", err)
}
if diff := cmp.Diff(string(wantDiff), gotDiff); diff != "" {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I see a whole lot of value in writing down both the out and diff files. They should theoretically be derivable from each other

What do you think about instead of doing the comparison like this, we instead apply the output of the diff operation to the input and make sure we get the out file?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, done. Also, I've extracted diff tests into a subtest.

Comment thread cmd/cmd.go
@maratori
Copy link
Copy Markdown
Author

@JeffFaer, thank you for your review and great comments. I've updated the PR.

P.S. Sorry for the delay.

Comment thread goldens/golden_test.go
Comment on lines +143 to +154
files, _, err := gitdiff.Parse(strings.NewReader(gotDiff))
if err != nil {
t.Fatalf("Had trouble parsing diff: %v", err)
}
if len(files) != 1 {
t.Fatalf("Exactly one file is expected in diff, got %d", len(files))
}
var b strings.Builder
err = gitdiff.Apply(&b, bytes.NewReader(in), files[0])
if err != nil {
t.Fatalf("Had trouble applying diff: %v", err)
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this just be ubdiff.ApplyUnified(gotDiff, in)?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are talking about aymanbagabas/go-udiff@v0.4.1, it fails to apply unified with context lines, see https://github.com/aymanbagabas/go-udiff/blob/v0.4.1/unified.go#L301

@maratori maratori requested a review from JeffFaer May 3, 2026 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: output diff

2 participants