Functions/Get-GitStatusPrompt.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
function Get-GitStatusPrompt {
    <#
    .SYNOPSIS
    Returns a git prompt.

    .DESCRIPTION
    Returns a string colored with ANSI escape sequences that contains a git status summary.
    To be used in a custom prompt() function.
    #>

    [CmdletBinding()]
    [OutputType([string])]
    param ()
    $repo = Find-GitRepository
    if ($repo) {
        $promptText += "`e[33m[`e[0m"

        if ($null -eq $repo.Info.WorkingDirectory) {
            $promptText += "`e[2mbare`e[0m"
        } else {
            # current branch
            $branch = $repo.Head
            $branchText = $branch.FriendlyName

            # tracking status
            if ($branch.IsTracking) {
                if ($branch.TrackedBranch.IsGone) {
                    $branchText += " ×"
                } else {
                    if ($branch.TrackingDetails.AheadBy) {
                        $branchText += " ↑" + $branch.TrackingDetails.AheadBy
                    }
                    if ($branch.TrackingDetails.BehindBy) {
                        $branchText += " ↓" + $branch.TrackingDetails.BehindBy
                    }
                    if (-not $branch.TrackingDetails.AheadBy -and -not $branch.TrackingDetails.BehindBy) {
                        $branchText += " ≡"
                    }
                }
            }
            $color = if ($branch.IsTracking -and $branch.TrackingDetails.AheadBy -and $branch.TrackingDetails.BehindBy) {
                "`e[33m"
            } elseif ($branch.IsTracking -and $branch.TrackingDetails.AheadBy) {
                "`e[32m"
            } elseif ($branch.IsTracking -and $branch.TrackingDetails.BehindBy) {
                "`e[31m"
            } else {
                "`e[36m"
            }
            $promptText += "$color$branchText`e[0m"

            # workdir and index status
            $status = Get-GitStatus 6>$null
            if ($status) {
                $counts = @{ }
                $indexIsDirty = $false
                $workDirIsDirty = $false
                foreach ($flag in [Enum]::GetValues([FileStatus])) {
                    $counts[$flag] = 0
                }
                foreach ($file in $status) {
                    foreach ($flag in [Enum]::GetValues([FileStatus])) {
                        if ($file.State.HasFlag($flag)) {
                            $counts[$flag]++
                        }
                    }
                    if ($null -ne $file.IndexChange) {
                        $indexIsDirty = $true
                    } elseif ($null -ne $file.WorkDirChange) {
                        $workDirIsDirty = $true
                    }
                }
                if ($workDirIsDirty) {
                    $promptText += " `e[31m+$($counts[[FileStatus]::NewInWorkdir]) ~$($counts[[FileStatus]::ModifiedInWorkdir]) -$($counts[[FileStatus]::DeletedFromWorkdir])`e[0m"
                    if ($counts[[FileStatus]::Conflicted]) {
                        $promptText += " !$($counts[[FileStatus]::Conflicted])"
                    }
                }
                if ($indexIsDirty) {
                    if ($workDirIsDirty) {
                        $promptText += " `e[33m|`e[0m"
                    }
                    $promptText += " `e[32m+$($counts[[FileStatus]::NewInIndex]) ~$($counts[[FileStatus]::ModifiedInIndex]) -$($counts[[FileStatus]::DeletedFromIndex])`e[0m"
                }
            }
        }

        $repo.Dispose()

        $promptText += "`e[33m]`e[0m"
        $promptText
    }
}