Created
November 19, 2016 20:44
-
-
Save anonymous/495cf3b5bab7e584ee04195091b8a518 to your computer and use it in GitHub Desktop.
Windows PowerShell の prompt で git のブランチ名を表示 (変数・関数の命名はテキトー)
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
# 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