> -----Original Message-----
> From: Daniel Shahaf [mailto:[email protected]]
> Sent: Tuesday, December 03, 2013 1:14 PM
> To: Andrew Reedick
> Cc: Alfred von Campe; [email protected]
> Subject: Re: Help with post-commit script
>
>
> > svnlook changed ... > $CHANGED_LIST || exit 1 cat $CHANGED_LIST | sed
> > 's/^....//g' | perl -ne 'print "$1$2\n" if
> > /^(trunk)\/|^(branches\/[^\/]*)\//' | sort -u | xargs -n 1 -i svnlook
> > propget $REPOS_PATH my:filelist_prop "{}" > $FILES_TO_REPORT_ON ||
> > exit 1
> >
> > cat $CHANGED_LIST | while read i
>
> 'read' splits on whitespace, so filenames that contain spaces won't
> DTRT.
Close. Read will drop leading/trailing whitespace. It does respect "internal"
whitespace though.
The fix is to set IFS to null:
$ OLD_IFS=$IFS
$ IFS=
$ echo " a b c d.txt " | while read i
> do
> echo ".$i."
> done
. a b c d.txt .
Now if someone has embedded newlines in their filenames then that would be a
problem. At that point you're talking about 'xargs -0 (--null)', sort -z,
(--zero-terminated), using perl to chomp the newline and output a null
character, etc. How does 'svnlook changed' output filenames with embedded
newlines anyway?
>
> > do
> > grep -q "$i" "$FILES_TO_REPORT_ON"
>
> Same for filenames that contain regex metacharacters.
The entries in the filelist svn property would need to be in a regex format.
So everything would be escaped already.
If not, then perl's quotemeta and greps --fixed-strings flags would be of use:
perl -ne 'chomp; print quotemeta($_) . "\n"' file.txt.
Anyway, IME, it's almost always a better idea to use the --xml option when
parsing svn commands, which implies writing a proper perl script. The work can
(probably) be done in bash, but with all the whitespace handling and
potentially multiple layers of interpolation going on, the code can get
unwieldy quickly.
Updated script:
#!/bin/bash
set -o pipefail
REPOS_PATH=$1
REV=$2
SVNLOOK_CMD=/path/to/svnlook
RECIPIENT_LIST=$($SVNLOOK_CMD propget ... my:email_list_prop)
if [[ -z "$RECIPIENT_LIST" ]] then
exit 0
fi
CHANGED_LIST=$(mktemp ...)
FILES_TO_REPORT_ON=$(mktemp ...)
$SVNLOOK_CMD changed ... | perl -ne 'chomp; print quotemeta($_) . "\n"' >
$CHANGED_LIST || exit 1
perl -i -pe 's/^....//' $CHANGED_LIST
perl -ne 'chomp; print quotemeta("$1$2") . "\n" if
/^(trunk)\/|^(branches\/[^\/]*)\//' $CHANGED_LIST | sort -u | xargs -n 1 -i
svnlook propget "$REPOS_PATH" my:filelist_prop "{}" > $FILES_TO_REPORT_ON ||
exit 1
cat $CHANGED_LIST | while read i
do
grep -q -F "$i" "$FILES_TO_REPORT_ON"
if [[ $? -eq 0 ]]
then
svnlook diff -r ... | sendmail -s " $i was touched in an impure
manner" $RECIPIENT_LIST
fi
done