Functions/Send-GitObject.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function Send-GitObject {
    <#
    .SYNOPSIS
    Sends Git refs and object to a remote repository.

    .DESCRIPTION
    The `Send-GitObject` functions sends objects from a local repository to a remote repository. You specify what refs and objects to send with the `Revision` parameter.

    This command implements the `git push` command.

    .EXAMPLE
    Send-GitObject -Revision 'refs/heads/master'

    Demonstrates how to push the commits on a specific branch to the default remote repository.

    .EXAMPLE
    Send-GitObject -Revision 'refs/heads/master' -RemoteName 'upstream'

    Demonstrates how to push an object (in this case, the master branch) to a specific remote repository, in this case the remote named "upstream".

    .EXAMPLE
    Send-GitObject -Revision 'refs/tags/4.45.6'

    Demonstrates how to push a tag to the default remote repository.

    .EXAMPLE
    Send-GitObject -Tags

    Demonstrates how to push all tags to the default remote repository.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, ParameterSetName = 'ByRefSpec')]
        [string[]]
        # The refs that should be pushed to the remote repository.
        $RefSpec,

        [Parameter(Mandatory = $true, ParameterSetname = 'Tags')]
        [Switch]
        # Push all tags to the remote repository.
        $Tags,

        [string]
        # The name of the remote repository to send the changes to. The default is `origin`.
        $RemoteName = 'origin',

        [string]
        # The path to the local repository from which to push changes. Defaults to the current directory.
        $RepoRoot = (Get-Location).ProviderPath,

        [pscredential]
        # The credentials to use to connect to the source repository.
        $Credential
    )

    Set-StrictMode -Version 'Latest'
    Use-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState

    $repo = Find-GitRepository -Path $RepoRoot -Verify

    $pushOptions = New-Object -TypeName 'LibGit2Sharp.PushOptions'
    if ( $Credential ) {
        $gitCredential = New-Object -TypeName 'LibGit2Sharp.SecureUsernamePasswordCredentials'
        $gitCredential.Username = $Credential.UserName
        $gitCredential.Password = $Credential.Password
        $pushOptions.CredentialsProvider = { return $gitCredential }
    }

    $remote = $repo.Network.Remotes | Where-Object { $_.Name -eq $RemoteName }
    if ( -not $remote ) {
        Write-Error -Message ('A remote named "{0}" does not exist.' -f $RemoteName)
        return [PowerGit.PushResult]::Failed
    }

    if ( $Tags ) {
        $RefSpec = $repo.Tags | ForEach-Object { $_.CanonicalName }
    }

    try {
        $repo.Network.Push($remote, $RefSpec, $pushOptions)
        return [PowerGit.PushResult]::Ok
    } catch {
        Write-Error -ErrorRecord $_

        switch ( $_.FullyQualifiedErrorId ) {
            'NonFastForwardException' { return [PowerGit.PushResult]::Rejected }
            'LibGit2SharpException' { return [PowerGit.PushResult]::Failed }
            'BareRepositoryException' { return [PowerGit.PushResult]::Failed }
            default { return [PowerGit.PushResult]::Failed }
        }
    } finally {
        $repo.Dispose()
    }
}