0

I want to add a pre-commit hook to run yapf on changed changed in a commit. I use

git diff --name-only --line-prefix=`git rev-parse --show-toplevel`

to get the list of full file names modified in a commit.

I am not sure on how to run yapf on the list of files produced by the above command as part of git pre-commit hook. I tried executing in bash

git diff --name-only --line-prefix=`git rev-parse --show-toplevel` | yapf -i

but it says yapf: error: cannot use --in-place or --diff flags when reading from stdin. My question here is there a way to run yapf on a list of changed files?

mcgusty
  • 1,354
  • 15
  • 21
  • Never heard of yapf before; black seems much more popular and is what I use. Yapf supports `git diff` output when run as `yapf-diff` but not when run as `yapf`; to run on specific files you'd want `yapf -i $(git diff --name-only)`, relying on the shell `$(...)` expansion here. – torek Dec 24 '22 at 01:31

0 Answers0