Functions/Update-GitRemote.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function Update-GitRemote {
    [CmdletBinding()]
    [OutputType([LibGit2Sharp.Remote])]
    param (
        [string] $RepoRoot,

        [Parameter(Mandatory, Position = 0, ValueFromPipelineByPropertyName)]
        [string] $Name,

        # Pipe in PSGitHub.Repository
        [Parameter(ValueFromPipelineByPropertyName, DontShow)]
        [string] $CloneUrl,

        [string] $Url,

        # Update the remote HEAD (also known as default branch).
        [Alias('DefaultBranch')]
        [string] $Head,

        [string] $PushUrl,
        [LibGit2Sharp.TagFetchMode] $TagFetchMode
    )

    process {
        $repo = Find-GitRepository -Verify
        if (-not $repo) {
            return
        }
        $outerParams = $PSBoundParameters
        if ($Url -or $CloneUrl -or $PushUrl -or $TagFetchMode) {
            $repo.Network.Remotes.Update($Name, @( {
                param([LibGit2Sharp.RemoteUpdater] $updater)
                if ($outerParams.ContainsKey('Url')) {
                    $updater.Url = $Url
                }
                if ($outerParams.ContainsKey('CloneUrl')) {
                    $updater.Url = $CloneUrl
                }
                if ($outerParams.ContainsKey('PushUrl')) {
                    $updater.PushUrl = $PushUrl
                }
                if ($outerParams.ContainsKey('TagFetchMode')) {
                    $updater.TagFetchMode = $TagFetchMode
                }
            }))
        }

        if ($Head) {
            $target = if ($Head.StartsWith('refs/')) {
                $repo.Refs[$Head]
            } elseif ($Head.StartsWith("$Name/")) {
                $repo.Refs["refs/remotes/$Head"]
            } else {
                $repo.Refs["refs/remotes/$Name/$Head"]
            }
            $repo.Refs.UpdateTarget($repo.Refs["refs/remotes/$Name/HEAD"], $target) | Out-Null
        }

        $repo.Network.Remotes[$Name]
    }
}