git服务器 文件夹 权限,使用Git保留文件权限

这已经很晚了,但可能对其他人有所帮助。我通过向我的存储库添加两个git钩子来做你想做的事情。

的.git /钩/预提交:

#!/bin/bash

#

# A hook script called by "git commit" with no arguments. The hook should

# exit with non-zero status after issuing an appropriate message if it wants

# to stop the commit.

SELF_DIR=`git rev-parse --show-toplevel`

DATABASE=$SELF_DIR/.permissions

# Clear the permissions database file

> $DATABASE

echo -n "Backing-up permissions..."

IFS_OLD=$IFS; IFS=$'\n'

for FILE in `git ls-files --full-name`

do

# Save the permissions of all the files in the index

echo $FILE";"`stat -c "%a;%U;%G" $FILE` >> $DATABASE

done

for DIRECTORY in `git ls-files --full-name | xargs -n 1 dirname | uniq`

do

# Save the permissions of all the directories in the index

echo $DIRECTORY";"`stat -c "%a;%U;%G" $DIRECTORY` >> $DATABASE

done

IFS=$IFS_OLD

# Add the permissions database file to the index

git add $DATABASE -f

echo "OK"

git的/钩/结账后:

#!/bin/bash

SELF_DIR=`git rev-parse --show-toplevel`

DATABASE=$SELF_DIR/.permissions

echo -n "Restoring permissions..."

IFS_OLD=$IFS; IFS=$'\n'

while read -r LINE || [[ -n "$LINE" ]];

do

ITEM=`echo $LINE | cut -d ";" -f 1`

PERMISSIONS=`echo $LINE | cut -d ";" -f 2`

USER=`echo $LINE | cut -d ";" -f 3`

GROUP=`echo $LINE | cut -d ";" -f 4`

# Set the file/directory permissions

chmod $PERMISSIONS $ITEM

# Set the file/directory owner and groups

chown $USER:$GROUP $ITEM

done < $DATABASE

IFS=$IFS_OLD

echo "OK"

exit 0

第一个钩子在您“提交”时被调用,并将读取存储库中所有文件的所有权和权限,并将它们存储在名为.permissions的存储库根目录中的文件中,然后将.permissions文件添加到提交中。

当您“结帐”时将调用第二个挂钩,并将浏览.permissions文件中的文件列表并恢复这些文件的所有权和权限。

您可能需要使用sudo进行提交和签出。

确保预提交和结帐后脚本具有执行权限。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值