Git hook to update superproject on submodule change?

爱⌒轻易说出口 提交于 2019-12-11 10:25:01

问题


I have three git repositories: A, B, and C. C is a superproject including submodules A and B, tracking each submodule's respective master branch.

When the master branch of A or B is changed, I must do the following in C:

git submodule update --remote
git add A
git add B
git commit -m "Update submodule A or B"
git push

At this point an automated build and deploy from C is executed.

I'm looking for ways to streamline this process. Ideally, any commit to the master branch of A or B would trigger a hook to update, commit, and push to C. Any ideas how to achieve this?

Edit: In case there's a vendor-specific feature I should be aware of, I'm using Azure DevOps for the git hosting and Azure Pipelines for the build and deploy process.


回答1:


You can use a git post-commit hook, when you do a commit in the submodules the hook will be executed and will do the commands.

  1. Go to each sub module repo.
  2. Create file .git/hooks/post-commit with following content:

    #!/bin/sh
    
    branch="$(git rev-parse --abbrev-ref HEAD)"
    
    if [ "$branch" = "master" ]; then
      cd {super repo location}
      git submodule update --remote
      git add A
      git add B
      git commit -m "Update submodule A or B"
      git push
    fi
    
  3. Make it executable (not required on Windows):

    $ chmod +x .git/hooks/pre-commit
    


来源:https://stackoverflow.com/questions/54895559/git-hook-to-update-superproject-on-submodule-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!