84 lines
2.0 KiB
PowerShell
84 lines
2.0 KiB
PowerShell
# detect-platform.ps1 - Detect git platform (Gitea or GitHub) for current repo
|
|
# Usage: . .\detect-platform.ps1; Get-GitPlatform
|
|
# or: .\detect-platform.ps1 (prints platform name)
|
|
|
|
function Get-GitPlatform {
|
|
[CmdletBinding()]
|
|
param()
|
|
|
|
$remoteUrl = git remote get-url origin 2>$null
|
|
|
|
if ([string]::IsNullOrEmpty($remoteUrl)) {
|
|
Write-Error "Not a git repository or no origin remote"
|
|
return $null
|
|
}
|
|
|
|
# Check for GitHub
|
|
if ($remoteUrl -match "github\.com") {
|
|
return "github"
|
|
}
|
|
|
|
# Check for common Gitea indicators
|
|
# Gitea URLs typically don't contain github.com, gitlab.com, bitbucket.org
|
|
if ($remoteUrl -notmatch "gitlab\.com" -and $remoteUrl -notmatch "bitbucket\.org") {
|
|
# Assume Gitea for self-hosted repos
|
|
return "gitea"
|
|
}
|
|
|
|
return "unknown"
|
|
}
|
|
|
|
function Get-GitRepoInfo {
|
|
[CmdletBinding()]
|
|
param()
|
|
|
|
$remoteUrl = git remote get-url origin 2>$null
|
|
|
|
if ([string]::IsNullOrEmpty($remoteUrl)) {
|
|
Write-Error "Not a git repository or no origin remote"
|
|
return $null
|
|
}
|
|
|
|
# Extract owner/repo from URL
|
|
# Handles: git@host:owner/repo.git, https://host/owner/repo.git, https://host/owner/repo
|
|
$repoPath = $remoteUrl
|
|
if ($remoteUrl -match "^git@") {
|
|
$repoPath = ($remoteUrl -split ":")[1]
|
|
} else {
|
|
# Remove protocol and host
|
|
$repoPath = $remoteUrl -replace "^https?://[^/]+/", ""
|
|
}
|
|
|
|
# Remove .git suffix if present
|
|
$repoPath = $repoPath -replace "\.git$", ""
|
|
|
|
return $repoPath
|
|
}
|
|
|
|
function Get-GitRepoOwner {
|
|
[CmdletBinding()]
|
|
param()
|
|
|
|
$repoInfo = Get-GitRepoInfo
|
|
if ($repoInfo) {
|
|
return ($repoInfo -split "/")[0]
|
|
}
|
|
return $null
|
|
}
|
|
|
|
function Get-GitRepoName {
|
|
[CmdletBinding()]
|
|
param()
|
|
|
|
$repoInfo = Get-GitRepoInfo
|
|
if ($repoInfo) {
|
|
return ($repoInfo -split "/")[-1]
|
|
}
|
|
return $null
|
|
}
|
|
|
|
# If script is run directly (not dot-sourced), output the platform
|
|
if ($MyInvocation.InvocationName -ne ".") {
|
|
Get-GitPlatform
|
|
}
|