我正在寻找一些帮助来在 Windows 上编写一个预提交钩子以在提交时检查 Jira 问题密钥。如果 Jira 密钥不存在,则不允许提交。我找不到任何方法。我是脚本的新手。任何帮助都会受到高度赞赏。
当我们提交更改时使用 Prettier 会格式化 Husky预提交中文件内的所有代码。有没有更好的方法来仅更改文件中已修改的代码行?
我想阻止调试函数var_dump, print_r, etc...被提交到repo,以便QA可以检查一些事情而不报告诸如"所有页面上都有大量文本!!"之类的错误.
我试过正则表达式(不是一个好主意......大概).
我也试过token_get_all但是由于某种原因,它会返回T_STRING每个调试函数,我想这会起作用,但它看起来很奇怪......
还有第三种更好的方法吗?
我正在尝试编写git pre-commit钩子脚本,它应该在修改文件的开头写入提交日期.我的问题是我无法将修改过的文件添加到以前的提交中.当我再次尝试调用git commit时,它会运行递归.如何编写脚本,在修改后的文件末尾添加修改时间?
我的代码:
#!/bin/bash
files_modified=`git diff-index --name-only HEAD`
for f in $files_modified; do
if [[ $f == *.groovy ]]; then
$line = $(head -1 f)
if [[ $line == "/%%*" ]];
then
sed -i 1d
fi
echo "/%% " + $(date +"%m_%d_%Y") + " %%\\" >> f
git add f
fi
done
git commit --amend #recursive
exit
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写服务器端预接收git挂钩来评估提交时的提交.
根据这里的答案,通过搜索git日志并使用'format:'过滤掉我想要的内容,这很容易实现.
我创建了以下预提交脚本.
#!/bin/bash
set -x #for debugging, TODO: remove
echo "parameters are" $@
echo "1 is " $1
#List of banned users
bannedusers=( root )
author_name=$(git show --pretty=oneline --pretty=format:%an | head -n1)
author_email=$(git show --pretty=oneline --pretty=format:%ae | head -n1)
committer_name=$(git show --pretty=oneline --pretty=format:%cn | head -n1)
committer_email=$(git show --pretty=oneline --pretty=format:%ce | head -n1)
commit_users=( "${author_name}" "${committer_name}" )
for acommituser in "${commit_users[@]}"
do
:
echo $acommituser #for debugging, TODO: remove
for abanneduser in "${bannedusers[@]}"
do
: …Run Code Online (Sandbox Code Playgroud) 是有办法有一个pre-commit钩子,其自动格式化代码(例如用astyle),但并没有破坏的部分提交吗?
工作流程:
# edit a file.txt
git add -p file.txt
# add one chunk, but not another
git commit -m 'a message'
[PRE_COMMIT_HOOK] Formatting source code
git status
# the "another" chunk is still not added
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果你git add在预提交钩子里面做了一个,这是在脚本格式化源代码之后需要的,那么也添加了"另一个"块.但我不希望这样.
有没有办法实现这个目标?
第一个问题......甚至有可能用git来完成这个吗?:)
我想要的是这个:
有时,为了自己的调试目的,我将代码中的一个变量切换为true( localMode = true;)。但这不应该被提交。我应该只提交变量设置为false. 当然,有时我会忘记进行此更改。如果我即将提交“错误”代码,git 是否有可能以某种方式停止或警告我?
UPD:
感谢您的帮助!我最终得到了以下 shell 脚本:
#!/bin/bash
git diff --cached --name-only | while read FILE; do
if [[ $(echo "$FILE" | grep -E "^.+main\-controller\.js$") ]]; then
content=$(<"$FILE")
if [[ $(echo "$content" | grep -E "rootScope\.localMode = true") ]]; then
echo -e "\e[1;31m\tCommit contains localMode set to true.\e[0m" >&2
exit 1
fi
fi
done
Run Code Online (Sandbox Code Playgroud) 我是提交钩子和 Clang 格式的新手,我正在尝试将两者结合起来。我设置了预提交挂钩,并且知道如何在命令行上运行 Clang 格式,但不确定如何将其添加到文件中。
这是我在命令行中运行的用于格式化的代码:
clang-format -i -style=llvm fileName
我也试图在所有提交提交的文件上运行它。 git diff --cached --name-only
这是我的pre-commit文件:
hook_enabled=true
# Redirect output to stderr.
exec 1>&2
# If the hook is enabled and there are one or more files added to the commit run
# code formatting.
if [ "$hook_enabled" != "false" ] &&
test $(git diff --cached --name-only $against | wc -c) != 0
then
cat <<\EOF
Code formatting changed some files, please review and re-add files with git …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置pre-commitPython 项目的存储库。运行后,pre-commit install我看到这样的输出:
[INFO] Installing environment for https://github.com/asottile/seed-isort-config.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
[INFO] Installing environment for https://github.com/pre-commit/mirrors-isort.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
[INFO] Installing environment for https://github.com/pre-commit/mirrors-pylint.
[INFO] Once installed this environment will be reused. …Run Code Online (Sandbox Code Playgroud) 我正在使用以下钩子为我的 C++ 项目使用预提交(版本 2.20.0):
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v14.0.6
hooks:
- id: clang-format
Run Code Online (Sandbox Code Playgroud)
我只是从几个不同的 .cpp/.h/cmakelists 文件中分别暂存了几行。当我尝试提交这些更改时,我从预提交中收到以下错误:
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to C:\Users\tyler.shellberg\.cache\pre-commit\patch1665600217-21836.
clang-format.............................................................Failed
- hook id: clang-format
- files were modified by this hook
[WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes...
[INFO] Restored changes from C:\Users\tyler.shellberg\.cache\pre-commit\patch1665600217-21836.
Run Code Online (Sandbox Code Playgroud)
我对此感到困惑。预提交是否不允许我部分提交文件,或者在提交时根本不允许进行未暂存的更改?
所有文件,无论是暂存的还是未暂存的,都已按 clang-format 进行格式化。我已经手动仔细检查了这一点。
为了清楚起见编辑:
如果我pre-commit run --files [filename]在每个文件(暂存和未暂存)上运行,所有报告都会返回“已通过”或“已跳过”(对于非 cpp 文件)。如果所有文件都通过,为什么会出现问题?
pre-commit-hook ×10
git ×8
githooks ×3
bash ×2
commit ×2
pre-commit ×2
svn ×2
clang ×1
clang-format ×1
debugging ×1
formatting ×1
hook ×1
php ×1
prettier ×1
python ×1
windows ×1