views:

712

answers:

3

I have just set up a post-commit script in our subversion repository that triggers a build by requesting a hudson build URL.

This works fine as expected, however now I only want to trigger this build if the commit was to the trunk.

our post-commit script looks like this:

SET REPOS=%1
SET REV=%2

SET DIR=%REPOS%/hooks
SET PATH=%PATH%;%DIR%
wget http://circus-09:8080/job/UE/build?delay=0sec

How to I check that the commit was to the trunk?

+1  A: 

As far as I know there is no easy way to do this with subversion: the post-commit script is run after any commit to the repository, regardless of whether it is in the trunk or in a branch.

You could try to determine the location of the changed files (possibly using svnlook changed and some regular expression) in your script, of course..

Kristian
+4  A: 

Here's a quick code snippet, that outputs different messages when something in trunk changed or nothing has:

set repos=%~1
set rev=%~2

call :did_it_change "%repos%" "%rev%" "trunk"
if %ERRORLEVEL%==1 (
    echo trunk changed
) else (
    echo no changes in trunk
)
exit /B 0

:did_it_change
    set repos=%~1
    set rev=%~2
    set dir=%~3
    set found=0
    for /F "delims=/" %%p in ('svnlook dirs-changed "%repos%" -r %rev% 2^>NUL') do call :check "%%p" "%dir%"
    exit /B %found%

:check
    set check_string=%~1
    set must_match=%~2
    if "$%check_string%" == "$%must_match%" set found=1
    exit /B 0

Note, that :did_it_change function can be used with any repository root level subdirectory and not just trunk. Very useful, to detect new tags or branches. Also note, that the function can be called any number of times.

Note: This doesn't actually check if source files were changed or not - it simply checks if trunk is mentioned in the revisions changed directories list. It could be that the change was to the svn attributes of some directories or files.

Paulius Maruška
I edited the code a little: very first version had "trunk" hardcoded in the function (doh!) and then I think it's more useful when the error output from svnlook is discarded.
Paulius Maruška
since i am very inexperienced in Windows batch commands, this post ended a two hour search, thank you very much
dhulk
+2  A: 

As Paulius' answer says, svnlook gives you the details for the revision, it just needs a little manipulation. Using the python pysvn library helps shield you from some of the internals of doing this, and opens the door for some fancier integrations.

Example to get you started:

import sys;
import urllib;
import svnlook;

#duckpunch to get access to the relative path for the revision
def relativePath(self):
    return self.path

baseUrl = sys.argv[1]
repo = sys.argv[2]
revision = sys.argv[3]

l = svnlook.changed(repo, revision);
#TODO this assumes all enries in the commit are against one project, so the first item found is sufficient
#May want to iterate the entries and check for any different paths
out = l[0]

changePath = relativePath(out)

print changePath

#TODO if 'trunk' is found in changePath, trigger build
Rich Seller
wow, I didn't know there's an api like that for python... pysvn sure looks nice.
Paulius Maruška