如何通过一对fifos push/pull Git 提交?

How to push/pull Git commits through a pair of fifos?

我想知道如何使用一对 fifos 作为在 Git 存储库克隆之间交换提交的方法。

git-remote-fd 记录了如何从客户端的角度使用它,但没有给出任何示例如何设置关联的服务器部分。

到目前为止,我尝试了以下方法:

# Prepare repos.
git init a
(cd a && echo one > file1 && git add file1 && git commit -m first)
git clone a b
(cd a && echo two > file2 && git add file2 && git commit -m second)
mkfifo ab && mkfifo ba
# Now I want to push from a to b via the fifos
(cd a && git push fd::8,9 master) 8< ba 9> ab &
(cd b && git pull fd::9,8 master) 8> ba 9< ab

不过运气不好。 fifos 明显阻塞。命令挂起且永远不会完成。

我想 "git push" 不是服务器部分的正确命令,"git send-pack" 或 "git upload-pack" 会更合适。但是我很难理解如何实际使用这两个命令。

有什么想法吗?

管道 git 通过管道提交很容易使用 git receive-packgit upload-pack

通常 git receive-packgit upload-pack 接受 1 个参数,一个 git 目录(包含 objects 文件夹的目录),然后通过其执行全双工通信stdin/stdout 与遥控器 git push/pull.

手动调用git receive-pack命令很简单:

mkfifo ab && mkfifo ba
# Now I want to push from a to b via the fifos
(git receive-pack b/.git) 1> ba 0< ab &
(cd a && git push fd::8,9 master) 8< ba 9> ab

git upload-pack 的方法相同:

mkfifo ab && mkfifo ba
# Now I want to push from a to b via the fifos
(git upload-pack a/.git) 1> ab 0< ba &
(cd b && git pull --no-edit fd::8,9 master) 8< ab 9> ba

请注意 git pullgit push 并不完全是彼此的逆运算:

  • git push 当另一方提交了它自己没有的提交时,默认情况下拒绝推送(除非 -f 已经给出,然后它会覆盖`),而 pull 则合并提交
  • git pull 可能会打开一个编辑器来编辑合并提交,除非给出了 --no-edit 标志(如果编辑器是 vi,则可能存在安全漏洞)