Skip to content

Commit

Permalink
warn about deleted used aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
ztrhgf committed Oct 12, 2021
1 parent 2acc029 commit 22e18e9
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion repo_content_set_up/.githooks/pre-commit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ try {


#
# warn about deleted ps1 script (that defines function for module generation), in case it is used somewhere in repository
# warn about deleted function & alias in case it is used somewhere in repository
if ($commitedDeletedPs1) {
$commitedDeletedPs1 = $commitedDeletedPs1 -replace "/", "\"
$commitedDeletedPs1 | Where-Object { $_ -match "scripts2module\\" } | ForEach-Object {
Expand All @@ -808,6 +808,35 @@ try {

_WarningAndExit "Deleted function $funcName is mentioned in following scripts:`n$($fileFuncUsed -join "`n")"
}

# get all aliases defined in last function version
$functionScriptUnixPath = $_ -replace "\\", "/"
$lastCommitContent = _startProcess git "show HEAD:$functionScriptUnixPath"
if (!$lastCommitContent -or $lastCommitContent -match "^fatal: ") {
Write-Warning "Previous version of function $funcName cannot be found (to check deleted aliases)."
} else {
$gitAST = [System.Management.Automation.Language.Parser]::ParseInput(($lastCommitContent -join "`n"), [ref]$null, [ref]$null)

$deletedAlias = _getAliasAST $gitAST $funcName
}

if ($deletedAlias) {
$deletedAlias | % {
$alias = $_
$escFuncName = [regex]::Escape($funcName)
$escAlias = [regex]::Escape($alias)
# get all files where changed function is mentioned (even in comments)
$fileUsed = git.exe grep --ignore-case -l "\b$escAlias\b"
# exclude scripts where this alias is defined
$fileUsed = $fileUsed | Where-Object { $_ -notmatch "/$escFuncName\.ps1" }

if ($fileUsed) {
$fileUsed = $fileUsed -replace "/", "\"

_WarningAndExit "Alias '$alias' of function $funcName was deleted, but is still used in following scripts:`n$($fileUsed -join "`n")"
}
}
}
}
#TODO kontrola funkci v profile.ps1? viz AST sekce https://devblogs.microsoft.com/scripting/learn-how-it-pros-can-use-the-powershell-ast/
}
Expand Down

0 comments on commit 22e18e9

Please sign in to comment.