Skip to content
Browse files

Rough start

  • Loading branch information...
0 parents commit 3033fa53c2117b0d35679461978cc0bcfd6a8ad6 @dahlbyk committed Mar 10, 2010
Showing with 254 additions and 0 deletions.
  1. +20 −0 GitProfile.ps1
  2. +42 −0 GitPrompt.ps1
  3. +121 −0 GitTabExpansion.ps1
  4. +71 −0 GitUtils.ps1
20 GitProfile.ps1
@@ -0,0 +1,20 @@
+Push-Location (Split-Path -Path $MyInvocation.MyCommand.Definition -Parent)
+
+. .\GitUtils.ps1
+. .\GitPrompt.ps1
+. .\GitTabExpansion.ps1
+
+function Prompt() {
+ Write-Host "$(1+(Get-History -Count 1).Id) " -nonewline
+ $dir = $pwd | Get-Item
+
+ $Host.UI.RawUi.WindowTitle = $dir.FullName
+ Write-Host $dir.Name -nonewline -foregroundcolor Green
+
+ $Global:GitStatus = Get-GitStatus
+ Write-GitStatus $GitStatus
+
+ return '> '
+}
+
+Pop-Location
42 GitPrompt.ps1
@@ -0,0 +1,42 @@
+# Inspired by Mark Embling
+# http://www.markembling.info/view/my-ideal-powershell-prompt-with-git-integration
+
+function Write-GitStatus($status) {
+ if ($status) {
+ $indexColor = [ConsoleColor]::Blue
+ $workingColor = [ConsoleColor]::Yellow
+
+ $currentBranch = $status.Branch
+
+ Write-Host(' [') -nonewline -foregroundcolor Yellow
+ if ($status.AheadBy -eq 0) {
+ # We are not ahead of origin
+ Write-Host($currentBranch) -nonewline -foregroundcolor Cyan
+ } else {
+ # We are ahead of origin
+ Write-Host($currentBranch) -nonewline -foregroundcolor Red
+ }
+
+ if($status.HasIndex) {
+ Write-Host " +$($status.IndexAdded.Count)" -nonewline -foregroundcolor $indexColor
+ Write-Host " ~$($status.IndexModified.Count)" -nonewline -foregroundcolor $indexColor
+ Write-Host " -$($status.IndexDeleted.Count)" -nonewline -foregroundcolor $indexColor
+
+ if($status.HasWorking) {
+ Write-Host " |" -nonewline -foregroundcolor Red
+ }
+ }
+
+ if($status.HasWorking) {
+ Write-Host " +$($status.Added.Count)" -nonewline -foregroundcolor $workingColor
+ Write-Host " ~$($status.Modified.Count)" -nonewline -foregroundcolor $workingColor
+ Write-Host " -$($status.Deleted.Count)" -nonewline -foregroundcolor $workingColor
+ }
+
+ if ($status.HasUntracked) {
+ Write-Host(' !') -nonewline -foregroundcolor Yellow
+ }
+
+ Write-Host(']') -nonewline -foregroundcolor Yellow
+ }
+}
121 GitTabExpansion.ps1
@@ -0,0 +1,121 @@
+# Initial implementation by Jeremy Skinner
+# http://www.jeremyskinner.co.uk/2010/03/07/using-git-with-windows-powershell/
+
+if(-not (Test-Path Function:\DefaultTabExpansion)) {
+ Rename-Item Function:\TabExpansion DefaultTabExpansion
+}
+
+function script:gitCommands($filter) {
+ $cmdList = @()
+ $output = git help
+ foreach($line in $output) {
+ if($line -match '^ (\S+) (.*)') {
+ $cmd = $matches[1]
+ if($filter -and $cmd.StartsWith($filter)) {
+ $cmdList += $cmd.Trim()
+ }
+ elseif(-not $filter) {
+ $cmdList += $cmd.Trim()
+ }
+ }
+ }
+
+ $cmdList | sort
+}
+
+function script:gitRemotes($filter) {
+ if($filter) {
+ git remote | where { $_.StartsWith($filter) }
+ }
+ else {
+ git remote
+ }
+}
+
+function script:gitLocalBranches($filter) {
+ git branch | foreach {
+ if($_ -match "^\*?\s*(.*)") {
+ if($filter -and $matches[1].StartsWith($filter)) {
+ $matches[1]
+ }
+ elseif(-not $filter) {
+ $matches[1]
+ }
+ }
+ }
+}
+
+function script:gitIndex($filter) {
+ if($GitStatus) {
+ if ($filter) {
+ $GitStatus.Index | Where-Object { $_.StartsWith($filter) }
+ } else {
+ $GitStatus.Index
+ }
+ }
+}
+
+function script:gitFiles($filter) {
+ if($GitStatus) {
+ if ($filter) {
+ $GitStatus.Working | Where-Object { $_.StartsWith($filter) }
+ } else {
+ $GitStatus.Working
+ }
+ }
+}
+
+function gitTabExpansion($line, $lastWord, $lastBlock) {
+ switch -regex ($lastBlock) {
+
+ #Handles git branch -x -y -z <branch name>
+ 'git branch -(d|D) (\S*)$' {
+ gitLocalBranches($matches[2])
+ }
+
+ #handles git checkout <branch name>
+ #handles git merge <brancj name>
+ 'git (checkout|merge) (\S*)$' {
+ gitLocalBranches($matches[2])
+ }
+
+ #handles git <cmd>
+ #handles git help <cmd>
+ 'git (help )?(\S*)$' {
+ gitCommands($matches[2])
+ }
+
+ #handles git push remote <branch>
+ #handles git pull remote <branch>
+ 'git (push|pull) (\S+) (\S*)$' {
+ gitLocalBranches($matches[3])
+ }
+
+ #handles git pull <remote>
+ #handles git push <remote>
+ 'git (push|pull) (\S*)$' {
+ gitRemotes($matches[2])
+ }
+
+ #handles git reset HEAD <path>
+ 'git reset HEAD (\S*)$' {
+ gitIndex($matches[1])
+ }
+
+ #handles git add <path>
+ 'git add (\S*)$' {
+ gitFiles($matches[1])
+ }
+
+ default {
+ DefaultTabExpansion $line $lastWord
+ }
+ }
+}
+
+function TabExpansion($line, $lastWord) {
+ switch -regex ($line) {
+ '(?:^\s*|[;|]\s*)git (.*)' { gitTabExpansion $line $lastWord $matches[0] }
+ default { DefaultTabExpansion $line $lastWord }
+ }
+}
71 GitUtils.ps1
@@ -0,0 +1,71 @@
+# Inspired by Mark Embling
+# http://www.markembling.info/view/my-ideal-powershell-prompt-with-git-integration
+
+function Test-GitDirectory {
+ (git rev-parse --git-dir 2> $null) -ne $null
+}
+
+function Get-GitBranch {
+ if (Test-GitDirectory) {
+ $headRef = (git symbolic-ref HEAD) 2> $null
+ if ($headRef) {
+ Split-Path -Leaf $headRef
+ }
+ }
+}
+
+function Get-GitStatus {
+ if(Test-GitDirectory)
+ {
+ $indexAdded = @()
+ $indexModified = @()
+ $indexDeleted = @()
+ $filesAdded = @()
+ $filesModified = @()
+ $filesDeleted = @()
+ $aheadCount = 0
+
+ $diffIndex = git diff-index -M --name-status --cached HEAD |
+ ConvertFrom-CSV -Delim "`t" -Header 'Status','Path'
+ $diffFiles = git diff-files -M --name-status |
+ ConvertFrom-CSV -Delim "`t" -Header 'Status','Path'
+
+ $grpIndex = $diffIndex | Group-Object Status -AsHashTable
+ $grpFiles = $diffFiles | Group-Object Status -AsHashTable
+
+ if($grpIndex.A) { $indexAdded += $grpIndex.A | %{ $_.Path } }
+ if($grpIndex.M) { $indexModified += $grpIndex.M | %{ $_.Path } }
+ if($grpIndex.R) { $indexModified += $grpIndex.R | %{ $_.Path } }
+ if($grpIndex.D) { $indexDeleted += $grpIndex.D | %{ $_.Path } }
+ if($grpFiles.M) { $filesModified += $grpFiles.M | %{ $_.Path } }
+ if($grpFiles.R) { $filesModified += $grpFiles.R | %{ $_.Path } }
+ if($grpFiles.D) { $filesDeleted += $grpFiles.D | %{ $_.Path } }
+
+ $untracked = git ls-files -o --exclude-standard
+ if($untracked) { $filesAdded += $untracked }
+
+ $output = git status
+
+ $output | foreach {
+ if ($_ -match "^\#.*origin/.*' by (\d+) commit.*") {
+ $aheadCount = $matches[1]
+ }
+ }
+
+ return New-Object PSObject -Property @{
+ Branch = Get-GitBranch
+ AheadBy = $aheadCount
+ HasIndex = [bool]$diffIndex
+ Index = $diffIndex | %{ $_.Path }
+ IndexAdded = $indexAdded
+ IndexModified = $indexModified
+ IndexDeleted = $indexDeleted
+ HasWorking = [bool]$diffFiles
+ Working = ($diffFiles | %{ $_.Path }) + $untracked
+ Added = $filesAdded
+ Modified = $filesModified
+ Deleted = $filesDeleted
+ HasUntracked = [bool]$filesAdded
+ }
+ }
+}

0 comments on commit 3033fa5

Please sign in to comment.
Something went wrong with that request. Please try again.