initial commit

This commit is contained in:
2025-01-12 19:54:41 -06:00
commit f637529a8d
52 changed files with 2190 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
NBC = {
'branch_format': "BRANCH_TYPE/BRANCH_ID",
'commit_format': "BRANCH_ID",
'types': {
'Feature': 'feature',
'Fix': 'fix',
'Refactor': 'refactor',
'Release': 'release',
'Documentation': 'docs',
}
}
DEFAULT = {
'branch_format': "BRANCH_TYPE(BRANCH_ID)",
'commit_format': "BRANCH_TYPE(BRANCH_ID)",
'types': {
'Feature': 'feat',
'Fix': 'fix',
'Refactor': 'refactor',
'Release': 'release',
'Documentation': 'docs',
}
}
REPO_FORMATS = {
'amped-up.git': NBC,
'nextjs-ramen.git': NBC,
'omega-player.git': DEFAULT,
'WEB.AMP.git': NBC,
'WEB.Phoenix.git': NBC,
'header-footer-service.git': NBC,
'ham-search': DEFAULT,
}
if __name__ == '__main__':
REPO_FORMATS

View File

@@ -0,0 +1,98 @@
import argparse
import sys
from scripts import (
add_worktree,
remove_worktree,
add_commit,
new_branch,
)
def help_menu():
parser = argparse.ArgumentParser(
add_help=False,
description="Git Formatter",
prog="gft",
formatter_class=argparse.RawTextHelpFormatter
)
worktree_group = parser.add_argument_group('Worktree Options')
branch_group = parser.add_argument_group('Branch Options')
commit_group = parser.add_argument_group('Commit Options')
options_group = parser.add_argument_group('General Options')
worktree_group.add_argument(
'-a',
default=False,
metavar="worktree",
nargs='?',
help="Create a git worktree",
)
worktree_group.add_argument(
'-i',
default=False,
metavar="worktree",
nargs='?',
help="Create a git worktree and automatically install npm packages",
)
worktree_group.add_argument(
'-r',
default=False,
metavar="worktree",
nargs='?',
help="Remove a git worktree",
)
branch_group.add_argument(
'-b',
default=False,
metavar="branch",
nargs='?',
help="Create a branch",
)
commit_group.add_argument(
'-m',
default=False,
metavar="message",
nargs='?',
help="Commit changes with a formatted message",
)
options_group.add_argument(
'-h',
'--help',
action='help',
help='Show this help menu'
)
if len(sys.argv) == 1:
parser.print_help()
exit(0)
return parser.parse_args()
def main():
args = help_menu()
if args.a is not False:
add_worktree(args.a)
if args.i is not False:
add_worktree(args.i, True)
if args.r is not False:
remove_worktree(args.r)
if args.b is not False:
new_branch()
if args.m is not False:
add_commit(args.m)
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,8 @@
from .worktree import (
add_worktree,
remove_worktree,
)
from .commit import add_commit
from .branch import (
new_branch,
)

View File

@@ -0,0 +1,9 @@
from utils.branch_details import branch_details
from utils.format import format_branch
from utils.git_commands import git_checkout_branch
def new_branch():
branch_type, branch_id = branch_details()
branch_name = format_branch(branch_type, branch_id)
git_checkout_branch(branch_name)

View File

@@ -0,0 +1,19 @@
from utils.format import format_commit
from utils.git_commands import git_commit
from utils.messages import (
title,
instructions,
commit_instructions,
)
def add_commit(message=''):
commit_title = f"{format_commit()}: "
commit_message = message
if commit_message == '':
title("Commit Message")
instructions(commit_instructions)
commit_message = input(commit_title)
git_commit(f"{commit_title}{commit_message}")

View File

@@ -0,0 +1,60 @@
import os
import subprocess
from .branch import branch_details
from utils.format import format_branch
from utils.git_commands import (
git_add_worktree,
git_remove_worktree,
)
from utils.messages import (
title,
instructions,
worktree_instructions,
)
def add_worktree(worktree=None, auto_install=False):
# Check for bare repo
try:
with open('config') as git_config:
content = ' '.join(git_config.readlines())
if 'bare = true' in content:
# Ask for worktree folder name
if worktree is None:
title('Create Worktree')
instructions(worktree_instructions)
worktree = input('Worktree Directory Name: ')
# Get branch details
branch_type, branch_id = branch_details()
# Format branch name
branch_name = format_branch(
branch_type,
branch_id,
worktree,
)
# Create worktree
git_add_worktree(worktree, branch_name)
# Install NPM packages after worktree creation
if auto_install:
os.chdir(f"./{worktree}")
subprocess.run(['/bin/bash', '-i', '-c', 'nvm use'])
subprocess.run(['npm', 'i'])
exit(0)
except Exception:
print('No worktree found')
exit(1)
def remove_worktree(worktree):
worktree_name = worktree if worktree else input('Worktree to remove: ')
git_remove_worktree(worktree_name)

