SVN Hooks to require log message, limit size and file type

219 views
Skip to first unread message

David Xie

unread,
Jan 11, 2011, 8:46:58 PM1/11/11
to us...@subversion.apache.org
Hello,
 
I am looking for a hook to require log message when commit. I succeded to do it.
 
At the same time, I want to limit size less than 10MB and forbid .zip .7z .rar file type.
I found some hooks from internet, tried multi times but all failed. I could always commit big than 10M files and zip files. I struggled two days but could not resolve it.
 
I post my pre-commit at the end.
RHEL 5.3
SVN 1.6.15
Apache 2.2.14
Java 1.6.0_17
 
Would someone give me some help? Great thanks!
  
Regards,
David
 
pre-commit :
#!/bin/sh
REPOS="$1"
TXN="$2"
MAX_SIZE=10240000
MIN_LOG=10
FILTER='\.(zip|rar|o|obj|tar|gz)$'
SVNLOOK=/local/svnroot/subversion/bin/svnlook
# Make sure that the log message contains some text.
LOGMSG=$($SVNLOOK log -t "$TXN" "$REPOS" | grep "[a-zA-Z0-9]" | wc -c)
if [ "$LOGMSG" -lt $MIN_LOG ]
then
echo -e "Please enter at least 10 characters for log message. Questions, contact David" >&2
exit 1
fi
 
files=$($SVNLOOK changed -t $TXN $REPOS |awk '{print $2}')
for f in $files
do

#check file type
if echo $f|tr A-Z a-z|grep -Eq $FILTER
then
echo "File $f is not allow ($FILTER) file" >&2
exit 1
fi
 
#check file size
filesize=$($SVNLOOK cat -t $TXN $REPOS $f|wc -c)
if [ "$filesize" -gt "$MAX_SIZE"]
then
echo "File $f is too large(must <=$MAX_SIZE)" >&2
exit 1
fi

done
 
#All checks passed, so allow the commit.
exit 0
 

Daniel Becroft

unread,
Jan 12, 2011, 1:00:29 AM1/12/11
to David Xie, us...@subversion.apache.org
On Wed, Jan 12, 2011 at 11:46 AM, David Xie <ddxie.cn@gmail.com> wrote:
Hello,
 
I am looking for a hook to require log message when commit. I succeded to do it.
 
At the same time, I want to limit size less than 10MB and forbid .zip .7z .rar file type.
I found some hooks from internet, tried multi times but all failed. I could always commit big than 10M files and zip files. I struggled two days but could not resolve it.
 
I post my pre-commit at the end.
RHEL 5.3
SVN 1.6.15
Apache 2.2.14
Java 1.6.0_17
 
Would someone give me some help? Great thanks!
  
Regards,
David

I'm unfamiliar with unix scripting, but you could try outputting debug messages to a basic text file, so you can trace it through as it runs.

Cheers,
Daniel B.
 

David Weintraub

unread,
Jan 12, 2011, 10:19:39 PM1/12/11
to David Xie, us...@subversion.apache.org
On Tue, Jan 11, 2011 at 8:46 PM, David Xie <ddxi...@gmail.com> wrote:
>
> Hello,
>
> I am looking for a hook to require log message when commit. I succeded to do it.
>
> At the same time, I want to limit size less than 10MB and forbid .zip .7z .rar file type.
> I found some hooks from internet, tried multi times but all failed. I could always
> commit big than 10M files and zip files. I struggled two days but could not resolve it.

I have a pre-commit hook at http://db.tt/H3o1i7S that will verify your
log message is at least 10 characters, and will also verify that your
files don't end in whatever suffixes you're trying to avoid. It
doesn't do a size check though.

Does your hook succeed in preventing files with your banned suffixes
from being committed? Is your problem just the size of the files? What
about comment length? Does your hook prevent people from writing
commit messages shorter than 10 characters?

After you calculate "filesize" add the following line:

echo "File size for $f is $filesize" 1>&2

Then, make the last line in your hook "exit 1" instead of "exit 0".

This will print out the file size of all the files you're attempting
to commit, and then fail the hook since the hook always exits with a
exit code of 1.

When you attempt to commit, you should see a failed commit and a bunch
of lines telling you the file size of each file. If not, you'll get a
better idea what the problem might be.


--
David Weintraub
qaz...@gmail.com

Reply all
Reply to author
Forward
0 new messages