def get_remote_branch(repo_info, branch): """ 获取远程分支到本地 @decorators staticmethod @param {dict} repo_info - repo信息字典 @see FCMMGitTools.get_repo_info @param {string} branch - 分支名 @returns {list} - 执行结果[returncode, msgstring, is_local_new] returncode - 0代表成功,其他代表失败 msgstring - 要返回显示的内容 is_local_new - 本地是否新增分支 """ res = [0, '', False] local_has_branch = FCMMGitTools.check_branch_exists(repo_info, branch) if local_has_branch: # 本地已有分支 res1 = FCMMTools.run_sys_cmd_list( ['git checkout ' + branch, 'git pull origin ' + branch]) res[0] = res1[0] else: # 本地没有分支,需新创建 res1 = FCMMTools.run_sys_cmd_list( ['git checkout -b %s origin/%s' % (branch, branch)]) res[0] = res1[0] res[2] = True return res
def overwrite_branch(repo_info, dest_branch, src_branch=None, tag=None, is_bare=False, commit=None): """ 通过某分支覆盖指定分支 @decorators staticmethod - [description] @param {dict} repo_info - repo信息字典 @see FCMMGitTools.get_repo_info @param {string} src_branch - 源分支名 @param {string} dest_branch - 目标分支名 @param {string} tag - 指定覆盖的版本标签 @param {is_bare} is_bare=False - 是否创建空分支 tag、src_branch、is_bare=True参数只需传入其中一个,优先取tag、其次为src_branch,最后为is_bare @param {string} commit=None - 如果是src_branch的情况,通过该参数获取指定commit的版本 @returns {list} - 执行结果[returncode, msgstring] returncode - 0代表成功,其他代表失败 msgstring - 要返回显示的内容 """ current_branch = FCMMGitTools.get_active_branch(repo_info) os.chdir(repo_info['work_dir']) cmd_list = [] cmd_list.append('git checkout master') cmd_list.append('git branch -d %s' % (dest_branch)) # 删除分支 if tag is not None: # 使用标签创建 commit_id = '' for tag_item in repo_info['repo'].tags: if tag_item.name == tag: commit_id = str(tag_item.commit) break if commit_id == '': return [1, 'tag_not_exists'] cmd_list.append('git branch %s %s' % (dest_branch, tag)) cmd_list.append('git checkout %s' % (dest_branch)) elif src_branch is not None: # 使用其他分支创建 cmd_list.append('git checkout %s' % (src_branch)) if commit is None: cmd_list.append('git checkout -b %s' % (dest_branch)) else: cmd_list.append('git checkout -b %s %s' % (dest_branch, commit)) elif is_bare: # 创建空库 cmd_list.append('git checkout master') cmd_list.append('git checkout --orphan %s' % (dest_branch)) cmd_list.append('git rm -rf .') cmd_list.append('git commit -am "add bare branch by fcmm4git"') else: return [1, 'para tag、src_branch、is_bare=True must input one!'] cmd_list.append('git push -f origin %s' % (dest_branch)) cmd_list.append('git checkout %s' % (current_branch)) return FCMMTools.run_sys_cmd_list(cmd_list)
def rollback_to_tag(repo_info, branch, tag): """ 回滚指定分支到指定的标签 @decorators staticmethod - [description] @param {dict} repo_info - repo信息字典 @see FCMMGitTools.get_repo_info @param {string} branch - 分支名 @param {string} tag - 标签名 @returns {list} - 执行结果[returncode, msgstring] returncode - 0代表成功,其他代表失败 msgstring - 要返回显示的内容 """ commit_id = '' for tag_item in repo_info['repo'].tags: if tag_item.name == tag: commit_id = str(tag_item.commit) break if commit_id == '': return [1, 'tag_not_exists'] current_branch = FCMMGitTools.get_active_branch(repo_info) os.chdir(repo_info['work_dir']) return FCMMTools.run_sys_cmd_list([ 'git checkout %s' % (branch), 'git reset --hard %s' % (commit_id), 'git push -f origin %s' % (branch), 'git checkout %s' % (current_branch) ])
def rollback_to_commit(repo_info, branch, commit): """ 回滚指定分支到指定的提交版本 @decorators staticmethod - [description] @param {dict} repo_info - repo信息字典 @see FCMMGitTools.get_repo_info @param {string} branch - 分支名 @param {string} commit - 提交标签 @returns {list} - 执行结果[returncode, msgstring] returncode - 0代表成功,其他代表失败 msgstring - 要返回显示的内容 """ current_branch = FCMMGitTools.get_active_branch(repo_info) os.chdir(repo_info['work_dir']) return FCMMTools.run_sys_cmd_list([ 'git checkout %s' % (branch), 'git reset --hard %s' % (commit), 'git push -f origin %s' % (branch), 'git checkout %s' % (current_branch) ])
def cmd_common_init(cmd_str='', dict_cmd_para=None): """ 通用命令前期通用处理 具体处理的内容包括: 1、判断是否帮助 2、基础参数校验 3、检查当前环境是否未提交,如果未提交不允许继续处理 4、检查本地配置文件及仓库信息是否准确 5、更新master分支和lb-pkg分支的最新本地版本 @decorators staticmethod @param {dict} dict_cmd_para=None - 参数字典 @returns {tuple} - 返回多个值的数组,按顺序如下 is_exit {bool} - 标识是否应直接,不进行后续的处理 return_obj {list} - 直接退出的执行结果[returncode, msgstring] returncode - 0代表成功,其他代表失败 msgstring - 要返回显示的内容 config {dict} - 获取到的全局参数config fcmm_config {dict} - 获取到的.fcmm4git配置信息 repo_info {dict} - 获取到的本地仓库信息 current_branch {string} - 当前本地工作分支 """ # 判断是否有帮助 if '-h' in dict_cmd_para.keys() or '-help' in dict_cmd_para.keys(): return (True, FCMMGitCmd.cmd_help({cmd_str: ''}), None, None, None, None) # 最基础的参数校验 res = FCMMTools.vailidate_cmd_para(dict_cmd_para, cmd_str) if res[0] != 0: return (True, res, None, None, None, None) # 本地仓库信息检查 config = RunTools.get_global_var('config') repo_info = FCMMGitTools.get_repo_info(os.getcwd()) fcmm_config = FCMMTools.get_fcmm_config(repo_info['work_dir']) if repo_info['repo'] is None or fcmm_config is None: return (True, [2, FCMMTools.get_i18n_tips(config, 'local_git_error') ], None, None, None, None) # 检查当前分支是否存在未提交信息 if FCMMGitTools.is_dirty(repo_info): return (True, [ 2, FCMMTools.get_i18n_tips(config, 'current_branch_is_dirty') ], None, None, None, None) # 下载最新的master分支 current_branch = FCMMGitTools.get_active_branch(repo_info) try: res = FCMMTools.run_sys_cmd_list( ['git checkout master', 'git pull origin master']) if res[0] != 0: return (True, [ res[0], FCMMTools.get_i18n_tips(config, 'execute_fail') ], None, None, None, None) fcmm_config = FCMMTools.get_fcmm_config(repo_info['work_dir']) if fcmm_config is None: return (True, [ 2, FCMMTools.get_i18n_tips(config, 'local_git_error') ], None, None, None, None) # 判断是否有版本分支 if fcmm_config['has_pkg'] == "true": # 要下载版本分支,绑定与本地版本分支的关系 res = FCMMGitTools.get_remote_branch(repo_info, 'lb-pkg') # 处理结果 if res[0] != 0: return (True, [ res[0], FCMMTools.get_i18n_tips(config, 'execute_fail') ], None, None, None, None) finally: # 最后尝试跳转回工作分支 FCMMTools.run_sys_cmd('git checkout %s' % (current_branch)) # 最后返回 return (False, [0, ''], config, fcmm_config, repo_info, current_branch)
def cmd_add_pkg(dict_cmd_para=None): """ 新增FCMM的pkg分支,如果原分支存在,可以重置分支 @decorators staticmethod @param {dict} dict_cmd_para=None - 参数字典 @returns {list} - 执行结果[returncode, msgstring] returncode - 0代表成功,其他代表失败 msgstring - 要返回显示的内容 """ # 进行初始化 is_exit, res, config, fcmm_config, repo_info, current_branch = FCMMGitCmd.cmd_common_init( 'add-pkg', dict_cmd_para) if is_exit: return res # 进一步检查 ver = FCMMTools.get_cmd_para_value(dict_cmd_para, '-v', '-version') has_pkg = FCMMGitTools.check_branch_exists(repo_info, 'lb-pkg') if not ('-f' in dict_cmd_para.keys() or '-force' in dict_cmd_para.keys()): if has_pkg: return [ 1, FCMMTools.get_i18n_tips(config, 'branch_has_exists', 'lb-pkg') ] # 检查版本 if ver is not None and not FCMMGitTools.check_tag_exists( repo_info, ver): return [ 1, FCMMTools.get_i18n_tips(config, 'tag_not_exists', ver) ] # 开始进行处理 if has_pkg: # 分支已存在,覆盖分支 if config['backup_before'] == 'true': res = FCMMGitTools.backup_branch( repo_info, 'lb-pkg', FCMMGitTools.get_git_config_user_name( repo_info, config['consle_encode'])) if res[0] == 0: res = FCMMGitTools.overwrite_branch(repo_info, 'lb-pkg', 'master', ver) else: # 分支不存在,创建分支并推送到远端 res = FCMMTools.run_sys_cmd('git checkout master') if res[0] == 0: fcmm_config['has_pkg'] = "true" FCMMTools.save_to_json_file(repo_info['work_dir'], fcmm_config) res = FCMMTools.run_sys_cmd_list([ 'git add *', 'git commit -m "change .fcmm4gig by tools"', 'git push origin master' ]) if res[0] == 0: res = FCMMGitTools.add_branch(repo_info, 'lb-pkg', 'master', ver) # 返回值 FCMMTools.run_sys_cmd('git checkout %s' % (current_branch)) if res[0] != 0: res[1] = FCMMTools.get_i18n_tips(config, 'execute_fail') return res