Skip to content

Commit 2a11234

Browse files
author
Markus Fleschutz
committed
Added search-repo.ps1
1 parent dc7abd6 commit 2a11234

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

scripts/search-repo.ps1

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<#
2+
.SYNOPSIS
3+
Searches for text in a repo
4+
.DESCRIPTION
5+
This PowerShell script searches for the given text pattern in a Git repository.
6+
.PARAMETER textPattern
7+
Specifies the text pattern to search for
8+
.PARAMETER path
9+
Specifies the file path to the local Git repository
10+
.EXAMPLE
11+
PS> ./search-repo.ps1 UFO
12+
list-calendar.ps1: Write-Host (" " * 4 * [int](Get-Date $day -uformat %u)) -NoNewLine
13+
list-calendar.ps1: $dayOffset = [int](Get-Date -day 1 -month ($month + $i) -year $year -uformat %u)
14+
.LINK
15+
https://github.com/fleschutz/PowerShell
16+
.NOTES
17+
Author: Markus Fleschutz | License: CC0
18+
#>
19+
20+
param([string]$textPattern = "", [string]$path = "$PWD")
21+
22+
23+
try {
24+
if ($textPattern -eq "" ) { $textPattern = Read-Host "Enter the text pattern, e.g. 'UFO'" }
25+
26+
if (-not(Test-Path "$path" -pathType container)) { throw "Can't access Git repository at: $path" }
27+
28+
$null = (git --version)
29+
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
30+
31+
& git -C "$path" grep $textPattern
32+
if ($lastExitCode -ne "0") { throw "'git grep' failed with exit code $lastExitCode" }
33+
34+
exit 0 # success
35+
} catch {
36+
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
37+
exit 1
38+
}

0 commit comments

Comments
 (0)