Update win_git to update remote URL if different

This commit is contained in:
Kenneth Benzie 2024-08-09 16:07:55 +01:00
parent 5f9de82ca3
commit 567ed28def

View File

@ -108,6 +108,16 @@ function Get-GitCurrentSha {
return $result.stdout.Trim()
}
function Get-GitRemoteUrl {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)] [String] $dest,
[Parameter(Mandatory = $true)] [String] $remote
)
$result = Run-Command -command "`"$git`" remote get-url $remote" -working_directory $dest
return $result.stdout.Trim()
}
function Invoke-GitClone {
[CmdletBinding()]
Param (
@ -187,6 +197,19 @@ function Invoke-GitSubmoduleUpdate {
}
}
function Invoke-GitRemoteSetUrl {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)] [String] $dest,
[Parameter(Mandatory = $true)] [String] $remote,
[Parameter(Mandatory = $true)] [String] $url
)
$result = Run-Command -command "`"$git`" remote set-url $remote `"$url`"" -working_directory $dest
if ($result.rc -ne 0) {
$module.FailJson("Failed to set remote URL:`n" + $result.stderr)
}
}
# ================================ Start logic =================================
if (!$dest) {
@ -207,6 +230,10 @@ if (($dest -and ![System.IO.File]::Exists($gitconfig))) {
if (Test-GitLocalChanges $dest) {
$module.FailJson('Local modifications exist in repository.')
}
$url = Get-GitRemoteUrl $dest $remote
if ($url -ne $repo) {
Invoke-GitRemoteSetUrl $dest $remote $repo
}
Invoke-GitFetch $dest $remote $version
Invoke-GitCheckout $dest $remote $version
Invoke-GitPull $dest $remote $version