1+ Import ('env' )
2+ import subprocess
3+ import re
4+
5+ def get_github_repo ():
6+ """Extract GitHub repository name from git remote URL.
7+
8+ Returns:
9+ str: Repository name in 'owner/repo' format for GitHub repos,
10+ 'unknown' for non-GitHub repos, missing git CLI, or any errors.
11+ """
12+ try :
13+ # Get the remote URL for origin
14+ result = subprocess .run (['git' , 'remote' , 'get-url' , 'origin' ],
15+ capture_output = True , text = True , check = True )
16+ remote_url = result .stdout .strip ()
17+
18+ # Check if it's a GitHub URL
19+ if 'github.com' not in remote_url .lower ():
20+ return 'unknown'
21+
22+ # Parse GitHub URL patterns:
23+ # https://github.com/owner/repo.git
24+ # [email protected] :owner/repo.git 25+ # https://github.com/owner/repo
26+
27+ # Remove .git suffix if present
28+ if remote_url .endswith ('.git' ):
29+ remote_url = remote_url [:- 4 ]
30+
31+ # Handle HTTPS URLs
32+ https_match = re .search (r'github\.com/([^/]+/[^/]+)' , remote_url , re .IGNORECASE )
33+ if https_match :
34+ return https_match .group (1 )
35+
36+ # Handle SSH URLs
37+ ssh_match = re .search (r'github\.com:([^/]+/[^/]+)' , remote_url , re .IGNORECASE )
38+ if ssh_match :
39+ return ssh_match .group (1 )
40+
41+ return 'unknown'
42+
43+ except FileNotFoundError :
44+ # Git CLI is not installed or not in PATH
45+ return 'unknown'
46+ except subprocess .CalledProcessError :
47+ # Git command failed (e.g., not a git repo, no remote, etc.)
48+ return 'unknown'
49+ except Exception :
50+ # Any other unexpected error
51+ return 'unknown'
52+
53+ repo = get_github_repo ()
54+ env .Append (BUILD_FLAGS = [f'-DWLED_REPO=\\ "{ repo } \\ "' ])
0 commit comments