Edgewall Software

source: trunk/contrib/trac-svn-hook

Last change on this file was 17759, checked in by Jun Omae, 6 months ago

1.7.1dev: merge [17757:17758] from 1.6-stable (fix for #13629)

[skip ci]

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
File size: 8.0 KB
Line 
1#!/bin/sh
2# -*- coding: utf-8 -*-
3#
4# Copyright (C) 2009-2023 Edgewall Software
5# Copyright (C) 2009 Christian Boos <cboos@edgewall.org>
6# All rights reserved.
7#
8# This software is licensed as described in the file COPYING, which
9# you should have received as part of this distribution. The terms
10# are also available at https://trac.edgewall.com/license.html.
11#
12# This software consists of voluntary contributions made by many
13# individuals. For the exact contribution history, see the revision
14# history and logs, available at https://trac.edgewall.org/.
15#
16# = trac-svn-hook =
17#
18# Purpose:: this script is meant to be called from the Subversion hooks
19# for notifying Trac when changesets are added or modified.
20#
21# Scope:: https://trac.edgewall.org/wiki/TracRepositoryAdmin#Synchronization
22# describes how to directly call the relevant trac-admin commands
23# from the Subversion hooks. This script makes configuration and
24# troubleshooting easier, and has support for notifying multiple
25# Trac environments.
26#
27# Usage:: copy this script to some location on your filesystem, such as
28# the TRAC_ENV or TRAC_PARENT_ENV folder, or the repository hooks
29# directory.
30# **Be sure to read the Configuration Notes section below first**
31#
32# For each Subversion repository ($REPOS) that has to be monitored by your
33# Trac environment(s), you need to modify the hooks to call the script:
34#
35# Add this to your `$REPOS/hooks/post-commit` script:
36#
37# /path/to/trac-svn-hook $REPOS $REV
38#
39# If you allow revision property editing in `$REPOS/hooks/pre-revprop-change`,
40# you can let Trac know about modified changesets by adding the following
41# lines to the `$REPOS/hooks/post-revprop-change` script:
42#
43# if [ "$PROPNAME" = "svn:log" -o "$PROPNAME" = "svn:author" ]; then
44# /path/to/trac-svn-hook $REPOS $REV $USER $PROPNAME
45# fi
46#
47# See also http://svnbook.red-bean.com/en/1.8/svn.reposadmin.create.html#svn.reposadmin.create.hooks
48#
49# Platform:: Unix or Cygwin.
50#
51# On Windows, if you have Cygwin installed, you can use this script instead
52# of `trac-svn-post-commit-hook.cmd`.
53# In your `post-commit.bat` and `post-revprop-change.bat` hooks, call
54# this script using:
55#
56# bash /path/to/trac-svn-hook "%1" "%2" "%3" "%4"
57#
58# -----------------------------------------------------------------------------
59#
60# == Configuration Notes
61#
62# As a preliminary remark, you should be aware that Subversion usually
63# run the hooks in a very minimal environment.
64# This is why we have to be very explicit about where to find things.
65#
66# According to http://subversion.apache.org/faq.html#hook-debugging,
67# one useful method for getting the post-commit hook to work is to call
68# the hook manually from a shell, as the user(s) which will end up running
69# the hook (e.g. wwwrun, www-data, nobody). For example:
70#
71# env - $REPOS/hooks/post-commit $REPOS 1234
72#
73# or:
74#
75# env - $REPOS/hooks/post-revprop-change $REPOS 1234 nobody svn:log
76#
77#
78# The TRAC_ENV environment variable must be set, and the TRAC_PATH
79# and TRAC_LD_LIBRARY_PATH variables may also need to be set.
80#
81# TRAC_ENV:: the path(s) to the Trac environment(s)
82#
83# In case you need to maintain more than one environment in sync with
84# the repository (using a different scope or not), simply specify more
85# than one path, using the ":" path separator, or ";" if the script is
86# used on Windows with Cygwin's bash (in this case also don't forget to
87# enclose the list of paths in quotes, e.g. TRAC_ENV="path1;path2").
88#
89# TRAC_PATH:: the folder containing the trac-admin script
90#
91# This folder is typically the same as your Python installation bin/ folder.
92# If this is /usr/bin, then you probably don't need to put it in TRAC_PATH.
93#
94# If you're using a python program installed in a non-default location
95# (such as /opt/python27 or a virtual environment), then you need to add it
96# to the TRAC_PATH.
97#
98# TRAC_LD_LIBRARY_PATH:: folder(s) containing additional required libraries
99#
100# You may also need to setup the TRAC_LD_LIBRARY_PATH accordingly.
101# The same goes for any custom dependency, such as SQLite libraries or
102# SVN libraries: make sure everything is reachable.
103# For example, if you get errors like "global name 'sqlite' is not defined"
104# or similar, then make sure the TRAC_LD_LIBRARY_PATH contains the path to all
105# the required libraries (libsqlite3.so in the above example).
106#
107# -----------------------------------------------------------------------------
108#
109# == Configuration
110#
111# You have 3 options for specifying the configuration:
112# 1. Uncomment the variables below and adapt to your local setup.
113# 2. Set the variables in your hook scripts and export them.
114# 3. In Subversion 1.8 and later, set the variables in a hook script
115# environment configuration.
116# See also http://svnbook.red-bean.com/en/1.8/svn.reposadmin.create.html#svn.reposadmin.hooks.configuration
117#
118# TRAC_ENV=/path/to/trac-env:/path/to/another/trac-env
119# TRAC_PATH=/path/to/python/bin
120# TRAC_LD_LIBRARY_PATH=/path/to/lib:/path/to/another/lib
121#
122# -----------------------------------------------------------------------------
123#
124# == Examples
125#
126# === Minimal setup example ===
127#
128# Python is installed in /usr/bin, Trac was easy_install'ed.
129#
130# {{{
131# TRAC_ENV=/srv/trac/the_trac_env
132# }}}
133#
134#
135# === Simple virtualenv setup example ===
136#
137# Here we're using a Trac installation set up using virtualenv
138# (https://pypi.org/project/virtualenv). The virtualenv has been
139# created at /srv/venv. The TRAC_PATH environment variable must be set
140# to the directory containing the python executable.
141#
142# {{{
143# TRAC_ENV=/srv/trac/the_trac_env
144# TRAC_PATH=/srv/venv/bin
145# }}}
146#
147# === More complex virtualenv setup example ===
148#
149#
150# In this example, the virtualenv is located in
151# /packages/trac/branches/trac-multirepos
152# and is based off a custom Python installation (/opt/python-2.7.11).
153# We're also using a custom SQLite build (/opt/sqlite-3.3.8).
154#
155# Note that virtualenv's activate script seems to only care about TRAC_PATH.
156#
157# We also want to notify two Trac instances:
158#
159# {{{
160# TRAC_ENV=/srv/trac/the_trac_env:/srv/trac/the_other_trac_env
161# TRAC_PATH=/packages/trac/branches/trac-multirepos/bin
162# TRAC_LD_LIBRARY_PATH=/opt/python-2.7.11/lib:/opt/sqlite-3.3.8/lib
163# }}}
164#
165#
166# === Cygwin setup example ===
167#
168# {{{
169# TRAC_ENV=C:/Workspace/local/trac/devel
170# PYTHONPATH=C:/Workspace/src/trac/repos/multirepos
171# TRAC_PATH=/C/Dev/Python27/Scripts
172# }}}
173#
174# -----------------------------------------------------------------------------
175#
176# This is the script itself, you shouldn't need to modify this part.
177
178export TRAC_ENV
179if [ -n "$PYTHONPATH" ]; then
180 export PYTHONPATH
181fi
182if [ -n "$TRAC_PATH" ]; then
183 export PATH="$TRAC_PATH:$PATH"
184fi
185if [ -n "$TRAC_LD_LIBRARY_PATH" ]; then
186 export LD_LIBRARY_PATH="$TRAC_LD_LIBRARY_PATH:$LD_LIBRARY_PATH"
187fi
188
189# -- Command line arguments (cf. usage)
190
191REPOS="$1"
192REV="$2"
193USER="$3"
194PROPNAME="$4"
195
196# -- Foolproofing
197
198if [ -z "$REPOS" -o -z "$REV" ]; then
199 >&2 echo "Usage: $0 REPOS REV"
200 exit 2
201fi
202
203if ! trac-admin -v 2>/dev/null; then
204 >&2 echo "trac-admin is not in the PATH ($PATH), check TRAC_PATH."
205 exit 2
206fi
207
208if [ -z "$TRAC_ENV" ]; then
209 >&2 echo "TRAC_ENV is not set."
210 exit 2
211fi
212
213# -- Feedback
214
215echo "----"
216
217if [ -z "$USER" -a -z "$PROPNAME" ]; then
218 EVENT="added"
219 echo "Changeset $REV was added in $REPOS"
220else
221 EVENT="modified"
222 echo "Changeset $REV was modified by $USER in $REPOS"
223fi
224
225# -- Call "trac-admin ... changeset ... $REPOS $REV" for each Trac environment
226
227ifs=$IFS
228IFS=:
229if [ -n "$BASH_VERSION" ]; then # we can use Bash syntax
230 if [[ ${BASH_VERSINFO[5]} = *cygwin ]]; then
231 IFS=";"
232 fi
233fi
234for env in $TRAC_ENV; do
235 if [ -r "$env/VERSION" ]; then
236 log=$env/log/svn-hooks-`basename $REPOS`.log
237 nohup sh <<EOF >> $log 2>&1 &
238 echo "Changeset $REV $EVENT"
239 trac-admin $env changeset $EVENT $REPOS $REV && \
240 echo "OK" || echo "FAILED: see the Trac log"
241EOF
242 else
243 echo "$env doesn't seem to be a Trac environment, skipping..."
244 fi
245done
246IFS=$ifs
Note: See TracBrowser for help on using the repository browser.