简体   繁体   English

GIT-预提交文件限制

[英]GIT - pre-commit file restriction

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. 我需要一个GIT存储库,其中包括几个目录,每个目录的文件具有不同的扩展名。 So for example: 因此,例如:

A\\ <- A directory will have *.a files only A \\ <-目录将仅具有* .a文件

B\\ <- B directory will have *.b files only B \\ <-B目录将仅具有* .b文件

C\\ <- C directory will have *.c, *.d, and *.f files only C \\ <-C目录将仅具有* .c,*。d和* .f文件

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. 我知道我需要使用pre-commit挂钩来检查验证,但是我不确定最好的方法。 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. 最直接的答案似乎是跳转到每个目录并确保仅存在预期的扩展名,但这可能会成为一个相当长/复杂的脚本,因为某些目录允许使用多个(有时为10个或更多)文件扩展名。

If knows of a simpler way I would love to hear any advice. 如果知道更简单的方法,我很乐意听取任何建议。

You probably want to parse the output of a Git command. 您可能想解析Git命令的输出。 The most obvious choice is: 最明显的选择是:

git diff --cached --name-only --diff-filter=ACR

That will print a list of all added (A), copied (C), and renamed (R) files. 这将打印所有已添加(A),已复制(C)和已重命名(R)文件的列表。 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. 如果需要,您还可以检查已修改的文件(M),但是只要您不让它们首先进入,就没有必要。 --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. --name-only使您无法获得实际的差异,-- --cached告诉它查看索引(临时区域)而不是工作树,该树可能包含未分级的更改。 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: 或者,您可以逐目录执行diff目录,以简化验证过程,例如:

git diff --cached --name-only --diff-filter=ACR directory-A | grep -v '\.a$'

which will yield a list of all files whose names don't end in ".a". 这将产生一个名称以“ .a”结尾的所有文件的列表。 Replace directory-A and '\\.a$' with variables, and then you could just iterate over a list of directory/regex pairs. directory-A'\\.a$'替换为变量,然后可以遍历目录/正则表达式对的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM