Only trigger a build on commits to the trunk with svn

别等时光非礼了梦想. 提交于 2019-12-03 16:49:06

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.

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..

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!