Last active
October 27, 2016 15:51
-
-
Save jmrnilsson/7fc78534b18b63cd63199c1ae7bd0ea4 to your computer and use it in GitHub Desktop.
Git grep all repositories in current directory
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
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$True,Position=1)] | |
[string]$expression | |
) | |
$originalPath = $pwd.Path | |
$repositories = gci | ?{ $_.PSIsContainer } | foreach { $_.Name } | |
# $cmd = "cd .\{0}\; git --no-pager grep -n ""{1}""; cd {2}" | |
# foreach ($r in $repositories) { Invoke-Expression $($cmd -f $r, $expression, $originalPath)} | |
$command = "cd .\{{0}}\; git --no-pager grep -n ""{0}""; cd {1}" -f $expression, $originalPath | |
foreach ($repository in $repositories) { Invoke-Expression $($command -f $repository)} |
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
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$True,Position=1)] | |
[string]$expression | |
) | |
$quotes = [regex]::matches($expression, '"'); | |
$singleQuotes = [regex]::matches($expression, "'"); | |
If ($quotes.Count % 2 -ne 0 -or $singleQuotes.Count % 2 -ne 0) { | |
Write-Error "Avoid using quotes if possible" | |
exit 1 | |
} | |
$originalPath = $pwd.Path | |
$repositories = gci | ?{ $_.PSIsContainer } | foreach { $_.Name } | |
$command = "cd .\""{{0}}""\; git --no-pager grep -n ""{0}""; cd ""{1}""" -f $expression, $originalPath | |
foreach ($repository in $repositories) { Invoke-Expression $($command -f $repository)} |
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
# ps> GrepRepositories.ps1 "venv" | |
# $ powershell ./GrepRepositories.ps1 "var" | |
# $ powershell ./GrepRepositories.ps1 "var" | grep iter | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$True,Position=1)] | |
[string]$expression, | |
[Parameter(Position=2)] | |
[string]$wildcard | |
) | |
$safe = [regex]::matches($expression, '"'), | |
[regex]::matches($expression, "'"), | |
[regex]::matches($wildcard, '"'), | |
[regex]::matches($wildcard, "'") | |
foreach ($quoted in $safe) { If ($quoted.Count % 2 -ne 0) { | |
Write-Error "Avoid using quotes if possible" | |
exit 1 | |
}} | |
$originalPath = $($pwd.Path) | |
$repositories = gci | ?{ $_.PSIsContainer } | foreach { $_.Name } | |
$formattedExpr = If ($wildcard -eq $null) {'"{0}"' -f $expression} Else {'"{0}" -- "{1}"' -f $expression, $wildcard} | |
$command = 'cd .\"{{0}}"\; git --no-pager grep -n {0}; cd "{1}"' -f $formattedExpr, $originalPath | |
foreach ($repository in $repositories) { Invoke-Expression $($command -f $repository)} | |
Write-Output $("`r`nExpression: '{0}'`r`nWildCard: '{1}'" -f $expression, $wildcard) |
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
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$True,Position=1)] | |
[string]$expression, | |
[Parameter(Position=2)] | |
[string]$wildcard | |
) | |
$safe = [regex]::matches($expression, '"'), | |
[regex]::matches($expression, "'"), | |
[regex]::matches($wildcard, '"'), | |
[regex]::matches($wildcard, "'") | |
foreach ($quoted in $safe) { If ($quoted.Count % 2 -ne 0) { | |
Write-Error "Avoid using quotes if possible" | |
exit 1 | |
}} | |
$restorePath = "cd {0}" -f $($pwd.Path) | |
$repos = gci | ?{ $_.PSIsContainer } | foreach { $_.FullName } | |
$formattedExpression = '"{0}"' -f $expression | |
If ($wildcard -ne $null) { $formattedExpression += ' -- "{0}"' -f $wildcard } | |
$command = 'cd "{{0}}"; git --no-pager grep -n {0}' -f $formattedExpression | |
foreach ($repo in $repos) { Invoke-Expression $($command -f $repo)} | |
Invoke-Expression $restorePath | |
Write-Output $("`r`nExpression: '{0}'`r`nWildCard: '{1}'" -f $expression, $wildcard) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment