@@ -6,9 +6,11 @@ CODENARC_RESULT="result.txt"
66VIOLATIONS_FLAG=" /tmp/found_violations.txt"
77ALL_DIFF=" /tmp/all_diff.txt"
88CHANGED_LINES_CACHE=" /tmp/changed_lines.txt"
9+ CHANGED_FILES_CACHE=" /tmp/changed_files.txt"
10+ TEMP_VIOLATIONS=" /tmp/temp_violations.txt"
911
1012cleanup_temp_files () {
11- rm -f " $CODENARC_RESULT " " $VIOLATIONS_FLAG " " $ALL_DIFF " " $CHANGED_LINES_CACHE " > /dev/null 2>&1
13+ rm -f " $CODENARC_RESULT " " $VIOLATIONS_FLAG " " $ALL_DIFF " " $CHANGED_LINES_CACHE " " $CHANGED_FILES_CACHE " " $TEMP_VIOLATIONS " > /dev/null 2>&1
1214}
1315
1416run_codenarc () {
@@ -52,11 +54,17 @@ generate_all_diff() {
5254
5355build_changed_lines_cache () {
5456 true > " $CHANGED_LINES_CACHE "
57+ true > " $CHANGED_FILES_CACHE "
5558 current_file=" "
5659
60+ [ ! -s " $ALL_DIFF " ] && return 0
61+
5762 while read -r line; do
5863 if echo " $line " | grep -q " ^diff --git" ; then
5964 current_file=$( echo " $line " | sed ' s|^diff --git a/\(.*\) b/.*|\1|' )
65+ if [ -n " $current_file " ]; then
66+ echo " $current_file " >> " $CHANGED_FILES_CACHE "
67+ fi
6068 elif echo " $line " | grep -q " ^@@" ; then
6169 if [ -n " $current_file " ]; then
6270 range=$( echo " $line " | sed ' s/.*+\([0-9,]*\).*/\1/' )
@@ -67,6 +75,9 @@ build_changed_lines_cache() {
6775 start=" $range "
6876 count=1
6977 fi
78+
79+ case " $start " in ' ' |* [!0-9]* ) continue ;; esac
80+ case " $count " in ' ' |* [!0-9]* ) continue ;; esac
7081
7182 i=" $start "
7283 while [ " $i " -lt " $(( start + count)) " ]; do
@@ -102,32 +113,32 @@ file_matches_patterns() {
102113 return 1
103114}
104115
105- parse_diff_range () {
106- range=$( echo " $1 " | sed ' s/.*+\([0-9,]*\).*/\1/' )
107-
108- if echo " $range " | grep -q " ," ; then
109- echo " $( echo " $range " | cut -d' ,' -f1) $( echo " $range " | cut -d' ,' -f2) "
110- else
111- echo " $range 1"
112- fi
113- }
114-
115116line_is_in_changed_range () {
116117 target_line=" $1 "
117118 file=" $2 "
118119
119120 grep -q " ^$file :$target_line $" " $CHANGED_LINES_CACHE "
120121}
121122
123+ file_was_changed () {
124+ file=" $1 "
125+ grep -q " ^$file $" " $CHANGED_FILES_CACHE "
126+ }
127+
122128check_blocking_rules () {
123- echo " 🔎 Verificando violacoes bloqueantes (priority 1)..."
129+ echo " 🔎 Verificando violações bloqueantes (priority 1)..."
130+
131+ [ ! -f " $CODENARC_RESULT " ] && echo " ❌ Arquivo de resultado não encontrado" && return 1
124132
125133 p1_count=$( get_p1_violations_count)
126134 echo " 📊 Total de P1 encontradas: $p1_count "
127135
128- [ " $p1_count " -eq 0 ] && echo " ✅ Nenhuma violacao P1 detectada." && return 0
136+ if [ " $p1_count " -eq 0 ]; then
137+ echo " ✅ Nenhuma P1 detectada → merge permitido (análise de diff desnecessária)"
138+ return 0
139+ fi
129140
130- echo " 🔎 Gerando cache de linhas alteradas..."
141+ echo " ⚠️ P1s detectadas → verificando se estão em linhas alteradas..."
131142 generate_all_diff
132143 build_changed_lines_cache
133144
@@ -138,32 +149,38 @@ check_blocking_rules() {
138149
139150 echo " 0" > " $VIOLATIONS_FLAG "
140151
141- grep -E ' :[0-9]+:' " $CODENARC_RESULT " | while IFS=: read -r file line rest; do
152+ grep -E ' :[0-9]+:|:[0-9]*:' " $CODENARC_RESULT " > " $TEMP_VIOLATIONS "
153+
154+ [ ! -s " $TEMP_VIOLATIONS " ] && echo " ✅ Nenhuma violação encontrada no resultado" && return 0
155+
156+ while IFS=: read -r file line rest; do
142157 [ -z " $file " ] && continue
143158
144- echo " $file " | grep -q ' \.groovy$' || continue
145-
146159 if ! file_matches_patterns " $file " " $allowed_patterns " ; then
147160 continue
148161 fi
149162
150- if line_is_in_changed_range " $line " " $file " ; then
151- echo " 🚨 Violacao P1 em linha alterada: $file :$line "
163+ if [ -z " $line " ]; then
164+ if file_was_changed " $file " ; then
165+ echo " 📍 Violação file-based em arquivo alterado: $file "
166+ echo " 1" > " $VIOLATIONS_FLAG "
167+ break
168+ fi
169+ elif line_is_in_changed_range " $line " " $file " ; then
170+ echo " 📍 Violação em linha alterada: $file :$line "
152171 echo " 1" > " $VIOLATIONS_FLAG "
172+ break
153173 fi
154- done
174+ done < " $TEMP_VIOLATIONS "
155175
156176 violations_in_diff=$( cat " $VIOLATIONS_FLAG " )
157177
158178 if [ " $violations_in_diff " -eq 1 ]; then
159- # echo "⛔ Violacoes P1 encontradas em linhas alteradas - bloqueando merge"
160- # echo "💡 Corrija as violacoes ou utilize bypass autorizado"
161- # exit 1
162-
163- echo " ⛔ Violacoes P1 encontradas em linhas alteradas - DEVERIA bloquear merge"
179+ echo " ⛔ P1s existem E há violações em linhas alteradas → DEVERIA bloquear merge"
164180 echo " 🔧 Exit desabilitado temporariamente para monitoramento"
181+ # exit 1
165182 else
166- echo " ⚠️ Violacoes P1 existem mas fora das linhas alteradas - merge permitido"
183+ echo " ✅ P1s existem mas FORA das linhas alteradas → merge permitido"
167184 fi
168185}
169186
@@ -178,4 +195,4 @@ run_codenarc
178195run_reviewdog
179196check_blocking_rules
180197
181- echo " 🏁 Concluido com sucesso"
198+ echo " 🏁 Concluído com sucesso"
0 commit comments