def main(command): # type: (str) -> None coloramaInit() try: repo_hidden_configger = LocalRepoConfigger() # throws if we are not in a Git repo repo_hidden_configger.loadJsonToConfigClass() except: uBelt.log('git-byLines can only run within a Git repo', isVerbose=True) exit(0) if command is COMMAND_COMMIT and repo_hidden_configger.config_class.enabled is False: uBelt.log('git-byLines is disabled per RepoHiddenConfigger', isVerbose=True) exit(0) last_commit_hash = uGit.getGitCommitHash() # Run this all the time after each command trigger (used to be only after a recent commit) if True or (uBelt.getCurrentTimeEpochMs() - uGit.getGitCommitEpochMs(last_commit_hash)) >= 10000: triggerByLineWorkFlow(repo_hidden_configger, last_commit_hash)
def addByLineToCommit(commit_hash, bylines_to_add): # type: (str, List[str]) -> bool uBelt.log('addByLineToCommit() ' + commit_hash + ': ' + str(bylines_to_add), isVerbose=True) if len(bylines_to_add) < 1: show_warning('No byLines selected to ammend to commit') return False ammended_commit_message = createAmmendedCommitMessage(commit_hash, bylines_to_add) cliPrint(reset_colors()) cliPrint('Commit message after ammendement:') for line in ammended_commit_message.splitlines(): cliPrint(reset_colors() + txtColor.LIGHTYELLOW_EX + line) cliPrint(reset_colors()) selected_input = uBelt.getInput('Confirm the ammended commit message above? [y/N]: ') if selected_input.lower().startswith('n'): show_warning('Ammend aborted') return False # Ammend the commit uGit.ammendCommitMessage(ammended_commit_message) cliPrint('... AMMEND DONE :D') return True
def saveConfigClassToJson(self): with open(self.getFilePath(), mode='w') as config_class_json_file: uBelt.log('writeConfigClassToJson... Preparing Json', isVerbose=True) json.dump(vars(self.config_class), fp=config_class_json_file, indent=2) uBelt.log('writeConfigClassToJson... Wrote Json', isVerbose=True) try: os_chmod(self.getFilePath(), 0o755) except Exception as e: uBelt.log('writeConfigClassToJson...' + str(e), isVerbose=True)
def loadJsonToConfigClass(self): # type: (BaseConfigger) -> bool uBelt.log('readJsonToConfigClass... Loading Json from:' + self.getFilePath(), isVerbose=True) try: with open(self.getFilePath(), mode='r') as config_class_json_file: loaded_dict = json.load(config_class_json_file) self.config_class.__dict__ = loaded_dict uBelt.log('readJsonToConfigClass... Loaded ConfigClass', isVerbose=True) return True except Exception as e: uBelt.log('readJsonToConfigClass...' + str(e), isVerbose=True) return False
assert testConfigger.doesConfigFileExist() is False testConfigger.saveConfigClassToJson() testConfigger.config_class.enabled = False assert testConfigger.config_class.enabled is False testConfigger.loadJsonToConfigClass() assert testConfigger.config_class.enabled is True testConfigger.config_class.enabled = False testConfigger.saveConfigClassToJson() testConfigger.loadJsonToConfigClass() assert testConfigger.config_class.enabled is False def testUtilityGit(): top_level_dir = uGit.getGitTopLevelDir() assert top_level_dir is not None path_to_test_file = os_path.join(top_level_dir, 'tests.py') assert os_path.exists(path_to_test_file) if __name__ == "__main__": uBelt.log('Python Version:' + sys_version) args = sys_argv uBelt.log('ARGS:' + str(args)) testUtilityGit() testBaseConfigger() uBelt.log('If you saw no errors above then the tests passed')
def ammendCommitMessage(new_commit_message): # type: (str) -> str output = uBelt.getOutputExecutingShellCommands( ['git', 'commit', '--amend', '-m ' + new_commit_message]) uBelt.log('ammendCommitMessage() got:' + output, isVerbose=True) return output
def getGitTopLevelDir(): # type: () -> str output = uBelt.getOutputExecutingShellCommands( ['git', 'rev-parse', '--show-toplevel']) uBelt.log('getGitTopLevelDir() got:' + output, isVerbose=True) return output
def triggerByLineWorkFlow(repo_hidden_configger, commit_hash): # type: (LocalRepoConfigger, str) -> None uBelt.log('triggerByLineWorkFlow() ' + commit_hash, isVerbose=True) up_fish = '´¯`·.´¯`·.¸¸.·´¯`·.¸¸.·´¯`·.¸.·´¯`·.¸.·´¯`·.¸.·´¯`·.¸.·´¯`·.¸.·´¯`·.¸.·><(((º>' dn_fish = '.·´¯`·.¸¸.·´¯`·.¸.·´¯`·.¸.·´¯`·.¸.·´¯`·.¸.·´¯`·.¸.·´¯`·.¸.·´¯`·.¸.·´¯`·><(((º>' sm_fish = '´¯`·.¸.·´¯`·.¸.·´¯`·.¸.·´¯`·.¸.·´¯`·.¸.><(((º>' cliPrint('') uBelt.log('last commit:' + commit_hash, isVerbose=True) cliPrint(reset_colors() + txtStyle.BRIGHT + "git-byLines", 0) cliPrint(up_fish, 0) cliPrint(reset_colors() + "Easily add multiple contributors/authors/byLines to a recent Git commit.", 0) cliPrint(dn_fish, 0) repo_visible_configger = RepoConfigger() repo_visible_configger.loadJsonToConfigClass() selected_bylines = [] # type: List[str] # Auto select the last commited byLines that we still have on file for recent_byline in repo_hidden_configger.config_class.lastByLines: if recent_byline in repo_visible_configger.config_class.byLines: selected_bylines.append(recent_byline) # Begin the Looping Interactive UI Prompts isFlowActive = True bylines = sorted(repo_visible_configger.config_class.byLines) while(isFlowActive): select_byline_prompt = '' if len(bylines) > 0: select_byline_prompt = 'or ' cliPrint('') cliPrint('Enter the number to toggle selection:') abc123_list = list(range(1,len(bylines) + 1)) for num, byline in enumerate(bylines, start=1): radio_selection_num = ' (' + str(num) + ') ' if byline in selected_bylines: cliPrint(reset_colors() + txtColor.LIGHTYELLOW_EX + txtStyle.BRIGHT + ' ✔' + radio_selection_num + byline) else: cliPrint(reset_colors() + txtColor.LIGHTYELLOW_EX + txtStyle.NORMAL + ' ' + radio_selection_num + byline) cliPrint(reset_colors()) cliPrint(reset_colors() + select_byline_prompt + 'Type a new byLine like ' + txtStyle.BRIGHT + '"Robin Hood <*****@*****.**>"') cliPrint(reset_colors() + txtColor.BLACK + ':q Quit/Cancel :x Disable byLines :a Ammend commit') cliPrint('') selected_input = uBelt.getInput(reset_colors() + '-> ').strip() if len(selected_input) == 0 and len(selected_bylines) == 0: continue if selected_input.lower().startswith('git '): continue if selected_input.lower() in ['x', ':x']: repo_hidden_configger.config_class.enabled = False repo_hidden_configger.saveConfigClassToJson() exitGracefully(0) if selected_input.lower() in ['q', ':q', 'c', ':c']: exitGracefully(0) if selected_input.lower() in ['a', ':a', 'y', ':y'] or (len(selected_input) == 0 and len(selected_bylines) >= 1): isFlowActive = False elif selected_input.isdigit(): tag_index = selected_input.strip() try: byline = bylines[int(tag_index) - 1] if byline in selected_bylines: selected_bylines.remove(byline) else: selected_bylines.append(byline) except IndexError: show_warning("number selection didn't match a byLine, please try again or exit") showPressAnyKeyToContinue() elif len(selected_input) <= 4: show_warning('unknown command and assumed too short to be a new byLine') showPressAnyKeyToContinue() else: cleaned_input = selected_input.replace('"', '') bylines.append(cleaned_input) bylines = sorted(bylines) selected_bylines.append(cleaned_input) cliPrint(sm_fish, 0) cliPrint('') # Update config with any newly added byLines did_add_byline = False for byline in bylines: if byline not in repo_visible_configger.config_class.byLines: did_add_byline = True repo_visible_configger.config_class.byLines.append(byline) if did_add_byline: # Only save if a change was actually made repo_visible_configger.saveConfigClassToJson() did_ammend = addByLineToCommit(commit_hash, selected_bylines) if did_ammend is True: repo_hidden_configger.config_class.lastByLines = selected_bylines repo_hidden_configger.saveConfigClassToJson() else: triggerByLineWorkFlow(repo_hidden_configger, commit_hash)
self.json_file_name = '.config.byLines.json' # type: str self.file_path_segments = [ uGit.getGitTopLevelDir(), self.json_file_name ] # type: List[str] self.config_class = RepoConfigClass() # type: RepoConfigClass ########################################################## def isFlagInArgs(flag, args): # type: (str, List[str]) -> bool for arg in args: if arg.lower().strip().startswith(flag): return True return False if __name__ == "__main__": uBelt.log('Python Version:' + sys_version, isVerbose=True) args = sys_argv uBelt.log('ARGS:' + str(args), isVerbose=True) if len(args) >= 2: switch_arg = args[1].lower().strip() uBelt.log('SwitchArg:' + switch_arg, isVerbose=True) if isFlagInArgs('--help', args) or isFlagInArgs('--amend', args) or isFlagInArgs('--dry-run', args) or isFlagInArgs('-z', args) or isFlagInArgs('--null', args): uBelt.log('Not triggering...', isVerbose=True) elif 'commit' == switch_arg: main(COMMAND_COMMIT) else: main(COMMAND_DIRECT)