2009-08-17 18 views

risposta

28

Un modo più semplice sarebbe solo l'installazione del modulo PowerShell posh-git. Esce dalla scatola con la richiesta desiderata:

Prompt

PowerShell genera una sua rapida eseguendo una funzione rapida, se presente. posh-git definisce una tale funzione in profile.example.ps1 che emette la directory corrente seguita da uno stato git abbreviata:

C:\Users\Keith [master]>

Per default, la sintesi di stato ha il seguente formato:

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

(Per l'installazione di posh-git io suggerisco di usare psget)

I f non avete psget utilizzare il seguente comando:

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

Per installare posh-git utilizzare il comando: Install-Module posh-git

Per garantire carichi posh-Git per ogni guscio, utilizzare il Add-PoshGitToPrompt command.

+0

Come abilitare questo modulo all'avvio? Sembra che powershell dimentichi questo modulo ogni volta che apro una nuova istanza. – Mihir

+0

@Mihir devi creare un profilo, guarda le risposte https://stackoverflow.com/questions/24914589/how-to-create-permanent-powershell-aliases# –

+1

@NicolaPeluchetti Grazie! Ho seguito https://www.howtogeek.com/50236/customizing-your-powershell-profile/ e ho appena aggiunto 'Import-Module posh-git' nel mio profilo PowerShell. Ha funzionato come un fascino! – Mihir

23

@ Paul-

mio profilo PowerShell per Git è basato su uno script che ho trovato qui:

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

ho modificato un po 'per visualizzare il percorso della directory e un po' di formattazione. Imposta anche il percorso del mio percorso Git bin da quando utilizzo 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 " " 
} 

Finora, questo ha funzionato molto bene. Mentre in un repository, il prompt sembra felicemente:

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

* NOTA: aggiornato con i suggerimenti di Jakub Narębski.

  • Rimosso il git branch/git status calls.
  • Indirizzato a un problema in cui 'git config - globale' sarebbe fallito perché $ HOME non era impostato.
  • È stato risolto un problema durante il quale navigare in una directory che non disponeva della directory .git causava il ritorno della formattazione al prompt PS.
+0

Saluti David, sono stato in grado di modificare facilmente questo e utilizzare le istruzioni del post bloggato collegato per ottenere qualcosa in esecuzione che mi si adatta. –

+6

Non raschiare l'output di git branch per ottenere il nome del ramo corrente; è pensato per l'utente finale (è in porcellana). Usa 'git simbolico-ref HEAD'. Non usare git-status; è pensato per l'utente finale ed è soggetto a modifiche (cambierebbe in 1.7.0). Usa git-diff-files, git-diff-tree, git-diff-index. –

+0

#Jakub Narębski - Aggiornamento del codice basato sui vostri suggerimenti - grazie mille. Dal momento che non chiama il ramo e lo stato, è anche un po 'più veloce. –

1

Ho ottimizzato il codice di richiesta (dalla risposta @ david-longnecker) per essere un po 'più colorato.

Ecco il mio codice

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

Il risultato: Results

Qui ci sono i comandi da frutto per visualizzare ciò che sarebbe simile:

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

Ecco il mio prendere su di esso. Ho modificato i colori un po 'per renderlo più leggibile.

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 
} 

Esempio 1:

enter image description here

Esempio 2:

enter image description here

+0

Neat! Ho appena cambiato i colori per il mio background. Qual è il tuo colore di sfondo? –

+0

@YogeeshSeralathan Sto utilizzando il tema Monokai per ConEmu. Il colore di sfondo è # 272822 – tamj0rd2

Problemi correlati