132 lines
3.6 KiB
Python
132 lines
3.6 KiB
Python
import subprocess
|
|
|
|
from lib.io import *
|
|
|
|
FLOWCONFIG_FILE = ".flowconfig"
|
|
CHANGELOG_FILE = "CHANGELOG.md"
|
|
UPDATED_CHANGELOG_MSG = "Updated " + CHANGELOG_FILE
|
|
|
|
|
|
class Status:
|
|
worktree: str
|
|
index: str
|
|
file: str
|
|
|
|
def __init__(self, line: str) -> None:
|
|
self.worktree = line[0]
|
|
self.index = line[1]
|
|
self.file = line[3:].strip("\n")
|
|
|
|
class Git:
|
|
FLOWCONFIG_FILE = ".flowconfig"
|
|
|
|
command: list[str]
|
|
check_returncode: bool = True
|
|
|
|
@staticmethod
|
|
def flow_config(*args, **kwargs):
|
|
return Git("config", *args, file=FLOWCONFIG_FILE, **kwargs)
|
|
|
|
@staticmethod
|
|
def get_current_branch():
|
|
return Git("rev-parse", "HEAD", abbrev_ref=True).firstline()
|
|
|
|
@staticmethod
|
|
def get_branches():
|
|
return Git._get_references("heads")
|
|
|
|
@staticmethod
|
|
def status():
|
|
return list(map(Status, Git("status", porcelain=True).lines(False)))
|
|
|
|
@staticmethod
|
|
def _get_references(kind: str):
|
|
prefix = "refs/" + kind + "/"
|
|
return map(
|
|
lambda r: r.removeprefix(prefix),
|
|
filter(
|
|
lambda l: l.startswith(prefix),
|
|
Git("for-each-ref", format="%(refname)").lines()
|
|
)
|
|
)
|
|
|
|
def __init__(self, subcommand: str, *args: str, **kwargs: str | int | bool) -> None:
|
|
self.command = ["git", subcommand]
|
|
|
|
for option, value in kwargs.items():
|
|
option = option.replace("_", "-")
|
|
|
|
if isinstance(value, bool):
|
|
if len(option) == 1:
|
|
self.command.append("-" + option)
|
|
else:
|
|
prefix = "--" if value else "--no-"
|
|
self.command.append(prefix + option)
|
|
else:
|
|
value = value if isinstance(value, str) else str(value)
|
|
|
|
if len(option) == 1:
|
|
self.command.append("-" + option)
|
|
self.command.append(value)
|
|
else:
|
|
self.command.append("--" + option + '=' + value)
|
|
|
|
self.command += args
|
|
|
|
def lines(self, strip: bool = True):
|
|
process = subprocess.run(self.command, capture_output=True, text=True)
|
|
|
|
self.__print_process(process.stdout, process.stderr)
|
|
|
|
if self.check_returncode:
|
|
process.check_returncode()
|
|
|
|
lines = process.stdout.splitlines()
|
|
|
|
return lines if not strip else list(map(lambda l: l.strip(), lines))
|
|
|
|
def firstline(self):
|
|
lines = self.lines()
|
|
|
|
return lines[0] if lines else ""
|
|
|
|
def exec(self):
|
|
process = subprocess.run(self.command, capture_output=True, text=True)
|
|
|
|
self.__print_process(process.stdout, process.stderr)
|
|
|
|
if self.check_returncode:
|
|
process.check_returncode()
|
|
|
|
def code(self):
|
|
process = subprocess.run(self.command, capture_output=True, text=True)
|
|
|
|
self.__print_process(process.stdout, process.stderr)
|
|
|
|
return process.returncode
|
|
|
|
def without_checking(self):
|
|
self.check_returncode = False
|
|
|
|
return self
|
|
|
|
def __print_process(self, stdout: str, stderr: str):
|
|
command = []
|
|
|
|
for arg in self.command:
|
|
if arg.startswith("--") and "=" in arg:
|
|
pos = arg.index("=")
|
|
arg = arg[:pos+1] + '"' + arg[pos+1:] + '"'
|
|
elif " " in arg:
|
|
arg = '"' + arg + '"'
|
|
|
|
command.append(arg)
|
|
|
|
print(COLOR_YELLOW + "> " + " ".join(command) + COLOR_RESET)
|
|
|
|
for line in stdout.splitlines():
|
|
print(COLOR_BLUE + "< " + line + COLOR_RESET)
|
|
|
|
for line in stderr.splitlines():
|
|
print(COLOR_RED + "! " + line + COLOR_RESET)
|