obsidian/SvnToGit.py
2025-03-26 00:02:56 +08:00

527 lines
17 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from email.policy import strict
from inspect import getcomments
import os
from re import S
import sys
import json
import urllib3
import re
DirPath = os.path.split(os.path.realpath(__file__))
# DirPath = "E:\\Jenkins3Agent\\workspace\\qiuqiuTest"
# 白名单
whiteList = ['info','svnUpdate','status','gitPull','versionLog','diffLog','gitStatus']
# DIR = sys.argv[1] # 方向
GitPath = sys.argv[1] # git地址
GITBRANCH = sys.argv[2] # git分支
SVN_USER = sys.argv[3] # svn用户
SVN_PASSWORD = sys.argv[4] # svn密码
GIT_USER = sys.argv[5] # git用户
GIT_PASSWORD = sys.argv[6] #git密码
PUSH_STYLE = sys.argv[7] #提交方式 #true为分批提交,false为一次提交
GIT_USER_TEMP = r"http://liaoyourong%40brmyx.com" #git临时用户
GIT_PASSWORD_TEMP = r"z123456" #git临时密码
# svn命令行
SVNDiffCommand = "svn diff -r "
SVNInfoCommand = "svn info >.\info"
SVNLogCommand = "svn log -r "
SVNUpdateToVersionCommand = "svn up -r "
SVNRevertCommand = "svn revert -R *"
SVNUpdateCommand = "svn update >.\svnUpdate"
SVNUpdate = "svn update"
SVNStatusCommand = "svn status > .\status"
SVNDeleteCommand = "svn del "
SVNAddOneCommand = "svn add "
SVNCommitCommand = 'svn commit --username ' + SVN_USER + ' --password ' + SVN_PASSWORD + ' -m "git提交"'
SVNBeforeRevision = "" #一次提交的版本迭代 前版本
SVNAfterRevision = "" #一次提交的版本迭代 后版本
SVNBeforeRevision_New = "" #分批提交的版本迭代 前版本
SVNAfterRevision_New = "" #分批提交的版本迭代 后版本
SVNVersionList = []
SVNVersionLogList = []
NewGitList = [] #git拉取下来相对于svn来说是改变的文件列表
# git命令行
addstr = "."
GitCheckoutCommand = "git checkout ."
GitPullCommand = "git pull > .\gitPull"
GitAddCommand = "git add "
GitAddAllCommand = "git add " + addstr
GitCommitCommand = 'git commit -m "'
GitCommitCommandLog = 'git commit '
GitPushCommand = r"git push " + GIT_USER_TEMP + ":" + GIT_PASSWORD_TEMP + "@" + GitPath + " " + GITBRANCH
# 前缀函数
def NormalFunc():
# 路径跳转
# os.chdir(listPath[0])
os.chdir(DirPath[0]) #跳转
# testarg = open("./testArgs",'w')
# for i in range(0,len(sys.argv)):
# testarg.write(sys.argv[i] + '\n')
# testarg.close()
# 拿到版本区间的提交信息
def GetLogCommand(after,before):
return SVNLogCommand + after + ":" + before + r" >.\versionLog"
def GetLog():
os.system(GetLogCommand(SVNBeforeRevision_New,SVNAfterRevision_New))
SvnVersionLog = open("./versionLog",'r')
svnLogList = SvnVersionLog.readlines()
for i in range(len(svnLogList)):
svnList = svnLogList[i].split('|')
if(len(svnList) > 1):
global SVNVersionList
global SVNVersionLogList
SVNVersionList.append(int(re.findall(r"\d+",svnList[0])[0]))
SVNVersionLogList.append(str(svnLogList[i+2]).strip())
SvnVersionLog.close()
os.remove(DirPath[0] + "\\" + "versionLog")
def JudgeGitClean():
GitStatusCommand = "git status > .\gitStatus"
os.system(GitStatusCommand)
StatusLog = open("./gitStatus",'r',encoding= "utf-8")
for StatusLogLine in StatusLog.readlines():
if(StatusLogLine.strip().find("nothing to commit") != -1):
StatusLog.close()
os.remove(DirPath[0] + "\\" + "gitStatus")
return True
StatusLog.close()
os.remove(DirPath[0] + "\\" + "gitStatus")
return False
def GetDiff(lastVersion,nowVersion):
diffList = []
svnDiff = SVNDiffCommand + str(lastVersion) + r":" + str(nowVersion) + " --summarize" + " >.\diffLog"
os.system(svnDiff)
diffLog = open("./diffLog",'r',encoding= "ansi")
for diffLogLine in diffLog.readlines():
# if(diffLogLine.find("Index:",0,7) != -1):
diffList.append(diffLogLine.strip()[8:len(diffLogLine.strip())])
diffLog.close()
os.remove(DirPath[0] + "\\" + "diffLog")
return diffList
def UpdateSVNByVersion():
# SVNVersionList.reverse()
# SVNVersionLogList.reverse()
for j in range(len(SVNVersionList)):
if(j > 0):
os.system(SVNUpdateToVersionCommand + str(SVNVersionList[j]))
if(JudgeGitClean() ==False):
diffList = GetDiff(SVNVersionList[j-1],SVNVersionList[j])
for m in range(len(diffList)):
if(diffList[m] in NewGitList): #如果修改列表里面,包含修改的内容,就修正改成单次提交
os.system(SVNUpdate)
os.system(GitAddAllCommand)
changeLog = 'git commit -m "' + r"svn提交,修改列表里面,包含git修改内容,本次自动修改成单次提交,从以下提交开始:" + '"'
for l in range(len(SVNVersionLogList)):
if l >= j:
changeLog = changeLog + ' -m "' + SVNVersionLogList[l].strip() + '"'
os.system(changeLog)
os.system(GitPushCommand)
return
add = GitAddCommand + '"' + diffList[m] + '"'
os.system(add)
os.system(GitCommitCommand+ r"svn版本号:" + str(SVNVersionList[j]) + r"/" + str(SVNVersionLogList[j]).strip())
os.system(GitPushCommand)
# print(SVNVersionLogList[j])
def GetLocalVersion():
os.system(SVNInfoCommand)
infoLog = open("./info",'r')
for infoLine in infoLog.readlines():
if(infoLine.strip().find('Last Changed Rev') != -1):
global SVNBeforeRevision_New
SVNBeforeRevision_New = infoLine.strip()[18:len(infoLine.strip())]
# if(infoLine.strip().find(r"URL: http:") != -1):
# global SVNURL
# SVNURL = infoLine.strip()[5:len(infoLine.strip())]
infoLog.close()
def GetNewVersion():
os.system(SVNInfoCommand)
infoLog = open("./info",'r')
for infoLine in infoLog.readlines():
if(infoLine.strip().find('Last Changed Rev') != -1):
global SVNAfterRevision_New
SVNAfterRevision_New = infoLine.strip()[18:len(infoLine.strip())]
# if(infoLine.strip().find(r"URL: http:") != -1):
# global SVNURL
# SVNURL = infoLine.strip()[5:len(infoLine.strip())]
infoLog.close()
os.remove(DirPath[0] + "\\" + "info")
def GetBeforeInfo():
infoLog = open("./info",'r')
for infoLine in infoLog.readlines():
if(infoLine.strip().find('Revision') != -1):
global SVNBeforeRevision
SVNBeforeRevision = infoLine.strip()[10:len(infoLine.strip())]
infoLog.close()
def GetAfterInfo():
infoLog = open("./info",'r')
for infoLine in infoLog.readlines():
if(infoLine.strip().find('Revision') != -1):
global SVNAfterRevision
SVNAfterRevision = infoLine.strip()[10:len(infoLine.strip())]
infoLog.close()
# svn发到git
def SvnToGit():
os.system(SVNRevertCommand)
os.system(SVNUpdateCommand)
# os.system(GitCheckoutCommand)
os.system(GitPullCommand)
# os.system(SVNRevertCommand)
# os.system(SVNStatusCommand)
# statusLog = open("./status")
# for line in statusLog.readlines():
# if line.strip()[0] == "?": #新增的文件: (实际上在svn已经删除的)
# os.remove(DirPath + "\\" + line.strip()[8:len(line.strip())])
# statusLog.close()
# os.system(GitAddCommand)
# os.system(GitCommitCommand)
# os.system(GitPushCommand)
# git发到svn
def GitToSVN():
os.system(SVNRevertCommand)
os.system(SVNUpdateCommand)
# os.system(GitCheckoutCommand)
os.system(GitPullCommand)
# os.system(SVNStatusCommand)
# statusLog = open("./status")
# for line in statusLog.readlines():
# if line.strip()[0] == "!": #被删除的文件
# os.system(SVNDeleteCommand + '"' + ".\\" + line.strip()[8:len(line.strip())] + '"')
# elif line.strip()[0] == "?": #新增的文件:
# os.system(SVNAddOneCommand + '"' + ".\\" + line.strip()[8:len(line.strip())] + '"')
# os.system(SVNCommitCommand)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36', 'Accept-Charset': 'utf-8', 'Content-Type': 'application/json'}
webhook = "https://open.feishu.cn/open-apis/bot/v2/hook/eb0834e0-4d2d-4d2c-a20c-90ce9079566e"
href = ""
# git pull失败
def notify():
content = {
"msg_type": "post",
"content": {
"post": {
"zh_cn": {
"title": "git和svn合并通知",
"content":
[
[
{
"tag": "text",
"text": "git拉取失败,有修改了相同文件: "
},
{
"tag": "at",
"user_id": "7109285249619836931"
}
]
]
}
}
}
}
http = urllib3.PoolManager(retries=3, timeout=10)
resp = http.request(method="POST", url=webhook,
headers=headers, body=json.dumps(content))
# print(resp.data)
# svn要删除文件,但是git修改了这个文件
def AlreadySVNDel(message,allMessage):
content = {
"msg_type": "post",
"content": {
"post": {
"zh_cn": {
"title": "git和svn合并通知",
"content":
[
[
{
"tag": "text",
"text": "svn要删除文件,但是git修改了这个文件:\n "
},
{
"tag": "text",
"text": f"{message}"
},
{
"tag": "text",
"text": "\n所有svn删除的文件列表为:\n "
},
{
"tag": "text",
"text": f"{allMessage}"
},
{
"tag": "at",
"user_id": "7109285249619836931"
}
]
]
}
}
}
}
http = urllib3.PoolManager(retries=3, timeout=10)
resp = http.request(method="POST", url=webhook,
headers=headers, body=json.dumps(content))
# print(resp.data)
# svn和git重命名了同一个文件
def Rename(message):
content = {
"msg_type": "post",
"content": {
"post": {
"zh_cn": {
"title": "git和svn合并通知",
"content":
[
[
{
"tag": "text",
"text": "svn重命名了这个文件,git也重命名了这个文件,会产生多余的文件:\n "
},
{
"tag": "text",
"text": f"{message}"
},
{
"tag": "at",
"user_id": "7109285249619836931"
}
]
]
}
}
}
}
http = urllib3.PoolManager(retries=3, timeout=10)
resp = http.request(method="POST", url=webhook,
headers=headers, body=json.dumps(content))
# print(resp.data)
def DelOther():
os.remove(DirPath[0] + "\\" + "svnlog")
os.remove(DirPath[0] + "\\" + "gitPull")
os.remove(DirPath[0] + "\\" + "status")
os.remove(DirPath[0] + "\\" + "svnUpdate")
os.remove(DirPath[0] + "\\" + "info")
# os.remove(DirPath[0] + "\\" + "versionLog")
def GetAfterInfo():
infoLog = open("./info",'r')
for infoLine in infoLog.readlines():
if(infoLine.strip().find('Revision') != -1):
global SVNAfterRevision
SVNAfterRevision = infoLine.strip()[10:len(infoLine.strip())]
infoLog.close()
# 双向发送
def Both():
os.system(SVNRevertCommand)
os.system(SVNInfoCommand)
GetBeforeInfo()
GetLocalVersion()
os.system(SVNUpdateCommand)
os.system(SVNInfoCommand)
GetAfterInfo()
os.system(GitPullCommand)
os.system(SVNStatusCommand)
svndelList = ""
svnDelTrueList = []
svnLog = open("./svnUpdate",'r')
for svnLine in svnLog.readlines():
if svnLine.strip()[0] == "D": #被删除的文件
svndelList = svndelList + svnLine + "\n"
splitList = svnLine.split("\\")
svnDelTrueList.append(splitList[len(splitList)-1])
svnLog.close()
gitLog = open("./gitPull",'r',encoding= "utf-8")
gitNormal = False
isRename = False
for gitLine in gitLog.readlines():
if len(gitLine.split('|')) > 1:
gitNormal = True
if(gitLine.find("rename",0,len(gitLine)-1) != -1):
for i in range(0,len(svnDelTrueList)):
cleanLine = gitLine.replace("\"",'')
if(cleanLine.strip().find(str(svnDelTrueList[i]).strip(),0,len(cleanLine)-1) != -1):
isRename = True
Rename(svnDelTrueList[i])
if(gitLine.find("Already",0,len(gitLine)-1) != -1):
gitNormal = True
break
gitLog.close()
if(isRename == True):
print("git something wrong!!!!!!!!!!!")
return
if(gitNormal == False):
print("git something wrong!!!!!!!!!!!")
notify()
return
else:
print("git normal ula!!!!!!!!!!!!!!!!!!!!")
statusLog = open("./status")
NewGitList.clear()
for line in statusLog.readlines():
if whiteList.count(line.strip()[8:len(line.strip())]) != 0 :
continue
fullName = line.strip()[8:len(line.strip())] + "@"
NewGitList.append(line.strip()[8:len(line.strip())]) #存储git对于svn而言变更的文件列表
if line.strip()[0] == "!": #被删除的文件
os.system(SVNDeleteCommand + '"' + ".\\" + fullName + '"')
elif line.strip()[0] == "?": #新增的文件:
if(svndelList.find(line.strip()[8:len(line.strip())],0,len(svndelList)-1) != -1):
AlreadySVNDel(line.strip()[8:len(line.strip())],svndelList)
print("git something wrong!!!!!!!!!!!")
return
else:
os.system(SVNAddOneCommand + '"' + ".\\" + fullName + '"')
statusLog.close()
os.system(SVNLogCommand + SVNBeforeRevision + ':' + SVNAfterRevision + " >.\svnlog")
versionLog = open("./svnlog",'r')
versionLogList = versionLog.readlines()
VersionMessageList = []
for i in range(len(versionLogList)):
svnList = versionLogList[i].split('|')
if(len(svnList) > 1):
logIndex = i + 2
if(logIndex <= len(versionLogList) -1):
message =re.findall(r"\d+",svnList[0])[0] + r":" + versionLogList[i+2].strip()
else:
message =re.findall(r"\d+",svnList[0])[0] + r":"
VersionMessageList.append(' -m "' + message + '"')
# for versionLine in versionLog.readlines():
# print("versionLine:"+versionLine)
# global GitCommitCommandLog
# GitCommitCommandLog = GitCommitCommandLog + ' -m "' + versionLine.replace('\n','') + '"'
versionLog.close()
VersionMessageList.reverse()
for j in range(len(VersionMessageList)-1):
global GitCommitCommandLog
GitCommitCommandLog = GitCommitCommandLog + VersionMessageList[j]
DelOther()
os.system(SVNCommitCommand)
os.system(SVNUpdate)
# 判断是分批提交,还是单次提交
if(str(PUSH_STYLE) == "true"):
GetNewVersion()
GetLog()
UpdateSVNByVersion()
else:
os.system(GitAddAllCommand)
os.system(GitCommitCommandLog)
os.system(GitPushCommand)
# direction = {"svn发给git":SvnToGit,"git发给svn":GitToSVN,"双向更新":Both}
NormalFunc()
# direction.get(DIR,Both)()
Both()