Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 169 lines (154 sloc) 6.89 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 bran...
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
7b9073b @dahlbyk Remove unnecessary symbolic-ref call and refactor detached scenarios (ta...
authored
41 $b = '({0})' -f (
42 Coalesce-Args `
43 { dbg 'Trying describe' $sw; git describe --exact-match HEAD 2>$null } `
44 {
204ec45 @dahlbyk Make HEAD parsing aware of refs
authored
45 dbg 'Falling back on parsing HEAD' $sw
7b9073b @dahlbyk Remove unnecessary symbolic-ref call and refactor detached scenarios (ta...
authored
46 $ref = Get-Content $gitDir\HEAD 2>$null
204ec45 @dahlbyk Make HEAD parsing aware of refs
authored
47 if ($ref -match 'ref: (?<ref>.+)') {
48 return $Matches['ref']
49 } elseif ($ref -and $ref.Length -ge 7) {
7b9073b @dahlbyk Remove unnecessary symbolic-ref call and refactor detached scenarios (ta...
authored
50 return $ref.Substring(0,7)+'...'
51 } else {
204ec45 @dahlbyk Make HEAD parsing aware of refs
authored
52 return 'unknown'
7b9073b @dahlbyk Remove unnecessary symbolic-ref call and refactor detached scenarios (ta...
authored
53 }
204ec45 @dahlbyk Make HEAD parsing aware of refs
authored
54 }
7b9073b @dahlbyk Remove unnecessary symbolic-ref call and refactor detached scenarios (ta...
authored
55 )
0cf809e Improved Get-GitBranch
unknown authored
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-GitStat...
authored
72 $settings = $Global:GitPromptSettings
73 $enabled = (-not $settings) -or $settings.EnablePromptStatus
74 if ($enabled -and $gitDir)
3033fa5 @dahlbyk Rough start
authored
75 {
f544c4c @dahlbyk Add $GitPromptSettings.Debug instead of relying on $DebugPreference
authored
76 if($settings.Debug) { $sw = [Diagnostics.Stopwatch]::StartNew(); Write-Host '' }
0f1a1e0 @dahlbyk Get branch from status if possible
authored
77 $branch = $null
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
78 $aheadBy = 0
f3a8ab8 @dahlbyk Convert Get-GitStatus to use 'git branch --short --branch', new in 1.7.1
authored
79 $behindBy = 0
3033fa5 @dahlbyk Rough start
authored
80 $indexAdded = @()
81 $indexModified = @()
82 $indexDeleted = @()
66572b9 Add unmerged count to status/prompt
unknown authored
83 $indexUnmerged = @()
3033fa5 @dahlbyk Rough start
authored
84 $filesAdded = @()
85 $filesModified = @()
86 $filesDeleted = @()
66572b9 Add unmerged count to status/prompt
unknown authored
87 $filesUnmerged = @()
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
88
fb7c0df @dahlbyk Add $GitPromptSettings.EnableFileStatus
authored
89 if($settings.EnableFileStatus) {
c00a37a @dahlbyk Add timing to debug statements
authored
90 dbg 'Getting status' $sw
fb7c0df @dahlbyk Add $GitPromptSettings.EnableFileStatus
authored
91 $status = git status --short --branch 2>$null
92 } else {
93 $status = @()
94 }
95
c00a37a @dahlbyk Add timing to debug statements
authored
96 dbg 'Parsing status' $sw
54c38a3 @dahlbyk Small status parsing change
authored
97 $status | foreach {
c00a37a @dahlbyk Add timing to debug statements
authored
98 dbg "Status: $_" $sw
54c38a3 @dahlbyk Small status parsing change
authored
99 if($_) {
100 switch -regex ($_) {
101 '^(?<index>[^#])(?<working>.) (?<path1>.*?)(?: -> (?<path2>.*))?$' {
102 switch ($matches['index']) {
103 'A' { $indexAdded += $matches['path1'] }
104 'M' { $indexModified += $matches['path1'] }
105 'R' { $indexModified += $matches['path1'] }
106 'C' { $indexModified += $matches['path1'] }
107 'D' { $indexDeleted += $matches['path1'] }
108 'U' { $indexUnmerged += $matches['path1'] }
109 }
110 switch ($matches['working']) {
111 '?' { $filesAdded += $matches['path1'] }
112 'A' { $filesAdded += $matches['path1'] }
113 'M' { $filesModified += $matches['path1'] }
114 'D' { $filesDeleted += $matches['path1'] }
115 'U' { $filesUnmerged += $matches['path1'] }
116 }
f3a8ab8 @dahlbyk Convert Get-GitStatus to use 'git branch --short --branch', new in 1.7.1
authored
117 }
8926763 @dahlbyk For status, move branch parsing (which is expensive) last
authored
118
54c38a3 @dahlbyk Small status parsing change
authored
119 '^## (?<branch>\S+)(?:\.\.\.(?<upstream>\S+) \[(?:ahead (?<ahead>\d+))?(?:, )?(?:behind (?<behind>\d+))?\])?$' {
0f1a1e0 @dahlbyk Get branch from status if possible
authored
120 $branch = $matches['branch']
54c38a3 @dahlbyk Small status parsing change
authored
121 $upstream = $matches['upstream']
122 $aheadBy = [int]$matches['ahead']
123 $behindBy = [int]$matches['behind']
124 }
9b41b27 @dahlbyk Fix branch name before initial commit
authored
125
126 '^## Initial commit on (?<branch>\S+)$' {
127 $branch = $matches['branch']
128 }
8926763 @dahlbyk For status, move branch parsing (which is expensive) last
authored
129 }
f3a8ab8 @dahlbyk Convert Get-GitStatus to use 'git branch --short --branch', new in 1.7.1
authored
130 }
131 }
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
132
0f1a1e0 @dahlbyk Get branch from status if possible
authored
133 if(!$branch) { $branch = Get-GitBranch $gitDir $sw }
c00a37a @dahlbyk Add timing to debug statements
authored
134 dbg 'Building status object' $sw
f3a8ab8 @dahlbyk Convert Get-GitStatus to use 'git branch --short --branch', new in 1.7.1
authored
135 $indexPaths = $indexAdded + $indexModified + $indexDeleted + $indexUnmerged
136 $workingPaths = $filesAdded + $filesModified + $filesDeleted + $filesUnmerged
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
137 $index = New-Object PSObject @(,@($indexPaths | ?{ $_ } | Select -Unique)) |
b30f998 @dahlbyk Add Added/Modified/Deleted properties to GitStatus.Index/Working
authored
138 Add-Member -PassThru NoteProperty Added $indexAdded |
139 Add-Member -PassThru NoteProperty Modified $indexModified |
66572b9 Add unmerged count to status/prompt
unknown authored
140 Add-Member -PassThru NoteProperty Deleted $indexDeleted |
141 Add-Member -PassThru NoteProperty Unmerged $indexUnmerged
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
142 $working = New-Object PSObject @(,@($workingPaths | ?{ $_ } | Select -Unique)) |
b30f998 @dahlbyk Add Added/Modified/Deleted properties to GitStatus.Index/Working
authored
143 Add-Member -PassThru NoteProperty Added $filesAdded |
144 Add-Member -PassThru NoteProperty Modified $filesModified |
66572b9 Add unmerged count to status/prompt
unknown authored
145 Add-Member -PassThru NoteProperty Deleted $filesDeleted |
146 Add-Member -PassThru NoteProperty Unmerged $filesUnmerged
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
147
fb7c0df @dahlbyk Add $GitPromptSettings.EnableFileStatus
authored
148 $result = New-Object PSObject -Property @{
0cf809e Improved Get-GitBranch
unknown authored
149 GitDir = $gitDir
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
150 Branch = $branch
151 AheadBy = $aheadBy
b30f998 @dahlbyk Add Added/Modified/Deleted properties to GitStatus.Index/Working
authored
152 HasIndex = [bool]$index
153 Index = $index
154 HasWorking = [bool]$working
155 Working = $working
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
156 HasUntracked = [bool]$filesAdded
3033fa5 @dahlbyk Rough start
authored
157 }
9d26b51 @dahlbyk Refactor Get-GitStatus
authored
158
c00a37a @dahlbyk Add timing to debug statements
authored
159 dbg 'Finished' $sw
f544c4c @dahlbyk Add $GitPromptSettings.Debug instead of relying on $DebugPreference
authored
160 if($sw) { $sw.Stop() }
fb7c0df @dahlbyk Add $GitPromptSettings.EnableFileStatus
authored
161 return $result
3033fa5 @dahlbyk Rough start
authored
162 }
163 }
26318c7 @markembling Added function for enabling colors in Git output.
markembling authored
164
165 function Enable-GitColors {
0cf809e Improved Get-GitBranch
unknown authored
166 $env:TERM = 'cygwin'
167 $env:LESS = 'FRSX'
da204ca @markembling Changed Status.HasWorking so that it checks both the diffFiles and the
markembling authored
168 }
Something went wrong with that request. Please try again.