我的点文件有一个 GitHub 存储库 here .
它包含一个 installdotfiles.sh 脚本,用于从 GitHub 安装点文件。我想在 GitLab 上有另一个 git 存储库用于备份(以及非常酷的自定义语法高亮 :P),镜像这个存储库,除了在存储库上运行 shell 命令:
grep -rl raw.githubusercontent . |
xargs sed -i ''
's/raw.githubusercontent/gitlab/g'; grep -rl 'dotfiles/master' . |
xargs sed -i '' 's/dotfiles\/master/dotfiles\/raw\/master/g'
用相应的 GitLab 链接替换 Github 链接。
基本上,如果我推送到我的 GitHub 点文件存储库,我会让我的 GitLab 存储库自动更新,除非先在存储库上运行脚本,所以我的 GitLab 存储库包含 GitLab 链接。
我将如何实现这样的钩子(Hook)?
我听说这可以使用 Git Hooks 来实现,但是我对它们不熟悉,阅读它们时,我并不完全理解如何完成这样的事情。
P.S 如果它可以双向工作会很好 - 所以当我推送到 GitLab 时,GitHub 将运行一个不同的脚本,用 GitLab 链接替换 GitLab 链接,这样每当我推送到一个时,两者都会相应地更新。
最佳答案
最简单的方法是让您的脚本 installdotfiles.sh
使用一个变量,您根据要获取文件的存储库设置(在调用脚本之前)。
例如:
origin=github curl https://raw.githubusercontent.com/thepiercingarrow2/dotfiles/master/installdotfiles.sh | sh
# or
origin=gitlab curl https://gitlab.com/thepiercingarrow2/dotfiles/raw/master/.bash_profile | sh
这意味着您的脚本包含一个简单的测试:
remote="raw.githubusercontent.com/thepiercingarrow2/dotfiles"
if [[ "${origin}" == "gitlab" ]]; then
remote="gitlab.com/thepiercingarrow2/dotfiles/raw"
fi
#bash_profile
curl -# https://${remote}/master/.bash_profile > ~/.bash_profile
换句话说,您真的不需要 git hook。任何类型。
关于git - 我如何制作一个 git 钩子(Hook)来修改然后镜像一个 git 存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36122684/