GIT-- 预提交文件限制

发布于 12-11 05:23 字数 348 浏览 0 评论 0原文

我仍在寻找其他类似的问题,但我还没有完全找到我要找的东西,希望得到一些帮助。

我需要一个 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗失的美好2024-12-18 05:23:22

您可能想要解析 Git 命令的输出。最明显的选择是:

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

这将打印所有添加 (A)、复制 (C) 和重命名 (R) 文件的列表。如果需要,您还可以检查修改的文件(M),但只要您一开始就不让它们进入,那就没有必要。 --name-only 阻止您获得实际的差异,并且 --cached 告诉它查看索引(暂存区域)而不是工作树,这可能包含未分阶段的更改。然后,您可以对每个打印的文件名进行任何您喜欢的验证。

或者,您可以逐个目录进行比较,以使验证更容易,例如:

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

这将生成名称​​不以“.a”结尾的所有文件的列表。将 directory-A'\.a$' 替换为变量,然后您可以迭代目录/正则表达式对的列表。

这将生成名称​​不以“.a”结尾的所有文件的列表。将 directory-A'\.a$' 替换为变量,然后您可以迭代目录/正则表达式对的列表。

You probably want to parse the output of a Git command. 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. 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:

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". Replace directory-A and '\.a$' with variables, and then you could just iterate over a list of directory/regex pairs.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文