Public/Set-BranchComment.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
<#
    .SYNOPSIS
        Sets a comment for the current branch replacing any existing one. Can be read by Get-BranchComment.
 
    .PARAMETER Comment
        The comment to store.
 
    .PARAMETER Branch
        The branch for which to store the comment. By default the current branch is used.
#>

function Set-BranchComment() {
    param(
        [Parameter(Mandatory, Position=0)]
        [string]$Comment,
        [Parameter(Position=1)]
        [ValidateSet([AllBranchesValuesGenerator])]
        [string]$Branch = 'HEAD'
    )
    # git branch --edit-description can be used, but opens a text editor.
    $branchName = Invoke-NativeCommand git rev-parse --abbrev-ref $Branch
    $configValue = "branch.$branchName.description"
    Invoke-NativeCommand git config $configValue $Comment
}

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