refactor: pull git wrapper together
This commit is contained in:
parent
d420b1dbbe
commit
635e1423d3
3 changed files with 36 additions and 10 deletions
|
@ -6,10 +6,11 @@ invocation environment (i.e. git context, ...) to make things work.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import subprocess
|
|
||||||
|
|
||||||
import pydantic
|
import pydantic
|
||||||
|
|
||||||
|
import frg.git as git
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@ -56,15 +57,8 @@ def get_git_context(*, domain_aliases: dict[str, str] | None = None) -> GitConte
|
||||||
if not domain_aliases:
|
if not domain_aliases:
|
||||||
domain_aliases = dict()
|
domain_aliases = dict()
|
||||||
|
|
||||||
current_branch_cmd = subprocess.run(
|
current_branch = git.get_current_branch().stdout
|
||||||
["git", "branch", "--show-current"], capture_output=True
|
remote_url = git.get_current_remote_url().stdout
|
||||||
)
|
|
||||||
current_branch = current_branch_cmd.stdout.decode("utf8").strip()
|
|
||||||
|
|
||||||
remote_url_cmd = subprocess.run(
|
|
||||||
["git", "config", "--get", "remote.origin.url"], capture_output=True
|
|
||||||
)
|
|
||||||
remote_url = remote_url_cmd.stdout.decode("utf8").strip()
|
|
||||||
|
|
||||||
host, owner, repo = parse_remote_url(remote_url)
|
host, owner, repo = parse_remote_url(remote_url)
|
||||||
|
|
||||||
|
|
29
frg/git.py
Normal file
29
frg/git.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
import pydantic
|
||||||
|
|
||||||
|
|
||||||
|
class CommandResult(pydantic.BaseModel):
|
||||||
|
return_code: int
|
||||||
|
stdout: str
|
||||||
|
stderr: str
|
||||||
|
|
||||||
|
|
||||||
|
def _git(args: list[str]) -> CommandResult:
|
||||||
|
result = subprocess.run(["git", *args], capture_output=True)
|
||||||
|
|
||||||
|
return CommandResult(
|
||||||
|
stdout=result.stdout.decode("utf8").strip(),
|
||||||
|
stderr=result.stderr.decode("utf8").strip(),
|
||||||
|
return_code=result.returncode,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_current_branch() -> CommandResult:
|
||||||
|
"""Returns the current checked out branch."""
|
||||||
|
return _git(["branch", "--show-current"])
|
||||||
|
|
||||||
|
|
||||||
|
def get_current_remote_url() -> CommandResult:
|
||||||
|
"""Returns the remote origin url."""
|
||||||
|
return _git(["config", "--get", "remote.origin.url"])
|
|
@ -28,6 +28,9 @@ def mock_subprocess(mock_context):
|
||||||
"utf8"
|
"utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return_val.stderr = b""
|
||||||
|
return_val.returncode = 0
|
||||||
|
|
||||||
return return_val
|
return return_val
|
||||||
|
|
||||||
with unittest.mock.patch("subprocess.run") as mock_run:
|
with unittest.mock.patch("subprocess.run") as mock_run:
|
||||||
|
|
Loading…
Reference in a new issue