I have created a subversion hook which does various things including sending out emails and updating a working copy on the server. When this is run from the bash propt it works perfectly. When run through either TortoiseSVN or Netbean开发者_JAVA技巧s on commit, emails etc are sent but the update is not executed, no errors appear either. The file is a php file and I am using the backtick method to run bash commands. Other bash commands are run to compose the emails so that isn't the issue.
Here is the line which should run the update and log the outcome. $location is pulled from a database of working copy locations.
$update_output = `/usr/local/bin/svn update /home/$location >> update.log`;
Thanks
James
Edit, More Complete Script:
#!/usr/local/bin/php
<?
$REPOS = $argv[1];
$REV = $argv[2];
$output[] = `/usr/local/bin/svnlook dirs-changed -r $REV $REPOS`;
foreach($output as $line)
{
preg_match("$([^/]+)$", $line, $array);
$projects[] = $array[0];
}
$projects = array_unique($projects);
$mysqli = new mysqli('localhost', 'svn_user', 'pringles', 'svn_maindb');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
foreach($projects as $project)
{
$query = "SELECT * FROM Project WHERE name = '$project' LIMIT 1";
$result = $mysqli->query($query);
$row = $result->fetch_assoc();
$proj_id = $row['id'];
$location = $row['location'];
if(!empty($location))
{
$update_output = `/usr/local/bin/svn update /home/$location >> update.log`;
}
/* The below line only works when the script is run by hand */
$test = `/usr/bin/lessecho test >> /home/svn/repo/hooks/update.log`;
/* Grab user from DB and call send_email for user */
}
function send_email($REPOS, $REV, $programmer, $email)
{
$author = `/usr/local/bin/svnlook author -r $REV $REPOS`;
$message .= "Project Committed By $author
Comments:
";
$message .= `/usr/local/bin/svnlook log -r $REV $REPOS`;
$message .= "
===========List of Changes========
U = Updated
A = Added
D = Deleted
";
$message .= `/usr/local/bin/svnlook changed -r $REV $REPOS`;
/* Compose and Send Email */
}
root 4231 0.0 0.0 61180 748 pts/0 S+ 17:09 0:00 grep svnserve
Is this because I ran it as root? If it is running as root however then there should be no permissions issue?
I assume that by "SVN hook", you mean a repository hook script that is executed by the server. Note the following warning in the SVN book:
For security reasons, the Subversion repository executes hook programs with an empty environment—that is, no environment variables are set at all, not even $PATH (or %PATH%, under Windows). Because of this, many administrators are baffled when their hook program runs fine by hand, but doesn't work when run by Subversion. Be sure to explicitly set any necessary environment variables in your hook program and/or use absolute paths to programs.
In this case, the lack of a $PATH means that you have to invoke svn with its full path, e.g. /usr/bin/svn
.
edit: since svn update
is still not working even with the full path, I would try to log the error output like this:
$update_output = `/usr/local/bin/svn update /home/$location 2>>/var/tmp/update-error.log`;
edit2: as the error output of svn update
shows, it is a permission issue. Find out as which user the script runs (e.g. by executing whoami > /var/tmp/whoami-output
in the script) and make sure that this user is allowed to manipulate the working copies you are trying to update.
I had same issue, fixed it as following:
#add apache user to group of target svn folder (project folder):
usermod -a -G [group_of_current_target_svn_folder] [apache_user, e.g. dhapache]
#change owner of target svn folder:
D=/home/pfu5er/public_html/dev2
chown -R [apache_user, e.g. dhapache] $D
精彩评论