Last active
November 17, 2022 04:42
-
-
Save ssg/5195412 to your computer and use it in GitHub Desktop.
Git/Mercurial PowerShell prompt.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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):