-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
27 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
import subprocess | ||
import re | ||
|
||
from dataclasses import dataclass | ||
|
||
# Regex designed to match [email protected]:gizmo385/lazy-github.git: | ||
# ".+:" Match everything to the first colon | ||
# "([^\/]+)" Match everything until the forward slash, which should be owner | ||
|
@@ -12,21 +10,24 @@ | |
_GIT_REMOTE_REGEX = re.compile(r".+:([^\/]+)\/([^.]+).git") | ||
|
||
|
||
@dataclass | ||
class LocalGitRepo: | ||
owner: str | ||
name: str | ||
|
||
|
||
def current_directory_git_repo_remote_owner(remote: str = "origin") -> LocalGitRepo | None: | ||
""" | ||
Returns the name and owner associated with the remote of the git repo in the current working directory. | ||
""" | ||
def current_local_repo_full_name(remote: str = "origin") -> str | None: | ||
"""Returns the owner/name associated with the remote of the git repo in the current working directory.""" | ||
try: | ||
output = subprocess.check_output(["git", "remote", "get-url", remote]).decode().strip() | ||
except subprocess.SubprocessError: | ||
return None | ||
|
||
if matches := re.match(_GIT_REMOTE_REGEX, output): | ||
owner, name = matches.groups() | ||
return LocalGitRepo(owner, name) | ||
return f"{owner}/{name}" | ||
|
||
|
||
def current_local_branch_name() -> str | None: | ||
"""Returns the name of the current branch for the git repo in the current working directory.""" | ||
try: | ||
return subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).decode().strip() | ||
except subprocess.SubprocessError: | ||
return None | ||
|
||
|
||
print(current_local_branch_name()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters