简体   繁体   English

Git:有没有办法自动推送子树?

[英]Git: is there a way to auto push a subtree?

I have a big private repository which is maintained on a local network.我有一个在本地网络上维护的大型私有存储库。 I'd like to automatically push a subtree of that repository outside of that network.我想自动将该存储库的子树推送到该网络之外。 I need it to be simple:我需要它很简单:

*Task* someone pushes to local remote repository --> a subtree is automatically pushed to some other repository *任务*某人推送到本地远程存储库 --> 子树会自动推送到其他某个存储库

I am not sure if this could be achieved with a server side hook because AFAIK there is no such thing as pushing subtrees from bare remotes.我不确定这是否可以通过服务器端挂钩来实现,因为 AFAIK 没有从裸遥控器推送子树这样的事情。 I came up with two ideas:我想出了两个想法:

  • I could clone the remote on the server and automatically split the subtree in the cloned remote.我可以在服务器上克隆远程并自动拆分克隆远程中的子树。 This doesn't really help because I don't know how to auto-pull the subtree (others also have this problem ).这并没有真正的帮助,因为我不知道如何自动拉取子树(其他人也有这个问题)。
  • Another idea is to write a custom client-side post-commit-hook and make every user install it, but this is terrible, isn't it?另一个想法是编写一个自定义的客户端post-commit-hook并让每个用户都安装它,但这很糟糕,不是吗? Git book specifically states that policies should be enforced on server side . Git book 特别指出应该在服务器端强制执行策略

Is there a simple way of achieving something like this?有没有一种简单的方法可以实现这样的目标? Or is this impossible and it's just git abuse?或者这是不可能的,这只是 git 滥用?

Umm, I'm a bit embarrassed.嗯,我有点不好意思。 Apparently this was much easier than I thought.显然这比我想象的要容易得多。 Here is a hasty solution which builds on @wrzasa suggestion:这是一个基于@wrzasa建议的草率解决方案:

  1. Clone your repository on the server to which you are pushing, like this (dir.git is a bare repo):在您推送到的服务器上克隆您的存储库,如下所示(dir.git 是一个裸存储库):

     . |- dir.git |- dir
  2. In dir do: git remote add <remote-name> <remote-address>dir执行: git remote add <remote-name> <remote-address>

  3. In dir.git/hooks/post-receive put:dir.git/hooks/post-receive

     #! /bin/bash unset GIT_DIR cd ../dir git pull ../dir.git git subtree split --prefix=<subdir-in-dir> --branch=<branch-name> git push <remote-name> <branch-name>

    Remember to make post-receive executable.请记住使post-receive可执行。 See this answer if you wanna know why unset GIT_DIR is needed.如果您想知道为什么需要unset GIT_DIR请参阅此答案

That's pretty much it.差不多就是这样。 Now whenever someone pushes to dir remote (ie dir.git ) subtree under <subdir-in-dir> will be pushed to <remote-name> .现在,每当有人推送到dir remote(即dir.git )时, <subdir-in-dir>下的子树将被推送到<remote-name>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM