免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1084 | 回复: 0
打印 上一主题 下一主题

git 提交到 gerrit [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-22 16:53 |只看该作者 |倒序浏览
使用python脚本 提交代码到 gerrit

[Python]代码
  1. #!/usr/bin/env python
  2. #coding:utf-8
  3. '''

  4. '''
  5. import os
  6. import sys

  7. originName = "origin"

  8. versionInfo = """
  9. 调用方式:
  10. 1.将 ssrepo 复制到 '~/bin' 目录下
  11. 2.sudo chmod +x ssrepo    为 ssrepo 增加可执行权限
  12. 3.将 '~/bin' 加入到环境变量 PATH 中

  13. 提交代码:
  14. 1.git add 将要提交修改加入暂存区
  15. 2.git commit -s 提交修改
  16. 3.ssrepo sync 同步服务器代码到当前所在分支,重排提交序列
  17. 4.ssrepo upload 提交当前分支到服务器 master 分支

  18. 注意事项:
  19. 1.可使用 ssrepo push 替代 ssrepo sync 和 ssrepo upload 一次完成同步和提交操作

  20. --= version info =--

  21. version 0.4
  22. 1.修正新文件位于文件夹中因文件夹不存在导致 sync时异常结束的bug

  23. version: 0.3
  24. 1.修正同步时将其他用户删除的文件加入cache中,而导致代码中出现多余的无用文件的问题
  25. 2.增加 push 方法,使用 ssrepo push 调用,自动进行 sync 后进行 upload。

  26. version: 0.2
  27. 1.修正master分支pull时出现 非快进式错误提示
  28. 2.修正再没有进行git stash 时,错误的调用了 git stash pop 的问题


  29. """

  30. def hasVersion(filename) :
  31.     logline = os.popen('git log {0}'.format(filename)).readlines()
  32.     return len(logline) > 0



  33. def removeCache(cachepath):
  34.     for parent,dirnames,filenames in os.walk(cachepath):   
  35.         for filename in filenames:                     
  36.             srcpath = (os.path.join(parent,filename))
  37.             targetpath = srcpath[1:]
  38.             
  39.             if os.path.exists(targetpath):
  40.                 pass
  41.             else:
  42.                 targetDir = os.path.abspath(os.path.join(targetpath,'..'))
  43.                 if os.path.exists(targetDir):
  44.                     pass
  45.                 else :
  46.                     os.makedirs(targetDir)
  47.                 os.rename(srcpath,targetpath)
  48.                 if hasVersion(targetpath) :
  49.                     #print('hasVersion need delete ' + targetpath)
  50.                     os.popen('rm {0}'.format(targetpath)).readlines()
  51.                 else :
  52.                     print("no version new file " +targetpath)

  53.     os.popen('rm -rf '+cachepath).readlines()


  54. def sync(needMV = True):
  55.     #print('--origin--')
  56.     HASHCODES = os.popen('git log {0} --format="%H"'.format(originName)).readlines()
  57.     #for x in HASHCODES:
  58.     #   print(x)

  59.     localcodes = [];

  60.     #print('--local--')

  61.     LOCALHASHCODES = os.popen('git log --format="%H"').readlines()

  62.     #for x in LOCALHASHCODES:
  63.     #   print(x)
  64.     #print('--same--')
  65.     tmp = [val for val in HASHCODES if val in LOCALHASHCODES]

  66.     #for t in tmp :
  67.     #   print(t)

  68.     HASHCODE = tmp[0].strip()
  69.     print("last same code is " + HASHCODE)

  70.     for CODE in LOCALHASHCODES :
  71.         CODE = CODE.strip()
  72.         if ( cmp(CODE ,HASHCODE) == 0) :
  73.             break
  74.         else :
  75.             localcodes.append(CODE)

  76.     cherryCount = len(localcodes)

  77.     print("need cherry count is {0}".format(cherryCount) )

  78.     for c in localcodes :
  79.         print ("need cherry code " + c)

  80.     localcodes.reverse()


  81.     line = os.popen('git branch -a | grep "*"').readlines()
  82.     branchName = line[0].strip('*').strip()
  83.     print('local branch is ' + branchName)

  84.     stash = os.popen('git stash').readlines()
  85.     os.popen('git reset '+HASHCODE ).readlines()

  86.     if (needMV) :
  87.         os.popen('mv src 1src').readlines()
  88.         os.popen('mv res 1res').readlines()
  89.         os.popen('mv libs 1libs').readlines()

  90.     os.popen('git reset --hard').readlines()

  91.     if cmp(branchName,'master') == 0:
  92.         os.popen('git pull').readlines()
  93.     else :
  94.         os.popen('git pull {0} master:{1}'.format(originName,branchName)).readlines()
  95.     for c in localcodes :
  96.         os.popen('git cherry-pick ' + c ).readlines()
  97.         print ('cherry-pick ' + c)

  98.     if len(stash) > 1 :
  99.         print('stash pop')
  100.         os.popen('git stash pop').readlines()
  101.     else :
  102.         print('stash is clear')

  103.     removeCache('1src')
  104.     removeCache('1res')
  105.     removeCache('1libs')
  106.          


  107.      
  108. def upload():
  109.     line = os.popen('git branch -a | grep "*"').readlines()
  110.     branchName = line[0].strip('*').strip()
  111.     print('the branch "{0}" will be push '.format(branchName))
  112.     os.popen('git push {0} {1}:refs/for/master'.format(originName,branchName)).readlines()


  113. def getOriginInfo():
  114.     originInfoline = os.popen('git remote -v').readlines()
  115.     for originInfo in originInfoline:
  116.         originInfo = originInfo.strip()
  117.         #print(originInfo)
  118.         if(originInfo.endswith('(push)')) :
  119.             #print(originInfo)
  120.             infos = originInfo.split('\t',2)

  121.             originName = infos[0]
  122.             #print(infos[1])
  123.             port = infos[1].split(':',3)[2].split('/')[0]
  124.             serverName = infos[1].split('@',2)[1].split(':')[0]
  125.             userName = infos[1].split('//',2)[1].split('@')[0]
  126.             #print(" --== origin info ==--")
  127.             #print(" originName : {0}".format(originName))
  128.             #print(" serverName : {0}".format(serverName))  
  129.             #print(" serverPort : {0}".format(port))
  130.             #print(" userName   : {0}".format(userName))

  131.             #scpStr = "scp -p {0} {1}@{2}:hooks/commit-msg .git/hooks/".format(port,userName,serverName)
  132. #           print(scpStr)



  133. def main():
  134.     workarg = sys.argv[-1].strip()
  135.     print('method "{0}"'.format(workarg))
  136.     fetchline = os.popen('git fetch').readlines()
  137.     if (len(fetchline) > 0 and fetchline[0].startswith('fatal')) or os.path.exists('src') is False :
  138.         print ('please make Terminal to code root path !!!')
  139.     else :
  140.         getOriginInfo()
  141.         if cmp(workarg,'sync') == 0 :
  142.             sync()
  143.         elif cmp(workarg,'upload') ==0 :
  144.             upload()
  145.         elif cmp(workarg,'push') ==0 :
  146.             print(" sync start...")
  147.             sync()
  148.             print("")
  149.             print(" upload start...")
  150.             upload()
  151.         elif cmp(workarg,'continue') ==0 :
  152.             print(" continue sync")
  153.             sync(False)
  154.             print("")
  155.         else :
  156.             print("*************************************************************")
  157.             print("")
  158.             print('method not find !!!   please use "sync" or "upload" or "push"')
  159.             print("")
  160.             print("----------------------    help    --------------------------")
  161.             print(versionInfo)
  162.             print("*************************************************************")
  163.             print("")

  164. if __name__ == '__main__':
  165.     main()
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP