Fix set_repo.py to detect tracked remote instead of hardcoding origin
Co-authored-by: netmindz <442066+netmindz@users.noreply.github.com>
This commit is contained in:
parent
c73636d96d
commit
ed2b170e1b
@ -5,13 +5,38 @@ import re
|
|||||||
def get_github_repo():
|
def get_github_repo():
|
||||||
"""Extract GitHub repository name from git remote URL.
|
"""Extract GitHub repository name from git remote URL.
|
||||||
|
|
||||||
|
Uses the remote that the current branch tracks, falling back to 'origin'.
|
||||||
|
This handles cases where repositories have multiple remotes or where the
|
||||||
|
main remote is not named 'origin'.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
str: Repository name in 'owner/repo' format for GitHub repos,
|
str: Repository name in 'owner/repo' format for GitHub repos,
|
||||||
'unknown' for non-GitHub repos, missing git CLI, or any errors.
|
'unknown' for non-GitHub repos, missing git CLI, or any errors.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# Get the remote URL for origin
|
remote_name = 'origin' # Default fallback
|
||||||
result = subprocess.run(['git', 'remote', 'get-url', 'origin'],
|
|
||||||
|
# Try to get the remote for the current branch
|
||||||
|
try:
|
||||||
|
# Get current branch name
|
||||||
|
branch_result = subprocess.run(['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
|
||||||
|
capture_output=True, text=True, check=True)
|
||||||
|
current_branch = branch_result.stdout.strip()
|
||||||
|
|
||||||
|
# Get the remote for the current branch
|
||||||
|
remote_result = subprocess.run(['git', 'config', f'branch.{current_branch}.remote'],
|
||||||
|
capture_output=True, text=True, check=True)
|
||||||
|
tracked_remote = remote_result.stdout.strip()
|
||||||
|
|
||||||
|
# Use the tracked remote if we found one
|
||||||
|
if tracked_remote:
|
||||||
|
remote_name = tracked_remote
|
||||||
|
except subprocess.CalledProcessError:
|
||||||
|
# If branch config lookup fails, continue with 'origin' as fallback
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Get the remote URL for the determined remote
|
||||||
|
result = subprocess.run(['git', 'remote', 'get-url', remote_name],
|
||||||
capture_output=True, text=True, check=True)
|
capture_output=True, text=True, check=True)
|
||||||
remote_url = result.stdout.strip()
|
remote_url = result.stdout.strip()
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user