Skip to content

Instantly share code, notes, and snippets.

Created November 19, 2016 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/495cf3b5bab7e584ee04195091b8a518 to your computer and use it in GitHub Desktop.
Save anonymous/495cf3b5bab7e584ee04195091b8a518 to your computer and use it in GitHub Desktop.
Windows PowerShell の prompt で git のブランチ名を表示 (変数・関数の命名はテキトー)
# author: Leonardone @ NEETSDKASU
function IsGitRepository {
Param([string]$Path)
if ($Path) {
if (Test-Path $Path -PathType Container) {
} else {
return (0, '', '')
}
} else {
$Path = Get-Location
}
$Here = Split-Path $Path -Leaf
while ($Path -ne "") {
if ((Split-Path $Path -Leaf) -eq '.git') {
return (2, $Path, $Here)
} elseif (Test-Path (Join-Path $Path '.git') -PathType Container) {
return (1, (Join-Path $Path '.git'), $Here)
}
$Path = Split-Path $Path -Parent
if ($Path) {
$Here = "$(Split-Path $Path -Leaf)\$Here"
}
}
return (0, '', '')
}
function prompt {
($Result, $GitPath, $Here) = IsGitRepository
if ($Result -eq 1) {
$tmp = Select-String -Path (Join-Path $GitPath 'HEAD') -Pattern "[^/]+./[^/]+/(.+)"
if ($tmp) {
Write-Host "PS .\$Here" -NoNewline
Write-Host " ($($tmp.Matches[0].Groups[1].Value))" -ForegroundColor Yellow -NoNewLine
return ">"
} else {
"PS .\$Here (Git Repository)>"
}
} elseif ($Result -eq 2) {
$tmp = Select-String -Path (Join-Path $GitPath 'HEAD') -Pattern "[^/]+./[^/]+/(.+)"
if ($tmp) {
"PS $Here ($($tmp.Matches[0].Groups[1].Value))[.git]>"
} else {
"PS $Here (Git Repository)[.git]>"
}
} else {
"PS $(Get-Location)>"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment