/var/www目录
创建一个Git仓库。我希望能够将Web内容从开发服务器推送到GitHub,然后将其拉取到生产服务器上,并在池边度过余下的时间。显然,我的计划中有一个问题是Git不会遵守文件权限(我还没有尝试过,只是现在才了解到这一点)。我想这是有道理的,因为不同的系统可能具有不同的用户/组设置。但如果我想强制权限传播,知道我的服务器配置相同,我有什么选择?还是有更简单的方法来解决我所尝试的事情吗?
/var/www目录
创建一个Git仓库。我希望能够将Web内容从开发服务器推送到GitHub,然后将其拉取到生产服务器上,并在池边度过余下的时间。git-cache-meta
(由 VonC 提到),或Metastore(由etckeeper使用)。或者您可以使用IsiSetup,据我所知,它将git用作后端。
请参阅Git Wiki上的Interfaces, frontends, and tools页面。git-contrib
软件包中找到了/usr/share/git-core/contrib/hooks/setgitperms.perl
脚本-- 这是一个类似目的的脚本。(“此脚本可用于在git工作树中保存/恢复完整的权限和所有权数据。”) - imz -- Ivan Zakharyaschev在SO问题“git - how to recover the file permissions git thinks the file should be?”中提到了git-cache-meta
(以及git FAQ),这是一种更简单的方法。
其思想是将文件和目录的权限存储在一个名为.git_cache_meta
的独立文件中。
它不是Git仓库直接版本化的文件。
这就是为什么它的用法是:
$ git bundle create mybundle.bdl master; git-cache-meta --store
$ scp mybundle.bdl .git_cache_meta machine2:
#then on machine2:
$ git init; git pull mybundle.bdl master; git-cache-meta --apply
所以你需要:
这可能有些晚了,但可能会对其他人有所帮助。我通过向我的代码库添加两个Git钩子来实现你想要的功能。
.git/hooks/pre-commit:
#!/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/hooks/post-checkout:
#!/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文件中的文件列表并恢复这些文件的所有权和权限。
$SELF_DIR/../../
不一定是仓库的根目录...但 git rev-parse --show-toplevel
是。 (不确定为什么您不只是使用 pwd
获取当前目录,但无论如何都是无关紧要的。) - PJSCopeland.permissions
文件的格式更改为可执行的chmod
语句,并使用-printf
参数来改进其他答案。以下是更简单的.git/hooks/pre-commit
文件:#!/usr/bin/env bash
echo -n "Backing-up file permissions... "
cd "$(git rev-parse --show-toplevel)"
find . -printf 'chmod %m "%p"\n' > .permissions
git add .permissions
echo done.
这里是简化版的.git/hooks/post-checkout
文件:
#!/usr/bin/env bash
echo -n "Restoring file permissions... "
cd "$(git rev-parse --show-toplevel)"
. .permissions
echo "done."
记得其他工具可能已经配置了这些脚本,所以您可能需要将它们合并在一起。例如,这里有一个包括git-lfs
命令的post-checkout
脚本:
#!/usr/bin/env bash
echo -n "Restoring file permissions... "
cd "$(git rev-parse --show-toplevel)"
. .permissions
echo "done."
command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on you
r path. If you no longer wish to use Git LFS, remove this hook by deleting .git/hooks/post-checkout.\n"; exit 2; }
git lfs post-checkout "$@"
pre-commit
钩子生成脚本的想法,以极大地简化post-checkout
钩子。然而,我认为这个pre-commit
钩子太过简单了;它将包括树中的所有文件,而不仅仅是提交到Git存储库中的文件。换句话说,它将包括.git/
中的所有内容,.gitignore
中的文件,未检入的临时文件等。 - jamesdlin#!/bin/bash
SELF_DIR=`git rev-parse --show-toplevel`
DATABASE=$SELF_DIR/.permissions
# Clear the permissions database file
> $DATABASE
echo -n "Backing-up file permissions..."
IFSold=$IFS
IFS=$'\n'
for FILE in `git ls-files`
do
# Save the permissions of all the files in the index
echo $FILE";"`stat -c "%a;%U;%G" $FILE` >> $DATABASE
done
IFS=${IFSold}
# Add the permissions database file to the index
git add $DATABASE
echo "OK"
$ cat .permissions
.gitignore;660;pauljohn;pauljohn
05.WhatToReport/05.WhatToReport.doc;664;pauljohn;pauljohn
05.WhatToReport/05.WhatToReport.pdf;664;pauljohn;pauljohn
正如您所见,我们拥有
filepath;perms;owner;group
在这种方法的评论中,有一个发帖者抱怨它只适用于相同的用户名,从技术上讲确实如此,但很容易解决。请注意,post-checkout脚本有两个操作部分,
# Set the file permissions
chmod $PERMISSIONS $FILE
# Set the file owner and groups
chown $USER:$GROUP $FILE
所以我只保留第一个,这就是我需要的全部内容。我的Web服务器用户名确实不同,但更重要的是,您只有在root用户下才能运行chown命令。但是可以运行“chgrp”命令。如何使用它很明显。
在此帖子的第一个答案中,最广泛接受的建议是使用git-cache-meta,这是一个执行与此处的预/后钩子脚本相同工作的脚本(解析来自git ls-files
的输出)。这些脚本对我来说更容易理解,而git-cache-meta代码则相当复杂。可以将git-cache-meta保留在路径中,并编写预提交和后检出脚本来使用它。
文件名中的空格是Omid的两个脚本都存在的问题。在post-checkout脚本中,如果您看到类似以下错误,则知道文件名中有空格:
$ git checkout -- upload.sh
Restoring file permissions...chmod: cannot access '04.StartingValuesInLISREL/Open': No such file or directory
chmod: cannot access 'Notebook.onetoc2': No such file or directory
chown: cannot access '04.StartingValuesInLISREL/Open': No such file or directory
chown: cannot access 'Notebook.onetoc2': No such file or directory
我正在寻找解决方案。这里有一种看起来可行的方法,但我只测试了一次。
#!/bin/bash
SELF_DIR=`git rev-parse --show-toplevel`
DATABASE=$SELF_DIR/.permissions
echo -n "Restoring file permissions..."
IFSold=${IFS}
IFS=$
while read -r LINE || [[ -n "$LINE" ]];
do
FILE=`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 permissions
chmod $PERMISSIONS $FILE
# Set the file owner and groups
chown $USER:$GROUP $FILE
done < $DATABASE
IFS=${IFSold}
echo "OK"
exit 0
由于权限信息是每行一条,因此我将IFS设置为$,这样只有换行符被视为新的分隔符。
我了解到非常重要的是将IFS环境变量恢复到原来的状态!如果你把$作为唯一的分隔符,那么Shell会话可能会出现问题。
我正在运行FreeBSD 11.1,freebsd jail虚拟化概念使操作系统更加优化。我目前使用的Git版本是2.15.1,我也喜欢在shell脚本上运行所有东西。考虑到这一点,我对上面的建议进行了修改:
git push:.git/hooks/pre-commit
#! /bin/sh -
#
# 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;
printf "Backing-up file permissions...\n";
OLDIFS=$IFS;
IFS=$'\n';
for FILE in $(git ls-files);
do
# Save the permissions of all the files in the index
printf "%s;%s\n" $FILE $(stat -f "%Lp;%u;%g" $FILE) >> $DATABASE;
done
IFS=$OLDIFS;
# Add the permissions database file to the index
git add $DATABASE;
printf "OK\n";
git pull:.git/hooks/post-merge
#! /bin/sh -
SELF_DIR=$(git rev-parse --show-toplevel);
DATABASE=$SELF_DIR/.permissions;
printf "Restoring file permissions...\n";
OLDIFS=$IFS;
IFS=$'\n';
while read -r LINE || [ -n "$LINE" ];
do
FILE=$(printf "%s" $LINE | cut -d ";" -f 1);
PERMISSIONS=$(printf "%s" $LINE | cut -d ";" -f 2);
USER=$(printf "%s" $LINE | cut -d ";" -f 3);
GROUP=$(printf "%s" $LINE | cut -d ";" -f 4);
# Set the file permissions
chmod $PERMISSIONS $FILE;
# Set the file owner and groups
chown $USER:$GROUP $FILE;
done < $DATABASE
IFS=$OLDIFS
pritnf "OK\n";
exit 0;
.gitignore;644;0;0
针对具有 root:wheel 644 权限的 .gitignore 文件:
请注意,我必须对 stat 选项进行一些更改。
祝使用愉快,
for DIR in $(find ./ -mindepth 1 -type d -not -path "./.git" -not -path "./.git/*" | sed 's@^\./@@')
do
# Save the permissions of all the files in the index
echo $DIR";"`stat -c "%a;%U;%G" $DIR` >> $DATABASE
done
改进版https://stackoverflow.com/users/9932792/tammer-saleh的答案:
.git/hooks/pre-commit:
#!/usr/bin/env bash
echo -n "Backing-up file permissions... "
cd "$(git rev-parse --show-toplevel)"
find . -type d ! -empty -printf 'X="%p"; chmod %m "$X"; chown %U:%G "$X"\n' > .permissions
find . -type f -printf 'X="%p"; chmod %m "$X"; chown %U:%G "$X"\n' >> .permissions
find . -type l -printf 'chown -h %U:%G "%p"\n' >> .permissions
git add .permissions
echo done.
.git/hooks/post-merge:
#!/usr/bin/env bash
echo -n "Restoring file permissions... "
cd "$(git rev-parse --show-toplevel)"
git diff -U0 .permissions | grep '^\+' | grep -Ev '^\+\+\+' | cut -c 2- | /usr/bin/bash
echo "done."
另一个选项是git-store-meta
。正如作者在this superuser answer中所描述的:
git-store-meta是一个Perl脚本,它集成了git-cache-meta、metastore、setgitperms和mtimestore的优秀特性。