def check_if_breaking_change(): global IS_BREAKING_CHANGE # required to be able to write to variable contains_break = "" print() # breakline from previous section while True: text = Ansi.b_yellow("Does commit contain breaking change? (no) ") contains_break = (prompt( ANSI(text), validator=YesNoValidator(answer_required=False), key_bindings=bindings, ).lower().strip()) if contains_break == "": # default IS_BREAKING_CHANGE = False break elif contains_break in ["y", "yes"]: IS_BREAKING_CHANGE = True return True else: IS_BREAKING_CHANGE = False return False
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()