Last active
December 18, 2024 18:59
-
-
Save tetri/e01abf8fbb49c79fbf55dc0f12396d86 to your computer and use it in GitHub Desktop.
Este script em PowerShell automatiza o processo de verificar e descartar alterações em arquivos dentro de uma pasta e suas subpastas em um repositório SVN, quando as diferenças são apenas espaços em branco e quebras de linha extras.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[string]$folderPath, | |
[switch]$WhatIf | |
) | |
function Normalize-Content { | |
param ( | |
[string]$content | |
) | |
# Remove espaços em branco extras, tabs e normaliza as quebras de linha | |
return ($content -replace '\t', ' ') -replace '\s+', '' -replace '[\r\n]+', '' | |
} | |
function Check-DiffIgnoringWhitespaceAndLineBreaks { | |
param ( | |
[string]$file | |
) | |
try { | |
# Obtém o conteúdo do arquivo original do repositório | |
$originalContent = svn cat $file | |
# Obtém o conteúdo do arquivo modificado | |
$modifiedContent = Get-Content $file -Raw | |
# Normaliza os conteúdos | |
$originalNormalized = Normalize-Content -content $originalContent | |
$modifiedNormalized = Normalize-Content -content $modifiedContent | |
return $originalNormalized -eq $modifiedNormalized | |
} catch { | |
Write-Host "Erro ao verificar diferenças no arquivo: $file. Detalhes: $_" -ForegroundColor Red | |
return $false | |
} | |
} | |
function Revert-File { | |
param ( | |
[string]$file | |
) | |
if ($WhatIf) { | |
Write-Host "[Simulação] Alterações revertidas para o arquivo: $file" | |
} else { | |
svn revert $file | |
Write-Host "Alterações revertidas para o arquivo: $file" -ForegroundColor Yellow | |
} | |
} | |
function Process-File { | |
param ( | |
[string]$filePath | |
) | |
if (Check-DiffIgnoringWhitespaceAndLineBreaks -file $filePath) { | |
Write-Host "Revertendo: $filePath" | |
Revert-File -file $filePath | |
} else { | |
Write-Host "Mantido: $filePath" | |
} | |
} | |
if (-Not (Test-Path $folderPath)) { | |
Write-Host "O caminho fornecido não é válido: $folderPath" -ForegroundColor Red | |
exit 1 | |
} | |
# Encontrar todos os arquivos modificados na pasta e subpastas | |
try { | |
$svnStatusOutput = svn status $folderPath | |
$modifiedFiles = $svnStatusOutput | Where-Object { $_ -match '^[M]' } | ForEach-Object { $_.Trim().Substring(8) } | |
} catch { | |
Write-Host "Erro ao obter status do SVN: $_" -ForegroundColor Red | |
exit 1 | |
} | |
# Processar cada arquivo modificado | |
foreach ($file in $modifiedFiles) { | |
Process-File -filePath $file | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment