简体   繁体   English

如何自动拉取git子模块?

[英]How can I pull git submodule automatically?

I am assigned a project, where I will be doing backend development and another person will be handling frontend development. 我被分配了一个项目,我将在那里进行后端开发,另一个人将处理前端开发。 There is a requirement that the frontend developer does not have access to the development server, nor the project code. 要求前端开发人员无权访问开发服务器,也无法访问项目代码。 So, we decided to create another repository just for frontend. 因此,我们决定为前端创建另一个存储库。

The problem is that I don't know how to make submodule to immediately pull from the remote server when something is committed to the remote repo. 问题是我不知道如何使子模块在远程服务器提交到远程服务器时立即从远程服务器拔出。 I need the developer to commit and immediately see the change in the frontend. 我需要开发人员提交并立即看到前端的更改。

I have been reading about git hooks and I have found that I can do this using post-receive hook. 我一直在阅读git hooks,我发现我可以使用post-receive hook来做到这一点。 However, it doesn't seem to work. 但是,它似乎不起作用。 Here is what I did: 这是我做的:

  1. From the main project's root directory, I found the following directory /.git/modules/submodule-directory/hooks 从主项目的根目录,我找到了以下目录/.git/modules/submodule-directory/hooks
  2. I created post-receive file with executable permission allowed. 我创建了允许可执行权限的post-receive文件。
  3. Contents of post-receive file: post-receive文件的内容:

     #!/bin/sh git pull 
  4. Pushed a change from my local machine to the submodule repo but nothing happened. 推动从我的本地机器到子模块仓库的更改但没有任何反应。

What am I doing wrong? 我究竟做错了什么?

EDIT: I am going to clarify some points regarding my question. 编辑:我将澄清有关我的问题的一些观点。

I have two repositories: The "main" repository, which is the entire project; 我有两个存储库:“主”存储库,它是整个项目; and another repository, which is frontend stuff (views, assets etc). 和另一个存储库,它是前端的东西(视图,资产等)。 Let's call this "frontend" repository. 我们称之为“前端”存储库。

To my main repository, I added frontend repo as a submodule, located inside /submodules/frontend. 在我的主存储库中,我添加了frontend repo作为子模块,位于/ submodules / frontend内。 This way, the outsourced frontend developers will not be able to see anything about the actual project. 这样,外包的前端开发人员将无法看到有关实际项目的任何信息。 They will only see what they are working on. 他们只会看到他们正在做的事情。

This actually works perfectly in terms of access control and I can easily monitor the frontend development's progress separately from project. 这实际上在访问控制方面非常有效,我可以轻松地与项目分开监控前端开发的进度。 But there is one problem that I don't know how to do. 但有一个问题我不知道该怎么做。

Our project has two separate servers - a development server and a production server. 我们的项目有两个独立的服务器 - 开发服务器和生产服务器。 The development server needs to always be in the master branch for both main repo and frontend repo. 开发服务器需要始终位于主分支和前端仓库的主分支中。 This way, both frontend team and the backend team will be able to see their updates. 这样,前端团队和后端团队都能够看到他们的更新。 Even though frontend team mainly works with templates, the templates have access to data that is stored in the database and are using a lot of stuff that are integrated to backend (eg models). 即使前端团队主要使用模板,模板也可以访问存储在数据库中的数据,并使用很多集成到后端的东西(例如模型)。

In order for this "system" to work, I need the frontend repo to always fetch and merge whenever there is a new commit in "frontend" remote repo. 为了使这个“系统”工作,我需要前端仓库总是在“前端” 远程仓库中有新的提交时进行获取和合并。 In other words, if the frontend team does git commit and git push , the "frontend" repo submodule in the development server needs to immediately git pull . 换句话说,如果前端团队执行git commitgit push ,开发服务器中的“前端”repo子模块需要立即git pull So, when the frontend guys go test their stuff, they will immediately see the latest changes. 所以,当前端人员测试他们的东西时,他们会立即看到最新的变化。

I read that post-receive hook can do that but I need this hook to exist only for submodule. 我读到post-receive hook可以做到这一点,但我需要这个钩子只存在于子模块。 So, after some digging I found that all the submodule hooks exist in /.git/modules/submodules/frontend/hooks . 因此,经过一些挖掘后,我发现所有子模块挂钩都存在于/.git/modules/submodules/frontend/hooks So, I created post-receive hook in that directory with the following code: 所以,我使用以下代码在该目录中创建了post-receive hook:

#!/bin/sh
git pull

But this didn't work. 但这没效果。 My question is how to make the hooks work with post-receive inside submodule? 我的问题是如何使钩子与子模块内的后接收一起工作?

I know the route that I have taken is far from perfect but this is the only way that makes sense to me when it comes to allowing devs to only access one directory without having access to the development server. 我知道我所采取的路线远非完美,但这是允许开发人员只访问一个目录而无需访问开发服务器的唯一方法。

I hope this makes sense. 我希望这是有道理的。

When you git pull , this is (basically) equivalent to git fetch && git merge . 当你git pull ,这(基本上)等同于git fetch && git merge This will not update submodules. 这不会更新子模块。

If I understand your needs, What you want can be done with the commands git submodule update (or git submodule foreach 'git pull' ) in your post-receive hooks (in the main .git/hooks directory). 如果我了解您的需求,您可以使用命令git submodule update (或git submodule foreach 'git pull' )在您的post-receive挂钩(主.git / hooks目录中)中完成。

ADD (based on your editions): based on what I understood, you need to add the post-receive hook on the gitlab frontend repository, and your hook must execute the command git submodule update (or maybe git submodule foreach 'git fetch && git merge' in the development server (with a ssh command or something). ADD(基于你的版本):根据我的理解,你需要在gitlab前端存储库中添加post-receive hook,你的钩子必须执行命令git submodule update (或者git submodule foreach 'git fetch && git merge'在开发服务器中git submodule foreach 'git fetch && git merge' (使用ssh命令或其他东西)。

But it may be easier to rethink how your development server is installed, you can install the 2 repos as totally separated (not frontend being a submodule), and maybe adding the submodule/frontend to your .gitignore directory. 但是重新考虑开发服务器的安装方式可能更容易,您可以将2个repos安装为完全分离(不是前端是子模块),也可以将子模块/前端添加到.gitignore目录中。 This not necessarily means you have to remove frontend from the submodule, but just not using it in your development server. 这不一定意味着您必须从子模块中删除frontend ,但不要在开发服务器中使用它。

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

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