-
Notifications
You must be signed in to change notification settings - Fork 31
feat: Add diff mode #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maratori
wants to merge
1
commit into
google:main
Choose a base branch
from
maratori:diff
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: Add diff mode #112
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ import ( | |
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/aymanbagabas/go-udiff" | ||
| "github.com/google/keep-sorted/keepsorted" | ||
| "github.com/rs/zerolog/log" | ||
| flag "github.com/spf13/pflag" | ||
|
|
@@ -84,6 +85,7 @@ var ( | |
| operations = map[string]operation{ | ||
| "lint": lint, | ||
| "fix": fix, | ||
| "diff": diff, | ||
| } | ||
| ) | ||
|
|
||
|
|
@@ -269,6 +271,46 @@ func lint(fixer *keepsorted.Fixer, filenames []string, modifiedLines []keepsorte | |
| return false, nil | ||
| } | ||
|
|
||
| func diff(fixer *keepsorted.Fixer, filenames []string, modifiedLines []keepsorted.LineRange) (ok bool, err error) { | ||
| ok = true | ||
|
|
||
| for _, fn := range filenames { | ||
| contents, err := read(fn) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| want, alreadyFixed, warnings := fixer.Fix(fn, contents, modifiedLines) | ||
| if !alreadyFixed { | ||
| ok = false | ||
| inName := fn | ||
| outName := fn | ||
| if fn == stdin { | ||
| inName = "stdin" | ||
| outName = "stdout" | ||
| } | ||
| if _, err := os.Stdout.WriteString(udiff.Unified(inName, outName, contents, want)); err != nil { | ||
| return false, err | ||
| } | ||
| } | ||
|
|
||
| for _, warn := range warnings { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extract a |
||
| ok = false | ||
| log := log.Warn() | ||
| if warn.Path != stdin { | ||
| log = log.Str("file", warn.Path) | ||
| } | ||
| if warn.Lines.Start == warn.Lines.End { | ||
| log = log.Int("line", warn.Lines.Start) | ||
| } else { | ||
| log = log.Int("start", warn.Lines.Start).Int("end", warn.Lines.End) | ||
| } | ||
| log.Msg(warn.Message) | ||
| } | ||
| } | ||
|
|
||
| return ok, nil | ||
| } | ||
|
|
||
| func read(fn string) (string, error) { | ||
| if fn == stdin { | ||
| b, err := io.ReadAll(os.Stdin) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,295 @@ | ||
| --- stdin | ||
| +++ stdout | ||
| @@ -1,157 +1,159 @@ | ||
| widgets := []widget{ | ||
| // keep-sorted-test start block=yes | ||
| { | ||
| - Name: "foo", | ||
| + Name: "bar", | ||
| }, | ||
| { | ||
| - Name: "bar", | ||
| + Name: "foo", | ||
| }, | ||
| // keep-sorted-test end | ||
| } | ||
|
|
||
| withStickComments := []widget{ | ||
| // keep-sorted-test start block=yes sticky_comments=yes | ||
| - { | ||
| + // Comment about bar. | ||
| + { | ||
| - Name: "foo", | ||
| + Name: "bar", | ||
| - SomeOtherField: 123, | ||
| + SomeOtherField: 456, | ||
| }, | ||
| - // Comment about bar. | ||
| - { | ||
| - Name: "bar", | ||
| - SomeOtherField: 456, | ||
| - }, | ||
| - // keep-sorted-test end | ||
| -} | ||
| - | ||
| -strings := []string{ | ||
| - // Note: since we're sorting based on the entire line, a new line comes before the | ||
| - // closing quote and thus a-b-c is sorted before a-b. | ||
| - // keep-sorted-test start block=yes | ||
| - `a | ||
| - c`, | ||
| - `a | ||
| - b`, | ||
| - `a | ||
| - b | ||
| - c`, | ||
| - // keep-sorted-test end | ||
| -} | ||
| - | ||
| -noIncreasedIndentation := []widget{ | ||
| - // keep-sorted-test start block=yes | ||
| - { | ||
| - Name: "foo", | ||
| - }, | ||
| - { | ||
| - Name: "bar", | ||
| - }, | ||
| - { | ||
| - Name: "baz", | ||
| - }, | ||
| - // keep-sorted-test end | ||
| -} | ||
| - | ||
| -unbalancedCommentsDoNotMatter := []widget{ | ||
| - // keep-sorted-test start block=yes sticky_comments=yes | ||
| - // Unbalanced comment: { | ||
| - { | ||
| - Name: "bar", | ||
| - }, | ||
| - { | ||
| - Name: "foo", | ||
| - }, | ||
| - // Also unbalanced: ] | ||
| - { | ||
| - Name: "baz", | ||
| - }, | ||
| - // keep-sorted-test end | ||
| -} | ||
| - | ||
| -withOneLiners := []widget{ | ||
| - // keep-sorted-test start block=yes | ||
| - { | ||
| - Name: "foo", | ||
| - }, | ||
| - { | ||
| - Name: "baz", | ||
| - }, | ||
| - {Name: "bar"}, | ||
| - // keep-sorted-test end | ||
| -} | ||
| - | ||
| -// gcl style code: | ||
| -// keep-sorted-test start block=yes | ||
| -Group xyz = external_groups.acl_group { | ||
| - group_expansion = 'self' | ||
| -} | ||
| -Group ijk = external_groups.acl_group { | ||
| - group_expansion = """''' | ||
| - Nested triple quotes aren\'t uncommon | ||
| - '''""" | ||
| -} | ||
| -Group lmn = external_groups.acl_group { | ||
| - group_expansion = """ | ||
| - Including another " shouldn't break groups | ||
| - """ | ||
| -} | ||
| -Group abc = external_groups.acl_group { | ||
| - group_expansion = "It's the members" | ||
| -} | ||
| -// keep-sorted-test end | ||
| - | ||
| -// gcl style code with trailing comments: | ||
| -// keep-sorted-test start block=yes | ||
| -Experiment xyz = templates.ExperimentBase { | ||
| - name = "XYZ" // Should be the last one :( | ||
| - description = "A bunch of opening brackets [{[ here" | ||
| - percent = 10 | ||
| -} | ||
| -Experiment ikj = | ||
| - templates.ExperimentBaseWithTestAccounts { | ||
| - name = "IJK" | ||
| - description = "A single quote in a trailing comment" | ||
| - test_accounts = [ | ||
| - 123, // Bobby's test account | ||
| - ] | ||
| -} | ||
| -Experiment abc = templates.LAUNCHED { | ||
| - name = "ABC" | ||
| - description = "Simple block" | ||
| -} | ||
| -// keep-sorted-test end | ||
| - | ||
| -// BUILD rule style | ||
| -// keep-sorted-test start block=yes newline_separated=yes | ||
| -some_build_rule( | ||
| - name = "def", | ||
| - src = "some-source", | ||
| -) | ||
| - | ||
| -some_build_rule( | ||
| - name = "xyz", | ||
| - src = "another-source", | ||
| -) | ||
| - | ||
| -some_build_rule( | ||
| - name = "abc", | ||
| - src = "one-more-source", | ||
| + { | ||
| + Name: "foo", | ||
| + SomeOtherField: 123, | ||
| + }, | ||
| + // keep-sorted-test end | ||
| +} | ||
| + | ||
| +strings := []string{ | ||
| + // Note: since we're sorting based on the entire line, a new line comes before the | ||
| + // closing quote and thus a-b-c is sorted before a-b. | ||
| + // keep-sorted-test start block=yes | ||
| + `a | ||
| + b | ||
| + c`, | ||
| + `a | ||
| + b`, | ||
| + `a | ||
| + c`, | ||
| + // keep-sorted-test end | ||
| +} | ||
| + | ||
| +noIncreasedIndentation := []widget{ | ||
| + // keep-sorted-test start block=yes | ||
| + { | ||
| + Name: "bar", | ||
| + }, | ||
| + { | ||
| + Name: "baz", | ||
| + }, | ||
| + { | ||
| + Name: "foo", | ||
| + }, | ||
| + // keep-sorted-test end | ||
| +} | ||
| + | ||
| +unbalancedCommentsDoNotMatter := []widget{ | ||
| + // keep-sorted-test start block=yes sticky_comments=yes | ||
| + // Unbalanced comment: { | ||
| + { | ||
| + Name: "bar", | ||
| + }, | ||
| + // Also unbalanced: ] | ||
| + { | ||
| + Name: "baz", | ||
| + }, | ||
| + { | ||
| + Name: "foo", | ||
| + }, | ||
| + // keep-sorted-test end | ||
| +} | ||
| + | ||
| +withOneLiners := []widget{ | ||
| + // keep-sorted-test start block=yes | ||
| + {Name: "bar"}, | ||
| + { | ||
| + Name: "baz", | ||
| + }, | ||
| + { | ||
| + Name: "foo", | ||
| + }, | ||
| + // keep-sorted-test end | ||
| +} | ||
| + | ||
| +// gcl style code: | ||
| +// keep-sorted-test start block=yes | ||
| +Group abc = external_groups.acl_group { | ||
| + group_expansion = "It's the members" | ||
| +} | ||
| +Group ijk = external_groups.acl_group { | ||
| + group_expansion = """''' | ||
| + Nested triple quotes aren\'t uncommon | ||
| + '''""" | ||
| +} | ||
| +Group lmn = external_groups.acl_group { | ||
| + group_expansion = """ | ||
| + Including another " shouldn't break groups | ||
| + """ | ||
| +} | ||
| +Group xyz = external_groups.acl_group { | ||
| + group_expansion = 'self' | ||
| +} | ||
| +// keep-sorted-test end | ||
| + | ||
| +// gcl style code with trailing comments: | ||
| +// keep-sorted-test start block=yes | ||
| +Experiment abc = templates.LAUNCHED { | ||
| + name = "ABC" | ||
| + description = "Simple block" | ||
| +} | ||
| +Experiment ikj = | ||
| + templates.ExperimentBaseWithTestAccounts { | ||
| + name = "IJK" | ||
| + description = "A single quote in a trailing comment" | ||
| + test_accounts = [ | ||
| + 123, // Bobby's test account | ||
| + ] | ||
| +} | ||
| +Experiment xyz = templates.ExperimentBase { | ||
| + name = "XYZ" // Should be the last one :( | ||
| + description = "A bunch of opening brackets [{[ here" | ||
| + percent = 10 | ||
| +} | ||
| +// keep-sorted-test end | ||
| + | ||
| +// BUILD rule style | ||
| +// keep-sorted-test start block=yes newline_separated=yes | ||
| +some_build_rule( | ||
| + name = "abc", | ||
| + src = "one-more-source", | ||
| +) | ||
| + | ||
| +some_build_rule( | ||
| + name = "def", | ||
| + src = "some-source", | ||
| +) | ||
| + | ||
| +some_build_rule( | ||
| + name = "xyz", | ||
| + src = "another-source", | ||
| ) | ||
| // keep-sorted-test end | ||
|
|
||
| // BUILD rule style with newlines=2 | ||
| // keep-sorted-test start block=yes newline_separated=2 | ||
| some_build_rule( | ||
| - name = "def", | ||
| + name = "abc", | ||
| - src = "some-source", | ||
| + src = "one-more-source", | ||
| -) | ||
| +) | ||
| + | ||
|
|
||
| some_build_rule( | ||
| - name = "xyz", | ||
| + name = "def", | ||
| - src = "another-source", | ||
| + src = "some-source", | ||
| -) | ||
| +) | ||
| + | ||
|
|
||
| some_build_rule( | ||
| - name = "abc", | ||
| + name = "xyz", | ||
| - src = "one-more-source", | ||
| + src = "another-source", | ||
| ) | ||
| // keep-sorted-test end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: I need to make sure this package is available internally. It probably is, but just leaving a note for myself next week