首页 > 解决方案 > git:如何防止人们删除不是他们创作的文件

问题描述

对于我们特定的 git 用例...

  1. 我需要防止人们删除不是他们创作的文件。我怎样才能做到这一点?也许以某种方式带有钩子?
  2. 可选:某种“-force”cli 参数可能允许这样的删除。
  3. 最好将此功能传递给分支(在 GitLab 上)。
  4. 如果无法阻止,那么至少如何在合并/拉取某些文件已(或即将)被未创作该文件的人删除时警告我?

标签: gitgitlab

解决方案


你可能在这里做错了什么。Git 和 gitlab 没有此功能,您正在尝试以非设计的方式使用软件。这通常是一个警告信号,也许是您的内部流程需要更改。

无论如何,你在正确的轨道上。你几乎可以用一个钩子来实现这一点。如果您是 gitlab 管理员(并托管您自己的 gitlab 实例),您可以设置 gitlab 所谓的自定义 git 钩子(来自 gitlabs 文档):

设置

通常,git hooks 放在存储库或项目的 hooks 目录中。GitLab 创建一个从每个项目的 hooks 目录到 gitlab-shell hooks 目录的符号链接,以便于 gitlab-shell 升级之间的维护。因此,自定义挂钩的实现方式略有不同。但是,一旦创建了钩子,行为就完全相同了。按照以下步骤设置自定义挂钩。

1. Pick a project that needs a custom git hook.
2. On the GitLab server, navigate to the project's repository directory. For an installation from source the path is usually /home/git/repositories/<group>/<project>.git. For Omnibus installs the path is usually /var/opt/gitlab/git-data/repositories/<group>/<project>.git.
3. Create a new directory in this location called custom_hooks.
4. Inside the new custom_hooks directory, create a file with a name matching the hook type. For a pre-receive hook the file name should be pre-receive with no extension.
5. Make the hook file executable and make sure it's owned by git.
6. Write the code to make the git hook function as expected. Hooks can be in any language. Ensure the 'shebang' at the top properly

反映语言类型。例如,如果脚本在 Ruby 中,则 shebang 可能是 #!/usr/bin/env ruby​​。

而已!假设钩子代码被正确实现,钩子将适当地触发。

我还没有测试过,但我认为这个钩子不会出现在 forks 中,所以你必须修改 gitlab 源代码才能做到这一点。

最后一个 gitlab 跟踪内容而不是文件。例如,文件重命名不会被猜测记录下来,这有时会导致奇怪的结果。我很确定你可以在这里找到一些边缘情况,你的方法会表现得很奇怪。


推荐阅读