Skip to content

Add remove_duplicates ignore_comments and merge_comments logic - #132

Open
jakos-sec wants to merge 2 commits into
google:mainfrom
jakos-sec:remove-duplicates-comments
Open

Add remove_duplicates ignore_comments and merge_comments logic#132
jakos-sec wants to merge 2 commits into
google:mainfrom
jakos-sec:remove-duplicates-comments

Conversation

@jakos-sec

Copy link
Copy Markdown

Added DuplicateResolution enum to handle advanced deduplication modes. The new mores are ignore_comments to keep only the first comment for deduplicated items and merge_comments to merge comments from all identical items when deduplicated (identical comment lines are also deduplicated when merging).

Added `DuplicateResolution` enum to handle advanced deduplication modes.
The new mores are `ignore_comments` to keep only the first comment for deduplicated items and `merge_comments` to merge comments from all identical items when deduplicated (identical comment lines are also deduplicated when merging).
Comment thread keepsorted/block.go Outdated
Comment thread keepsorted/options.go
Comment thread keepsorted/options_parser.go Outdated

@wachsler-google wachsler-google left a comment

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.

Sorry I don't have much experience with github reviews, so I posted a couple comments separate from this review before I figured out how this works. Anyway, this all looks pretty good, aside from the few things I've commented on.

- Use slices.Clone before deduplication loop when merging comments
- Rename remove_duplicates=ignore_comments to keep_first_comment everywhere
- Remove duplicate new line
- Add tests for remove_duplicates option parsing and resolution
@jakos-sec

Copy link
Copy Markdown
Author

Sorry I don't have much experience with github reviews, so I posted a couple comments separate from this review before I figured out how this works. Anyway, this all looks pretty good, aside from the few things I've commented on.

Feedback should be all addressed now. Thank you for the review!

Comment thread keepsorted/block.go

if b.metadata.opts.RemoveDuplicates == DuplicateResolutionMergeComments {
firstLg.comment = slices.Clone(firstLg.comment)
for _, newComment := range lg.comment {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you should not be looping over lines and doing a contains check. Doesn't that backfire if there are common lines? Particularly, I think this would strip all subsequent paragraph breaks.

I think this deduping should only ignore comments where all the lines exist in that order (i.e., join the comment strings first).

Can we also add a test case for more complex, multi-line comments, to ensure that removing duplicates correctly handles paragraph breaks and doesn't get confused by similar lines? e.g., like:

// Foo is a solution to all problems.
//
// - It notably does both everything and nothing at the same time.
// - For details on what it does, see
// http://example.com/some-long-url-that-takes-up-the-whole-line
foo

// Foo is the most important value in this list.
// 
// A record of past conflicts stemming from this value is available at
// http://example.com/some-long-url-that-takes-up-the-whole-line
foo

Which I think should just result in the following naive appending, since I see those comments to not be duplicates even though they contain individual lines that technically are duplicates.

// Foo is a solution to all problems.
//
// - It notably does both everything and nothing at the same time.
// - For details on what it does, see
// http://example.com/some-long-url-that-takes-up-the-whole-line
// Foo is the most important value in this list.
// 
// A record of past conflicts stemming from this value is available at
// http://example.com/some-long-url-that-takes-up-the-whole-line
foo

I didn't run it to check, but I think your code would result in:

// Foo is a solution to all problems.
//
// - It notably does both everything and nothing at the same time.
// - For details on what it does, see
// http://example.com/some-long-url-that-takes-up-the-whole-line
// Foo is the most important value in this list.
// A record of past conflicts stemming from this value is available at
foo

Which IMO is a more confusing result. There's no achieving perfection, but I think merging at a line level rather than the entire comment is just going to confuse.

Remove duplicates ignoring comments, when the first item has no comment:
// keep-sorted-test start remove_duplicates=keep_first_comment sticky_comments=yes
bar
foo

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this behavior definitely what you want? I'm just not clear on the use case for it. Keeping the first actual comment seems like it would make more sense than letting the no-comment overwrite the duplicate that does have a comment.

WDYT if we change it so that keep_first_comment mode will check if the comment is empty for the first line group and if so, loop over all the line groups until it finds one with a non-empty comment (if any) and copies that comment over to the first line group?

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.

3 participants