Skip to content

Instantly share code, notes, and snippets.

@ssg
Last active November 17, 2022 04:42
Show Gist options
  • Save ssg/5195412 to your computer and use it in GitHub Desktop.
Save ssg/5195412 to your computer and use it in GitHub Desktop.
Git/Mercurial PowerShell prompt.
# ssg's powershell profile - latest version is at https://gist.github.com/ssg/5195412
# only feature is to show hg/git current branch on the prompt
$vcTypes = @(
@{
Name = "hg"
Directory = ".hg"
BranchScript = { Get-Content ".hg\branch" }
},
@{
Name = "git"
Directory = ".git"
BranchScript = {
(Get-Content ".git\HEAD"
| Select-String -Pattern "/([^/]+)$").Matches.Groups[1].Value
}
}
)
function getRepositoryType($folder) {
while ($folder) {
$path = $folder.FullName
foreach ($vc in $vcTypes) {
if (Test-Path (Join-Path $path $($vc.Directory))) {
return $vc;
}
}
$folder = $folder.Parent
}
return $null
}
function writeBranch {
$vc = getRepositoryType (Get-Item .)
if ($vc) {
Write-Host -NoNewLine -ForegroundColor White " [$($vc.Name):"
try {
$branch = Invoke-Command -NoNewScope $vc.BranchScript
Write-Host -NoNewLine -ForegroundColor Cyan "$branch"
}
catch {
Write-Host -NoNewLine -ForegroundColor Red "???"
}
Write-Host -NoNewLine -ForegroundColor White "]"
}
}
# show current branch in mercurial at the prompt
function prompt {
Write-Host -NoNewLine "PS $pwd"
writeBranch
return ">"
}
Set-Alias ll dir
@ssg
Copy link
Author

ssg commented Mar 15, 2016

To install it, run this PowerShell command (WARNING: it overwrites your existing PowerShell profile if it exists, so don't run this if you already customized it):

mkdir -Force (Split-Path $profile);iwr -Uri "https://gist.github.com/ssg/5195412/raw/" -OutFile $Profile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment