[PATCH] gdb: add .pre-commit-config.yaml

Andrew Burgess aburgess@redhat.com
Wed Mar 20 13:44:05 GMT 2024


Simon Marchi <simon.marchi@efficios.com> writes:

> [I'm CCing the binutils list because this patch adds a file at the
>  top-level, but it only concerns gdb for now.  But if you are want to use
>  pre-commit for some things in binutils too, you are welcome to use that
>  file for your needs.]
>
> Add a pre-commit [1] config file, with a single hook to run black
> whenever a Python file is modified.  We can always add more hooks if we
> find some that are useful.
>
> Using pre-commit to run hooks is opt-in, as in it's not mandatory at all
> for development, but it can be useful to run some checks that are easy
> to forget (like running black).  The hooks run locally on the
> developer's machine when doing `git commit` (although they can also be
> configured to run at other stages of the git workflow).
>
> Follow these instructions to install the hooks in your local development
> git repository:
>
>  - Install pre-commit the way you prefer.  It can be using your OS
>    package manager if it has a recent enough version, or using `pip
>    install pre-commit`.
>  - Go to the binutils-gdb repository and run `pre-commit install`.
>
> This installs a git hook at `.git/hooks/pre-commit`.
>
> Now, whenever you modify and try to commit a Python file, pre-commit
> will run black on it.  For instance, if I try to insert something
> misformatted, I get this when doing `git commit`:
>
>     $ git commit
>     black....................................................................Failed
>     - hook id: black
>     - files were modified by this hook
>
>     reformatted gdb/python/lib/gdb/dap/breakpoint.py
>
>     All done! ✨ 🍰 ✨
>     1 file reformatted.
>
> At this point, black has already reformatted the files in place, so the
> changes that fix the formatting are ready to add and commit.  black is
> only ran on files modified in the commit.
>
> The hook defines a black version, which is downloaded at `pre-commit
> install` time.  pre-commit manages its own env at
> `$HOME/.cache/pre-commit/<some-hash>`, so it won't use the version of
> black you have installed already.  This may help ensure that
> contributors use the right black version.

This sounds great.  I got a recent commit wrong because the version of
black was rolled forward and I'd not updated locally, so this would
really help.

+1 from me for making this a thing.

Thanks,
Andrew



More information about the Binutils mailing list