概要
PowerShellでgit操作する際に、ブランチ名や変更状況等をビジュアライズしたいので、スクリプトを作成しましたー
設定
Documents\WindowsPowerShellに配置するMicrosoft.PowerSHell_profile.ps1に下記のスクリプトを配置する。
こちらの記事 を参考に改造。
Microsoft.PowerSHell_profile.ps1
function prompt {
$branch = ""
$git_cahnge_count = 0
$git_staged_count = 0
if (git branch) {
(git branch | select-string "^\*").ToString() | set-variable -name branch
$branch = $branch.trim() -replace "^\* *", ""
git diff --shortstat | %{ echo $_.Trim().Substring(0, $_.Trim().indexof(" ")) } | set-variable -name git_cahnge_count
git diff --shortstat --cached | %{ echo $_.Trim().Substring(0, $_.Trim().indexof(" ")) } | set-variable -name git_staged_count
}
Write-Host("[") -NoNewline -ForegroundColor White
$mylocal_path = $(get-location) -replace "C:\\Users\\yuya.yamaji", "~"
Write-Host($mylocal_path) -NoNewline -ForegroundColor Cyan
Write-Host("] ") -NoNewline -ForegroundColor White
if ($branch -ne "") {
Write-Host("(") -NoNewline -ForegroundColor White
if ($git_cahnge_count -ne $git_staged_count) {
if ($git_cahnge_count -ne "0") {
Write-Host($branch) -NoNewline -ForegroundColor DarkRed
} else {
Write-Host($branch) -NoNewline -ForegroundColor DarkYellow
}
} else {
Write-Host($branch) -NoNewline -ForegroundColor DarkGreen
}
Write-Host(")`n") -NoNewline -ForegroundColor White
}
return "PS > "
}