最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

git - Issues with commit message when pre-commit hook changes files - Stack Overflow

programmeradmin0浏览0评论

I have prepared the following pre-commit hook:

#!/bin/sh

# Run flake8 and mypy
flake8 ./src
FLAKE8_EXIT_CODE=$?

mypy ./src
MYPY_EXIT_CODE=$?

# If either flake8 or mypy return an error, skip black and the commit process
if [ $FLAKE8_EXIT_CODE -ne 0 ] || [ $MYPY_EXIT_CODE -ne 0 ]; then
  echo "flake8 or mypy checks failed. Skipping black and commit."
  exit 1
fi
black .
git add -u

if ! git diff-index --quiet HEAD; then
  git commit --no-verify -m "Same message all the times!"
fi

Note that black . reformat the files and therefore I have a second git add -u. To avoid infinite recursions, I added a last check that git commit with --no-verify, but then the commit message is the same for every commit and I like to avoid it. Instead, I want to be prompted for a commit message when I enter the last if branch. I tried to use read command inside the last if branch but with no success. I would also like to avoid external tools as pre-commit, but I would rather a native solution, if possible.

How to fix it?

I have prepared the following pre-commit hook:

#!/bin/sh

# Run flake8 and mypy
flake8 ./src
FLAKE8_EXIT_CODE=$?

mypy ./src
MYPY_EXIT_CODE=$?

# If either flake8 or mypy return an error, skip black and the commit process
if [ $FLAKE8_EXIT_CODE -ne 0 ] || [ $MYPY_EXIT_CODE -ne 0 ]; then
  echo "flake8 or mypy checks failed. Skipping black and commit."
  exit 1
fi
black .
git add -u

if ! git diff-index --quiet HEAD; then
  git commit --no-verify -m "Same message all the times!"
fi

Note that black . reformat the files and therefore I have a second git add -u. To avoid infinite recursions, I added a last check that git commit with --no-verify, but then the commit message is the same for every commit and I like to avoid it. Instead, I want to be prompted for a commit message when I enter the last if branch. I tried to use read command inside the last if branch but with no success. I would also like to avoid external tools as pre-commit, but I would rather a native solution, if possible.

How to fix it?

Share Improve this question edited Nov 20, 2024 at 8:09 Guildenstern 3,9272 gold badges28 silver badges54 bronze badges asked Nov 19, 2024 at 15:25 Barzi2001Barzi2001 1,7961 gold badge21 silver badges32 bronze badges 5
  • Why black . but flake8 ./src ; mypy ./src though? – 9769953 Commented Nov 19, 2024 at 15:37
  • Because I want to reformat my code 100% of the times. flake8 and mypy can only read files and tell you what is wrong, otherwise if they could fix the code I would have run them as I am doing with black straight away. – Barzi2001 Commented Nov 19, 2024 at 15:41
  • That's not what I meant (or I misunderstand your response): why is black run on the current directory, but flake8 and mypy on the src directory? What (type of) files are in the current directory (and what other subdirectories) that you want to use black on, but not flake8 and mypy? – 9769953 Commented Nov 19, 2024 at 15:44
  • Because inside ./src I want max compliance, whereas outside ./src I have scratch scripts that I don't mind if they are not flake8 or mypy compliant, yet I want them to be formatted according to` black`. – Barzi2001 Commented Nov 19, 2024 at 15:52
  • The pre-commit hook can only be used to check whether a commit can be made or not. If the hook modifies anything that is used for the commit, then you are operating it outside the specifications. Your script may break in the future without advance warning. – j6t Commented Nov 19, 2024 at 22:02
Add a comment  | 

1 Answer 1

Reset to default 2

I'd suggest to do the same for black as for pyflakes and mypy: run it as a checker, and refuse the commit if the checker fails, but don't actually reformat the code. So you'll always have to run black manually, but it won't reformat files without you seeing and knowing it.

black --check .

can do that (you can send the output of that check to /dev/null to reduce commit-hook noise if you want to). It (also) returns a non-zero value when the check fails, indicating black would reformat the code.


But for what you specifically want, perhaps

git commit --no-verify --amend --no-edit

does that: it keeps the last commit message by amending the current commit (and doesn't open an editor for adjusting the commit message).

发布评论

评论列表(0)

  1. 暂无评论