Skip to content

Instantly share code, notes, and snippets.

@Sidneys1
Last active February 23, 2016 16:07
Show Gist options
  • Save Sidneys1/ffcb560c4cbcfd2df094 to your computer and use it in GitHub Desktop.
Save Sidneys1/ffcb560c4cbcfd2df094 to your computer and use it in GitHub Desktop.
Point at a root directory and run `Update-Git`. Will recursively search subdirectories and fetch/pull all branches that don't start with `local`
function Write-Color($string,[switch]$nonewline) {
# Reference:
# 0 = Black
# 1 = DarkBlue
# 2 = DarkGreen
# 3 = DarkCyan
# 4 = DarkRed
# 5 = DarkMagenta
# 6 = DarkYellow
# 7 = Gray
# 8 = DarkGray
# 9 = Blue
# a = Green
# b = Cyan
# c = Red
# d = Magenta
# e = Yellow
# f = White
$sections = $string -split "~~"
foreach ($section in $sections) {
if ($section.length -eq 0) {continue}
$color = [ConsoleColor]([Convert]::ToInt16($section[0], 16))
write-host $section.substring(1) -foregroundcolor $color -nonewline
}
if(-not $nonewline) {
write-host;
}
}
function Update-Git($base,$lastlen) {
$popd = $false
$currbranch = $false
$top=$false
$pre = ""
try {
if(-not $base){$base = $pwd;[System.Console]::CursorVisible = $false;$top=$true}
$baselen = $base.path.lastindexof("\")
$pre = $pwd.path.substring($baselen, ($pwd.path.lastindexof("\")+1)-$baselen)
$post = $pwd.path.substring($pwd.path.lastindexof("\")+1)
$search = "Searching ...$pre$post";
$len = $search.length;
if($lastlen -gt $search.length) {$search += " " * ($lastlen - $search.length)}
$search += "`r"
write-host $search -foregroundcolor darkgray -nonewline;
if (Test-path ".git" -type container) {
write-host "Fetching $post $(if($pre){" "*$pre.length})`r" -foregroundcolor yellow -nonewline;
. git fetch -p 1>$fetch 2>$fetcherr;
if ($lastexitcode -eq 0 -or ($fetcherr)) {
if ($fetch) {
Write-Color "8`r...$pre~~f$post "
$branches = . git branch
$currbranch = $branches|?{$_.contains('*')}[0]
$currbranch = $currbranch.replace('*', '').trim()
$branches = $branches|% {$_.replace('*', '').trim()}
foreach ($branch in $branches) {
$prefix = "f`t["
if ($branch -like $currbranch) { $prefix += "~~a*" }
if ($branch.startswith("local")) {
write-color "$prefix~~8$branch~~f] ~~4(Skipping 'local' branch)"
continue
} else { write-color "$prefix~~b$branch~~f]" }
write-host "`tCheckout..." -foregroundcolor yellow -nonewline
. git checkout $branch *>$null
write-host "`r`tPulling... " -foregroundcolor yellow -nonewline
$stdout = . git pull 2>$null;
if ($LastExitCode -eq 0) {
$stdout = $stdout.trim() -split "`r`n" |% {$_.replace("`n", "`n`t ")}
$stdout |%{write-host "`r`t> $_" -foregroundcolor green}
} else {
write-host "`r`t> Could not pull branch" -foregroundcolor red
}
}
. git checkout $currbranch *>$null
$currbranch = $false
} else {
Write-Color "8`r...$pre~~f$post~~7 was up-to-date"
}
} else { # $LastExitCode -ne 0 -or $fetcherr
write-color "4`r...$pre~~c$post~~4 Could not be fetched: 0x$([Convert]::ToString($LastExitCode, 16))"
}
}
foreach ($dir in (ls -Directory)) {
pushd $dir.Name;
$popd = $true
Update-Git $base $len;
popd;
$popd = $false
}
write-host "`r$(" "*$len)`r" -nonewline
} finally {
write-host "`r$(" "*$len)`r" -nonewline
if ($currbranch) { . git checkout $currbranch *>$null }
if ($popd) { popd }
if($top){
[System.Console]::CursorVisible = $true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment