Skip to content

diff_util: Move diff renderer format length check to constructor #6253

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

wrzian
Copy link

@wrzian wrzian commented Apr 5, 2025

This is a nice simplification that I found in another commit that was fully unrelated to my pull request, so I'm breaking it out into a new one.

In cli_util, I chose to put the maybe_diff_renderer function in the command environment instead of the helper because in log.rs there is no command helper already existing, and it would be non-trivial to build one first.

Checklist

(I don't think any of these are applicable)

If applicable:

  • I have updated CHANGELOG.md
  • I have updated the documentation (README.md, docs/, demos/)
  • I have updated the config schema (cli/src/config-schema.json)
  • I have added tests to cover my changes

In cli_util, I chose to put the `maybe_diff_renderer` function in the command
environment instead of the helper because in `log.rs` there is no command
helper already existing, and it would be non-trivial to build one first.
@wrzian wrzian requested a review from a team as a code owner April 5, 2025 01:32
Copy link
Contributor

@yuja yuja left a comment

Choose a reason for hiding this comment

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

Thanks.

Comment on lines +303 to +305
if formats.is_empty() {
None
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would move this to WorkspaceCommandEnvironment::maybe_diff_renderer() or its callers.

Copy link
Author

Choose a reason for hiding this comment

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

I like that it's part of the type's constructor. I think this should stay with the type, because it really is nonsensical to have a diff renderer that outputs nothing.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's not wrong to construct a diff function that does nothing. I think this could be a contract at cli_util layer, but as I said, I assumed it would be easier to test the condition by caller.

FWIW, we'll need to reorganize the current diff format configuration as "short" and "long" formats pair . (see DiffFormatArgs and #3327) DiffRenderer wouldn't have to know this distinction, but that might change how we construct the renderer object.

.then(|| DiffRenderer::new(merged_repo, path_converter, conflict_marker_style, formats))
};
let formats = diff_formats_for_log(settings, &args.diff_format, args.patch)?;
let diff_renderer = workspace_env.maybe_diff_renderer(merged_repo, formats);
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps, it's simpler to handle .is_empty() here instead of adding maybe_*() and wrapping the result?

(!formats.is_empty()).then(|| workspace_env.diff_renderer(merged_repo, formats));

Copy link
Author

Choose a reason for hiding this comment

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

diff_renderer only takes a single format, not a vector of formats.
You should try rewriting this if you want to see the pitfalls. I don't think a simpler solution really exists.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean you can revert the change to accept multiple formats.

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.

2 participants