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

The status of git-rustfmt #4103

Open
elichai opened this issue Mar 30, 2020 · 9 comments
Open

The status of git-rustfmt #4103

elichai opened this issue Mar 30, 2020 · 9 comments

Comments

@elichai
Copy link

elichai commented Mar 30, 2020

Hi,
I started looking into writing my own tool that wraps rustfmt and git together and then I stumbled into https://github.com/rust-lang/rustfmt/blob/master/Cargo.toml#L30

What's the status of this tool? I see it's not in rustup components. if it doesn't quite work yet I'd like to help working on it.

Thank you :)

@calebcartwright
Copy link
Member

What's the status of this tool?

To be honest, I'm not sure. I believe it's complete in terms of the original scope when it was added but it's not a core part of rustfmt like cargo fmt and rustfmt are. There were some semi-recent conversations about incorporating both format-diff and git-rustfmt (or at least their respective capabilities) as subcommands on rustfmt.

It's probably worth revisiting to codify the desired capabilities (format only modified files, format only modified lines, etc.)and determining what the delta is between the current and desired states, if any.

if it doesn't quite work yet I'd like to help working on it

👍

@elichai
Copy link
Author

elichai commented Apr 20, 2020

I'd like to start working on it, at least get it to a minimal working binary (right now it doesn't really work)
anyone wants to talk about behavior? or should I just go for trying to match git-clang-format?

CC @topecongiro @nrc

@topecongiro
Copy link
Contributor

Thank you for your interest in contributing to rustfmt!

TBH, I've never used git-clang-format myself, so I cannot say about it, but for git-rustfmt, I am thinking about making a tool with the following behavior:

  • It accepts similar arguments as git diff
    • git-rustfmt formats unstaged diffs
    • git-rustfmt --cached formats unstaged and staged diffs
    • git-rustfmt HEAD^ formats diffs contained in the last commit
  • It accepts similar command-line options as rustfmt
    • --check, --verbose, --edition, --config-path and --config

Thoughts?

@elichai
Copy link
Author

elichai commented Apr 21, 2020

Sound good, I think git-clang-format doesn't let you format unstaged changes, and if you go git clang-format COMMITT it will actually format the diff between HEAD(or current staged) and that commit.

I like the idea of trying to stay close to git diff, although what behavior should git rustfmt COMMIT do? if it formats only the diff in the commit maybe we should allow amending it, the problem with formatting the diff in the commit, is that it might conflict with HEAD/cached.

I think I'll start playing with it and adding features, mostly passing them through to git diff except the flags that rustfmt wants (I'm curious if clap will let me easily pass also the inputs after -- )

I'm also trying to decide if I want to use the current approach and execute git as a command, which let's me easily pass in the flags/inputs or should I use https://crates.io/crates/git2 which makes everything nicer but requires me to parse all the options/flags, What do you think?

@calebcartwright
Copy link
Member

The conversation on #1324 may be of interest for this thread

@calebcartwright
Copy link
Member

There's also cargo scout that may be worth checking out as well

@tv42
Copy link

tv42 commented May 4, 2024

For projects where you can rely on everything being formatted, instead of this seemingly-abandoned diff formatter, you can simply make git store only properly formatted code:

$ grep rust ~/.config/git/attributes
*.rs filter=rustfmt
$ grep rustfmt ~/.config/git/config
[filter "rustfmt"]
	clean = "rustfmt --edition=2021"

At the cost of force-reformatting all legacy code you interact with (which probably shouldn't exist in 2024 anymore), all diffs etc are always well behaved from here on.

@Lucretiel
Copy link

At the cost of force-reformatting all legacy code you interact with (which probably shouldn't exist in 2024 anymore), all diffs etc are always well behaved from here on.

As someone who's editor is already set to fmt-on-save, I can tell you that there are many rust projects out there that simply refuse to use rustfmt, often deliberately. Unconditionally forcing your git to refuse to commit unformatted rust code is a non-starter if you have designs on contributing to arbitrary third-party projects.

@tv42
Copy link

tv42 commented May 6, 2024

You can disable the thing per git repository. I've had to do that too, but I mostly welcome knowledge that the project I'm looking at is being weird.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants