0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows PowerShellでgitのブランチを表示して、変更があったらブランチ名の色を変えるスクリプト

Last updated at Posted at 2022-03-28

概要

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 > "
}

こんな感じで表示されるよ

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?