This script was originally taken from Jacques Marneweck's site. I've added extension checking of added files against a list of known binary extensions. If the svn:mime-type property of these files has not been set to application/octet-stream it fails with a message explaining how to set the files as binary. This is important for users of svnnotify as some clients (TortoiseSVN) may fail to auto-set the binary attribute for certain types of files (fla, pdf), causing the commit e-mail to be large and unhelpful.

#!/usr/bin/python
"""
Subversion pre-commit hook which currently checks that the commit contains
a commit message to avoid commiting empty changesets which tortoisesvn seems
to have a habbit of committing.
 
Based on http://svn.collab.net/repos/svn/branches/1.2.x/contrib/hook-scripts/commit-block-joke.py
and hooks/pre-commit.tmpl
 
Hacked together by Jacques Marneweck <jacques@php.net>
http://textsnippets.com/posts/show/55
 
Extension checking added by Justin Patrin <papercrane@reversefold.com>
 
$Id$
"""
 
import sys, os, string
 
SVNLOOK='/usr/bin/svnlook'
 
binaryExtensions = ['doc', 'xls', 'pdf', 'vsd', 'wav', 'zip', 'fla', 'swf', 'png', 'jpg', 'gif', 'jpeg', 'psd', 'tif', 'tiff', 'rtf', 'eps', 'ai', 'flv', 'mov', 'avi', 'wmf', 'wma', 'aiff', 'pcm', 'mp3', 'm4a']
 
def main(repos, txn):
    log_cmd = "%s log -t '%s' '%s'" % (SVNLOOK, txn, repos)
    log_msg = os.popen(log_cmd, 'r').readline().rstrip('\n')
    log_msg = log_msg.strip()
 
    change_cmd = "%s changed -t '%s' '%s'" % (SVNLOOK, txn, repos)
    change = os.popen(change_cmd, 'r')
    files = []
    while True:
        line = change.readline().rstrip('\n')
        if not line:
            break
        if line[0] != 'A':
            continue
        if line.split('.').pop().lower() in binaryExtensions:
            file = line[2:].lstrip()
            prop_cmd = "%s propget -t '%s' '%s' 'svn:mime-type' '%s' 2>/dev/null" % (SVNLOOK, txn, repos, file)
            prop = os.popen(prop_cmd, 'r')
            if prop.read() != 'application/octet-stream':
                files.append(file)
 
 
    error = ""
    if len(files) > 0:
        error += "The following files need to be set binary for this commit to happen:\n"
        for file in files:
            error += "  %s\n" % file
        error += "\nUsing TortoiseSVN:\n"
        error += "  1) Right click on the file and go to Properties\n"
        error += "  2) Go to the Subversion tab\n"
        error += "  3) Choose svn:mime-type in the drop down in the Properties area\n"
        error += "  4) Enter application/octet-stream in the text box below the drop-down\n"
        error += "  5) Click the Set button, then click the OK button\n"
        error += "\nOn the command-line:\n"
        error += "  svn propset svn:mime-type application/octet-stream FILENAME\n"
 
    if len(log_msg) < 5:
        if len(error) > 0:
            error += "\n"
        error += "Please enter a commit message which details what has changed during this commit.\n"
 
    if len(error) > 0:
        sys.stderr.write (error)
        sys.exit(1)
    else:
        sys.exit(0)
 
if __name__ == '__main__':
    if len(sys.argv) < 3:
        sys.stderr.write("Usage: %s REPOS TXN\n" % (sys.argv[0]))
    else:
        main(sys.argv[1], sys.argv[2])
 
svn/pre-commit.txt · Last modified: 2008/09/16 11:49 (external edit)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki