#!/usr/bin/env python #coding:utf-8 ''' ''' import os import sys originname = "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.4 1.修正新文件位于文件夹中因文件夹不存在导致 sync时异常结束的bug version: 0.3 1.修正同步时将其他用户删除的文件加入cache中,而导致代码中出现多余的无用文件的问题 2.增加 push 方法,使用 ssrepo push 调用,自动进行 sync 后进行 upload。 version: 0.2 1.修正master分支pull时出现 非快进式错误提示 2.修正再没有进行git stash 时,错误的调用了 git stash pop 的问题 """ def hasversion(filename) : logline = os.popen('git log {0}'.format(filename)).readlines() return len(logline) > 0 def 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()
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字
上传中....