Git多仓库同步全部分支代码

将一个git远程仓库的所有分支代码同步到另外一个git仓库。

#!/usr/bin/env python3
#coding: utf-8

import os
import sys

git_url = 'git@git.coding.net:tenlee'
remote_name = 'coding'

def get_all_branch_form_path(path):
    branchs = os.popen('cd {} && git branch -r'.format(path))
    all_branch = []
    for branch in branchs.readlines():
        branch = branch.strip()
        if branch.find('HEAD') > 0:
            branch = branch.split()[2]
        branch = branch.split('/')[1]
        all_branch.append(branch)
    return all_branch

def is_git_dir(dir):
    if os.path.isdir(dir):  # 是文件夹
        sub_dirs = os.listdir(dir)
        if '.git' in sub_dirs:
            return True
    return False

def do_push(path):
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if is_git_dir(os.path.abspath(file_path)):
            all_branch = get_all_branch_form_path(file_path)
            for branch in all_branch:
                cmd = ('cd {path} &&' 
                        + ' git remote add {remote} {git_url}/{project}.git').format(
                                path=file_path, remote=remote_name,
                                git_url=git_url, project=file)
                os.system(cmd)

                cmd = ('cd {path} &&' 
                        + ' git checkout {branch}').format(
                            path=file_path, branch=branch)
                os.system(cmd)

                cmd = ('cd {path} &&  git push {remote} {branch}').format(
                            path=file_path, remote=remote_name,
                            branch=branch)
                print(cmd)
                if os.system(cmd) != 0:
                    print('Push Error')
                else:
                    print('Push Success')


def main():
    path = os.getcwd()
    if len(sys.argv) > 1:
        path = os.argv[1]
    do_push(path)

if __name__ == '__main__':
    main()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Git是目前最流行的版本管理系统,也是最先进的分布式版本控制系统(distributed version cont...
    pro648阅读 11,051评论 1 17
  • 今天被儿子狠狠的批评了,越是周末儿子起的越早,大清早钻到我房间跟我吆喝,“真懒,还不起床!”我也没当回事,继续在被...
    李宇航妈妈阅读 1,417评论 0 0
  • [1] 有关QQ、微信的技术文章:《微信团队原创分享:Android版微信的臃肿之困与模块化实践之路》 《微信后台...
    显生宙阅读 4,346评论 0 3
  • 和老公刚刚领了证。两人在一起还是处朋友的状态。他总觉得我要是一天到晚都不生气是件神奇的事,时不时言语上逗我着急。还...
    美女一号阅读 3,142评论 1 2