-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEnable-GitPrompt.ps1
119 lines (103 loc) · 3.59 KB
/
Enable-GitPrompt.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Mark Embling (http://www.markembling.info/)
# http://www.markembling.info/view/my-ideal-powershell-prompt-with-git-integration
# Displays git branch and stats when inside a git repository.
function prompt {
$path = ""
$pathbits = ([string]$pwd).split("\", [System.StringSplitOptions]::RemoveEmptyEntries)
if($pathbits.length -eq 1) {
$path = $pathbits[0] + "\"
} else {
$path = $pathbits[$pathbits.length - 1]
}
$userLocation = $env:username + '@' + [System.Environment]::MachineName + ' ' + $path
$host.UI.RawUi.WindowTitle = $userLocation
Write-Host($userLocation) -nonewline -foregroundcolor Green
if (isCurrentDirectoryGitRepository) {
$status = gitStatus
$currentBranch = $status["branch"]
Write-Host(' [') -nonewline -foregroundcolor Yellow
if ($status["ahead"] -eq $FALSE) {
# We are not ahead of origin
Write-Host($currentBranch) -nonewline -foregroundcolor Cyan
} else {
# We are ahead of origin
Write-Host($currentBranch) -nonewline -foregroundcolor Red
}
Write-Host(' +' + $status["added"]) -nonewline -foregroundcolor Yellow
Write-Host(' ~' + $status["modified"]) -nonewline -foregroundcolor Yellow
Write-Host(' -' + $status["deleted"]) -nonewline -foregroundcolor Yellow
if ($status["untracked"] -ne $FALSE) {
Write-Host(' !') -nonewline -foregroundcolor Yellow
}
Write-Host(']') -nonewline -foregroundcolor Yellow
}
Write-Host('>') -nonewline -foregroundcolor Green
return " "
}
# Git functions
# Mark Embling (http://www.markembling.info/)
# http://www.markembling.info/view/my-ideal-powershell-prompt-with-git-integration
# Is the current directory a git repository/working copy?
function isCurrentDirectoryGitRepository {
if ((Test-Path ".git") -eq $TRUE) {
return $TRUE
}
# Test within parent dirs
$checkIn = (Get-Item .).parent
while ($checkIn -ne $NULL) {
$pathToTest = $checkIn.fullname + '/.git'
if ((Test-Path $pathToTest) -eq $TRUE) {
return $TRUE
} else {
$checkIn = $checkIn.parent
}
}
return $FALSE
}
# Get the current branch
function gitBranchName {
$currentBranch = ''
git branch | foreach {
if ($_ -match "^\* (.*)") {
$currentBranch += $matches[1]
}
}
return $currentBranch
}
# Extracts status details about the repo
function gitStatus {
$untracked = $FALSE
$added = 0
$modified = 0
$deleted = 0
$ahead = $FALSE
$aheadCount = 0
$output = git status
$branchbits = $output[0].Split(' ')
$branch = $branchbits[$branchbits.length - 1]
$output | foreach {
if ($_ -match "^\#.*origin/.*' by (\d+) commit.*") {
$aheadCount = $matches[1]
$ahead = $TRUE
}
elseif ($_ -match "deleted:") {
$deleted += 1
}
elseif (($_ -match "modified:") -or ($_ -match "renamed:")) {
$modified += 1
}
elseif ($_ -match "new file:") {
$added += 1
}
elseif ($_ -match "Untracked files:") {
$untracked = $TRUE
}
}
return @{"untracked" = $untracked;
"added" = $added;
"modified" = $modified;
"deleted" = $deleted;
"ahead" = $ahead;
"aheadCount" = $aheadCount;
"branch" = $branch}
}