Skip to content

Instantly share code, notes, and snippets.

@SingingBush
Created July 3, 2017 11:08
Show Gist options
  • Save SingingBush/1011b9e9d54de8a2c3c489ec8d7bc4c8 to your computer and use it in GitHub Desktop.
Save SingingBush/1011b9e9d54de8a2c3c489ec8d7bc4c8 to your computer and use it in GitHub Desktop.
list local git repos using powershell
Function List-GitRepos {
<#
.SYNOPSIS
List local Git repositories
.DESCRIPTION
Use this command to find Git repositories in the specified folder. It is assumed that you have the Git command line tools already installed.
.PARAMETER Path
The top level path to search.
.EXAMPLE
PS List-GitRepos -path /Users/me/dev
Repository Branch LastAuthor LastLog
---------- ------ ---------- -------
/Users/me/dev/DLangKoans master andrea 07/04/2016 14:06:52
/Users/me/dev/gradle-intellij-plugin master Alexander Zolotov 30/03/2017 13:41:02
/Users/me/dev/gradle-jflex-plugin-fork master singingbush 08/05/2017 19:00:30
/Users/me/dev/intellij-d-plugin feature/dub-tool-window singingbush 29/07/2016 17:22:37
or pipe through to other functions to perform searches:
List-GitRepos -path /Users/me/dev | Where-Object -Property "Branch" -EQ "develop"
.NOTES
NAME : List-GitRepos
VERSION : 1.0.0
LAST UPDATED: 3rd July 2017
.INPUTS
[string]
.OUTPUTS
[pscustomobject]
#>
[cmdletbinding()]
Param(
[Parameter(Position = 0, HelpMessage = "The top level path to search")]
[ValidateScript({
if (Test-Path $_) {
$True
} else {
Throw "Cannot validate path $_"
}
})]
[alias("p", "path")]
[string]$rootPath = "."
)
Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)"
Write-Verbose "[PROCESS] Searching $(Convert-Path -path $rootPath) for Git repositories"
dir -path $rootPath -Hidden -filter .git -Recurse |
select @{ Name="Repository"; Expression={Convert-Path $_.PSParentPath} },
@{ Name="Branch"; Expression = {
#save current location
Push-Location
#change location to the repository
Set-Location -Path (Convert-Path -path ($_.psparentPath))
#get current branch with out the leading asterisk
(git branch).where({$_ -match "\*"}).Substring(2)
}
},
@{ Name="LastAuthor"; Expression = { git log --date=local --format=%an -1 }},
@{ Name="LastLog"; Expression = {
(git log --date=iso --format=%ad -1 ) -as [datetime]
#change back to original location
Pop-Location
}
}
Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)"
} #end function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment