Skip to content

Instantly share code, notes, and snippets.

@drlongnecker
Created April 6, 2011 21:04
Show Gist options
  • Save drlongnecker/906520 to your computer and use it in GitHub Desktop.
Save drlongnecker/906520 to your computer and use it in GitHub Desktop.
Just parses the git command output for a swanky prompt
# 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 " "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment