public

Just parses the git command output for a swanky prompt

  • Download Gist
PowerShell Profile for Git
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
# when in a git directory, outputs:
# GIT [branch] +# ~# -# | C:\my\path
# when in any other directory, outputs:
# PS C:\my\other\path
function prompt {
Write-Host("")
$status_string = ""
# check to see if this is a directory containing a symbolic reference,
# fails (gracefully) on non-git repos.
$symbolicref = git symbolic-ref HEAD
if($symbolicref -ne $NULL) {
# if a symbolic reference exists, snag the last bit as our
# branch name. eg "[master]"
$status_string += "GIT [" + `
$symbolicref.substring($symbolicref.LastIndexOf("/") +1) + "] "
# grab the differences in this branch
$differences = (git diff-index --name-status HEAD)
# use a regular expression to count up the differences.
# M`t, A`t, and D`t refer to M {tab}, etc.
$git_update_count = [regex]::matches($differences, "M`t").count
$git_create_count = [regex]::matches($differences, "A`t").count
$git_delete_count = [regex]::matches($differences, "D`t").count
# place those variables into our string.
$status_string += "+" + $git_create_count + `
" ~" + $git_update_count + `
" -" + $git_delete_count + " | "
}
else {
# Not in a Git environment, must be PowerShell!
$status_string = "PS "
}
# write out the status_string with the approprate color.
# prompt is done!
if ($status_string.StartsWith("GIT")) {
Write-Host ($status_string + $(get-location)) -foregroundcolor yellow
Write-Host "$ " -foregroundcolor white -nonewline
}
else {
Write-Host ($status_string + $(get-location)) -foregroundcolor green
Write-Host "$ " -foregroundcolor white -nonewline
}
return " "
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.