initial commit
This commit is contained in:
30
bash/.git-formatter/utils/__init__.py
Normal file
30
bash/.git-formatter/utils/__init__.py
Normal 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
|
24
bash/.git-formatter/utils/branch_details.py
Normal file
24
bash/.git-formatter/utils/branch_details.py
Normal 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
|
36
bash/.git-formatter/utils/format.py
Normal file
36
bash/.git-formatter/utils/format.py
Normal 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])
|
||||
)
|
53
bash/.git-formatter/utils/git_commands.py
Normal file
53
bash/.git-formatter/utils/git_commands.py
Normal 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])
|
14
bash/.git-formatter/utils/messages.py
Normal file
14
bash/.git-formatter/utils/messages.py
Normal 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..."""
|
7
bash/.git-formatter/utils/repo_format.py
Normal file
7
bash/.git-formatter/utils/repo_format.py
Normal 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']
|
11
bash/.git-formatter/utils/repo_name.py
Normal file
11
bash/.git-formatter/utils/repo_name.py
Normal 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)
|
Reference in New Issue
Block a user