gpt4 book ai didi

git clang-format removes unstaged changes(Git clang-Format删除未暂存的更改)

转载 作者:bug小助手 更新时间:2023-10-28 10:08:34 25 4
gpt4 key购买 nike



I am writing a pre-commit hook for a repo where if some of the staged files are not formatted the hook will format the files and gives a message. Here is the content of the hook

我正在为repo编写一个预提交钩子,其中如果一些暂存的文件没有格式化,钩子将格式化文件并给出消息。这是钩子里的东西


#!/bin/bash

PYTHON=""
ROOT_DIR=$(git rev-parse --show-toplevel)
SCRIPT_DIR="${ROOT_DIR}/tools/source-formatter"
CLANG_BINARY=""
OS_TYPE=$(uname -s)
CLANG_FORMAT_SCRIPT="${SCRIPT_DIR}/git-clang-format"

function handle_exit() {
exit_code=$?
if [[ $exit_code -ne 0 ]] && [[ $exit_code -ne 10 ]]
then
echo ""
echo "** Commit failed **"
echo ""
if [[ $MSG == *"changed files"* ]]
then
echo "File structure are updated to comply with clang-format."
echo ""
echo "${MSG}"
echo ""
echo "Please review the changed, add to index and commit again."
echo "** If you need to commit without the formatting please use the --no-verify option for commit **"
else
echo "Staged changes needs to be formatted but clang-format refused to do so. Cause:"
echo "$MSG"
fi
fi
}

trap 'handle_exit' EXIT

if [ "$OS_TYPE" = "Linux" ]; then
CLANG_BINARY="clang-format.linux"
elif [ "$OS_TYPE" = "Darwin" ]; then
CLANG_BINARY="clang-format"
elif [ "$OS_TYPE" = "MINGW" ] || [ "$os_type" = "Windows_NT" ]; then
CLANG_BINARY="clang-format.exe"
else
echo "Unknown platform $OS_TYPE"
exit 10
fi

set -e
MSG=$(${CLANG_FORMAT_SCRIPT} --staged --extensions "h,cpp" --binary="${SCRIPT_DIR}/${CLANG_BINARY}" 2>&1)

Some clarification on the script:

以下是对剧本的一些澄清:



  1. I have a copy of git-clang-format and clang-format in the repo directory.

  2. The version of the clang-format and the script is 16

  3. I am running this on MacOs


Now here is the steps to produce my problem:

下面是产生我的问题的步骤:



  1. Have staged changes in file A which require reformatting

  2. Have staged changes in file B which don’t require reformatting

  3. Have unstaged changes in file B which are actual changes (let’s say new function)

  4. Try to commit staged changes, the hook doesn’t allow it because of file A.


The effect:

其效果是:



  • The result of code formater is unstaged changed in A.

  • The problem is that the new changes in file B are gone.


Can someone please tell me why this happens and how to stop it from happening?
I should also mention that if the changes that need formatting are staged in B and anything else is not staged in B then the git-clang-format refuses to format and prints a message saying that there are unstaged changes.

有人能告诉我为什么会发生这种情况,以及如何阻止它发生吗?我还应该提到的是,如果需要格式化的更改在B中暂存,而其他任何更改都没有在B中暂存,那么git-clang-format拒绝格式化并打印一条消息说有未暂存的更改。


更多回答
优秀答案推荐

Apparently this is a known bug and will be solved with the next release git-clang-format script.

显然,这是一个已知的错误,将在下一个版本的git-clang格式脚本中解决。


Here is the relevant PR.

以下是相关的公关。


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com