diff --git a/scripts/Set-GitRemotes.ps1 b/scripts/Set-GitRemotes.ps1 index f0684f2..c166009 100644 --- a/scripts/Set-GitRemotes.ps1 +++ b/scripts/Set-GitRemotes.ps1 @@ -29,9 +29,26 @@ function Test-GitRepo { } } +function Get-RepoNameFromRemoteUrl { + param([string]$Url) + if ($Url -match '/([^/]+?)(?:\.git)?/?$') { + return $Matches[1] + } + return $null +} + function Get-RepoName { param([string]$Name) if ($Name) { return $Name } + + foreach ($remote in @('kalinamall', 'home', 'github', 'origin')) { + $url = git remote get-url $remote 2>$null + if ($url) { + $fromUrl = Get-RepoNameFromRemoteUrl -Url $url.Trim() + if ($fromUrl) { return $fromUrl } + } + } + return (Split-Path -Leaf (git rev-parse --show-toplevel)) } diff --git a/scripts/Set-GitRemotes.sh b/scripts/Set-GitRemotes.sh index 1a03a04..7492b4b 100644 --- a/scripts/Set-GitRemotes.sh +++ b/scripts/Set-GitRemotes.sh @@ -15,7 +15,23 @@ if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then exit 1 fi -repo_name="${REPO:-$(basename "$(git rev-parse --show-toplevel)")}" +repo_name_from_url() { + local url="$1" + if [[ "$url" =~ /([^/]+)(\.git)?/?$ ]]; then + echo "${BASH_REMATCH[1]}" + fi +} + +repo_name="${REPO:-}" +if [[ -z "$repo_name" ]]; then + for remote in kalinamall home github origin; do + if url="$(git remote get-url "$remote" 2>/dev/null)"; then + repo_name="$(repo_name_from_url "$url")" + [[ -n "$repo_name" ]] && break + fi + done +fi +repo_name="${repo_name:-$(basename "$(git rev-parse --show-toplevel)")}" home_url="ssh://git@git.papatramp.ru:2222/${OWNER}/${repo_name}.git" kalinamall_url="ssh://git@git.kalinamall.ru:2222/${OWNER}/${repo_name}.git" github_url="git@github.com:${GITHUB_ORG}/${repo_name}.git"