Functions/Send-GitBranch.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
# 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-GitBranch {
    <#
    .SYNOPSIS
    Pushes commits from the current git branch to its remote repository.

    .DESCRIPTION
    The `Send-GitBranch` function sends all commits on the current branch of the
    local Git repository to its upstream remote repository. If you are pushing a
    new branch, use the `SetUpstream` switch to ensure Git tracks the new remote
    branch as a copy of the local branch.

    If the repository requires authentication, pass the username/password via
    the `Credential` parameter.

    This function implements the `git push` command.

    .EXAMPLE
    Send-GitBranch

    Pushes commits from the repository at the current location to its upstream
    remote repository

    .EXAMPLE
    Send-GitBranch -RepoRoot 'C:\Build\TestGitRepo' -Credential $PsCredential

    Pushes commits from the repository located at 'C:\Build\TestGitRepo' to its
    remote using authentication
    #>

    [CmdletBinding()]
    [OutputType([LibGit2Sharp.Branch])]
    param(
        [Parameter(ValueFromPipeline)]
        [LibGit2Sharp.Branch] $InputObject,

        # The remote to push the branch to
        [Parameter(Position = 0)]
        [string] $Remote,

        # The name of the branch to push
        [Parameter(Position = 1)]
        [string] $Name,

        # Specifies the location of the repository to synchronize. Defaults to the current directory.
        [string] $RepoRoot = (Get-Location).ProviderPath,

        # Add tracking information for any new branches pushed so Git sees the local branch and remote branch as the same.
        [Alias('u')]
        [switch] $SetUpstream,

        # Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it.
        # This flag disables this check by prefixing all refspecs with "+".
        [switch] $Force,

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

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

        if (-not $InputObject) {
            $InputObject = if ($Name) {
                Get-GitBranch -RepoRoot $RepoRoot -Name $Name
            } else {
                Get-GitHead -RepoRoot $RepoRoot
            }
        }
        $sendParams = @{
            RepoRoot = $RepoRoot
            SetUpstream = [bool]$SetUpstream
            Force = [bool]$Force
        }
        if ($Credential) {
            $sendParams.Credential = $Credential
        }
        if ($Remote) {
            $sendParams.Remote = $Remote
        }
        if (-not $InputObject) {
            Write-Warning "No git branch matching $Name"
            return
        }
        $InputObject | Send-GitObject @sendParams
    }
}