Skip to content

Commit 33d79e0

Browse files
authored
Merge pull request wled#4947 from wled/copilot/fix-4946
Fix set_repo.py to detect tracked remote instead of hardcoding 'origin'
2 parents 3410b78 + ed2b170 commit 33d79e0

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

pio-scripts/set_repo.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,38 @@
55
def get_github_repo():
66
"""Extract GitHub repository name from git remote URL.
77
8+
Uses the remote that the current branch tracks, falling back to 'origin'.
9+
This handles cases where repositories have multiple remotes or where the
10+
main remote is not named 'origin'.
11+
812
Returns:
913
str: Repository name in 'owner/repo' format for GitHub repos,
1014
'unknown' for non-GitHub repos, missing git CLI, or any errors.
1115
"""
1216
try:
13-
# Get the remote URL for origin
14-
result = subprocess.run(['git', 'remote', 'get-url', 'origin'],
17+
remote_name = 'origin' # Default fallback
18+
19+
# Try to get the remote for the current branch
20+
try:
21+
# Get current branch name
22+
branch_result = subprocess.run(['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
23+
capture_output=True, text=True, check=True)
24+
current_branch = branch_result.stdout.strip()
25+
26+
# Get the remote for the current branch
27+
remote_result = subprocess.run(['git', 'config', f'branch.{current_branch}.remote'],
28+
capture_output=True, text=True, check=True)
29+
tracked_remote = remote_result.stdout.strip()
30+
31+
# Use the tracked remote if we found one
32+
if tracked_remote:
33+
remote_name = tracked_remote
34+
except subprocess.CalledProcessError:
35+
# If branch config lookup fails, continue with 'origin' as fallback
36+
pass
37+
38+
# Get the remote URL for the determined remote
39+
result = subprocess.run(['git', 'remote', 'get-url', remote_name],
1540
capture_output=True, text=True, check=True)
1641
remote_url = result.stdout.strip()
1742

0 commit comments

Comments
 (0)