Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 162 lines (148 sloc) 6.618 kb
3033fa5 @dahlbyk Rough start
authored
1 # Inspired by Mark Embling
2 # http://www.markembling.info/view/my-ideal-powershell-prompt-with-git-integration
3
0cf809e Improved Get-GitBranch
unknown authored
4 function Get-GitDirectory {
2a209ff @dahlbyk Change Test-LocalOrParentPath to Get-LocalOrParentPath, which fixes b…
authored
5 Get-LocalOrParentPath .git
3033fa5 @dahlbyk Rough start
authored
6 }
7
c00a37a @dahlbyk Add timing to debug statements
authored
8 function Get-GitBranch($gitDir = $(Get-GitDirectory), [Diagnostics.Stopwatch]$sw) {
0cf809e Improved Get-GitBranch
unknown authored
9 if ($gitDir) {
c00a37a @dahlbyk Add timing to debug statements
authored
10 dbg 'Finding branch' $sw
7ae4298 @dahlbyk Fix some minor bugs in Get-GitStatus
authored
11 $r = ''; $b = ''; $c = ''
0cf809e Improved Get-GitBranch
unknown authored
12 if (Test-Path $gitDir\rebase-merge\interactive) {
c00a37a @dahlbyk Add timing to debug statements
authored
13 dbg 'Found rebase-merge\interactive' $sw
0cf809e Improved Get-GitBranch
unknown authored
14 $r = '|REBASE-i'
15 $b = "$(Get-Content $gitDir\rebase-merge\head-name)"
16 } elseif (Test-Path $gitDir\rebase-merge) {
c00a37a @dahlbyk Add timing to debug statements
authored
17 dbg 'Found rebase-merge' $sw
0cf809e Improved Get-GitBranch
unknown authored
18 $r = '|REBASE-m'
19 $b = "$(Get-Content $gitDir\rebase-merge\head-name)"
20 } else {
21 if (Test-Path $gitDir\rebase-apply) {
c00a37a @dahlbyk Add timing to debug statements
authored
22 dbg 'Found rebase-apply' $sw
0cf809e Improved Get-GitBranch
unknown authored
23 if (Test-Path $gitDir\rebase-apply\rebasing) {
c00a37a @dahlbyk Add timing to debug statements
authored
24 dbg 'Found rebase-apply\rebasing' $sw
0cf809e Improved Get-GitBranch
unknown authored
25 $r = '|REBASE'
26 } elseif (Test-Path $gitDir\rebase-apply\applying) {
c00a37a @dahlbyk Add timing to debug statements
authored
27 dbg 'Found rebase-apply\applying' $sw
0cf809e Improved Get-GitBranch
unknown authored
28 $r = '|AM'
29 } else {
c00a37a @dahlbyk Add timing to debug statements
authored
30 dbg 'Found rebase-apply' $sw
0cf809e Improved Get-GitBranch
unknown authored
31 $r = '|AM/REBASE'
32 }
33 } elseif (Test-Path $gitDir\MERGE_HEAD) {
c00a37a @dahlbyk Add timing to debug statements
authored
34 dbg 'Found MERGE_HEAD' $sw
8750015 @dahlbyk Minor fixes
authored
35 $r = '|MERGING'
0cf809e Improved Get-GitBranch
unknown authored
36 } elseif (Test-Path $gitDir\BISECT_LOG) {
c00a37a @dahlbyk Add timing to debug statements
authored
37 dbg 'Found BISECT_LOG' $sw
8750015 @dahlbyk Minor fixes
authored
38 $r = '|BISECTING'
0cf809e Improved Get-GitBranch
unknown authored
39 }
40
c00a37a @dahlbyk Add timing to debug statements
authored
41 $b = ?? { dbg 'Trying symbolic-ref' $sw; git symbolic-ref HEAD 2>$null } `
0cf809e Improved Get-GitBranch
unknown authored
42 { "($(
43 Coalesce-Args `
c00a37a @dahlbyk Add timing to debug statements
authored
44 { dbg 'Trying describe' $sw; git describe --exact-match HEAD 2>$null } `
0cf809e Improved Get-GitBranch
unknown authored
45 {
c00a37a @dahlbyk Add timing to debug statements
authored
46 dbg 'Falling back on SHA' $sw
0cf809e Improved Get-GitBranch
unknown authored
47 $ref = Get-Content $gitDir\HEAD 2>$null
48 if ($ref -and $ref.Length -ge 7) {
49 return $ref.Substring(0,7)+'...'
50 } else {
51 return $null
52 }
53 } `
54 'unknown'
55 ))" }
56 }
57
58 if ('true' -eq $(git rev-parse --is-inside-git-dir 2>$null)) {
c00a37a @dahlbyk Add timing to debug statements
authored
59 dbg 'Inside git directory' $sw
0cf809e Improved Get-GitBranch
unknown authored
60 if ('true' -eq $(git rev-parse --is-bare-repository 2>$null)) {
61 $c = 'BARE:'
62 } else {
63 $b = 'GIT_DIR!'
64 }
65 }
66
67 "$c$($b -replace 'refs/heads/','')$r"
68 }
3033fa5 @dahlbyk Rough start
authored
69 }
70
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
71 function Get-GitStatus($gitDir = (Get-GitDirectory)) {
ecce857 @dahlbyk Move affect of $GitPromptSettings.EnablePromptStatus up into Get-GitS…
authored
72 $settings = $Global:GitPromptSettings
73 $enabled = (-not $settings) -or $settings.EnablePromptStatus
74 if ($enabled -and $gitDir)
3033fa5 @dahlbyk Rough start
authored
75 {
c00a37a @dahlbyk Add timing to debug statements
authored
76 $sw = [Diagnostics.Stopwatch]::StartNew()
77 $branch = Get-GitBranch $gitDir $sw
78 dbg 'Got branch' $sw
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
79 $aheadBy = 0
f3a8ab8 @dahlbyk Convert Get-GitStatus to use 'git branch --short --branch', new in 1.7.1
authored
80 $behindBy = 0
3033fa5 @dahlbyk Rough start
authored
81 $indexAdded = @()
82 $indexModified = @()
83 $indexDeleted = @()
66572b9 Add unmerged count to status/prompt
unknown authored
84 $indexUnmerged = @()
3033fa5 @dahlbyk Rough start
authored
85 $filesAdded = @()
86 $filesModified = @()
87 $filesDeleted = @()
66572b9 Add unmerged count to status/prompt
unknown authored
88 $filesUnmerged = @()
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
89
fb7c0df @dahlbyk Add $GitPromptSettings.EnableFileStatus
authored
90 if($settings.EnableFileStatus) {
c00a37a @dahlbyk Add timing to debug statements
authored
91 dbg 'Getting status' $sw
fb7c0df @dahlbyk Add $GitPromptSettings.EnableFileStatus
authored
92 $status = git status --short --branch 2>$null
93 } else {
94 $status = @()
95 }
96
c00a37a @dahlbyk Add timing to debug statements
authored
97 dbg 'Parsing status' $sw
f3a8ab8 @dahlbyk Convert Get-GitStatus to use 'git branch --short --branch', new in 1.7.1
authored
98 $status | where { $_ } | foreach {
c00a37a @dahlbyk Add timing to debug statements
authored
99 dbg "Status: $_" $sw
f3a8ab8 @dahlbyk Convert Get-GitStatus to use 'git branch --short --branch', new in 1.7.1
authored
100 switch -regex ($_) {
101 '^## (?<branch>\S+)(?:\.\.\.(?<upstream>\S+) \[(?:ahead (?<ahead>\d+))?(?:, )?(?:behind (?<behind>\d+))?\])?$' {
102 $upstream = $matches['upstream']
103 $aheadBy = [int]$matches['ahead']
104 $behindBy = [int]$matches['behind']
105 }
106
107 '^(?<index>[^#])(?<working>.) (?<path1>.*?)(?: -> (?<path2>.*))?$' {
108 switch ($matches['index']) {
109 'A' { $indexAdded += $matches['path1'] }
110 'M' { $indexModified += $matches['path1'] }
111 'R' { $indexModified += $matches['path1'] }
112 'C' { $indexModified += $matches['path1'] }
113 'D' { $indexDeleted += $matches['path1'] }
114 'U' { $indexUnmerged += $matches['path1'] }
115 }
116 switch ($matches['working']) {
117 '?' { $filesAdded += $matches['path1'] }
118 'A' { $filesAdded += $matches['path1'] }
119 'M' { $filesModified += $matches['path1'] }
120 'D' { $filesDeleted += $matches['path1'] }
121 'U' { $filesUnmerged += $matches['path1'] }
122 }
123 }
124 }
125 }
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
126
c00a37a @dahlbyk Add timing to debug statements
authored
127 dbg 'Building status object' $sw
f3a8ab8 @dahlbyk Convert Get-GitStatus to use 'git branch --short --branch', new in 1.7.1
authored
128 $indexPaths = $indexAdded + $indexModified + $indexDeleted + $indexUnmerged
129 $workingPaths = $filesAdded + $filesModified + $filesDeleted + $filesUnmerged
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
130 $index = New-Object PSObject @(,@($indexPaths | ?{ $_ } | Select -Unique)) |
b30f998 @dahlbyk Add Added/Modified/Deleted properties to GitStatus.Index/Working
authored
131 Add-Member -PassThru NoteProperty Added $indexAdded |
132 Add-Member -PassThru NoteProperty Modified $indexModified |
66572b9 Add unmerged count to status/prompt
unknown authored
133 Add-Member -PassThru NoteProperty Deleted $indexDeleted |
134 Add-Member -PassThru NoteProperty Unmerged $indexUnmerged
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
135 $working = New-Object PSObject @(,@($workingPaths | ?{ $_ } | Select -Unique)) |
b30f998 @dahlbyk Add Added/Modified/Deleted properties to GitStatus.Index/Working
authored
136 Add-Member -PassThru NoteProperty Added $filesAdded |
137 Add-Member -PassThru NoteProperty Modified $filesModified |
66572b9 Add unmerged count to status/prompt
unknown authored
138 Add-Member -PassThru NoteProperty Deleted $filesDeleted |
139 Add-Member -PassThru NoteProperty Unmerged $filesUnmerged
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
140
fb7c0df @dahlbyk Add $GitPromptSettings.EnableFileStatus
authored
141 $result = New-Object PSObject -Property @{
0cf809e Improved Get-GitBranch
unknown authored
142 GitDir = $gitDir
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
143 Branch = $branch
144 AheadBy = $aheadBy
b30f998 @dahlbyk Add Added/Modified/Deleted properties to GitStatus.Index/Working
authored
145 HasIndex = [bool]$index
146 Index = $index
147 HasWorking = [bool]$working
148 Working = $working
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
149 HasUntracked = [bool]$filesAdded
3033fa5 @dahlbyk Rough start
authored
150 }
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
151
c00a37a @dahlbyk Add timing to debug statements
authored
152 dbg 'Finished' $sw
153 $sw.Stop()
fb7c0df @dahlbyk Add $GitPromptSettings.EnableFileStatus
authored
154 return $result
3033fa5 @dahlbyk Rough start
authored
155 }
156 }
26318c7 @markembling Added function for enabling colors in Git output.
markembling authored
157
158 function Enable-GitColors {
0cf809e Improved Get-GitBranch
unknown authored
159 $env:TERM = 'cygwin'
160 $env:LESS = 'FRSX'
da204ca @markembling Changed Status.HasWorking so that it checks both the diffFiles and the
markembling authored
161 }
Something went wrong with that request. Please try again.