4

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?

0xC0DEFACE
  • 8,825
  • 7
  • 34
  • 35
  • 2
    Why don'T you use Hudsons own svn observer? – tuergeist Jul 20 '09 at 06:57
  • Because (from what i understand) it periodically trolls the repository and can have a performance impact on our small fragile server. plus im not sure that it would solve the problem of only building when there is a commit to the trunk. – 0xC0DEFACE Jul 20 '09 at 08:17
  • @tuergeist: a) what 0xC0DEFACE said - no need to poll the repository all the time and b) it's easy to do in batch (see my answer). – Paulius Jul 20 '09 at 08:21
  • 2
    The polling has no big impact on our buildserver. Did you really measure a performance impact? – Mnementh Jul 20 '09 at 08:28
  • If someone gives me a task - I don't want him to come to me every 5 minutes and ask me if I'm done, I'd rather tell him myself when I'm done... Same thing. – Paulius Jul 20 '09 at 08:32
  • @Mnementh: I totaly agree. I cannot believe that there is a real impact. Sure the server polls the svn server, but that is nearly for free. The other "solution" - these ugly platform dependent scripts - is very error-prone. – tuergeist Jul 20 '09 at 13:39
  • + scripts tend to be a hard to maintain thing only the author understands. – tuergeist Jul 20 '09 at 13:40
  • Our server is already used to capacity and is showing performance artifacts from 20 people using it for our server for just about everything. Why add to that frequently by polling the repository? It would be nonsensical. – 0xC0DEFACE Jul 20 '09 at 23:43
  • @Mnementh, @tuergeist: If the scripts are hard to maintain - you're doing it wrong... You can keep them in a separate repository as well, you know... Besides, there is no need to use any of the platform dependent stuff - pick up python (or lua, or Java or any other language that has svn bindings) and use that instead. – Paulius Jul 21 '09 at 05:36
  • @0xC0DEFACE The problem seems not to be a real svn/hudson/build problem. If your argumentations is that the server reached its capacity, you shall stop using Hudson and start using ant scripts only. You can wire them together with the python svn post commit script ;) – tuergeist Jul 21 '09 at 07:31
  • @tuergeist yeah I COULD do that, but using hudson is preferable, and will also put pressure on the company to upgrade our crappy server. I just want it to be optimal so I have the best performance for the team in the interim. – 0xC0DEFACE Jul 22 '09 at 01:32

4 Answers4

7

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
  • 5,790
  • 7
  • 42
  • 47
  • 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 Jul 20 '09 at 08:28
  • since i am very inexperienced in Windows batch commands, this post ended a two hour search, thank you very much – Blair Scott Aug 24 '09 at 17:05
2

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
  • 6,357
  • 4
  • 36
  • 37
2

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
  • 83,208
  • 23
  • 172
  • 177
0

In bash it can be done this way:

REPOS="$1"
REV="$2"
TXN_NAME="$3"
SVN=/usr/bin/svn
SVNLOOK=/usr/bin/svnlook
export LANG=en_US.UTF-8

RES=$($SVNLOOK dirs-changed $REPOS -r $REV)
if [[ $RES == *"trunk"* ]]
then
   Call whichever command you want to call when there are changes in trunk
fi
Leandro Jacques
  • 413
  • 5
  • 19