Is there a way to detect if `./pants fmt` has made...
# general
r
Is there a way to detect if
./pants fmt
has made actual changes or not? I'd like to use it in the pre-commit hook but I don't want to blindly run
git add -u
but just abort the commit when there are changed files.
1
c
if you run
./pants lint
it will fail if there would be changes made by `fmt`… does that help?
r
ah… then i could just run them together!
hmmmm
c
ooh… if you run
./pants lint fmt
you will not get to fmt if there are changes to be made… and if your run
./pants fmt lint
you won’t catch changes made by fmt in lint… so I’m not sure what you meant there… 🙂
1
r
yeah i just found that..
👍 1
i want to do in the git pre-commit hook: • make changes by
fmt
if required • abort the commit by returning exit code 1 if we had to make changes
i often partially add changes for a commit
so i don't want to run
git add -u
always after
./pants fmt
in the git pre-commit hook
c
I agree that the pre-commit hook should not add anything. I’d venture to say that it should change anything at all (i.e. just run
lint
)
r
what i want is just printing a warning and "inspect by yourself" the auto-format results with unstaged results
c
if lint fails, instruct to run `fmt`…
r
hmmm
yes i think
./pants fmt
should be run by human...
💯 1
it's hard to properly automate with hooks
c
to me it would be surprising if a pre-commit hook had changed files for me…
r
haha
c
+1 - i have
lint
on a pre-commit hook. I think if you want to automated
fmt
you should do it in your editor. I wired up vim to run
black
and
isort
on save.
👍 2
w
@rhythmic-morning-87313: to be clear: everything that runs in
fmt
is also checked in `lint`: so
lint
will definitely fail if
fmt
needs to run (it might also fail for non-autoformatters)
w
@rhythmic-morning-87313 If you're interested, I extracted the a stripped down Pants mainline pre-commit hook and talked about it here, I think this is a reasonable compromise and
fmt
isn't run automatically, but also gives hints to the user: https://sureshjoshi.com/development/pants-plugin-code-quality#pre-commit-hooks