Skip to content

Instantly share code, notes, and snippets.

@khebbie
Forked from markembling/gitutils.ps1
Last active December 13, 2015 22:39
Show Gist options
  • Save khebbie/4986469 to your computer and use it in GitHub Desktop.
Save khebbie/4986469 to your computer and use it in GitHub Desktop.
# Git functions
# 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 {
return Split-Path -Leaf (git symbolic-ref HEAD)
}
# 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}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment