diff --git a/library/win_git.ps1 b/library/win_git.ps1 index c34c1b1..c29970c 100644 --- a/library/win_git.ps1 +++ b/library/win_git.ps1 @@ -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