Functions/Add-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
function Add-GitRemote {
    <#
    .INPUTS
    PSGitHub.Repository. You can pipe the output of Get-GitHubRepository, New-GitHubRepository or Start-GitHubFork.
    #>

    [CmdletBinding()]
    [OutputType([LibGit2Sharp.Remote])]
    param (
        [string] $RepoRoot,

        # Name of the new remote
        [Parameter(Mandatory, Position = 0, ParameterSetName = 'Parameters')]
        [ValidateNotNullOrEmpty()]
        [string] $Name,

        # URL for the new remote
        [Parameter(Mandatory, Position = 1, ParameterSetName = 'Parameters')]
        [ValidateNotNullOrEmpty()]
        [string] $Url,

        [Parameter(Mandatory, ValueFromPipeline, DontShow, ParameterSetName = 'Input')]
        [psobject] $Input
    )

    process {
        # PSGitHub support
        if ($PSCmdlet.ParameterSetName -eq 'Input') {
            $Name = $Input.Owner
            $Url = $Input.CloneUrl
        }
        $repo = Find-GitRepository -Verify
        if (-not $repo) {
            return
        }
        try {
            $repo.Network.Remotes.Add($Name, $Url)
        } catch {
            Write-Error -Exception $_.Exception
        }
    }
}