简介
本文记录使用Python批量从bare类型的git仓库上push到bitbucket.
python代码
废话不说,先来看代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/5/20 下午12:50 # @Site : # @File : main.py # @Software: PyCharm Community Edition import os def main(): DIR = '/Users/yanzi/work/git/xxx-android-framework' if not os.path.exists(DIR): print('error: %s not exist' % DIR) return if os.path.isfile(DIR): print('error: %s is not dir' % DIR) return dirs = os.listdir(DIR) PATH = [] REPO = [] i = 0 for git in dirs: if str(git).endswith('.git'): # if i == 3: # break PATH = getPath(DIR, git) pushRepo(PATH, git) i += 1
#遍历文件夹DIR # for (root, dirs, files) in os.walk(DIR): # if root == DIR: # print(dirs) # break
def getPath(dir, git): return dir + '/' + git
def getRepo(git): return git[:-4]
def pushRepo(path, git): os.chdir(path) pushCmd = 'git push --mirror git@bitbucket.org:xxx/' + git print(pushCmd) os.system(pushCmd)
if __name__ == '__main__': main()
|
要点
git push --mirror xxx可以push仓下的所有分支和tag至origin上。
- 通过python进入不同的工作目录应通过
os.chdir(path)而不是os.system('cd /Users/xxx').