3
I’m writing a git hook to check if the commit message matches an array of words.
$array = @('Adiciona', 'Altera', 'Corrige', 'Refatora', 'Remove')
$msg = "Coloca coisas ao projeto."
if (!($msg.toLower().Contains($array.toLower()))){
Write-Host ""
Write-Host -ForegroundColor "DarkGreen" "Mensagem de confirmação segue o formato."
Write-Host "`r`nConfirmando alterações ao git.`r`n"
return 0
}
elseif ($msg.toLower().Contains($array.toLower())){
Write-Host ""
Write-Host -ForegroundColor "DarkRed" "Mensagem de confirmação não segue o formato!"
Write-Host "`r`nA mensagem de confirmação precisa começar com as seguintes palavras:`r`n"
$array | Sort
Write-Host "`r`nAlterações não foram confirmadas ao git.`r`n"
return 1
}
At the value in $msg
, is expected to return the script False
and display the content of elseif
, but it returns the content of if
as if $msg
were True
. What I’m doing wrong?
I’m sorry if I wasn’t clear, I’ve been trying to figure this out since dawn and neither vision nor cognition is working out.
– João Antonio Santana
It seems that you are checking whether a string contains an array... it would not be better to go through that array and check that each element of it is in the string?
– Gabriel Hardoim
@Gabrielhardoim, thanks for the comment. I did it with
ForEach
and the result was a return ofFalse
five times, one for each element in the array.– João Antonio Santana
Show! test with another string to see if it returns true as well!
– Gabriel Hardoim