def project(cc): user_git_repo_list = [i for i in cc.values_json('repo_json')] names = [] repos = [] version = [] repo = {} create_msg = cc.value('project', 'create_message') # TODO: max lenght for each column + 2 for r in user_git_repo_list: for k, v in r.items(): n = display_string.format(v["name"], v["version"], v["path"]) names.append(n) names.append(create_msg) lns = cc.value('project', 'branch') + "\n" lns += display_string.format(" Project", " Tag", "Path") name = cutie.select_propmt(lns, names) name = name[:PROJECT_SPACE - 1].strip() if name == create_msg: repo = create_project(cc) elif name.lower() in ('exit', 'cancel'): cutie.get_exit() return None else: for r in user_git_repo_list: for k, v in r.items(): if name == k: repo = v branches.pick_branch(cc, repo) cc.save_json('repo_json', repo["name"], repo) return repo
def main(): cc = config.conf('.git_talk.ini') cutie.cprint('info', cc.value('git-talk', 'welcome')) if not cc.value('access', 'git_api'): cutie.cprint('blank', '') git_api = cutie.get_input(cc.value('git-talk', 'git_api_message')) if not git_api: git_api = 'https://github.com.au/api/v3' cutie.cprint('alarm', cc.value('git-talk', 'token_help_message')) cutie.cprint('', '') token = cutie.get_input(cc.value('git-talk', 'token_message')) g = github_api.GitHubAPI(git_api=git_api, token=token) if g is not None: cc.save("access", "git_api", git_api) cc.save("access", "token", token) else: cutie.cprint('error', "invalid api or token.") if cutie.get_exit(value='Exit', ex=True, message=m): exit() repo = {} user_git_repo_list = [i for i in cc.values_json('repo_json')] for r in user_git_repo_list: for k, v in r.items(): if k == cc.value("access", "current_repo"): repo = v if not repo: repo = projects.create_project(cc) # g.init(repo) while True: names = cc.value('git-talk', 'steps').split(',') b, status = stts.current_status(cc, repo) if status == 1: break repo["branch"] = b msg = cc.value('git-talk', 'message').format(repo["name"], b, status) name = cutie.select_propmt(msg, names, exit='Exit') #name = cutie.select_propmt(cc.value('git-talk','message'),names) #names = cc.value('git-talk','steps').split(',') if name == names[0]: graph.git_simple(cc, repo) elif name == names[1]: r = projects.project(cc) if r is not None: repo = r elif name == names[2]: rebase.rebase(cc, repo) elif name == names[3]: pr_tag_release(cc, repo) elif name == names[4]: branches.pick_branch(cc, repo) elif name == names[5]: commit.commit(cc, repo) elif name == names[6]: m = 'Exit git-talk CLI gracefully!' if cutie.get_exit(value=name, ex=True, message=m): break
def commit(cc, repo, comment="", up_stream="origin"): if repo['branch'] in ('master'): cutie.cprint('alert', (cc.value('commit', 'error').format(repo['branch']))) else: summary = '' names = cc.value('commit', 'commit_prefix').strip().split(",") while len(summary) < 1: summary = cutie.select_propmt(cc.value('commit', 'summary'), names) if summary.lower() in ('exit', 'cancel'): cutie.get_exit() return None while len(comment) < 1: comment = cutie.get_input(cc.value('commit', 'comment')) comment = summary + ': ' + comment cutie.cprint('wait', (cc.value('git-talk', 'wait'))) path = repo['path'] # git_path = "C:/Program Files/Git/cmd/git.exe" command = [["git", "add", "."], ["git", "commit", "-m", comment], ["git", "pull", up_stream], ["git", "push", "-u", up_stream, repo['branch'].strip()]] b, error = gfunc.subprocess_cmd(path, command)
def pr_tag_release(cc, repo): while True: names = cc.value('pr-tag-release', 'steps').split(',') b, status = stts.current_status(cc, repo) if status == 1: break msg = cc.value('git-talk', 'message').format(repo["name"], b, status) name = cutie.select_propmt(msg, names) if name == names[0]: pr.pr(cc, repo) elif name == names[1]: branches.add_tag(cc, repo) # elif name == names[2]: # pass # #merge_tag_master(cc,repo) elif name == names[2]: m = 'back to upper menu' if cutie.get_exit(value=name, ex=False, message=m): break
def create_project(cc): repo = {} # if prompt_yn(cc.value('project','create_doyou')): # input directory and git clone #pro = cutie.get_input(cc.value('project','project_name')) cnt = 0 user, password = '', '' if (cutie.prompt_yn("Do you use ssh connect to repository?")): repo["connection-ssh"] = True else: repo["connection-ssh"] = False while True: cnt += 1 repo['url'] = cutie.get_input(cc.value('project', 'project_url')) repo['name'] = stts.get_repo_name(url=repo['url']) # {"name": "ddo-data-images", # "path": "c:/github/ddo-data-images", # "url": "https://github.customerlabs.com.au/iagcl/ddo-data-images.git", if repo['name']: break elif cnt > 2: cutie.cprint('error', "try failed, exit") cutie.get_exit() else: cutie.cprint('error', "invalid url, must contain .git") path = cutie.linux_path( cutie.get_input(cc.value('project', 'path_message'))) repo["path"] = path if not repo["connection-ssh"]: user = cutie.get_input(cc.value('project', 'user_message')) password = cutie.get_pass(cc.value('project', 'password_message')) repo["up-stream"] = "" repo["version"] = cutie.get_input("Latest Tag Version (x.x.x) format:") repo["task"] = cutie.get_input("task branch prefix (CLD- ):") repo["hotfix"] = cutie.get_input("hotfix branch prefix (HFX- ):") repo["is-current"] = False repo["git-flow"] = True repo["descption"] = "" repo["jira-link"] = "" repo["issue"] = "" # if cddir(path) : if cutie.prompt_yn( cc.value('project', 'gitclone_message').format(repo['url'], path)): cutie.cprint('wait', cc.value('git-talk', 'wait')) try: created = False if not os.path.exists(path): os.mkdir(path) # cutie.cprint('info','my_repositories',repo['repo'],path) # To save credentials you can clone Git repository by setting a username and password on the command line: # https://www.shellhacks.com/git-config-username-password-store-credentials/ if not repo["connection-ssh"]: git_url_user_password = repo['url'].replace( "://", "://" + user + ":" + password + "@") else: git_url_user_password = repo['url'] command = [['git', 'clone', git_url_user_password]] b, _ = gfunc.subprocess_cmd(path, command) # b = gfunc.execute_git_command( # path, 'clone {0}'.format(git_url_user_password)) created = True # else: # if prompt_yn(cc.value('project','gitclone_existing').format(repo['url'],path)): # created = True if created: repo["path"] = cutie.linux_path( os.path.join(path, repo["name"])) #gitflow(repo["path"], cc) # rp = repo["path"] + "|" + repo["url"] + "|VERSION=0.0.0" cc.save_json('repo_json', repo["name"], repo) cutie.cprint('done', cc.value('git-talk', 'done')) repo["branch"] = branches.pick_branch(cc, repo) except Exception as e: cutie.cprint('error', str(e)) cutie.cprint('error', cc.value('git-talk', 'error')) cutie.get_exit() else: cutie.get_exit() else: cutie.get_exit() return repo
def pick_branch(cc, repo): path = repo['path'] cc.save("access","current_repo", repo["name"]) command =[['git','branch']] b,error = gfunc. subprocess_cmd(path, command) selected_branch = '' working_list = [] for ib in b[0].split('\n'): if ib.strip(): if ib.strip().startswith('*'): selected_branch = ib.strip().replace('*', '') if ib.strip().replace('*', '') not in ('master', 'dev', 'release'): working_list.append(ib) create_b = cc.value("branch", "create_branch") working_list.append(create_b) delete_b = cc.value("branch", "close_branch") working_list.append(delete_b) name = cutie.select_propmt(cc.value('project', 'branch'), working_list).replace('*', '').strip() if not cutie.get_exit(name): # TODO: will config prefix_ ,created by xxxxx and linked issue or jira if name == create_b: br_type = {'feature': repo["task"], 'hotfix': repo["hotfix"]} ty = cutie.select_propmt(cc.value('branch', 'branch_type'), [ 'feature', 'hotfix']).strip() if not cutie.get_exit(ty): if ty == 'hotfix': nb_from = 'master' else: nb_from = 'dev' nb = cutie.get_input(cc.value('branch', 'new_branch')) nb = br_type[ty] + nb cutie.cprint('info', 'creating {0} branch: {1}'.format(ty, nb)) if nb not in b: cmd_branch = [["git", "checkout", nb_from], [ "git", "pull"], ["git", "checkout", "-b", nb],["git","push","--set-upstream", "origin", nb]] b, error = gfunc.subprocess_cmd(path, cmd_branch) if error == 0: selected_branch = nb else: cutie.cprint('error', cc.value('git-talk', 'error')) # b = gfunc. subprocess_cmd(path, 'checkout {0}'.format(nb_from)) # b = gfunc. subprocess_cmd(path, 'checkout -b {0}'.format(nb)) else: cutie.cprint('alarm', cc.value( 'branch', 'existing_branch').fromat(nb)) elif name == delete_b: command = [["git", "branch"]] b, error = gfunc.subprocess_cmd(repo['path'], command) if error == 0: ty = cutie.select_propmt( cc.value('branch', 'close_picked'), b[0].split('\n')).strip() if not cutie.get_exit(ty): if '*' in ty: cutie.cprint('alarm', cc.value( 'branch', 'current_alarm')) else: b, error = gfunc.subprocess_cmd(repo['path'], [["git", "branch", "-d", ty]]) if error == 1 : if cutie.prompt_yn(cc.value('branch', 'confirm_delete').format(ty)): b, error = gfunc.subprocess_cmd(repo['path'], [["git", "branch", "-D", ty]]) if error == 0 : b, error = gfunc.subprocess_cmd(repo['path'], [["git", "push", "origin", "--delete",ty]]) else: b, error = gfunc.subprocess_cmd(repo['path'], [["git", "push", "origin", "--delete",ty]]) else: # checkout the branch if name != selected_branch: command =[['git','checkout', name]] bb,error = gfunc. subprocess_cmd(path,command ) if error == 0: cutie.cprint('info', cc.value( 'project', 'checkout').format(name)) selected_branch = name else: cutie.cprint('error', bb + '------------\n error:', str(error)) cutie.cprint('info', cc.value('project', 'checkout_error').format(name)) cutie.cprint('error', cc.value('git-talk', 'error')) repo['branch'] = selected_branch cc.save_json("repo_json", repo["name"], repo) return repo