Skip to content

Instantly share code, notes, and snippets.

@JeremySkinner
Last active June 13, 2018 08:54
Show Gist options
  • Save JeremySkinner/988017f1a16207157309227568c29b53 to your computer and use it in GitHub Desktop.
Save JeremySkinner/988017f1a16207157309227568c29b53 to your computer and use it in GitHub Desktop.
Original gitutils that were the precusor of posh-git. Created by Mark Embling and Jeremy Skinner in 2009
function gitPrompt {
if(gitIsDirectory) {
write-host ' [' -nonewline -foregroundcolor Yellow
$status = gitStatus
$currentBranch = $status['branch']
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
}
if($status["added"] -gt 0) {
Write-Host(' +' + $status["added"]) -nonewline -foregroundcolor Yellow
}
if($status["modified"] -gt 0) {
Write-Host(' ~' + $status["modified"]) -nonewline -foregroundcolor Yellow
}
if($status["deleted"] -gt 0) {
Write-Host(' -' + $status["deleted"]) -nonewline -foregroundcolor Yellow
}
if ($status["untracked"] -ne $FALSE) {
Write-Host(' !') -nonewline -foregroundcolor Yellow
}
write-host ']' -nonewline -foregroundcolor Yellow
}
}
# Git functions
# Mark Embling (http://www.markembling.info/)
# Is the current directory a git repository/working copy?
function gitIsDirectory {
if ((Test-Path ".git") -eq $TRUE) {
return $TRUE
}
if(test-path ".hg") {
return $false; #short circuit hg repo
}
# 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:") {
$modified += 1
}
elseif ($_ -match "added:") {
$added += 1
}
elseif ($_ -match "Untracked files:") {
$untracked = $TRUE
}
}
return @{"untracked" = $untracked;
"added" = $added;
"modified" = $modified;
"deleted" = $deleted;
"ahead" = $ahead;
"aheadCount" = $aheadCount;
"branch" = $branch}
}
# Tab expansion by Jeremy Skinner
function gitTabExpansion($lastBlock) {
switch -regex ($lastBlock) {
# List commands
#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])
}
}
}
function 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 gitRemotes($filter) {
if($filter) {
git remote | where { $_.StartsWith($filter) }
}
else {
git remote
}
}
function gitLocalBranches($filter) {
git branch | foreach {
if($_ -match "^\*?\s*(.*)") {
if($filter -and $matches[1].StartsWith($filter)) {
$matches[1]
}
elseif(-not $filter) {
$matches[1]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment