PowerShell/docs/search-repo.md
2024-11-08 12:40:31 +01:00

2.7 KiB

The search-repo.ps1 Script

This PowerShell script searches for the given text pattern in a Git repository.

Parameters

/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

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

https://github.com/fleschutz/PowerShell

Script Content

<#
.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 as of 11/08/2024 12:40:22)