|
5 | 5 | def get_github_repo(): |
6 | 6 | """Extract GitHub repository name from git remote URL. |
7 | 7 | |
| 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 | + |
8 | 12 | Returns: |
9 | 13 | str: Repository name in 'owner/repo' format for GitHub repos, |
10 | 14 | 'unknown' for non-GitHub repos, missing git CLI, or any errors. |
11 | 15 | """ |
12 | 16 | 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], |
15 | 40 | capture_output=True, text=True, check=True) |
16 | 41 | remote_url = result.stdout.strip() |
17 | 42 |
|
|
0 commit comments