如何防止对现有文件的更改,但允许在git reto中创建文件?

发布于 2025-02-01 04:30:18 字数 158 浏览 1 评论 0原文

我假设服务器端的git挂钩至少是解决方案的一部分,但是我没有看到允许创建的方法,但 desok of toce of files

这里的用例是用于跟踪数据库模式更改的“迁移”脚本,因此文件将仅限于目录结构的特定部分中的特定类型。

I'm assuming a server-side git hook will be at least part of the solution, but I'm not seeing a way to allow creation but prevent modification of files.

The use-case here is "migration" scripts for tracking database schema changes, so the files will be limited to a particular type in a particular part of the directory structure.

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

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

发布评论

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

评论(1

眸中客 2025-02-08 04:30:18

一种方法是简单地使用git diff-index-cached Head检查更改的类型。这将向您显示要更改哪些文件以及进行更改的类型:

>>> git diff-index --cached HEAD
:100644 000000 dadd545e60815b2c9870d06949fec14698d70eef 0000000000000000000000000000000000000000 D      a-deleted-file
:100644 100644 b13f8d4cd9f0aa4d12a07326bdcd8ff9ce3d04fa 0eb9b914fc18b8f5db13d095f95657c6dedd40ab M      a-modified-file
:000000 100644 0000000000000000000000000000000000000000 96081681f18aaa7a70ed5d59549098a2685684e4 A      a-new-file

因此,您可以为更改类型字段解析此输出:git git diff-index |切割-d''-f 5 |剪切-f 1。这将为您提供所做更改类型的列表:

D
M
A

您可以简单地检查任何修改或删除,并退出挂钩,如果对跟踪文件进行了“禁止”更改:

if git diff-index | cut -d ' ' -f 5 | cut -f 1 | grep --quiet '[M|D]'; then
    echo 'files can only be added, not deleted or modified'
    exit 1
fi

One approach would be to simply check for the type of changes using git diff-index --cached HEAD. This will show you which files are being changed and the type of change being made:

>>> git diff-index --cached HEAD
:100644 000000 dadd545e60815b2c9870d06949fec14698d70eef 0000000000000000000000000000000000000000 D      a-deleted-file
:100644 100644 b13f8d4cd9f0aa4d12a07326bdcd8ff9ce3d04fa 0eb9b914fc18b8f5db13d095f95657c6dedd40ab M      a-modified-file
:000000 100644 0000000000000000000000000000000000000000 96081681f18aaa7a70ed5d59549098a2685684e4 A      a-new-file

So you can parse this output for the change type field: git diff-index | cut -d ' ' -f 5 | cut -f 1. This will give you a list of the type of changes being made:

D
M
A

And you can simply check for any modifications or deletions and exit the hook if any "disallowed" change was made to tracked files:

if git diff-index | cut -d ' ' -f 5 | cut -f 1 | grep --quiet '[M|D]'; then
    echo 'files can only be added, not deleted or modified'
    exit 1
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文