我想在提交之前使用php-cs-fixer自动修复文件,然后提交更改,包括此修复
所以我创建了预提交文件,但是我遇到了问题:
1)我无法知道哪个文件被更改了(可能只是一个bash问题)
2)如果我跑" git add"没有条件,包括更改提交,但不包括文件本身
我试图在钩子的评论中清楚地表明,所以这里是:
#!/usr/bin/env bash
# get the list of changed files
staged_files=$(git diff --cached --name-only)
# command to fix files
cmd='vendor/bin/php-cs-fixer fix %s -q'
if [ -f 'php_cs_fixer_rules.php' ]; then
cmd='vendor/bin/php-cs-fixer fix %s -q --config=php_cs_fixer_rules.php'
fi
for staged in ${staged_files}; do # this cycle exactly works
# work only with existing files
if [[ -f ${staged} && ${staged} == *.php ]]; then # this condition exactly works
# use php-cs-fixer and get flag of correction
eval '$(printf "$cmd" "$staged")' # this command exactly works and corrects the file
correction_code=$? # but this doesn't work
# if fixer fixed the file
if [[ ${correction_code} -eq 1 ]]; then #accordingly this condition never works
$(git add "$staged") # even if the code goes here, then all changes will go into the commit, but the file itself will still be listed as an altered
fi
fi
done
exit 0 # do commit
提前感谢您提供任何帮助
特别是我想知道为什么correction_code没有获得价值和原因 " git add"之后的文件内容相同但未提交
答案 0 :(得分:0)
在const allItems = [{
id: 0,
title: "Item 0",
belongsTo: 'admin'
},
{
id: 1,
title: "Item 1",
belongsTo: 'user'
},
{
id: 2,
title: "Item 2",
belongsTo: 'all'
},
{
id: 3,
title: "Item 3",
belongsTo: 'user'
},
{
id: 4,
title: "Item 4",
belongsTo: 'all'
}
];
const selItemsSet = new Set([0, 2, 4]);
const output = allItems.filter(({ id }) => selItemsSet.has(id));
console.log(output);
中,如果通过pre-commit
添加一些文件,这些文件将出现在要提交的文件中。
您的git add
中的问题是pre-commit
。
[[ ${correction_code} -eq 1 ]]
成功时,它返回0,而不是1。
因此,php-cs-fixer fix
应该是:
pre-commit