https://pantsbuild.org/ logo
#general
Title
# general
r

rhythmic-morning-87313

07/06/2022, 3:16 PM
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

curved-television-6568

07/06/2022, 3:24 PM
if you run
./pants lint
it will fail if there would be changes made by `fmt`… does that help?
r

rhythmic-morning-87313

07/06/2022, 3:24 PM
ah… then i could just run them together!
hmmmm
c

curved-television-6568

07/06/2022, 3:25 PM
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

rhythmic-morning-87313

07/06/2022, 3:26 PM
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

curved-television-6568

07/06/2022, 3:27 PM
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

rhythmic-morning-87313

07/06/2022, 3:27 PM
what i want is just printing a warning and "inspect by yourself" the auto-format results with unstaged results
c

curved-television-6568

07/06/2022, 3:28 PM
if lint fails, instruct to run `fmt`…
r

rhythmic-morning-87313

07/06/2022, 3:29 PM
hmmm
yes i think
./pants fmt
should be run by human...
💯 1
it's hard to properly automate with hooks
c

curved-television-6568

07/06/2022, 3:29 PM
to me it would be surprising if a pre-commit hook had changed files for me…
r

rhythmic-morning-87313

07/06/2022, 3:30 PM
haha
c

clean-city-64472

07/06/2022, 4:31 PM
+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

witty-crayon-22786

07/06/2022, 4:44 PM
@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

wide-midnight-78598

07/06/2022, 7:05 PM
@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