2009-08-17 10 views

Odpowiedz

28

Łatwiejszym sposobem byłoby po prostu zainstalowanie modułu Powershell posh-git. Okazuje się z pudełka z pożądanym polecenia:

wierszu

PowerShell generuje wiersz przez wykonanie szybkiej funkcję, jeżeli istnieje. posh-git definiuje taką funkcję w profile.example.ps1 że wyprowadza bieżący katalog roboczy następnie skróconej git status:

C:\Users\Keith [master]>

Domyślnie podsumowanie stanu ma następujący format:

[{HEAD-name} +A ~B -C !D | +E ~F -G !H]

(Do montażu posh-git Proponuję za pomocą psget)

I f nie masz psget użyć następującego polecenia:

(new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | iex 

Aby zainstalować posh-git użyć polecenia: Install-Module posh-git

celu zapewnienia ładunki posh-git dla każdej powłoki, użyj Add-PoshGitToPrompt command.

+0

Jak włączyć ten moduł podczas uruchamiania? Wygląda na to, że Powerhell zapomina o tym module za każdym razem, gdy otwieram nowe wystąpienie. – Mihir

+0

@Mihir musisz utworzyć profil, spójrz na odpowiedzi https://stackoverflow.com/questions/24914589/how-to-create-permanent-powershell-aliases# –

+1

@NicolaPeluchetti Dzięki! Poszedłem za https://www.howtogeek.com/50236/customizing-your-powershell-profile/ i właśnie dodałem 'Import-Module posh-git' w moim profilu PowerShell. Pracował jak urok! – Mihir

23

@ Paul-

Mój profil PowerShell dla Git jest opartym na scenariuszu I znaleźć tutaj:

http://techblogging.wordpress.com/2008/10/12/displaying-git-branch-on-your-powershell-prompt/

Mam zmodyfikowanej go trochę, aby wyświetlić ścieżkę i trochę formatowania. Ustawia również ścieżkę do mojej lokalizacji odbiornika Git, ponieważ używam PortableGit.

# General variables 
$pathToPortableGit = "D:\shared_tools\tools\PortableGit" 
$scripts = "D:\shared_tools\scripts" 

# Add Git executables to the mix. 
[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";" + (Join-Path $pathToPortableGit "\bin") + ";" + $scripts, "Process") 

# Setup Home so that Git doesn't freak out. 
[System.Environment]::SetEnvironmentVariable("HOME", (Join-Path $Env:HomeDrive $Env:HomePath), "Process") 

$Global:CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent() 
$UserType = "User" 
$CurrentUser.Groups | foreach { 
    if ($_.value -eq "S-1-5-32-544") { 
     $UserType = "Admin" } 
    } 

function prompt { 
    # Fun stuff if using the standard PowerShell prompt; not useful for Console2. 
    # This, and the variables above, could be commented out. 
    if($UserType -eq "Admin") { 
     $host.UI.RawUI.WindowTitle = "" + $(get-location) + " : Admin" 
     $host.UI.RawUI.ForegroundColor = "white" 
     } 
    else { 
     $host.ui.rawui.WindowTitle = $(get-location) 
    } 

    Write-Host("") 
    $status_string = "" 
    $symbolicref = git symbolic-ref HEAD 
    if($symbolicref -ne $NULL) { 
     $status_string += "GIT [" + $symbolicref.substring($symbolicref.LastIndexOf("/") +1) + "] " 

     $differences = (git diff-index --name-status HEAD) 
     $git_update_count = [regex]::matches($differences, "M`t").count 
     $git_create_count = [regex]::matches($differences, "A`t").count 
     $git_delete_count = [regex]::matches($differences, "D`t").count 

     $status_string += "c:" + $git_create_count + " u:" + $git_update_count + " d:" + $git_delete_count + " | " 
    } 
    else { 
     $status_string = "PS " 
    } 

    if ($status_string.StartsWith("GIT")) { 
     Write-Host ($status_string + $(get-location) + ">") -nonewline -foregroundcolor yellow 
    } 
    else { 
     Write-Host ($status_string + $(get-location) + ">") -nonewline -foregroundcolor green 
    } 
    return " " 
} 

Jak dotąd, to pracował bardzo dobrze. Podczas repozytorium szczęśliwie wygląda tak:

GIT [master] c: 0 u: 1 d: 0 | J: \ Projects \ forks \ fluent-nhibernate>

* UWAGA: Zaktualizowane z sugestiami Jakuba Narębskiego.

  • Usunięto wywołania statusu gałęzi/git.
  • Rozwiązano problem polegający na tym, że "git config --global" - nie powiodło się, ponieważ $ HOME nie został ustawiony.
  • Rozwiązano problem polegający na tym, że po przejściu do katalogu, który nie ma katalogu .git, formatowanie powróciłoby do znaku PS.
+0

Pozdrawiam Davida, łatwo go zmodyfikować i użyć instrukcji z połączonego bloga, aby uzyskać coś, co pasuje do mnie. –

+6

Nie skrobać wyjścia z gałęzi git, aby uzyskać nazwę bieżącego oddziału; jest przeznaczony dla użytkownika końcowego (jest to porcelana). Użyj 'git symbolic-ref HEAD'. Nie używaj statusu git; jest przeznaczony dla użytkownika końcowego i może ulec zmianie (zmieni się w 1.7.0). Użyj plików git-diff, git-diff-tree, git-diff-index. –

+0

#Jakub Narębski - Zaktualizowano kod na podstawie twoich sugestii - wielkie dzięki. Ponieważ nie wywołuje on gałęzi i statusu, jest również nieco szybszy. –

1

I manipulowane kodu szybkiego (od @ david-longnecker odpowiedzi) być nieco bardziej kolorowe.

Oto

Function Prompt { 
Write-Host("") 
Remove-Variable status_string 
Remove-Variable branch 
Remove-Variable localchanges 
$symbolicref = git symbolic-ref HEAD 

if($symbolicref -ne $NULL) { 
    $status_string += "GIT" 
    $branch = $symbolicref.substring($symbolicref.LastIndexOf("/") +1) 

    $differences = (git diff-index --name-status HEAD) 
    If ($differences -ne $NULL) { 
    $localchanges = $true 
    $git_create_count = [regex]::matches($differences, "A`t").count 
    $git_update_count = [regex]::matches($differences, "M`t").count 
    $git_delete_count = [regex]::matches($differences, "D`t").count 
    } 
    else { 
    $localchanges = $false 
    $git_create_count = 0 
    $git_update_count = 0 
    $git_delete_count = 0 
    } 
    #$status_string += "c:" + $git_create_count + " u:" + $git_update_count + " d:" + $git_delete_count + " | " 
} 
else { 
    $status_string = "PS " 
} 

if ($status_string.StartsWith("GIT")) { 
    Write-Host ($status_string) -nonewline -foregroundcolor White 

    #Output the branch in prettier colors 
    Write-Host (" [") -nonewline -foregroundcolor White 
     If ($branch -ne "master") {Write-Host ($branch) -nonewline -foregroundcolor Red} 
     else {Write-Host ($branch) -nonewline -foregroundcolor DarkYellow} 
    Write-Host ("] ") -nonewline -foregroundcolor White 

    #Output changes count, if any, in pretty colors 
    If ($localchanges) { 
    Write-Host ("c:") -nonewline -foregroundcolor White 
     If ($git_create_count -gt 0) {Write-Host ($git_create_count) -nonewline -foregroundcolor Green} 
     else {Write-Host ($git_create_count) -nonewline -foregroundcolor White} 
    Write-Host (" u:") -nonewline -foregroundcolor White 
     If ($git_update_count -gt 0) {Write-Host ($git_update_count) -nonewline -foregroundcolor Yellow} 
     else {Write-Host ($git_update_count) -nonewline -foregroundcolor White} 
    Write-Host (" d:") -nonewline -foregroundcolor White 
     If ($git_delete_count -gt 0) {Write-Host ($git_delete_count) -nonewline -foregroundcolor Red} 
     else {Write-Host ($git_delete_count + " ") -nonewline -foregroundcolor White} 
    } 

    #Output the normal prompt details, namely the path 
    Write-Host ("| " + $((get-location).Path) + ">") -nonewline -foregroundcolor White 
} 
else { 
    Write-Host ($status_string + $((get-location).Path) + ">") -nonewline -foregroundcolor White 
} 
return " "} 

Wynikiem mój kod: Results

Oto polecenia od wyniku aby zobaczyć co to będzie wyglądać:

mkdir c:\git\newrepo | Out-Null 
cd c:\git\newrepo 
git init 
"test" >> ".gitignore" 
"test" >> ".gitignore2" 
git add -A 
git commit -m "test commit" | Out-Null 
"test" >> ".gitignore1" 
git add -A 
"test1" >> ".gitignore2" 
git rm .gitignore 
git add -A 
git commit -m "test commit2" | Out-Null 
git checkout -b "newfeature1" 
git checkout "master" 
8

Oto moje zdanie na jej temat. Zmieniłem trochę kolory, aby były bardziej czytelne.

Microsoft.PowerShell_profile.ps1

function Write-BranchName() { 
    try { 
     $branch = git rev-parse --abbrev-ref HEAD 

     if ($branch -eq "HEAD") { 
      # we're probably in detached HEAD state, so print the SHA 
      $branch = git rev-parse --short HEAD 
      Write-Host " ($branch)" -ForegroundColor "red" 
     } 
     else { 
      # we're on an actual branch, so print it 
      Write-Host " ($branch)" -ForegroundColor "blue" 
     } 
    } catch { 
     # we'll end up here if we're in a newly initiated git repo 
     Write-Host " (no branches yet)" -ForegroundColor "yellow" 
    } 
} 

function prompt { 
    $base = "PS " 
    $path = "$($executionContext.SessionState.Path.CurrentLocation)" 
    $userPrompt = "$('>' * ($nestedPromptLevel + 1)) " 

    Write-Host "`n$base" -NoNewline 

    if (Test-Path .git) { 
     Write-Host $path -NoNewline -ForegroundColor "green" 
     Write-BranchName 
    } 
    else { 
     # we're not in a repo so don't bother displaying branch name/sha 
     Write-Host $path -ForegroundColor "green" 
    } 

    return $userPrompt 
} 

Przykład 1:

enter image description here

Przykład 2:

enter image description here

+0

Schludny! Po prostu zmieniłem kolory dla mojego tła. Jaki jest twój kolor tła? –

+0

@YogeeshSeralathan Używam motywu Monokai dla ConEmu. Kolor tła to # 272822 – tamj0rd2

Powiązane problemy