当我推送到服务器时,Git 正在更改我的文件的权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11230171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:08:14  来源:igfitidea点击:

Git is changing my file's permissions when I push to server

gitpermissionsfile-permissions

提问by ThomasReggi

I am using git to manage a website on a server.

我正在使用 git 来管理服务器上的网站。

I have a local repository shown below

我有一个如下所示的本地存储库

local@workstation:myapp$ ls -l | awk '{k=0;for(i=0;i<=8;i++)k+=((substr(,i+2,1)~/[rwx]/)*2^(8-i));if(k)printf("%0o ",k);print}'
total 16
755 drwxr-xr-x@ 18 thomas  staff   612 Jun 13 15:35 application
755 drwxr-xr-x@ 11 thomas  staff   374 Jun 12 16:25 assets
644 -rw-r--r--@  1 thomas  staff  6399 Jun 22 11:45 index.php
755 drwxr-xr-x@ 10 thomas  staff   340 May 14 15:22 system

I have a bare repository on the server that uses post-receiveto point the repo in front of apache. Apache's publicfolders contents are below -not the bare repository.

我在服务器上有一个裸存储库,用于post-receive将存储库指向 apache。Apache 的public文件夹内容在下面 - 不是裸存储库。

root@server:/srv/public/myapp# ls -l | awk '{k=0;for(i=0;i<=8;i++)k+=((substr(,i+2,1)~/[rwx]/)*2^(8-i));if(k)printf("%0o ",k);print}'
total 20
700 drwx------ 15 root root 4096 Jun 27 11:31 application
700 drwx------ 10 root root 4096 Jun 27 11:31 assets
600 -rw-------  1 root root 6399 Jun 27 11:31 index.php
700 drwx------  8 root root 4096 Jun 27 11:31 system

This is causing mayhem to my code on the webserver.

这对我在网络服务器上的代码造成了混乱。

How can I fix this? I'm using gitolite if that makes any difference.

我怎样才能解决这个问题?如果这有什么不同,我正在使用 gitolite。

git server config file

git 服务器配置文件

[core]
        repositoryformatversion = 0
        filemode = true
        bare = true

回答by vergenzt

This thread postoffers a very good explanation:

这个线程帖子提供了一个很好的解释:

This is by design. While the git data structure can technically store unix mode bits in its trees, it was found early on in git's history that respecting anything beyond a simple executable bit ended up being more cumbersome for git's normal use cases (i.e., people storing code or other shared files in a repository).

We could add in a config option to respect file modes, but it has generally been seen as not worthwhile. It solves only a part of the general metadata problem, as it omits owner and group names or ids, as well as extended metadata like ACLs.

If modes are important to you, the suggested fixes are one of:

  1. Use a tool like "metastore" that can be called from git hooks, and will save and restore file permissions in a file that is tracked in the repository. Do note that when using such a tool there is a race condition in protecting files (i.e., git will create your file as 644, and then metastore will correct it to 600; in the meantime, somebody could read your file).

  2. Depending on exactly what you're storing, it may make sense to keep your repository in another directory, protected by permissions, and then use a separate tool to deploy your files from the repository to their ultimate location (e.g., a Makefile or other install tool).

这是设计使然。虽然 git 数据结构在技术上可以在它的树中存储 unix 模式位,但在 git 历史的早期发现,尊重任何超越简单可执行位的东西最终对于 git 的正常用例(即,人们存储代码或其他共享存储库中的文件)。

我们可以添加一个配置选项来尊重文件模式,但它通常被认为不值得。它仅解决了一般元数据问题的一部分,因为它省略了所有者和组名称或 ID,以及扩展元数据(如 ACL)。

如果模式对您很重要,建议的修复是以下之一:

  1. 使用可以从 git hooks 调用的“metastore”之类的工具,并将在存储库中跟踪的文件中保存和恢复文件权限。请注意,在使用此类工具时,保护文件存在竞争条件(即,git 会将您的文件创建为 644,然后 Metastore 会将其更正为 600;同时,有人可以读取您的文件)。

  2. 根据您存储的确切内容,将您的存储库保存在另一个目录中,受权限保护,然后使用单独的工具将您的文件从存储库部署到它们的最终位置(例如,Makefile 或其他安装)可能是有意义的工具)。