git 提交到 gerrit,gitgerrit,#!/usr/bin/e


#!/usr/bin/env python#coding:utf-8''''''import osimport sysoriginName = "origin"versionInfo = """调用方式:1.将 ssrepo 复制到 '~/bin' 目录下2.sudo chmod +x ssrepo    为 ssrepo 增加可执行权限 3.将 '~/bin' 加入到环境变量 PATH 中提交代码:1.git add 将要提交修改加入暂存区2.git commit -s 提交修改3.ssrepo sync 同步服务器代码到当前所在分支,重排提交序列4.ssrepo upload 提交当前分支到服务器 master 分支注意事项:1.可使用 ssrepo push 替代 ssrepo sync 和 ssrepo upload 一次完成同步和提交操作--= version info =--version 0.41.修正新文件位于文件夹中因文件夹不存在导致 sync时异常结束的bugversion: 0.31.修正同步时将其他用户删除的文件加入cache中,而导致代码中出现多余的无用文件的问题2.增加 push 方法,使用 ssrepo push 调用,自动进行 sync 后进行 upload。version: 0.21.修正master分支pull时出现 非快进式错误提示2.修正再没有进行git stash 时,错误的调用了 git stash pop 的问题"""def hasVersion(filename) :    logline = os.popen('git log {0}'.format(filename)).readlines()    return len(logline) > 0def removeCache(cachepath):    for parent,dirnames,filenames in os.walk(cachepath):            for filename in filenames:                                  srcpath = (os.path.join(parent,filename))             targetpath = srcpath[1:]            if os.path.exists(targetpath):                pass            else:                targetDir = os.path.abspath(os.path.join(targetpath,'..'))                if os.path.exists(targetDir):                    pass                else :                    os.makedirs(targetDir)                os.rename(srcpath,targetpath)                if hasVersion(targetpath) :                    #print('hasVersion need delete ' + targetpath)                    os.popen('rm {0}'.format(targetpath)).readlines()                else :                    print("no version new file " +targetpath)    os.popen('rm -rf '+cachepath).readlines()def sync(needMV = True):    #print('--origin--')    HASHCODES = os.popen('git log {0} --format="%H"'.format(originName)).readlines()    #for x in HASHCODES:    #   print(x)    localcodes = [];    #print('--local--')    LOCALHASHCODES = os.popen('git log --format="%H"').readlines()    #for x in LOCALHASHCODES:    #   print(x)    #print('--same--')    tmp = [val for val in HASHCODES if val in LOCALHASHCODES]    #for t in tmp :    #   print(t)    HASHCODE = tmp[0].strip()    print("last same code is " + HASHCODE)    for CODE in LOCALHASHCODES :        CODE = CODE.strip()        if ( cmp(CODE ,HASHCODE) == 0) :            break        else :            localcodes.append(CODE)    cherryCount = len(localcodes)    print("need cherry count is {0}".format(cherryCount) )    for c in localcodes :         print ("need cherry code " + c)    localcodes.reverse()    line = os.popen('git branch -a | grep "*"').readlines()    branchName = line[0].strip('*').strip()    print('local branch is ' + branchName)    stash = os.popen('git stash').readlines()    os.popen('git reset '+HASHCODE ).readlines()    if (needMV) :        os.popen('mv src 1src').readlines()        os.popen('mv res 1res').readlines()        os.popen('mv libs 1libs').readlines()    os.popen('git reset --hard').readlines()    if cmp(branchName,'master') == 0:        os.popen('git pull').readlines()    else :        os.popen('git pull {0} master:{1}'.format(originName,branchName)).readlines()    for c in localcodes :         os.popen('git cherry-pick ' + c ).readlines()        print ('cherry-pick ' + c)    if len(stash) > 1 :        print('stash pop')        os.popen('git stash pop').readlines()    else :        print('stash is clear')    removeCache('1src')    removeCache('1res')    removeCache('1libs')def upload():    line = os.popen('git branch -a | grep "*"').readlines()    branchName = line[0].strip('*').strip()    print('the branch "{0}" will be push '.format(branchName))    os.popen('git push {0} {1}:refs/for/master'.format(originName,branchName)).readlines()def getOriginInfo():    originInfoline = os.popen('git remote -v').readlines()    for originInfo in originInfoline:        originInfo = originInfo.strip()        #print(originInfo)        if(originInfo.endswith('(push)')) :            #print(originInfo)            infos = originInfo.split('\t',2)            originName = infos[0]            #print(infos[1])            port = infos[1].split(':',3)[2].split('/')[0]            serverName = infos[1].split('@',2)[1].split(':')[0]            userName = infos[1].split('//',2)[1].split('@')[0]            #print(" --== origin info ==--")            #print(" originName : {0}".format(originName))            #print(" serverName : {0}".format(serverName))              #print(" serverPort : {0}".format(port))            #print(" userName   : {0}".format(userName))            #scpStr = "scp -p {0} {1}@{2}:hooks/commit-msg .git/hooks/".format(port,userName,serverName)#           print(scpStr)def main():    workarg = sys.argv[-1].strip()    print('method "{0}"'.format(workarg))    fetchline = os.popen('git fetch').readlines()    if (len(fetchline) > 0 and fetchline[0].startswith('fatal')) or os.path.exists('src') is False :        print ('please make Terminal to code root path !!!')    else :        getOriginInfo()        if cmp(workarg,'sync') == 0 :            sync()        elif cmp(workarg,'upload') ==0 :            upload()        elif cmp(workarg,'push') ==0 :            print(" sync start...")            sync()            print("")            print(" upload start...")            upload()        elif cmp(workarg,'continue') ==0 :            print(" continue sync")            sync(False)            print("")        else :            print("*************************************************************")            print("")            print('method not find !!!   please use "sync" or "upload" or "push"')            print("")            print("----------------------    help    --------------------------")            print(versionInfo)            print("*************************************************************")            print("")if __name__ == '__main__':    main()

评论关闭