def check_for_update(verbose=False): try: tags = get_github_tags() except Exception as e: print( "An error occured whilst checking for updates. Check your network connection." ) return latest_tag_version = tags[0][1:] cur_version = find_version() if version.parse(cur_version) < version.parse(latest_tag_version): Ansi.print_ok("There is an update available for conventional-commit.") upgrade_command = "pip install --upgrade conventional-commit" pyperclip.copy(upgrade_command) text = FormattedText([ ("", "Version "), ("class:red", cur_version), ("", " → "), ("class:green", latest_tag_version), ("", "\nAdded to your clipboard: "), ("class:command", upgrade_command), ]) print_formatted_text(text, style=style) elif verbose: print("Current version:", cur_version)
def run(args): # print(sys.version + "/n") if len(args) > 0 and args[0] in ["version", "update"]: check_for_update(verbose=True) return # Exit early # Ensure the config directory exists os.makedirs(CONFIG_HOME_DIR, exist_ok=True) commits_file_path = os.path.join(CONFIG_HOME_DIR, "commit_msg_history") commit_msg_history = FileHistory(commits_file_path) if WINDOW_WIDTH < 80: Ansi.print_error( f"It is recommended you increase your window width ({WINDOW_WIDTH}) to at least 80." ) commit_msg = "" if len(args) > 0 and args[0] == "retry": args = args[1:] # consume the "retry" first arg cm_histories_iter = commit_msg_history.load_history_strings() last_commit_msg = next(cm_histories_iter, "") if last_commit_msg == "": Ansi.print_error("Could not find a previous commit message") commit_msg = last_commit_msg if commit_msg == "": print("Starting a conventional git commit...") print( Ansi.colour(Ansi.fg.bright_red, "Tip: Press the up arrow key to recall history!")) commit_msg = add_type(commit_msg) commit_msg = add_scope(commit_msg) check_if_breaking_change() commit_msg = add_description(commit_msg) commit_msg = add_body(commit_msg) commit_msg = add_footer(commit_msg) print() Ansi.print_ok("This is your commit message:") print() print(commit_msg) print() # print("\nNOTE: This was a dry run and no commit was made.\n") # It is expected that all remaining args at this point are for the git # commit command args_string = " ".join(args) text = Ansi.colour(Ansi.fg.bright_yellow, "Extra args for git commit: ") extra_args_file_path = os.path.join(CONFIG_HOME_DIR, "extra_args_history") extra_args_str = prompt( ANSI(text), default=args_string, history=FileHistory(extra_args_file_path), key_bindings=bindings, ) if extra_args_str != "": argv_passthrough = extra_args_str.split(" ") else: argv_passthrough = [] # Ask for confirmation to commit confirmation_validator = YesNoValidator(answer_required=True) text = Ansi.b_yellow("Do you want to make your commit? [y/n] ") confirm = prompt(ANSI(text), validator=confirmation_validator, key_bindings=bindings).lower() if confirm in confirmation_validator.confirmations: commit_msg_history.store_string(commit_msg) cmds = ["git", "commit", "-m", commit_msg] + argv_passthrough returncode = subprocess.run(cmds).returncode print() if returncode == 0: Ansi.print_ok( "\nCommit has been made to conventional commits standards!") else: Ansi.print_error( "\nThere was an error whilst attempting the commit!") elif confirm in confirmation_validator.rejections: print("Aborting the commit...") check_for_update()