Last active
August 4, 2017 14:13
-
-
Save tawashley/c185d200ca2eabd62d55 to your computer and use it in GitHub Desktop.
Powershell Aliases with git prompt (scroll down for main alias / profile file)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Git functions | |
# Credit not myself but Mark Embling (http://www.markembling.info/) | |
# Is the current directory a git repository/working copy? | |
function isCurrentDirectoryGitRepository { | |
if ((Test-Path ".git") -eq $TRUE) { | |
return $TRUE | |
} | |
# Test within parent dirs | |
$checkIn = (Get-Item .).parent | |
while ($checkIn -ne $NULL) { | |
$pathToTest = $checkIn.fullname + '/.git' | |
if ((Test-Path $pathToTest) -eq $TRUE) { | |
return $TRUE | |
} else { | |
$checkIn = $checkIn.parent | |
} | |
} | |
return $FALSE | |
} | |
# Get the current branch | |
function gitBranchName { | |
$currentBranch = '' | |
git branch | foreach { | |
if ($_ -match "^\* (.*)") { | |
$currentBranch += $matches[1] | |
} | |
} | |
return $currentBranch | |
} | |
# Extracts status details about the repo | |
function gitStatus { | |
$untracked = $FALSE | |
$added = 0 | |
$modified = 0 | |
$deleted = 0 | |
$ahead = $FALSE | |
$aheadCount = 0 | |
$output = git status | |
$branchbits = $output[0].Split(' ') | |
$branch = $branchbits[$branchbits.length - 1] | |
$output | foreach { | |
if ($_ -match "^\#.*origin/.*' by (\d+) commit.*") { | |
$aheadCount = $matches[1] | |
$ahead = $TRUE | |
} | |
elseif ($_ -match "deleted:") { | |
$deleted += 1 | |
} | |
elseif (($_ -match "modified:") -or ($_ -match "renamed:")) { | |
$modified += 1 | |
} | |
elseif ($_ -match "new file:") { | |
$added += 1 | |
} | |
elseif ($_ -match "Untracked files:") { | |
$untracked = $TRUE | |
} | |
} | |
return @{"untracked" = $untracked; | |
"added" = $added; | |
"modified" = $modified; | |
"deleted" = $deleted; | |
"ahead" = $ahead; | |
"aheadCount" = $aheadCount; | |
"branch" = $branch} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################### | |
# GENERAL ALIASES | |
############################################### | |
New-Alias ll dir | |
New-Alias cl clear | |
Function GoBackUpOne { cd .. } | |
Function GoBackUpTwo { cd ..; cd .. } | |
Function GoToProjects { e:; dir; } | |
Function GoToDesktop { c:; cd .\Desktop} | |
Function GoToHomeDirectory { cd ~ } | |
New-Alias .. GoBackUpOne | |
New-Alias .... GoBackUpTwo | |
New-Alias code GoToProjects | |
New-Alias desktop GoToDesktop | |
New-Alias home GoToHomeDirectory | |
############################################### | |
# GIT ALIASES | |
############################################### | |
#Functions are required for multi-line commands | |
#Bind alias with function call | |
Function GitAdd { git add -A } | |
Function GitBranch { git branch } | |
Function GitCommit { git commit -m } | |
Function GitDiff { git diff } | |
Function GitLog { git log --graph } | |
Function GitMove { git mv } | |
Function GitCheckout { git checkout } | |
Function GitPush { git push } | |
Function GitPushBranch { git push -u origin } | |
Function GitGetStatus { git status } | |
New-Alias ga GitAdd | |
New-Alias gb GitBranch | |
New-Alias gco GitCommit | |
New-Alias gd GitDiff | |
New-Alias glog GitLog | |
New-Alias gmov GitMove | |
New-Alias go GitCheckout | |
New-Alias gpu GitPush | |
New-Alias gpb GitPushBranch | |
New-Alias gs GitGetStatus | |
############################################### | |
# GIT PROMPT | |
############################################### | |
# My preferred prompt for Powershell. | |
# Displays git branch and stats when inside a git repository. | |
. (Resolve-Path ~/Documents/WindowsPowershell/gitutils.ps1) | |
function prompt { | |
$path = [string]$pwd | |
$host.UI.RawUi.WindowTitle = $userLocation | |
Write-Host($env:username + ' ') -nonewline -foregroundcolor Green | |
Write-Host('(' + $path + ')') -nonewline -foregroundcolor Yellow | |
if (isCurrentDirectoryGitRepository) { | |
$status = gitStatus | |
$currentBranch = $status["branch"] | |
Write-Host(' [') -nonewline -foregroundcolor Yellow | |
if ($status["ahead"] -eq $FALSE) { | |
# We are not ahead of origin | |
Write-Host($currentBranch) -nonewline -foregroundcolor Cyan | |
} else { | |
# We are ahead of origin | |
Write-Host($currentBranch) -nonewline -foregroundcolor Red | |
} | |
Write-Host(' +' + $status["added"]) -nonewline -foregroundcolor Yellow | |
Write-Host(' ~' + $status["modified"]) -nonewline -foregroundcolor Yellow | |
Write-Host(' -' + $status["deleted"]) -nonewline -foregroundcolor Yellow | |
if ($status["untracked"] -ne $FALSE) { | |
Write-Host(' !') -nonewline -foregroundcolor Yellow | |
} | |
Write-Host(']') -nonewline -foregroundcolor Yellow | |
} | |
Write-Host(' $') -nonewline -foregroundcolor Green | |
return " " | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment