【问题标题】:VSTS - Prevent commit to root pathVSTS - 防止提交到根路径
【发布时间】:2018-03-10 22:13:57
【问题描述】:
我正在尝试找到一种方法来防止文件被提交到 repo 的根目录。找不到直接通过分支策略执行此操作的方法。例如,在 /* 上设置必需的审阅者,将一个组添加到正在签入到 repo 的任何文件中。如果有人尝试将文件only签入到 repo 的根目录(/),是否有办法将特定的组/个人添加为审阅者。
唯一的其他选项似乎是添加一个运行自定义脚本的构建定义,如果 PR 包含添加到 root 的文件,则构建失败。是否有可能对此有所帮助的 vsts 构建任务?
【问题讨论】:
标签:
azure-devops
azure-pipelines
azure-pipelines-build-task
build-definition
【解决方案1】:
您可以在 PR 验证构建定义中使用 PowerShell task 来检查 repo 的根目录中是否有文件,脚本如下:
$files=$(git ls-files)
echo $files
echo $files.length
for ($i=0; $i -lt $files.Length; $i++)
{
$file = $files[$i]
if ($file -match "/")
{ echo "the file $file in subdir" }
else
{
echo "the file $file in root dir"
exit 1
}
}
此外,您可以在本地 repo 中使用 pre-commit hook,以便在提交和推送之前检测根目录中是否有要提交的文件 >。该脚本可用于预提交挂钩,如下所示:
#!/bin/sh
for sfile in $(git diff --name-only --cached)
do
{
if [[ $sfile =~ "/" ]]; then
echo "the file $sfile in subdir"
else
echo "the file $sfile in root, stop to commit!"
exit 1
fi
}
done
for ufile in $(git diff --name-only)
do
{
if [[ $ufile =~ "/" ]]; then
echo "the file $ufile in subdir"
else
echo "the file $ufile in root, stop to commit!"
exit 1
fi
}
done