¿Cómo verificar si los archivos comprometidos con SVN están codificados en UTF-8 con ganchos de confirmación previa?
-
27-10-2019 - |
Pregunta
Estoy tratando de escribir un script de enlace de pre-compromiso SVN en Linux Bash que rechazará un compromiso si los archivos no se pueden analizar como UTF-8.
Hasta ahora he escrito este script:
REPOS="$1"
TXN="$2"
SVNLOOK=/usr/bin/svnlook
ICONV=/usr/bin/iconv
# Make sure that all files to be committed are encoded in UTF-8
for FILE in $($SVNLOOK changed -t "$TXN" "$REPOS"); do
if [$ICONV -f UTF-8 $FILE -o /dev/null]; then
echo "Only UTF-8 files can be committed ("$FILE")" 1>&2
exit 1
fi
# All checks passed, so allow the commit.
exit 0
El problema es que iconv requiere la ruta a los archivos comprometidos (o el texto en alguna otra forma) y no sé cómo obtenerlo.
¿Alguien puede ayudar?
Solución
Utilice svnlook cat
para obtener el contenido de un archivo de una transacción:
$SVNLOOK cat -t "$TXN" "$REPOS" "$FILE"
Otros consejos
Por cierto, hay un problema en esta respuesta . Debe probar el resultado del comando $ SVNLOOK ($?) Porque la instrucción "exit 1" está en un proceso secundario, por lo que el script nunca bloqueará la confirmación:
#!/bin/bash
REPOS="$1"
TXN="$2"
SVNLOOK=/usr/bin/svnlook
ICONV=/usr/bin/iconv
# Make sure that all files to be committed are encoded in UTF-8.
$SVNLOOK changed -t "$TXN" "$REPOS" | while read changeline; do
# Get just the file (not the add / update / etc. status).
file=${changeline:4}
# Only check source files.
if [[ $file == *.cpp || $file == *.hpp || $file == *.c || $file == *.h ]] ; then
$SVNLOOK cat -t "$TXN" "$REPOS" $file 2>&1 | iconv -f UTF-8 -t UTF-8 >& /dev/null
if [ $? -ne 0 ] ; then
exit 1
fi
fi
done
test $? -eq 1 && echo "Only UTF-8 files can be committed" 1>&2 && exit 1
exit 0
Según el script de la pregunta original y esta respuesta , aquí hay un gancho de confirmación previa que incluye todo estojuntos:
#!/bin/bash
REPOS="$1"
TXN="$2"
SVNLOOK=/usr/bin/svnlook
ICONV=/usr/bin/iconv
# Make sure that all files to be committed are encoded in UTF-8.
$SVNLOOK changed -t "$TXN" "$REPOS" | while read changeline; do
# Get just the file (not the add / update / etc. status).
file=${changeline:4}
# Only check source files.
if [[ $file == *.cpp || $file == *.hpp || $file == *.c || $file == *.h ]] ; then
$SVNLOOK cat -t "$TXN" "$REPOS" "$file" 2>&1 | iconv -f UTF-8 -t UTF-8 >& /dev/null
if [ $? -ne 0 ] ; then
echo "Only UTF-8 files can be committed ("$file")" 1>&2
exit 1
fi
fi
done
# All checks passed, so allow the commit.
exit 0