mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-11-21 23:43:25 +01:00
95 lines
2.7 KiB
Markdown
95 lines
2.7 KiB
Markdown
|
Script: *search-repo.ps1*
|
||
|
========================
|
||
|
|
||
|
This PowerShell script searches for the given text pattern in a Git repository.
|
||
|
|
||
|
Parameters
|
||
|
----------
|
||
|
```powershell
|
||
|
/home/markus/Repos/PowerShell/scripts/search-repo.ps1 [[-textPattern] <String>] [[-path] <String>] [<CommonParameters>]
|
||
|
|
||
|
-textPattern <String>
|
||
|
Specifies the text pattern to search for
|
||
|
|
||
|
Required? false
|
||
|
Position? 1
|
||
|
Default value
|
||
|
Accept pipeline input? false
|
||
|
Accept wildcard characters? false
|
||
|
|
||
|
-path <String>
|
||
|
Specifies the file path to the local Git repository
|
||
|
|
||
|
Required? false
|
||
|
Position? 2
|
||
|
Default value "$PWD"
|
||
|
Accept pipeline input? false
|
||
|
Accept wildcard characters? false
|
||
|
|
||
|
[<CommonParameters>]
|
||
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
||
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
||
|
```
|
||
|
|
||
|
Example
|
||
|
-------
|
||
|
```powershell
|
||
|
PS> ./search-repo.ps1 UFO
|
||
|
list-calendar.ps1: Write-Host (" " * 4 * [int](Get-Date $day -uformat %u)) -NoNewLine
|
||
|
list-calendar.ps1: $dayOffset = [int](Get-Date -day 1 -month ($month + $i) -year $year -uformat %u)
|
||
|
|
||
|
```
|
||
|
|
||
|
Notes
|
||
|
-----
|
||
|
Author: Markus Fleschutz | License: CC0
|
||
|
|
||
|
Related Links
|
||
|
-------------
|
||
|
https://github.com/fleschutz/PowerShell
|
||
|
|
||
|
Script Content
|
||
|
--------------
|
||
|
```powershell
|
||
|
<#
|
||
|
.SYNOPSIS
|
||
|
Searches for text in a repo
|
||
|
.DESCRIPTION
|
||
|
This PowerShell script searches for the given text pattern in a Git repository.
|
||
|
.PARAMETER textPattern
|
||
|
Specifies the text pattern to search for
|
||
|
.PARAMETER path
|
||
|
Specifies the file path to the local Git repository
|
||
|
.EXAMPLE
|
||
|
PS> ./search-repo.ps1 UFO
|
||
|
list-calendar.ps1: Write-Host (" " * 4 * [int](Get-Date $day -uformat %u)) -NoNewLine
|
||
|
list-calendar.ps1: $dayOffset = [int](Get-Date -day 1 -month ($month + $i) -year $year -uformat %u)
|
||
|
.LINK
|
||
|
https://github.com/fleschutz/PowerShell
|
||
|
.NOTES
|
||
|
Author: Markus Fleschutz | License: CC0
|
||
|
#>
|
||
|
|
||
|
param([string]$textPattern = "", [string]$path = "$PWD")
|
||
|
|
||
|
|
||
|
try {
|
||
|
if ($textPattern -eq "" ) { $textPattern = Read-Host "Enter the text pattern, e.g. 'UFO'" }
|
||
|
|
||
|
if (-not(Test-Path "$path" -pathType container)) { throw "Can't access Git repository at: $path" }
|
||
|
|
||
|
$null = (git --version)
|
||
|
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
|
||
|
|
||
|
& git -C "$path" grep $textPattern
|
||
|
if ($lastExitCode -ne "0") { throw "'git grep' failed with exit code $lastExitCode" }
|
||
|
|
||
|
exit 0 # success
|
||
|
} catch {
|
||
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||
|
exit 1
|
||
|
}
|
||
|
```
|
||
|
|
||
|
*(generated by convert-ps2md.ps1 using the comment-based help of search-repo.ps1 as of 11/08/2024 12:34:55)*
|