Over a million developers have joined DZone.

Useful Subversion Pre-Commit Hook Script for Linux Servers

DZone's Guide to

Useful Subversion Pre-Commit Hook Script for Linux Servers

Looking for useful subversion pre-commit hooks? Maybe this script is for you. It's a Linux bash shell script and also makes use of python.

· DevOps Zone
Free Resource

In response to accelerated release cycles, a new set of testing capabilities is now required to deliver quality at speed. This is why there is a shake-up in the testing tools landscape—and a new leader has emerged in the just released Gartner Magic Quadrant for Software Test Automation.

Looking for useful subversion pre-commit hooks? Maybe this script is for you. It's a Linux bash shell script and also makes use of python.
The script does the following:

  1. Checks whether the commit message is not empty
  2. Checks whether the commit message consists of at least 5 characters
  3. Checks if the committed files are UTF-8 compliant
  4. Checks whether the svn:eol-style property is set to LF on newly added files
  5. Checks if the committed files have no TAB characters
The UTF-8 and TAB checks are performed on the following file suffixes
  • *.java
  • *.js
  • *.xhtml
  • *.css
  • *.xml
  • *.properties (only check for TABs here, no check for UTF-8 compliance)
It should be easy to adjust those settings to your needs.


 # Make sure that the log message contains some text.  

 $SVNLOOK log -t "$TXN" "$REPOS" | \  
 grep "[a-zA-Z0-9]" > /dev/null || SVNLOOKOK=0  
 if [ $SVNLOOKOK = 0 ]; then  
  echo "Empty log messages are not allowed. Please provide a proper log message." >&2  
  exit 1  

 # Comments should have more than 5 characters  
 LOGMSG=$($SVNLOOK log -t "$TXN" "$REPOS" | grep [a-zA-Z0-9] | wc -c)  

 if [ "$LOGMSG" -lt 6 ]; then  
  echo -e "Please provide a meaningful comment when committing changes." 1>&2  
  exit 1  

 # Make sure that all files to be committed are encoded in UTF-8.  
 while read changeline;   

   # Get just the file (not the add / update / etc. status).  

   # Only check source files.  
   if [[ $file == *.java || $file == *.xhtml || $file == *.css || $file == *.xml || $file == *.js ]] ; then  
     $SVNLOOK cat -t "$TXN" "$REPOS" "$file" | $ICONV -f UTF-8 -t UTF-8 -o /dev/null  
     if [ "${PIPESTATUS[1]}" != 0 ] ; then  
       echo "Only UTF-8 files can be committed ("$file")" 1>&2  
       exit 1  
 done < <($SVNLOOK changed -t "$TXN" "$REPOS")  

 # Check files for svn:eol-style property  
 # Exit on all errors.  
 set -e  
 echo "`$SVNLOOK changed -t "$TXN" "$REPOS"`" | while read REPOS_PATH  
  if [[ $REPOS_PATH =~ A[[:blank:]]{3}(.*)\.(java|css|properties|xhtml|xml|js) ]]  
   if [ ${#BASH_REMATCH[*]} -ge 2 ]  

   # Make sure every file has the right svn:eol-style property set  
    if [ $EOL_STYLE != "`$SVNLOOK propget -t \"$TXN\" \"$REPOS\" svn:eol-style \"$FILENAME\" 2> /dev/null`" ]  
       echo "svn ps svn:eol-style $EOL_STYLE \"$FILENAME\"" >&2  
  test -z $ERROR || (echo "Please execute above commands to correct svn property settings. EOL Style LF must be used!" >& 2; exit 1)  

 # Block commits with tabs  
 # This is coded in python  
 # Exit on all errors  
 set -e  

 $SVNLOOK diff -t "$TXN" "$REPOS" | python /dev/fd/3 3<<'EOF'  
 import sys  
 ignore = True  
 SUFFIXES = [ ".java", ".css", ".xhtml", ".js", ".xml", ".properties" ]  
 filename = None  

 for ln in sys.stdin:  

     if ignore and ln.startswith("+++ "):  
         filename = ln[4:ln.find("\t")].strip()  
         ignore = not reduce(lambda x, y: x or y, map(lambda x: filename.endswith(x), SUFFIXES))  

     elif not ignore:  
         if ln.startswith("+"):  

            if ln.count("\t") > 0:  
               sys.stderr.write("\n*** Transaction blocked, %s contains tab character:\n\n%s" % (filename, ln))  

         if not (ln.startswith("@") or \  
            ln.startswith("-") or \  
            ln.startswith("+") or \  
            ln.startswith(" ")):  

            ignore = True  


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

Recently published Gartner Magic Quadrant Report for Software Test Automation provides an objective benchmark of all test automation solutions based on industry surveys, customer inquiries, product evaluations, and more. 

java ,devops ,tips and tricks

Published at DZone with permission of Ralf Quebbemann, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}