View File

@@ -0,0 +1,30 @@
from .repo_format import (
branch_format,
branch_types,
commit_format,
repo_format,
)
from .format import (
format_branch,
format_commit,
)
from .git_commands import (
git_add_worktree,
git_branches,
git_checkout_branch,
git_commit,
git_current_branch,
git_remove_worktree,
)
from .messages import (
title,
instructions,
worktree_instructions,
commit_instructions,
)
from .repo_name import repo_name
from .branch_details import branch_details

View File

@@ -0,0 +1,24 @@
from utils.messages import (
title,
instructions,
worktree_instructions,
)
from utils.repo_format import branch_types
def branch_details():
# Ask for branch type
for idx, type in enumerate(branch_types):
print(f"[{idx + 1}] {type}")
title("Branch Types")
instructions(worktree_instructions)
branch_type: str = input('Branch Type: ')
branch_type_idx: int = int(branch_type) - 1
branch_type_value: str = list(branch_types.values())[branch_type_idx]
# Ask for branch id
branch_id: str = input('Branch ID: ').upper()
return branch_type_value, branch_id

View File

@@ -0,0 +1,36 @@
import re
from .git_commands import git_current_branch
from .repo_format import (
branch_format,
commit_format,
)
def format_branch(branch_type, branch_id, worktree_name=''):
if branch_type != '' and branch_id != '':
return (
branch_format
.replace('BRANCH_TYPE', branch_type)
.replace('BRANCH_ID', branch_id)
)
if branch_type == '' and branch_id == '' and worktree_name != '':
return worktree_name
if branch_type == '' and branch_id != '':
return branch_id
return 'Error: Something went wrong'
def format_commit():
branch_list = re.findall(r'([a-zA-Z0-9-]+)', git_current_branch())
if len(branch_list) == 1:
return branch_list[0]
elif len(branch_list) > 1:
return (
commit_format
.replace('BRANCH_TYPE', branch_list[0])
.replace('BRANCH_ID', branch_list[1])
)

View File

@@ -0,0 +1,53 @@
import subprocess
git_branches = subprocess.run(
['git', 'branch'],
stdout=subprocess.PIPE,
text=True,
).stdout.strip()
def git_add_worktree(worktree_name, branch_name, git_branches=git_branches):
# FIXME: This should check for the exact branch name.
# If NGAV-1000 already exists, an error is thrown when
# NGAV-100 tries to be created
existing_branch = branch_name in git_branches
subprocess.run([
'git',
'worktree',
'add',
worktree_name,
'--checkout' if existing_branch else '-b',
branch_name
])
def git_remove_worktree(worktree_name):
subprocess.run([
'git',
'worktree',
'remove',
worktree_name
])
def git_checkout_branch(branch_name):
subprocess.run([
'git',
'checkout',
'-b',
branch_name
])
def git_current_branch():
return subprocess.run(
['git', 'branch', '--show-current'],
stdout=subprocess.PIPE,
text=True,
).stdout
def git_commit(commit_message):
subprocess.run(['git', 'commit', '-m', commit_message])

View File

@@ -0,0 +1,14 @@
def title(text) -> None:
border_char = '='
print(f"\n{text}\n{border_char * len(text)}")
def instructions(text) -> None:
print(f"{text}\n")
worktree_instructions = """Name the worktree directory.
This can be different than your branch name.
"""
commit_instructions = """Some great commit instructions..."""

View File

@@ -0,0 +1,7 @@
from config import REPO_FORMATS
from .repo_name import repo_name
repo_format = REPO_FORMATS[repo_name()]
branch_types = repo_format['types']
branch_format = repo_format['branch_format']
commit_format = repo_format['commit_format']

View File

@@ -0,0 +1,11 @@
import os
from config import REPO_FORMATS
def repo_name() -> str:
for repo in REPO_FORMATS:
if repo in os.getcwd():
return repo
print("Sorry, this repo isn't setup to use Git Formatter")
exit(1)