
假設我有一個具有 root 存取權的本機工作站和一個沒有 root 存取權限的伺服器。我想在這兩台電腦之間共用(主要是設定)檔案。因此,我在我的主目錄中設定了一個頂級的 git 儲存庫,並添加了這些檔案。到目前為止,一切都很好。
進一步假設兩台計算機上都存在我需要的文件。它帶有套件管理器,因此將安裝在我的主目錄之外。它沒有安裝在伺服器上,也不會安裝在伺服器上。我現在有兩個選擇:
我在本地使用套件管理器安裝它,然後手動將其安裝在遠端伺服器上。這樣文件就不會同步。這有點沒關係,因為該檔案來自套件管理器,所以它實際上並不是我正在處理的檔案。然而,當我搬到新伺服器時,我總是需要單獨安裝它,而且這種情況經常發生。不過,我可以新增一個用於安裝軟體包的 shell 腳本,並將該 shell 腳本新增到 git 儲存庫中。
我將其本地安裝在我的主目錄中並將其添加到存儲庫中。這樣我就不必在不同的機器上單獨安裝它,它保持同步,但不再透過套件管理器更新。這就是我現在正在做的事情。
問題是:是否有第三種更好的方法來做到這一點?有git
符號連結魔法嗎?
答案1
根據您的描述,我認為您想在遠端電腦上執行 shell 腳本。但也許設定一個 shell 腳本僅在本機電腦上執行,將套件從本機套件目錄推送到本機 git 儲存庫會更方便。然後,您可以使用 cron 或更簡潔的 git commit hook 來執行此腳本,以便始終保持同步。
[根據要求從評論移至回答]
答案2
我現在使用以下預推掛鉤:
#!/usr/bin/env bash
# Copy local files that are outside the repository (because they are controlled
# by the package manager) into the repository and commit them
# Although this is a pre-push hook, the files are not included in this push
# See also: http://unix.stackexchange.com/q/321328/58056
#remote="$1"
#url="$2"
localFiles=(
/usr/bin/rg
/usr/share/man/man1/rg.1.gz
/usr/share/vim/vimfiles/autoload/pathogen.vim
)
remoteFiles=(
/home/foo/pkg/bin/rg
/home/foo/pkg/man/man1/rg.1
/home/foo/.vim/autoload/pathogen.vim
)
echo "Execute git pre-push hook"
for idx in "${!localFiles[@]}"; do
localFile="${localFiles[$idx]}"
remoteFile="${remoteFiles[$idx]}"
echo -n "Copy ${localFile} to ${remoteFile}... "
cp "${localFile}" "${remoteFile}"
echo "Done."
echo -n "Add ${remoteFile} to repository... "
git add -f "${remoteFile}"
echo "Done."
done
echo "Commit if there is anything to commit... "
git commit -m "Automatically add files by git pre-push hook" \
&& echo -n "Some files were added and commited (but not pushed) " \
&& echo "by the git pre-push hook"
# Don't interfere with the push, so always exit with success
exit 0