From c913ec9315c84e9885976619931ef3c7f0b8907e Mon Sep 17 00:00:00 2001 From: Larry Meadors Date: Sun, 8 Aug 2021 12:42:46 -0600 Subject: [PATCH] new: allow renaming master new: allow changing task and release prefixes --- .gittyrc | 3 +++ gitty/gitty_command.py | 31 +++++++++++++------------- gitty/gitty_project_type_maven.py | 16 ++++++------- gitty/gitty_project_type_node.py | 10 ++++----- gitty/gitty_project_type_pip.py | 10 ++++----- gitty/gitty_support.py | 37 +++++++++++++++++++++++++++++++ lifecycle.puml | 12 ++++++++++ 7 files changed, 86 insertions(+), 33 deletions(-) create mode 100644 .gittyrc create mode 100644 lifecycle.puml diff --git a/.gittyrc b/.gittyrc new file mode 100644 index 0000000..2c91d97 --- /dev/null +++ b/.gittyrc @@ -0,0 +1,3 @@ +trunk=master +task_prefix=tasks +release_prefix=releases diff --git a/gitty/gitty_command.py b/gitty/gitty_command.py index 5e54616..2992ef7 100644 --- a/gitty/gitty_command.py +++ b/gitty/gitty_command.py @@ -117,6 +117,7 @@ def bindings_string(self): @staticmethod def add_branch_info_to_context(context, current_branch): + # what is the commit hash if 'git_ref' not in context: # by default, use the current HEAD @@ -134,18 +135,18 @@ def add_branch_info_to_context(context, current_branch): branch_parts = current_branch.split("/") # are we on THE master branch or any other master branch? - the_master = current_branch == 'master' - a_master = branch_parts[-1] == 'master' + the_master = current_branch == context['trunk'] + a_master = branch_parts[-1] == context['trunk'] # are we on a task branch? - a_task = 'tasks' in branch_parts - a_release = 'releases' in branch_parts + a_task = context['task_prefix'] in branch_parts + a_release = context['release_prefix'] in branch_parts if not a_task and not a_release: if the_master: - task_prefix = 'tasks/' + task_prefix = context['task_prefix'] + '/' else: - task_prefix = branch_parts[0] + '/tasks/' + task_prefix = branch_parts[0] + '/' + context['task_prefix'] + '/' else: # we're on a task or release branch - we don't create task branches from those task_prefix = None @@ -333,16 +334,16 @@ def get_version_info(context): if context['is_stable']: if context['a_task'] or context['a_release']: # we're on a task or release branch - the parent is different... - context['parent_version_branch'] = context['branch_parts'][0] + '/master' + context['parent_version_branch'] = context['branch_parts'][0] + '/' + context['trunk'] else: if len(context['current_version_parts']) <= 4: # parent is just master - context['parent_version_branch'] = 'master' + context['parent_version_branch'] = context['trunk'] else: # parent is a shortened version - context['parent_version_branch'] = '.'.join(context['current_version_parts'][:-2]) + '/master' + context['parent_version_branch'] = '.'.join(context['current_version_parts'][:-2]) + '/' + context['trunk'] else: - context['parent_version_branch'] = 'master' + context['parent_version_branch'] = context['trunk'] return context @@ -442,10 +443,10 @@ class GitCheckoutMasterCommand(CommandStep): def describe(self, context): executor = DescribeExecutor() - return context['git_api'].checkout_existing(context, 'master', False, executor) + return context['git_api'].checkout_existing(context, context['trunk'], False, executor) def execute(self, context, quiet): - return context['git_api'].checkout_existing(context, 'master', quiet, None) + return context['git_api'].checkout_existing(context, context['trunk'], quiet, None) class GitShowUnmergedBranchesStep(CommandStep): @@ -484,9 +485,9 @@ def execute(self, context, quiet): branch_name = line.split()[-1] retain_reason = '' - if branch_name.endswith('/master') or branch_name == 'master': - retain_reason = 'master branches are preserved' - if branch_name.endswith('/releases'): + if branch_name.endswith('/'+context['trunk']) or branch_name == context['trunk']: + retain_reason = context['trunk'] + ' branches are preserved' + if branch_name.endswith('/' + context['release_prefix']): retain_reason = 'release branches are preserved' if branch_name == context['current_branch']: retain_reason = 'current branch is preserved' diff --git a/gitty/gitty_project_type_maven.py b/gitty/gitty_project_type_maven.py index 2b2e85a..22129a7 100644 --- a/gitty/gitty_project_type_maven.py +++ b/gitty/gitty_project_type_maven.py @@ -49,19 +49,19 @@ def get_version_info(self, context): else: if context['is_stable']: # we're already on a stabilization branch, so this includes the full version - context['new_stabilization_branch'] = '.'.join(release_version_split) + '/master' - context['new_release_branch'] = '.'.join(release_version_split) + '/releases' + context['new_stabilization_branch'] = '.'.join(release_version_split) + '/' + context['trunk'] + context['new_release_branch'] = '.'.join(release_version_split) + '/' + context['release_prefix'] context['new_stabilization_version'] = '.'.join(release_version_split) + '.0-SNAPSHOT' else: # we're not in a stabilization ecosystem - this will make one... - context['new_stabilization_branch'] = '.'.join(release_version_split[:-1]) + '/master' - context['new_release_branch'] = '.'.join(release_version_split[:-1]) + '/releases' + context['new_stabilization_branch'] = '.'.join(release_version_split[:-1]) + '/' + context['trunk'] + context['new_release_branch'] = '.'.join(release_version_split[:-1]) + '/' + context['release_prefix'] context['new_stabilization_version'] = '.'.join(release_version_split) + '-SNAPSHOT' if context['is_stable']: context['current_release_branch'] = '/'.join([ context['branch_parts'][0], - 'releases' + context['release_prefix'] ]) else: # we're on THE master branch - so there is not a current release branch @@ -79,13 +79,13 @@ def get_version_info(self, context): release_version_split = context['release_version'].split(".") # we're on an actual release branch, so this includes the full version - context['new_release_branch'] = '.'.join(release_version_split) + '/releases' - context['new_stabilization_branch'] = '.'.join(release_version_split) + '/master' + context['new_release_branch'] = '.'.join(release_version_split) + '/' + context['release_prefix'] + context['new_stabilization_branch'] = '.'.join(release_version_split) + '/' + context['trunk'] context['new_stabilization_version'] = '.'.join(release_version_split) + '.0-SNAPSHOT' context['current_release_branch'] = '/'.join([ context['branch_parts'][0], - 'releases' + context['release_prefix'] ]) self.build_next_version_numbers(context, release_version_split) diff --git a/gitty/gitty_project_type_node.py b/gitty/gitty_project_type_node.py index e957856..6097bc8 100644 --- a/gitty/gitty_project_type_node.py +++ b/gitty/gitty_project_type_node.py @@ -30,14 +30,14 @@ def get_version_info(self, context): release_version_split = context['release_version'].split(".") if context['is_stable']: - context['current_release_branch'] = '.'.join(release_version_split[:-1]) + '/releases' + context['current_release_branch'] = '.'.join(release_version_split[:-1]) + '/' + context['release_prefix'] if context['a_task']: context['new_release_branch'] = None context['new_stabilization_branch'] = None context['new_stabilization_version'] = None else: - context['new_release_branch'] = '.'.join(release_version_split) + '/releases' - context['new_stabilization_branch'] = '.'.join(release_version_split) + '/master' + context['new_release_branch'] = '.'.join(release_version_split) + '/' + context['release_prefix'] + context['new_stabilization_branch'] = '.'.join(release_version_split) + '/' + context['trunk'] context['new_stabilization_version'] = '.'.join(release_version_split) + '.0' else: context['current_release_branch'] = None @@ -46,8 +46,8 @@ def get_version_info(self, context): context['new_stabilization_branch'] = None context['new_stabilization_version'] = None else: - context['new_release_branch'] = '.'.join(release_version_split[:-1]) + '/releases' - context['new_stabilization_branch'] = '.'.join(release_version_split[:-1]) + '/master' + context['new_release_branch'] = '.'.join(release_version_split[:-1]) + '/' + context['release_prefix'] + context['new_stabilization_branch'] = '.'.join(release_version_split[:-1]) + '/' + context['trunk'] context['new_stabilization_version'] = '.'.join(release_version_split) # increment the patch diff --git a/gitty/gitty_project_type_pip.py b/gitty/gitty_project_type_pip.py index 8d9ca54..beb3556 100644 --- a/gitty/gitty_project_type_pip.py +++ b/gitty/gitty_project_type_pip.py @@ -38,13 +38,13 @@ def get_version_info(self, context): stable_branch_version = '.'.join(current_version_split[:-2]) if context['is_stable']: # this means we're NOT on THE master branch or a task branch - context['new_stabilization_branch'] = release_version + '/master' - context['new_release_branch'] = release_version + '/releases' + context['new_stabilization_branch'] = release_version + '/' + context['trunk'] + context['new_release_branch'] = release_version + '/' + context['release_prefix'] context['new_stabilization_version'] = release_version + '.0.dev0' else: # this means we're on a branch like 1.0/master - context['new_stabilization_branch'] = stable_branch_version + '/master' - context['new_release_branch'] = stable_branch_version + '/releases' + context['new_stabilization_branch'] = stable_branch_version + '/' + context['trunk'] + context['new_release_branch'] = stable_branch_version + '/' + context['release_prefix'] context['release_version'] = stable_branch_version + '.0' if context['the_master']: @@ -67,7 +67,7 @@ def get_version_info(self, context): context['next_stable_version'] = next_stable_version + '.dev0' if len(context['branch_parts']) > 1: - context['current_release_branch'] = stable_branch_version + '/releases' + context['current_release_branch'] = stable_branch_version + '/' + context['release_prefix'] else: context['current_release_branch'] = None diff --git a/gitty/gitty_support.py b/gitty/gitty_support.py index b5dad61..f21bb2f 100644 --- a/gitty/gitty_support.py +++ b/gitty/gitty_support.py @@ -1,9 +1,46 @@ +import os +from pathlib import Path + from gitty.gitty_command import * import sys def setup(context): + # look for .gittyrc files - they can live in 2 places: + # - the user's home directory + # - the current working directory (CWD) + # if both exist and define overlapping values, the one in the CWD is used + # these are the global defaults + grc_config = { + 'trunk': 'master', + 'task_prefix': 'tasks', + 'release_prefix': 'releases' + } + home_config_location = os.path.join(Path.home(), ".gittyrc") + local_config_location = ".gittyrc" + if os.path.exists(home_config_location): + with open(home_config_location) as f: + for line in f: + (key, val) = line.split("=") + grc_config[key] = val.strip() + + if os.path.exists(local_config_location): + with open(local_config_location) as f: + for line in f: + (key, val) = line.split("=") + grc_config[key] = val.strip() + + # if len(grc_config) == 0: + # print( + # "no config files found at '{}' or '{}', using defaults.".format( + # home_config_location, local_config_location + # ) + # ) + + context.update(grc_config) + # print(context) + # get any CLI switches in the context: # look at any parameter that starts with '--', and if the NEXT parameter doesn't # start with '--', assume it's the value for this parameter diff --git a/lifecycle.puml b/lifecycle.puml new file mode 100644 index 0000000..7f69a77 --- /dev/null +++ b/lifecycle.puml @@ -0,0 +1,12 @@ +@startuml +'https://plantuml.com/sequence-diagram +autonumber +participant gitty +participant setup as "setup()\n(gitty_support.py)" + +gitty -> setup: init config +setup -> command_handler: find and execute\ncommand +command_handler --> setup +setup --> gitty: exit value + +@enduml \ No newline at end of file