GIT-- 预提交文件限制
我仍在寻找其他类似的问题,但我还没有完全找到我要找的东西,希望得到一些帮助。
我需要一个 GIT 存储库,其中包含多个目录,每个目录都有不同扩展名的文件。例如:
A\ <- A 目录将仅包含 *.a 文件
B\ <- B 目录将仅包含 *.b 文件
C\ <- C 目录将包含 *.c、*.d 和*.f 文件只有
我知道我需要使用预提交挂钩来检查验证,但我不确定最好的方法。简单的答案似乎是跳转到每个目录并确保仅存在预期的扩展名,但这可能会是一个相当长/复杂的脚本,因为某些目录允许多个(有时 10 个或更多)文件扩展名。
如果知道更简单的方法,我很想听听任何建议。
I am still looking through other similar questions, but I haven't quite found what I'm looking for and was hoping for some help.
I need to have a GIT repo that includes several directories each with files that have different extensions. So for example:
A\ <- A directory will have *.a files only
B\ <- B directory will have *.b files only
C\ <- C directory will have *.c, *.d, and *.f files only
I know I need to use the pre-commit hook to check for validation, but I'm not sure the best way to do it. The straightforward answer appears to be jumping to each directory and making sure only expected extensions exist, but that could get to be a fairly long/complicated script since some directories have multiple (sometimes 10 or more) file extensions allowed.
If knows of a simpler way I would love to hear any advice.
您可能想要解析 Git 命令的输出。最明显的选择是:
这将打印所有添加 (A)、复制 (C) 和重命名 (R) 文件的列表。如果需要,您还可以检查修改的文件(M),但只要您一开始就不让它们进入,那就没有必要。
--name-only
阻止您获得实际的差异,并且--cached
告诉它查看索引(暂存区域)而不是工作树,这可能包含未分阶段的更改。然后,您可以对每个打印的文件名进行任何您喜欢的验证。或者,您可以逐个目录进行比较,以使验证更容易,例如:
这将生成名称不以“.a”结尾的所有文件的列表。将
directory-A
和'\.a$'
替换为变量,然后您可以迭代目录/正则表达式对的列表。You probably want to parse the output of a Git command. The most obvious choice is:
That will print a list of all added (A), copied (C), and renamed (R) files. If desired you could also check modified files (M), but as long as you don't let them get in in the first place, that won't be necessary.
--name-only
keeps you from getting an actual diff, and--cached
tells it to look at the index (staging area) rather than the work tree, which might contain unstaged changes. You can then do whatever verification you like on each printed filename.Alternatively, you could do the diff directory-by-directory, to make the verification easier, something like:
which will yield a list of all files whose names don't end in ".a". Replace
directory-A
and'\.a$'
with variables, and then you could just iterate over a list of directory/regex pairs.