forked from clarenceV1/LibFrameWork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckBranch.py
executable file
·107 lines (105 loc) · 3.26 KB
/
CheckBranch.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/python
# coding=utf-8
import zipfile
import shutil
import os
import shlex
import subprocess
import time
import re
#获取当前路径
currentDir = os.getcwd()
#print("-->当前目录%s" % currentDir)
#分支是否已经存在本地
def isBranchExist(branchname):
cmd = "git fetch"
r = os.popen(cmd)
cmd = "git branch"
r = os.popen(cmd)
result = r.read()
r.close()
if branchname in result:
return True
else:
return False
#执行cmd命令
def execCmd(cmd):
r = os.popen(cmd)
text = r.read()
r.close()
return text
#init git
exist = os.path.exists(".git")
if exist==False:
os.system("git init")
print("--->git init")
else:
print("--->已初始化git")
#Open file
channel_file = 'branch.config'
f = open(channel_file)
currentDir = os.path.dirname(currentDir)
for line in f:
#print("----->行内容:%s"%line)
array = line.split(";")
project_address = array[0]
project_name=array[1]
branch_name=array[2]
branch_name = branch_name.strip()
gitIntoPath = currentDir+"/"+project_name
print("======================>"+project_name+" branch to %s" % branch_name)
if os.path.exists(gitIntoPath)==False:
#进入子目录
os.makedirs(gitIntoPath)
#git clone into
execCmd("git clone "+project_address+" "+gitIntoPath)
#cd dir
os.chdir(gitIntoPath)
if branch_name.strip()!='' and branch_name.find("master",0,len(branch_name))==-1 and branch_name.find("tag:",0,len(branch_name))==-1:
#switch branch
execCmd("git checkout --track "+" origin/"+branch_name)
execCmd("git pull")
elif branch_name.strip()!='' and branch_name.find("tag:",0,len(branch_name))!=-1:
#switch tag
arrayTag = branch_name.split(":")
branchtagname=arrayTag[1]+"-from-tags"
#if branchname exists
isExistBranchAtLocal=isBranchExist(branchtagname)
if isExistBranchAtLocal is False:
execCmd("git checkout "+arrayTag[1])
execCmd("git checkout -b "+branchtagname)
execCmd("git push origin "+branchtagname)
execCmd("git branch --set-upstream-to=origin/"+branchtagname+" "+branchtagname)
else:
print("##################tag 本地已存在该分支,无需创建tag分支,直接切换")
execCmd("git checkout "+branchtagname)
#退出子目录
os.chdir(currentDir)
else:
#进入子目录
os.chdir(gitIntoPath)
if branch_name.strip()!='' and branch_name.find("tag:",0,len(branch_name))==-1:
#查看本地分支
isExistBranchAtLocal=isBranchExist(branch_name)
if isExistBranchAtLocal is False:
execCmd("git checkout --track "+" origin/"+branch_name)
else:
execCmd("git checkout "+branch_name)
execCmd("git pull")
elif branch_name.strip()!='' and branch_name.find("tag:",0,len(branch_name))!=-1:
#switch tag
arrayTag = branch_name.split(":")
branchtagname=arrayTag[1]+"-from-tags"
#if branchname exists
isExistBranchAtLocal=isBranchExist(branchtagname)
if isExistBranchAtLocal is False:
execCmd("git checkout "+arrayTag[1])
execCmd("git checkout -b "+branchtagname)
execCmd("git push origin "+branchtagname)
execCmd("git branch --set-upstream-to=origin/"+branchtagname+" "+branchtagname)
else:
print("##################tag 本地已存在该分支,无需创建tag分支,直接切换")
execCmd("git checkout "+branchtagname)
#退出子目录
os.chdir(currentDir)
f.close()