Public/Merge-Branch.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
<#
    .SYNOPSIS
        Merges the specified branch.
 
    .PARAMETER Branch
            The name of the branch that should be merged. By default the main branch is used.
 
    .PARAMETER Rebase
        If true a rebase is done instead of merging.
#>

function Merge-Branch() {
    param(
        [Parameter(Position=0)]
        [ValidateSet([BranchesValuesGenerator])]
        [string]$Branch = (Get-MainBranch),
        [Parameter(Position=1)]
        [switch]$Rebase
    )
    process {
        Invoke-NativeCommand git fetch
        if ($Rebase) {
            Invoke-NativeCommand git rebase "origin/$Branch"
            Invoke-NativeCommand git push --force-with-lease
        }
        else {
            Invoke-NativeCommand git merge "origin/$Branch"
        }
    }
}

class BranchesValuesGenerator : System.Management.Automation.IValidateSetValuesGenerator {
    [string[]] GetValidValues() {
        return Get-Branches
    }
}