Skip to content
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

x fmt should only format modified files by default #105688

Closed
jyn514 opened this issue Dec 14, 2022 · 9 comments · Fixed by #105702
Closed

x fmt should only format modified files by default #105688

jyn514 opened this issue Dec 14, 2022 · 9 comments · Fixed by #105702
Assignees

Comments

@jyn514
Copy link
Member

jyn514 commented Dec 14, 2022

Since rustfmt is deterministic and idempotent, running it twice on the same file should always have the same output (if it's not both of those things, it's a bug). So we can avoid running it unless the file has changed, which should make x fmt without arguments much faster.

Originally posted by @chenyukang in #105648 (comment)

@jyn514
Copy link
Member Author

jyn514 commented Dec 14, 2022

This will need to be careful to still rerun rustfmt when we bump its version (which happens about every 6 weeks). Maybe we can generate a .rustfmt-all-files.stamp file and format everything if it doesn't yet exist or has the wrong version.

@albertlarsan68
Copy link
Member

Hello,
I want to work on that.
How would the detection of modified files work ?

@jyn514
Copy link
Member Author

jyn514 commented Dec 14, 2022

@albertlarsan68 you can use git diff-index --name-only HEAD to see modified files. Make sure to fall back to formatting all files if that command fails for some reason, we support building from source tarballs without git installed.

@albertlarsan68
Copy link
Member

This means that already-commited files that were not formatted when committed would not be formatted, right ?

@jyn514
Copy link
Member Author

jyn514 commented Dec 14, 2022

Ah, good point. You can use git merge-base HEAD origin/master to find where the two diverged and compare all files since then. And fmt --check should still look at all files unconditionally.

@albertlarsan68
Copy link
Member

Thanks, I will start working on this !

@rustbot claim

@the8472
Copy link
Member

the8472 commented Dec 14, 2022

You can use git merge-base HEAD origin/master

That assumes that the rust-lang remote is labeled as origin. I call that upstream and my fork origin. Using the local master may also not work if one does git fetch upstream && git rebase upstream/master on branch.

@jyn514
Copy link
Member Author

jyn514 commented Dec 14, 2022

Yes, #105058 has a more general solution.

@albertlarsan68
Copy link
Member

PR is up at #105702

albertlarsan68 added a commit to albertlarsan68/rust that referenced this issue Dec 27, 2022
As discussed on rust-lang#105688, this makes x fmt only format modified files
@bors bors closed this as completed in 0630677 Dec 29, 2022
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 a pull request may close this issue.

3 participants