Skip to content
This repository
Newer
Older
100644 80 lines (67 sloc) 2.763 kb
3033fa53 »
2010-03-10 Rough start
1 # Inspired by Mark Embling
2 # http://www.markembling.info/view/my-ideal-powershell-prompt-with-git-integration
3
4 function Test-GitDirectory {
5 (git rev-parse --git-dir 2> $null) -ne $null
6 }
7
8 function Get-GitBranch {
9 if (Test-GitDirectory) {
10 $headRef = (git symbolic-ref HEAD) 2> $null
11 if ($headRef) {
12 Split-Path -Leaf $headRef
13 }
14 }
15 }
16
17 function Get-GitStatus {
18 if(Test-GitDirectory)
19 {
20 $indexAdded = @()
21 $indexModified = @()
22 $indexDeleted = @()
23 $filesAdded = @()
24 $filesModified = @()
25 $filesDeleted = @()
26 $aheadCount = 0
27
28 $diffIndex = git diff-index -M --name-status --cached HEAD |
29 ConvertFrom-CSV -Delim "`t" -Header 'Status','Path'
30 $diffFiles = git diff-files -M --name-status |
31 ConvertFrom-CSV -Delim "`t" -Header 'Status','Path'
32
33 $grpIndex = $diffIndex | Group-Object Status -AsHashTable
34 $grpFiles = $diffFiles | Group-Object Status -AsHashTable
35
36 if($grpIndex.A) { $indexAdded += $grpIndex.A | %{ $_.Path } }
37 if($grpIndex.M) { $indexModified += $grpIndex.M | %{ $_.Path } }
38 if($grpIndex.R) { $indexModified += $grpIndex.R | %{ $_.Path } }
39 if($grpIndex.D) { $indexDeleted += $grpIndex.D | %{ $_.Path } }
40 if($grpFiles.M) { $filesModified += $grpFiles.M | %{ $_.Path } }
41 if($grpFiles.R) { $filesModified += $grpFiles.R | %{ $_.Path } }
42 if($grpFiles.D) { $filesDeleted += $grpFiles.D | %{ $_.Path } }
43
44 $untracked = git ls-files -o --exclude-standard
45 if($untracked) { $filesAdded += $untracked }
46
47 $output = git status
48
49 $output | foreach {
50 if ($_ -match "^\#.*origin/.*' by (\d+) commit.*") {
51 $aheadCount = $matches[1]
52 }
53 }
54
923796ce »
2010-03-12 Clarified some of the git status object field names.
55 $status = New-Object PSObject -Property @{
56 Branch = Get-GitBranch
57 AheadBy = $aheadCount
58 HasIndex = [bool]$diffIndex
59 Index = $diffIndex | %{ $_.Path }
60 IndexAdded = $indexAdded
61 IndexModified = $indexModified
62 IndexDeleted = $indexDeleted
da204ca5 »
2010-03-24 Changed Status.HasWorking so that it checks both the diffFiles and the
63 HasWorking = [bool]$diffFiles -or [bool]$filesAdded
923796ce »
2010-03-12 Clarified some of the git status object field names.
64 Working = ($diffFiles | %{ $_.Path })
65 WorkingAdded = $filesAdded
66 WorkingModified = $filesModified
67 WorkingDeleted = $filesDeleted
68 HasUntracked = [bool]$filesAdded
3033fa53 »
2010-03-10 Rough start
69 }
923796ce »
2010-03-12 Clarified some of the git status object field names.
70 if ($untracked) { $status.Working += $untracked }
71
72 return $status
3033fa53 »
2010-03-10 Rough start
73 }
74 }
26318c79 »
2010-03-15 Added function for enabling colors in Git output.
75
76 function Enable-GitColors {
77 $env:TERM = 'cygwin'
78 $env:LESS = 'FRSX'
da204ca5 »
2010-03-24 Changed Status.HasWorking so that it checks both the diffFiles and the
79 }
Something went wrong with that request. Please try again.