Public/Set-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
37
38
39
40
41
<#
    .SYNOPSIS
        Checks out the latest version of the specified branch. Avoids unnecessary churning by
        checking out the latest version immediately instead of doing a checkout followed by pull
        when possible.
 
    .PARAMETER Branch
        Name of the branch to checkout.
#>

function Set-Branch() {
    param(
        [Parameter(Mandatory, Position=0)]
        [ValidateSet([LocalBranchesValuesGenerator])]
        [string]$Branch
    )
    process {
        git show-ref --verify --quiet "refs/heads/$Branch"
        $branchExists = $LastExitCode -eq 0
        if ($branchExists) {
            $currentBranchName = Invoke-NativeCommand git rev-parse --abbrev-ref HEAD
            if ($currentBranchName -eq $Branch) {
                Invoke-NativeCommand git pull
            }
            else {
                Invoke-NativeCommand git fetch origin "${Branch}:${Branch}"
                Invoke-NativeCommand git checkout $Branch
            }
        }
        else {
            Invoke-NativeCommand git checkout $Branch
            Invoke-NativeCommand git pull
        }
    }
}

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